k8s.io/kubernetes@v1.29.3/test/cmd/discovery.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2018 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  set -o errexit
    18  set -o nounset
    19  set -o pipefail
    20  
    21  run_RESTMapper_evaluation_tests() {
    22    set -o nounset
    23    set -o errexit
    24  
    25    create_and_use_new_namespace
    26    kube::log::status "Testing RESTMapper"
    27  
    28    RESTMAPPER_ERROR_FILE="${KUBE_TEMP}/restmapper-error"
    29  
    30    ### Non-existent resource type should give a recognizable error
    31    # Pre-condition: None
    32    # Command
    33    kubectl get "${kube_flags[@]:?}" unknownresourcetype 2>"${RESTMAPPER_ERROR_FILE}" || true
    34    if grep -q "the server doesn't have a resource type" "${RESTMAPPER_ERROR_FILE}"; then
    35      kube::log::status "\"kubectl get unknownresourcetype\" returns error as expected: $(cat "${RESTMAPPER_ERROR_FILE}")"
    36    else
    37      kube::log::status "\"kubectl get unknownresourcetype\" returns unexpected error or non-error: $(cat "${RESTMAPPER_ERROR_FILE}")"
    38      exit 1
    39    fi
    40    rm "${RESTMAPPER_ERROR_FILE}"
    41    # Post-condition: None
    42  
    43    set +o nounset
    44    set +o errexit
    45  }
    46  
    47  run_assert_short_name_tests() {
    48    set -o nounset
    49    set -o errexit
    50  
    51    create_and_use_new_namespace
    52    kube::log::status "Testing assert short name"
    53  
    54    kube::log::status "Testing propagation of short names for resources"
    55    output_message=$(kubectl get --raw=/api/v1)
    56  
    57    ## test if a short name is exported during discovery
    58    kube::test::if_has_string "${output_message}" '{"name":"configmaps","singularName":"configmap","namespaced":true,"kind":"ConfigMap","verbs":\["create","delete","deletecollection","get","list","patch","update","watch"\],"shortNames":\["cm"\],"storageVersionHash":'
    59  
    60    # check that there is no pod with the name test-crd-example
    61    output_message=$(kubectl get pod)
    62    kube::test::if_has_not_string "${output_message}" "test-crd-example"
    63  
    64      kubectl create -f - << __EOF__
    65  apiVersion: apiextensions.k8s.io/v1
    66  kind: CustomResourceDefinition
    67  metadata:
    68    name: examples.test.com
    69  spec:
    70    group: test.com
    71    scope: Namespaced
    72    versions:
    73      - name: v1
    74        served: true
    75        storage: true
    76        schema:
    77          openAPIV3Schema:
    78            type: object
    79            properties:
    80              spec:
    81                type: object
    82                properties:
    83                  test:
    84                    type: string
    85    names:
    86      plural: examples
    87      singular: example
    88      shortNames:
    89        - pod
    90      kind: Example
    91  __EOF__
    92  
    93    # Test that we can list this new custom resource
    94    kube::test::wait_object_assert customresourcedefinitions "{{range.items}}{{if eq ${id_field:?} \"examples.test.com\"}}{{$id_field}}:{{end}}{{end}}" 'examples.test.com:'
    95  
    96    kubectl create -f - << __EOF__
    97  apiVersion: test.com/v1
    98  kind: Example
    99  metadata:
   100    name: test-crd-example
   101  spec:
   102    test: test
   103  __EOF__
   104  
   105    # Test that we can list this new custom resource
   106    kube::test::wait_object_assert examples "{{range.items}}{{${id_field:?}}}:{{end}}" 'test-crd-example:'
   107  
   108    output_message=$(kubectl get examples)
   109    kube::test::if_has_string "${output_message}" "test-crd-example"
   110  
   111    # test that get pod returns v1/pod instead crd
   112    output_message=$(kubectl get pod)
   113    kube::test::if_has_not_string "${output_message}" "test-crd-example"
   114  
   115    # invalidate cache and assure that still correct resource is shown
   116    kubectl api-resources
   117  
   118    # retest the above cases after invalidating cache
   119    output_message=$(kubectl get examples)
   120    kube::test::if_has_string "${output_message}" "test-crd-example"
   121  
   122    output_message=$(kubectl get pod)
   123    kube::test::if_has_not_string "${output_message}" "test-crd-example"
   124  
   125    # Cleanup
   126    kubectl delete examples/test-crd-example
   127    kubectl delete customresourcedefinition examples.test.com
   128  
   129    set +o nounset
   130    set +o errexit
   131  }
   132  
   133  run_assert_singular_name_tests() {
   134    set -o nounset
   135    set -o errexit
   136  
   137    create_and_use_new_namespace
   138    kube::log::status "Testing assert singular name"
   139  
   140    # check that there is no pod with the name test-crd-example
   141    output_message=$(kubectl get pod)
   142    kube::test::if_has_not_string "${output_message}" "test-crd-example"
   143  
   144    kubectl create -f - << __EOF__
   145  apiVersion: apiextensions.k8s.io/v1
   146  kind: CustomResourceDefinition
   147  metadata:
   148    name: examples.test.com
   149  spec:
   150    group: test.com
   151    scope: Namespaced
   152    versions:
   153      - name: v1
   154        served: true
   155        storage: true
   156        schema:
   157          openAPIV3Schema:
   158            type: object
   159            properties:
   160              spec:
   161                type: object
   162                properties:
   163                  test:
   164                    type: string
   165    names:
   166      plural: examples
   167      singular: pod
   168      kind: Example
   169  __EOF__
   170  
   171    # Test that we can list this new custom resource
   172    kube::test::wait_object_assert customresourcedefinitions "{{range.items}}{{if eq ${id_field:?} \"examples.test.com\"}}{{$id_field}}:{{end}}{{end}}" 'examples.test.com:'
   173  
   174    kubectl create -f - << __EOF__
   175  apiVersion: test.com/v1
   176  kind: Example
   177  metadata:
   178    name: test-crd-example
   179  spec:
   180    test: test
   181  __EOF__
   182  
   183    # Test that we can list this new custom resource
   184    kube::test::wait_object_assert examples "{{range.items}}{{$id_field}}:{{end}}" 'test-crd-example:'
   185  
   186    output_message=$(kubectl get examples)
   187    kube::test::if_has_string "${output_message}" "test-crd-example"
   188  
   189    output_message=$(kubectl get pod)
   190    kube::test::if_has_not_string "${output_message}" "test-crd-example"
   191  
   192    # invalidate cache and assure that still correct resource is shown
   193    kubectl api-resources
   194  
   195    output_message=$(kubectl get examples)
   196    kube::test::if_has_string "${output_message}" "test-crd-example"
   197  
   198    output_message=$(kubectl get pod)
   199    kube::test::if_has_not_string "${output_message}" "test-crd-example"
   200  
   201    # Cleanup
   202    kubectl delete examples/test-crd-example
   203    kubectl delete customresourcedefinition examples.test.com
   204  
   205    set +o nounset
   206    set +o errexit
   207  }
   208  
   209  run_assert_categories_tests() {
   210    set -o nounset
   211    set -o errexit
   212  
   213    kube::log::status "Testing propagation of categories for resources"
   214    output_message=$(kubectl get --raw=/api/v1 | grep -o '"name":"pods"[^}]*}')
   215    kube::test::if_has_string "${output_message}" '"categories":\["all"\]'
   216  
   217    set +o nounset
   218    set +o errexit
   219  }
   220  
   221  run_resource_aliasing_tests() {
   222    set -o nounset
   223    set -o errexit
   224  
   225    create_and_use_new_namespace
   226    kube::log::status "Testing resource aliasing"
   227    kubectl create -f test/e2e/testing-manifests/statefulset/cassandra/controller.yaml "${kube_flags[@]}"
   228    kubectl create -f test/e2e/testing-manifests/statefulset/cassandra/service.yaml "${kube_flags[@]}"
   229  
   230    object="all -l app=cassandra"
   231    request="{{range.items}}{{range .metadata.labels}}{{.}}:{{end}}{{end}}"
   232  
   233    # all 4 cassandra's might not be in the request immediately...
   234    # :? suffix is for possible service.kubernetes.io/headless
   235    # label with "" value
   236    kube::test::get_object_assert "$object" "$request" '(cassandra:){2}(cassandra:(cassandra::?)?)?'
   237  
   238    kubectl delete all -l app=cassandra "${kube_flags[@]}"
   239  
   240    set +o nounset
   241    set +o errexit
   242  }
   243  
   244  run_crd_deletion_recreation_tests() {
   245    set -o nounset
   246    set -o errexit
   247  
   248    create_and_use_new_namespace
   249    kube::log::status "Testing resource creation, deletion, and re-creation"
   250  
   251    output_message=$(kubectl apply -f hack/testdata/CRD/example-crd-1-cluster-scoped.yaml)
   252    kube::test::if_has_string "${output_message}" 'created'
   253    output_message=$(kubectl apply -f hack/testdata/CRD/example-crd-1-cluster-scoped-resource.yaml)
   254    kube::test::if_has_string "${output_message}" 'created'
   255    output_message=$(kubectl delete -f hack/testdata/CRD/example-crd-1-cluster-scoped.yaml)
   256    kube::test::if_has_string "${output_message}" 'deleted'
   257    # Invalidate local cache because cluster scoped CRD in cache is stale.
   258    # Invalidation of cache may take up to 6 hours and we are manually
   259    # invalidate cache and expect that scope changed CRD should be created without problem.
   260    kubectl api-resources
   261    output_message=$(kubectl apply -f hack/testdata/CRD/example-crd-1-namespaced.yaml)
   262    kube::test::if_has_string "${output_message}" 'created'
   263    output_message=$(kubectl apply -f hack/testdata/CRD/example-crd-1-namespaced-resource.yaml)
   264    kube::test::if_has_string "${output_message}" 'created'
   265  
   266    # Cleanup
   267    kubectl delete -f hack/testdata/CRD/example-crd-1-namespaced-resource.yaml
   268    kubectl delete -f hack/testdata/CRD/example-crd-1-namespaced.yaml
   269  
   270    set +o nounset
   271    set +o errexit
   272  }
   273  
   274  run_kubectl_explain_tests() {
   275    set -o nounset
   276    set -o errexit
   277  
   278    kube::log::status "Testing kubectl(v1:explain)"
   279    kubectl explain pods
   280    # shortcuts work
   281    kubectl explain po
   282    kubectl explain po.status.message
   283    # cronjob work
   284    kubectl explain cronjob
   285  
   286    set +o nounset
   287    set +o errexit
   288  }
   289  
   290  run_swagger_tests() {
   291    set -o nounset
   292    set -o errexit
   293  
   294    kube::log::status "Testing swagger"
   295  
   296    # Verify schema
   297    file="${KUBE_TEMP}/schema.json"
   298    curl -kfsS -H 'Authorization: Bearer admin-token' "https://127.0.0.1:${SECURE_API_PORT}/openapi/v2" > "${file}"
   299    grep -q "list of returned" "${file}"
   300    grep -q "List of services" "${file}"
   301    grep -q "Watch for changes to the described resources" "${file}"
   302  
   303    set +o nounset
   304    set +o errexit
   305  }
   306  
   307  run_ambiguous_shortname_tests() {
   308    set -o nounset
   309    set -o errexit
   310  
   311    create_and_use_new_namespace
   312    kube::log::status "Testing ambiguous short name"
   313  
   314    kubectl create -f - << __EOF__
   315  apiVersion: apiextensions.k8s.io/v1
   316  kind: CustomResourceDefinition
   317  metadata:
   318    name: foos.bar.com
   319  spec:
   320    group: bar.com
   321    scope: Namespaced
   322    versions:
   323      - name: v1
   324        served: true
   325        storage: true
   326        schema:
   327          openAPIV3Schema:
   328            type: object
   329            properties:
   330              spec:
   331                type: object
   332                properties:
   333                  test:
   334                    type: string
   335    names:
   336      plural: foos
   337      singular: foo
   338      shortNames:
   339        - exmp
   340      kind: Foo
   341      categories:
   342        - all
   343  __EOF__
   344  
   345    # Test that we can list this new custom resource
   346    kube::test::wait_object_assert customresourcedefinitions "{{range.items}}{{if eq ${id_field:?} \"foos.bar.com\"}}{{$id_field}}:{{end}}{{end}}" 'foos.bar.com:'
   347  
   348    kubectl create -f - << __EOF__
   349  apiVersion: bar.com/v1
   350  kind: Foo
   351  metadata:
   352    name: test-crd-foo
   353  spec:
   354    test: test
   355  __EOF__
   356  
   357    # Test that we can list this new custom resource
   358    kube::test::wait_object_assert foos "{{range.items}}{{$id_field}}:{{end}}" 'test-crd-foo:'
   359  
   360    output_message=$(kubectl get exmp)
   361    kube::test::if_has_string "${output_message}" "test-crd-foo"
   362  
   363    kubectl create -f - << __EOF__
   364  apiVersion: apiextensions.k8s.io/v1
   365  kind: CustomResourceDefinition
   366  metadata:
   367    name: examples.test.com
   368  spec:
   369    group: test.com
   370    scope: Namespaced
   371    versions:
   372      - name: v1
   373        served: true
   374        storage: true
   375        schema:
   376          openAPIV3Schema:
   377            type: object
   378            properties:
   379              spec:
   380                type: object
   381                properties:
   382                  test:
   383                    type: string
   384    names:
   385      plural: examples
   386      singular: example
   387      shortNames:
   388        - exmp
   389      kind: Example
   390  __EOF__
   391  
   392    # Test that we can list this new custom resource
   393    kube::test::wait_object_assert customresourcedefinitions "{{range.items}}{{if eq ${id_field:?} \"examples.test.com\"}}{{$id_field}}:{{end}}{{end}}" 'examples.test.com:'
   394  
   395    output_message=$(kubectl  get examples 2>&1 "${kube_flags[@]}")
   396    kube::test::if_has_string "${output_message}" 'No resources found'
   397  
   398    output_message=$(kubectl get exmp 2>&1)
   399    kube::test::if_has_string "${output_message}" "test-crd-foo"
   400    kube::test::if_has_string "${output_message}" "short name \"exmp\" could also match lower priority resource examples.test.com"
   401  
   402    # Cleanup
   403    kubectl delete foos/test-crd-foo
   404    kubectl delete customresourcedefinition foos.bar.com
   405    kubectl delete customresourcedefinition examples.test.com
   406  
   407    set +o nounset
   408    set +o errexit
   409  }