k8s.io/kubernetes@v1.29.3/test/cmd/wait.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_wait_tests() {
    22      set -o nounset
    23      set -o errexit
    24  
    25      kube::log::status "Testing kubectl wait"
    26  
    27      create_and_use_new_namespace
    28  
    29      ### Wait for deletion using --all flag
    30  
    31      # create test data
    32      kubectl create deployment test-1 --image=busybox
    33      kubectl create deployment test-2 --image=busybox
    34  
    35      # Post-Condition: deployments exists
    36      kube::test::get_object_assert "deployments" "{{range .items}}{{.metadata.name}},{{end}}" 'test-1,test-2,'
    37  
    38      # wait with jsonpath will timout for busybox deployment
    39      set +o errexit
    40      # Command: Wait with jsonpath support fields not exist in the first place
    41      output_message=$(kubectl wait --for=jsonpath=.status.readyReplicas=1 deploy/test-1 2>&1)
    42      set -o errexit
    43  
    44      # Post-Condition: Wait failed
    45      kube::test::if_has_string "${output_message}" 'timed out'
    46  
    47      # Delete all deployments async to kubectl wait
    48      ( sleep 2 && kubectl delete deployment --all ) &
    49  
    50      # Command: Wait for all deployments to be deleted
    51      output_message=$(kubectl wait deployment --for=delete --all)
    52  
    53      # Post-Condition: Wait was successful
    54      kube::test::if_has_string "${output_message}" 'test-1 condition met'
    55      kube::test::if_has_string "${output_message}" 'test-2 condition met'
    56  
    57      # create test data to test timeout error is occurred in correct time
    58      kubectl apply -f - <<EOF
    59  apiVersion: apps/v1
    60  kind: Deployment
    61  metadata:
    62    labels:
    63      app: dtest
    64    name: dtest
    65  spec:
    66    replicas: 3
    67    selector:
    68      matchLabels:
    69        app: dtest
    70    template:
    71      metadata:
    72        labels:
    73          app: dtest
    74      spec:
    75        containers:
    76        - name: bb
    77          image: busybox
    78          command: ["/bin/sh", "-c", "sleep infinity"]
    79  EOF
    80  
    81      # Make sure deployment is successfully applied
    82      kube::test::wait_object_assert deployments "{{range.items}}{{${id_field:?}}}{{end}}" 'dtest'
    83  
    84      set +o errexit
    85      # wait timeout error because condition is invalid
    86      start_sec=$(date +"%s")
    87      output_message=$(time kubectl wait pod --selector=app=dtest --for=condition=InvalidCondition --timeout=1s 2>&1)
    88      end_sec=$(date +"%s")
    89      len_sec=$((end_sec-start_sec))
    90      set -o errexit
    91      kube::test::if_has_string "${output_message}" 'timed out waiting for the condition '
    92      test $len_sec -ge 1 && test $len_sec -le 2
    93  
    94      # Clean deployment
    95      kubectl delete deployment dtest
    96  
    97      # create test data
    98      kubectl create deployment test-3 --image=busybox
    99  
   100      # wait with jsonpath without value to succeed
   101      set +o errexit
   102      # Command: Wait with jsonpath without value
   103      output_message_0=$(kubectl wait --for=jsonpath='{.status.replicas}' deploy/test-3 2>&1)
   104      # Command: Wait with relaxed jsonpath and filter expression
   105      output_message_1=$(kubectl wait \
   106          --for='jsonpath=spec.template.spec.containers[?(@.name=="busybox")].image=busybox' \
   107          deploy/test-3)
   108      set -o errexit
   109  
   110      # Post-Condition: Wait succeed
   111      kube::test::if_has_string "${output_message_0}" 'deployment.apps/test-3 condition met'
   112      kube::test::if_has_string "${output_message_1}" 'deployment.apps/test-3 condition met'
   113  
   114      # Clean deployment
   115      kubectl delete deployment test-3
   116  
   117      set +o nounset
   118      set +o errexit
   119  }