Skip to content

Commit

Permalink
Merge pull request kata-containers#9128 from microsoft/danmihai1/test…
Browse files Browse the repository at this point in the history
…-genpolicy

tests: k8s: auto-generated policy
  • Loading branch information
danmihai1 authored Mar 7, 2024
2 parents a0a50f5 + c08b696 commit b3a02d5
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 24 deletions.
14 changes: 11 additions & 3 deletions tests/integration/kubernetes/k8s-parallel.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ setup() {
get_pod_config_dir
job_name="jobtest"
names=( "test1" "test2" "test3" )
}

@test "Parallel jobs" {
# Create genpolicy settings - common for all of the test jobs
policy_settings_dir="$(create_tmp_policy_settings_dir "${pod_config_dir}")"
add_requests_to_policy_settings "${policy_settings_dir}" "ReadStreamRequest"

# Create yaml files
for i in "${names[@]}"; do
sed "s/\$ITEM/$i/" ${pod_config_dir}/job-template.yaml > ${pod_config_dir}/job-$i.yaml
yaml_file="${pod_config_dir}/job-$i.yaml"
sed "s/\$ITEM/$i/" ${pod_config_dir}/job-template.yaml > ${yaml_file}
auto_generate_policy "${policy_settings_dir}" "${yaml_file}"
done
}

