github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/integration/kubernetes/k8s-cpu-ns.bats (about)

     1  #!/usr/bin/env bats
     2  #
     3  # Copyright (c) 2018 Intel Corporation
     4  #
     5  # SPDX-License-Identifier: Apache-2.0
     6  #
     7  
     8  load "${BATS_TEST_DIRNAME}/../../.ci/lib.sh"
     9  load "${BATS_TEST_DIRNAME}/tests_common.sh"
    10  
    11  setup() {
    12  	[ "${KATA_HYPERVISOR}" == "firecracker" ] && skip "test not working see: ${fc_limitations}"
    13  
    14  	pod_name="constraints-cpu-test"
    15  	container_name="first-cpu-container"
    16  	sharessyspath="/sys/fs/cgroup/cpu/cpu.shares"
    17  	quotasyspath="/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
    18  	periodsyspath="/sys/fs/cgroup/cpu/cpu.cfs_period_us"
    19  	total_cpus=2
    20  	total_requests=512
    21  	total_cpu_container=1
    22  
    23  	get_pod_config_dir
    24  }
    25  
    26  @test "Check CPU constraints" {
    27  	[ "${KATA_HYPERVISOR}" == "firecracker" ] && skip "test not working see: ${fc_limitations}"
    28  
    29  	# Create the pod
    30  	kubectl create -f "${pod_config_dir}/pod-cpu.yaml"
    31  
    32  	# Check pod creation
    33  	kubectl wait --for=condition=Ready --timeout=$timeout pod "$pod_name"
    34  
    35  	retries="10"
    36  
    37  	num_cpus_cmd='grep -e "^processor" /proc/cpuinfo |wc -l'
    38  	# Check the total of cpus
    39  	for _ in $(seq 1 "$retries"); do
    40  		# Get number of cpus
    41  		total_cpus_container=$(kubectl exec pod/"$pod_name" -c "$container_name" \
    42  			-- sh -c "$num_cpus_cmd")
    43  		# Verify number of cpus
    44  		[ "$total_cpus_container" -le "$total_cpus" ]
    45  		[ "$total_cpus_container" -eq "$total_cpus" ] && break
    46  		sleep 1
    47  	done
    48  	[ "$total_cpus_container" -eq "$total_cpus" ]
    49  
    50  	# Check the total of requests
    51  	total_requests_container=$(kubectl exec $pod_name -c $container_name \
    52  		-- sh -c "cat $sharessyspath")
    53  
    54  	[ "$total_requests_container" -eq "$total_requests" ]
    55  
    56  	# Check the cpus inside the container
    57  
    58  	total_cpu_quota=$(kubectl exec $pod_name -c $container_name \
    59  		-- sh -c "cat $quotasyspath")
    60  
    61  	total_cpu_period=$(kubectl exec $pod_name -c $container_name \
    62  		-- sh -c "cat $periodsyspath")
    63  
    64  	division_quota_period=$(echo $((total_cpu_quota/total_cpu_period)))
    65  
    66  	[ "$division_quota_period" -eq "$total_cpu_container" ]
    67  }
    68  
    69  teardown() {
    70  	[ "${KATA_HYPERVISOR}" == "firecracker" ] && skip "test not working see: ${fc_limitations}"
    71  
    72  	# Debugging information
    73  	kubectl describe "pod/$pod_name"
    74  
    75  	kubectl delete pod "$pod_name"
    76  }