tests: k8s: k8s-attach-handlers generated policy

Automatically generate the test policy for k8s-attach-handlers.bats,
if AUTO_GENERATE_POLICY is enabled.

Steps:

- Create a temporary directory for the current test and copy the
  common genpolicy settings into this new directory.

- Change genpolicy settings in the temp directory to allow the
  "kubectl exec" command that this test needs. (For CoCo, exec is
  blocked by the default policy settings)

- Auto-generate the policy for the test YAML file.

- Test as usual, using the YAML file.

- Clean-up the temporary settings described above.

Fixes: #8921

Signed-off-by: Dan Mihai <dmihai@microsoft.com>
This commit is contained in:
Dan Mihai 2024-02-01 21:42:23 +00:00
parent 0de407f8b7
commit dd16bc393f

View File

@ -15,28 +15,39 @@ setup() {
pod_name="handlers"
get_pod_config_dir
yaml_file="${pod_config_dir}/test-lifecycle-events.yaml"
# Create yaml
sed -e "s/\${nginx_version}/${nginx_image}/" \
"${pod_config_dir}/lifecycle-events.yaml" > "${yaml_file}"
# Add policy to yaml
policy_settings_dir="$(create_tmp_policy_settings_dir "${pod_config_dir}")"
display_message="cat /usr/share/message"
exec_command="sh -c ${display_message}"
add_exec_to_policy_settings "${policy_settings_dir}" "${exec_command}"
auto_generate_policy "${policy_settings_dir}" "${yaml_file}"
}
@test "Running with postStart and preStop handlers" {
# Create yaml
sed -e "s/\${nginx_version}/${nginx_image}/" \
"${pod_config_dir}/lifecycle-events.yaml" > "${pod_config_dir}/test-lifecycle-events.yaml"
# Create the pod with postStart and preStop handlers
kubectl create -f "${pod_config_dir}/test-lifecycle-events.yaml"
kubectl create -f "${yaml_file}"
# Check pod creation
kubectl wait --for=condition=Ready --timeout=$timeout pod $pod_name
# Check postStart message
display_message="cat /usr/share/message"
check_postStart=$(kubectl exec $pod_name -- sh -c "$display_message" | grep "Hello from the postStart handler")
check_postStart=$(kubectl exec $pod_name -- sh -c "$display_message")
echo "check_postStart=$check_postStart"
echo "$check_postStart" | grep "Hello from the postStart handler"
}
teardown(){
# Debugging information
kubectl describe "pod/$pod_name"
rm -f "${pod_config_dir}/test-lifecycle-events.yaml"
rm -f "${yaml_file}"
kubectl delete pod "$pod_name"
delete_tmp_policy_settings_dir "${policy_settings_dir}"
}