1
0
mirror of https://gitlab.alpinelinux.org/alpine/cloud/tiny-cloud.git synced 2026-02-04 04:22:43 +03:00

Refactor aws tests' fake meta-data

Refactor fake wget to use a yaml so we can set aws fake meta-data
similar to how we set nocloud fake meta-data.

Also move fake netcat to tests/bin/nc so its always there and we don't
need to remember to call fake_netcat when needed.
This commit is contained in:
Natanael Copa 2023-05-17 17:27:13 +02:00 committed by Jake Buchholz Göktürk
parent a90bf444d3
commit 0f40c146f2
6 changed files with 53 additions and 34 deletions

6
tests/bin/nc Executable file
View File

@ -0,0 +1,6 @@
#!/bin/sh
input="$(cat)"
case "$input" in
-*) echo "nc: bad input: \$input" >&2; exit 1;;
esac
echo "token-foo"

View File

@ -78,8 +78,17 @@ for url; do
esac esac
done done
host="${url#http*://}"
host="${host%%/*}"
path="${url#http*://$host}"
path="${path#${WGET_STRIP_PREFIX:-/}}"
if [ -z "$WGETCONTENT" ]; then if [ -z "$WGETCONTENT" ]; then
WGETCONTENT=$( cat "$(echo ${url#http*://} | tr '/' '_')" 2>/dev/null) (
IFS=/
set -- ${path#/}
yx -f "${WGET_YAML:-$host.yaml}" "$@" 2>/dev/null
)
fi fi
: "${outfile:=index.html}" : "${outfile:=index.html}"

View File

@ -14,7 +14,8 @@ init_tests \
fake_wget_missing_url \ fake_wget_missing_url \
fake_wget_empty_url \ fake_wget_empty_url \
fake_wget_header \ fake_wget_header \
fake_wget_content_from_path fake_wget_content_from_path \
fake_wget_content_from_path_strip_prefix
fake_wget_usage_body() { fake_wget_usage_body() {
atf_check -s exit:0 \ atf_check -s exit:0 \
@ -70,7 +71,9 @@ fake_wget_stdout_body() {
-o match:"hello world" \ -o match:"hello world" \
-e match:"writing to stdout" \ -e match:"writing to stdout" \
wget -O - https://example.com wget -O - https://example.com
! test -f - || atf_fail "- was created" if [ -f - ]; then
atf_fail "- was created"
fi
} }
fake_wget_fail_body() { fake_wget_fail_body() {
@ -104,9 +107,22 @@ fake_wget_header_body() {
} }
fake_wget_content_from_path_body() { fake_wget_content_from_path_body() {
mkdir -p example.com/foo cat > example.com.yaml <<-EOF
echo value > example.com_foo_key foo:
key: value
EOF
atf_check -s exit:0 \ atf_check -s exit:0 \
-o match:"^value$" \ -o match:"^value$" \
wget --quiet -O - https://example.com/foo/key wget --quiet -O - https://example.com/foo/key
} }
fake_wget_content_from_path_strip_prefix_body() {
cat > 169.254.169.254.yaml <<-EOF
foo:
key: value
EOF
export WGET_STRIP_PREFIX="/latest/meta-data"
atf_check -s exit:0 \
-o match:"^value$" \
wget --quiet -O - https://169.254.169.254/latest/meta-data/foo/key
}

View File

@ -25,29 +25,27 @@ imds_space_body() {
CLOUD=nocloud atf_check -o match:'^$' imds +n CLOUD=nocloud atf_check -o match:'^$' imds +n
} }
aws_set_fake_meta() {
local key="$1" value="$2"
echo "$value" > $(echo 169.254.169.254/latest/meta-data/"$key" \
| tr '/' '_')
}
imds_aws_hostname_body() { imds_aws_hostname_body() {
fake_netcat fake_metadata_aws <<-EOF
aws_set_fake_meta hostname myhostname hostname: myhostname
EOF
CLOUD=aws atf_check -o match:"myhostname" imds @hostname CLOUD=aws atf_check -o match:"myhostname" imds @hostname
} }
imds_aws_local_hostname_body() { imds_aws_local_hostname_body() {
fake_netcat fake_metadata_aws <<-EOF
aws_set_fake_meta local-hostname myhostname local-hostname: myhostname
EOF
CLOUD=aws atf_check -o match:"myhostname" imds @local-hostname CLOUD=aws atf_check -o match:"myhostname" imds @local-hostname
} }
imds_aws_ssh_keys_body() { imds_aws_ssh_keys_body() {
fake_netcat fake_metadata_aws <<-EOF
aws_set_fake_meta public-keys 0=testuser public-keys:
aws_set_fake_meta public-keys/0/openssh-key "ssh-ed25519 keydata" 0=testuser:
0:
openssh-key: ssh-ed25519 keydata
EOF
CLOUD=aws atf_check -o match:"ssh-ed25519 keydata" imds @ssh-keys CLOUD=aws atf_check -o match:"ssh-ed25519 keydata" imds @ssh-keys
} }

View File

@ -63,6 +63,11 @@ fake_userdata_nocloud() {
fake_data_nocloud user-data fake_data_nocloud user-data
} }
fake_metadata_aws() {
cat > "169.254.169.254.yaml"
export WGET_STRIP_PREFIX="/latest/meta-data"
}
fake_interfaces() { fake_interfaces() {
local n=1 local n=1
for i; do for i; do
@ -72,14 +77,3 @@ fake_interfaces() {
n=$((n+1)) n=$((n+1))
done done
} }
fake_netcat() {
fake_bin nc <<-EOF
#!/bin/sh
input="\$(cat)"
case "\$input" in
-*) echo "nc: bad input: \$input" >&2; exit 1;;
esac
echo "token-foo"
EOF
}

View File

@ -27,7 +27,6 @@ tiny_cloud_help_body() {
} }
no_metadata_early_body() { no_metadata_early_body() {
fake_netcat
for provider in $PROVIDERS; do for provider in $PROVIDERS; do
CLOUD="$provider" atf_check \ CLOUD="$provider" atf_check \
-e not-match:"unknown" \ -e not-match:"unknown" \
@ -39,7 +38,6 @@ no_metadata_early_body() {
} }
no_userdata_net_body() { no_userdata_net_body() {
fake_netcat
for provider in $PROVIDERS; do for provider in $PROVIDERS; do
CLOUD="$provider" atf_check \ CLOUD="$provider" atf_check \
-e not-match:"unknown" \ -e not-match:"unknown" \
@ -49,7 +47,6 @@ no_userdata_net_body() {
} }
no_userdata_main_body() { no_userdata_main_body() {
fake_netcat
for provider in $PROVIDERS; do for provider in $PROVIDERS; do
# we should not set empty hostname # we should not set empty hostname
# we should not create .ssh dir for non-existing user # we should not create .ssh dir for non-existing user
@ -68,7 +65,6 @@ no_userdata_main_body() {
} }
no_userdata_final_body() { no_userdata_final_body() {
fake_netcat
for provider in $PROVIDERS; do for provider in $PROVIDERS; do
CLOUD="$provider" atf_check \ CLOUD="$provider" atf_check \
-e not-match:"unknown" \ -e not-match:"unknown" \