k8s.io/kubernetes@v1.29.3/test/cmd/plugins.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_plugins_tests() {
    22    set -o nounset
    23    set -o errexit
    24  
    25    kube::log::status "Testing kubectl plugins"
    26  
    27    # Create a folder which only contains our kubectl executable
    28    TEMP_PATH=$(mktemp -d /tmp/tmp-kubectl-path-XXXXX)
    29    ln -s "$(which kubectl)" "${TEMP_PATH}/kubectl"
    30  
    31    # test plugins that overwrite existing kubectl commands
    32    output_message=$(! PATH=${TEMP_PATH}:"test/fixtures/pkg/kubectl/plugins/version" kubectl plugin list 2>&1)
    33    kube::test::if_has_string "${output_message}" 'kubectl-version overwrites existing command: "kubectl version"'
    34  
    35    # test plugins that overwrite similarly-named plugins
    36    output_message=$(! PATH=${TEMP_PATH}:"test/fixtures/pkg/kubectl/plugins:test/fixtures/pkg/kubectl/plugins/foo" kubectl plugin list 2>&1)
    37    kube::test::if_has_string "${output_message}" 'test/fixtures/pkg/kubectl/plugins/foo/kubectl-foo is overshadowed by a similarly named plugin'
    38  
    39    # test plugins with no warnings
    40    output_message=$(PATH=${TEMP_PATH}:"test/fixtures/pkg/kubectl/plugins" kubectl plugin list 2>&1)
    41    kube::test::if_has_string "${output_message}" 'plugins are available'
    42  
    43    # no plugins
    44    output_message=$(! PATH=${TEMP_PATH}:"test/fixtures/pkg/kubectl/plugins/empty" kubectl plugin list 2>&1)
    45    kube::test::if_has_string "${output_message}" 'unable to find any kubectl plugins in your PATH'
    46  
    47    # attempt to run a plugin in the user's PATH
    48    output_message=$(PATH=${TEMP_PATH}:"test/fixtures/pkg/kubectl/plugins" kubectl foo)
    49    kube::test::if_has_string "${output_message}" 'plugin foo'
    50  
    51    # check arguments passed to the plugin
    52    output_message=$(PATH=${PATH}:"test/fixtures/pkg/kubectl/plugins/bar" kubectl bar arg1)
    53    kube::test::if_has_string "${output_message}" 'test/fixtures/pkg/kubectl/plugins/bar/kubectl-bar arg1'
    54  
    55    # ensure that a kubectl command supersedes a plugin that overshadows it
    56    output_message=$(PATH=${TEMP_PATH}:"test/fixtures/pkg/kubectl/plugins/version" kubectl version --client)
    57    kube::test::if_has_string "${output_message}" 'Client Version'
    58    kube::test::if_has_not_string "${output_message}" 'overshadows an existing plugin'
    59  
    60    # attempt to run a plugin as a subcommand of kubectl create in the user's PATH
    61    output_message=$(PATH=${TEMP_PATH}:"test/fixtures/pkg/kubectl/plugins/create" kubectl create foo)
    62    kube::test::if_has_string "${output_message}" 'plugin foo as a subcommand of kubectl create command'
    63  
    64    # ensure that a kubectl create cronjob builtin command supersedes a plugin that overshadows it
    65    output_message=$(PATH=${TEMP_PATH}:"test/fixtures/pkg/kubectl/plugins/create" kubectl create cronjob --help)
    66    kube::test::if_has_not_string "${output_message}" 'plugin cronjob as a subcommand of kubectl create command'
    67  
    68    rm -fr "${TEMP_PATH}"
    69  
    70    set +o nounset
    71    set +o errexit
    72  }