diff --git a/tests/bin/nc b/tests/bin/nc new file mode 100755 index 0000000..e705ce9 --- /dev/null +++ b/tests/bin/nc @@ -0,0 +1,6 @@ +#!/bin/sh +input="$(cat)" +case "$input" in + -*) echo "nc: bad input: \$input" >&2; exit 1;; +esac +echo "token-foo" diff --git a/tests/bin/wget b/tests/bin/wget index 9afe1b7..22c16be 100755 --- a/tests/bin/wget +++ b/tests/bin/wget @@ -78,8 +78,17 @@ for url; do esac done +host="${url#http*://}" +host="${host%%/*}" +path="${url#http*://$host}" +path="${path#${WGET_STRIP_PREFIX:-/}}" + 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 : "${outfile:=index.html}" diff --git a/tests/fake-wget.test b/tests/fake-wget.test index 053739b..f6bb814 100755 --- a/tests/fake-wget.test +++ b/tests/fake-wget.test @@ -14,7 +14,8 @@ init_tests \ fake_wget_missing_url \ fake_wget_empty_url \ fake_wget_header \ - fake_wget_content_from_path + fake_wget_content_from_path \ + fake_wget_content_from_path_strip_prefix fake_wget_usage_body() { atf_check -s exit:0 \ @@ -70,7 +71,9 @@ fake_wget_stdout_body() { -o match:"hello world" \ -e match:"writing to stdout" \ wget -O - https://example.com - ! test -f - || atf_fail "- was created" + if [ -f - ]; then + atf_fail "- was created" + fi } fake_wget_fail_body() { @@ -104,9 +107,22 @@ fake_wget_header_body() { } fake_wget_content_from_path_body() { - mkdir -p example.com/foo - echo value > example.com_foo_key + cat > example.com.yaml <<-EOF + foo: + key: value + EOF atf_check -s exit:0 \ -o match:"^value$" \ 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 +} diff --git a/tests/imds.test b/tests/imds.test index c99d13a..c36943b 100755 --- a/tests/imds.test +++ b/tests/imds.test @@ -25,29 +25,27 @@ imds_space_body() { 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() { - fake_netcat - aws_set_fake_meta hostname myhostname + fake_metadata_aws <<-EOF + hostname: myhostname + EOF CLOUD=aws atf_check -o match:"myhostname" imds @hostname } imds_aws_local_hostname_body() { - fake_netcat - aws_set_fake_meta local-hostname myhostname + fake_metadata_aws <<-EOF + local-hostname: myhostname + EOF CLOUD=aws atf_check -o match:"myhostname" imds @local-hostname } imds_aws_ssh_keys_body() { - fake_netcat - aws_set_fake_meta public-keys 0=testuser - aws_set_fake_meta public-keys/0/openssh-key "ssh-ed25519 keydata" - + fake_metadata_aws <<-EOF + public-keys: + 0=testuser: + 0: + openssh-key: ssh-ed25519 keydata + EOF CLOUD=aws atf_check -o match:"ssh-ed25519 keydata" imds @ssh-keys } diff --git a/tests/test_env.sh b/tests/test_env.sh index 41d891d..eb222aa 100644 --- a/tests/test_env.sh +++ b/tests/test_env.sh @@ -63,6 +63,11 @@ fake_userdata_nocloud() { fake_data_nocloud user-data } +fake_metadata_aws() { + cat > "169.254.169.254.yaml" + export WGET_STRIP_PREFIX="/latest/meta-data" +} + fake_interfaces() { local n=1 for i; do @@ -72,14 +77,3 @@ fake_interfaces() { n=$((n+1)) 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 -} diff --git a/tests/tiny-cloud.test b/tests/tiny-cloud.test index 3f98423..c06730f 100755 --- a/tests/tiny-cloud.test +++ b/tests/tiny-cloud.test @@ -27,7 +27,6 @@ tiny_cloud_help_body() { } no_metadata_early_body() { - fake_netcat for provider in $PROVIDERS; do CLOUD="$provider" atf_check \ -e not-match:"unknown" \ @@ -39,7 +38,6 @@ no_metadata_early_body() { } no_userdata_net_body() { - fake_netcat for provider in $PROVIDERS; do CLOUD="$provider" atf_check \ -e not-match:"unknown" \ @@ -49,7 +47,6 @@ no_userdata_net_body() { } no_userdata_main_body() { - fake_netcat for provider in $PROVIDERS; do # we should not set empty hostname # we should not create .ssh dir for non-existing user @@ -68,7 +65,6 @@ no_userdata_main_body() { } no_userdata_final_body() { - fake_netcat for provider in $PROVIDERS; do CLOUD="$provider" atf_check \ -e not-match:"unknown" \