discovery.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  19. "k8s.io/kubernetes/test/utils/crd"
  20. "github.com/onsi/ginkgo"
  21. )
  22. var storageVersionServerVersion = utilversion.MustParseSemantic("v1.13.99")
  23. var _ = SIGDescribe("Discovery", func() {
  24. f := framework.NewDefaultFramework("discovery")
  25. var namespaceName string
  26. ginkgo.BeforeEach(func() {
  27. namespaceName = f.Namespace.Name
  28. e2eskipper.SkipUnlessServerVersionGTE(storageVersionServerVersion, f.ClientSet.Discovery())
  29. ginkgo.By("Setting up server cert")
  30. setupServerCert(namespaceName, serviceName)
  31. })
  32. ginkgo.It("Custom resource should have storage version hash", func() {
  33. testcrd, err := crd.CreateTestCRD(f)
  34. if err != nil {
  35. return
  36. }
  37. defer testcrd.CleanUp()
  38. spec := testcrd.Crd.Spec
  39. resources, err := testcrd.APIExtensionClient.Discovery().ServerResourcesForGroupVersion(spec.Group + "/" + spec.Versions[0].Name)
  40. if err != nil {
  41. framework.Failf("failed to find the discovery doc for %v: %v", resources, err)
  42. }
  43. found := false
  44. var storageVersion string
  45. for _, v := range spec.Versions {
  46. if v.Storage {
  47. storageVersion = v.Name
  48. }
  49. }
  50. // DISCLAIMER: the algorithm of deriving the storageVersionHash
  51. // is an implementation detail, which shouldn't be relied on by
  52. // the clients. The following calculation is for test purpose
  53. // only.
  54. expected := discovery.StorageVersionHash(spec.Group, storageVersion, spec.Names.Kind)
  55. for _, r := range resources.APIResources {
  56. if r.Name == spec.Names.Plural {
  57. found = true
  58. if r.StorageVersionHash != expected {
  59. framework.Failf("expected storageVersionHash of %s/%s/%s to be %s, got %s", r.Group, r.Version, r.Name, expected, r.StorageVersionHash)
  60. }
  61. }
  62. }
  63. if !found {
  64. framework.Failf("didn't find resource %s in the discovery doc", spec.Names.Plural)
  65. }
  66. })
  67. })