@test "Parallel jobs" {
# Create the jobs
for i in "${names[@]}"; do
kubectl create -f "${pod_config_dir}/job-$i.yaml"
Expand All @@ -45,4 +51,6 @@ teardown() {
for i in "${names[@]}"; do
rm -f ${pod_config_dir}/job-$i.yaml
done

delete_tmp_policy_settings_dir "${policy_settings_dir}"
}
25 changes: 22 additions & 3 deletions tests/integration/kubernetes/k8s-qos-pods.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ setup() {

@test "Guaranteed QoS" {
pod_name="qos-test"
yaml_file="${pod_config_dir}/pod-guaranteed.yaml"

# Add policy to the yaml file
policy_settings_dir="$(create_tmp_policy_settings_dir "${pod_config_dir}")"
add_requests_to_policy_settings "${policy_settings_dir}" "ReadStreamRequest"
auto_generate_policy "${policy_settings_dir}" "${yaml_file}"

# Create pod
kubectl create -f "${pod_config_dir}/pod-guaranteed.yaml"
kubectl create -f "${yaml_file}"

# Check pod creation
kubectl wait --for=condition=Ready --timeout=$timeout pod "$pod_name"
Expand All @@ -29,9 +35,15 @@ setup() {

@test "Burstable QoS" {
pod_name="burstable-test"
yaml_file="${pod_config_dir}/pod-burstable.yaml"

# Add policy to the yaml file
policy_settings_dir="$(create_tmp_policy_settings_dir "${pod_config_dir}")"
add_requests_to_policy_settings "${policy_settings_dir}" "ReadStreamRequest"
auto_generate_policy "${policy_settings_dir}" "${yaml_file}"

# Create pod
kubectl create -f "${pod_config_dir}/pod-burstable.yaml"
kubectl create -f "${yaml_file}"

# Check pod creation
kubectl wait --for=condition=Ready --timeout=$timeout pod "$pod_name"
Expand All @@ -42,9 +54,15 @@ setup() {

@test "BestEffort QoS" {
pod_name="besteffort-test"
yaml_file="${pod_config_dir}/pod-besteffort.yaml"

# Add policy to the yaml file
policy_settings_dir="$(create_tmp_policy_settings_dir "${pod_config_dir}")"
add_requests_to_policy_settings "${policy_settings_dir}" "ReadStreamRequest"
auto_generate_policy "${policy_settings_dir}" "${yaml_file}"

# Create pod
kubectl create -f "${pod_config_dir}/pod-besteffort.yaml"
kubectl create -f "${yaml_file}"

# Check pod creation
kubectl wait --for=condition=Ready --timeout=$timeout pod "$pod_name"
Expand All @@ -55,4 +73,5 @@ setup() {

teardown() {
kubectl delete pod "$pod_name"
delete_tmp_policy_settings_dir "${policy_settings_dir}"
}
19 changes: 13 additions & 6 deletions tests/integration/kubernetes/k8s-replication.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@ setup() {
nginx_image="nginx:$nginx_version"

get_pod_config_dir

# Create yaml
test_yaml="${pod_config_dir}/test-replication-controller.yaml"
sed -e "s/\${nginx_version}/${nginx_image}/" \
"${pod_config_dir}/replication-controller.yaml" > "${test_yaml}"

# Add policy to the yaml file
policy_settings_dir="$(create_tmp_policy_settings_dir "${pod_config_dir}")"
add_requests_to_policy_settings "${policy_settings_dir}" "ReadStreamRequest"
auto_generate_policy "${policy_settings_dir}" "${test_yaml}"
}

@test "Replication controller" {
replication_name="replicationtest"

# Create yaml
sed -e "s/\${nginx_version}/${nginx_image}/" \
"${pod_config_dir}/replication-controller.yaml" > "${pod_config_dir}/test-replication-controller.yaml"

# Create replication controller
kubectl create -f "${pod_config_dir}/test-replication-controller.yaml"
kubectl create -f "${test_yaml}"

# Check replication controller
local cmd="kubectl describe replicationcontrollers/$replication_name | grep replication-controller"
Expand Down Expand Up @@ -57,6 +63,7 @@ teardown() {
# Debugging information
kubectl describe replicationcontrollers/"$replication_name"

rm -f "${pod_config_dir}/test-replication-controller.yaml"
rm -f "${test_yaml}"
kubectl delete rc "$replication_name"
delete_tmp_policy_settings_dir "${policy_settings_dir}"
}
19 changes: 13 additions & 6 deletions tests/integration/kubernetes/k8s-scale-nginx.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ setup() {
replicas="3"
deployment="nginx-deployment"
get_pod_config_dir
}

@test "Scale nginx deployment" {

# Create the yaml file
test_yaml="${pod_config_dir}/test-${deployment}.yaml"
sed -e "s/\${nginx_version}/${nginx_image}/" \
"${pod_config_dir}/${deployment}.yaml" > "${pod_config_dir}/test-${deployment}.yaml"
"${pod_config_dir}/${deployment}.yaml" > "${test_yaml}"

kubectl create -f "${pod_config_dir}/test-${deployment}.yaml"
# Add policy to the yaml file
policy_settings_dir="$(create_tmp_policy_settings_dir "${pod_config_dir}")"
add_requests_to_policy_settings "${policy_settings_dir}" "ReadStreamRequest"
auto_generate_policy "${policy_settings_dir}" "${test_yaml}"
}

@test "Scale nginx deployment" {
kubectl create -f "${test_yaml}"
kubectl wait --for=condition=Available --timeout=$timeout deployment/${deployment}
kubectl expose deployment/${deployment}
kubectl scale deployment/${deployment} --replicas=${replicas}
Expand All @@ -30,7 +36,8 @@ setup() {
}

teardown() {
rm -f "${pod_config_dir}/test-${deployment}.yaml"
rm -f "${test_yaml}"
kubectl delete deployment "$deployment"
kubectl delete service "$deployment"
delete_tmp_policy_settings_dir "${policy_settings_dir}"
}
33 changes: 27 additions & 6 deletions tests/integration/kubernetes/k8s-shared-volume.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,57 @@ setup() {
pod_name="test-shared-volume"
first_container_name="busybox-first-container"
second_container_name="busybox-second-container"
cmd="cat /tmp/pod-data"
yaml_file="${pod_config_dir}/pod-shared-volume.yaml"

# Add policy to the yaml file
policy_settings_dir="$(create_tmp_policy_settings_dir "${pod_config_dir}")"

exec_command="sh -c ${cmd}"
add_exec_to_policy_settings "${policy_settings_dir}" "${exec_command}"

add_requests_to_policy_settings "${policy_settings_dir}" "ReadStreamRequest"
auto_generate_policy "${policy_settings_dir}" "${yaml_file}"

# Create pod
kubectl create -f "${pod_config_dir}/pod-shared-volume.yaml"
kubectl create -f "${yaml_file}"

# Check pods
kubectl wait --for=condition=Ready --timeout=$timeout pod $pod_name

# Communicate containers
cmd="cat /tmp/pod-data"
msg="Hello from the $second_container_name"
kubectl exec "$pod_name" -c "$first_container_name" -- sh -c "$cmd" | grep "$msg"
}

@test "initContainer with shared volume" {
pod_name="initcontainer-shared-volume"
last_container="last"
cmd='test $(cat /volume/initContainer) -lt $(cat /volume/container)'
yaml_file="${pod_config_dir}/initContainer-shared-volume.yaml"

# Add policy to the yaml file
policy_settings_dir="$(create_tmp_policy_settings_dir "${pod_config_dir}")"

exec_command="sh -c ${cmd}"
add_exec_to_policy_settings "${policy_settings_dir}" "${exec_command}"

add_requests_to_policy_settings "${policy_settings_dir}" "ReadStreamRequest"
auto_generate_policy "${policy_settings_dir}" "${yaml_file}"

# Create pod
kubectl create -f "${pod_config_dir}/initContainer-shared-volume.yaml"
kubectl create -f "${yaml_file}"

# Check pods
kubectl wait --for=condition=Ready --timeout=$timeout pod $pod_name

cmd='test $(cat /volume/initContainer) -lt $(cat /volume/container)'
kubectl exec "$pod_name" -c "$last_container" -- sh -c "$cmd"
}

teardown() {
# Debugging information
kubectl describe "pod/$pod_name"
kubectl describe "pod/$pod_name" || true

kubectl delete pod "$pod_name"
kubectl delete pod "$pod_name" || true
delete_tmp_policy_settings_dir "${policy_settings_dir}"
}

0 comments on commit b3a02d5

Please sign in to comment.