discovery.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package apimachinery
  14. import (
  15. utilversion "k8s.io/apimachinery/pkg/util/version"
  16. "k8s.io/apiserver/pkg/endpoints/discovery"
  17. "k8s.io/kubernetes/test/e2e/framework"
  18. "k8s.io/kubernetes/test/utils/crd"
  19. "github.com/onsi/ginkgo"
  20. )
  21. var storageVersionServerVersion = utilversion.MustParseSemantic("v1.13.99")
  22. var _ = SIGDescribe("Discovery", func() {
  23. f := framework.NewDefaultFramework("discovery")
  24. var namespaceName string
  25. ginkgo.BeforeEach(func() {
  26. namespaceName = f.Namespace.Name
  27. framework.SkipUnlessServerVersionGTE(storageVersionServerVersion, f.ClientSet.Discovery())
  28. ginkgo.By("Setting up server cert")
  29. setupServerCert(namespaceName, serviceName)
  30. })
  31. ginkgo.It("Custom resource should have storage version hash", func() {
  32. testcrd, err := crd.CreateTestCRD(f)
  33. if err != nil {
  34. return
  35. }
  36. defer testcrd.CleanUp()
  37. spec := testcrd.Crd.Spec
  38. resources, err := testcrd.APIExtensionClient.Discovery().ServerResourcesForGroupVersion(spec.Group + "/" + spec.Versions[0].Name)
  39. if err != nil {
  40. framework.Failf("failed to find the discovery doc for %v: %v", resources, err)
  41. }
  42. found := false
  43. var storageVersion string
  44. for _, v := range spec.Versions {
  45. if v.Storage {
  46. storageVersion = v.Name
  47. }
  48. }
  49. // DISCLAIMER: the algorithm of deriving the storageVersionHash
  50. // is an implementation detail, which shouldn't be relied on by
  51. // the clients. The following calculation is for test purpose
  52. // only.
  53. expected := discovery.StorageVersionHash(spec.Group, storageVersion, spec.Names.Kind)
  54. for _, r := range resources.APIResources {
  55. if r.Name == spec.Names.Plural {
  56. found = true
  57. if r.StorageVersionHash != expected {
  58. framework.Failf("expected storageVersionHash of %s/%s/%s to be %s, got %s", r.Group, r.Version, r.Name, expected, r.StorageVersionHash)
  59. }
  60. }
  61. }
  62. if !found {
  63. framework.Failf("didn't find resource %s in the discovery doc", spec.Names.Plural)
  64. }
  65. })
  66. })