github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/tests/suites/spaces_ec2/task.sh (about)

     1  test_spaces_ec2() {
     2  	if [ "$(skip 'test_spaces_ec2')" ]; then
     3  		echo "==> TEST SKIPPED: space tests (EC2)"
     4  		return
     5  	fi
     6  
     7  	# Ensure that the aws cli and juju both use the same aws region
     8  	export AWS_DEFAULT_REGION="${BOOTSTRAP_REGION}"
     9  
    10  	set_verbosity
    11  
    12  	echo "==> Checking for dependencies"
    13  	check_dependencies juju aws
    14  
    15  	echo "==> Ensure subnet for alternative space exists"
    16  	subnet_id=$(ensure_subnet)
    17  
    18  	echo "==> Setting up additional EC2 resources for tests"
    19  	hotplug_nic_id=$(setup_nic_for_space_tests "${subnet_id}")
    20  	echo "[+] created secondary NIC with ID ${hotplug_nic_id}"
    21  
    22  	file="${TEST_DIR}/test-spaces-ec2.log"
    23  
    24  	bootstrap "test-spaces-ec2" "${file}"
    25  
    26  	test_machines_in_spaces
    27  	test_upgrade_charm_with_bind "$hotplug_nic_id"
    28  	test_juju_bind "$hotplug_nic_id"
    29  
    30  	destroy_controller "test-spaces-ec2"
    31  
    32  	# We don't really care if this fails; stale NICs will be auto-purged the
    33  	# next time we run the test-suite
    34  	# shellcheck disable=SC2086
    35  	aws ec2 delete-network-interface --network-interface-id "$hotplug_nic_id" 2>/dev/null || true
    36  }
    37  
    38  ensure_subnet() {
    39  	isolated_subnet_id=$(aws ec2 describe-subnets --filters Name=cidr-block,Values=172.31.254.0/24 2>/dev/null | jq -r '.Subnets[0].SubnetId')
    40  	if [ "$isolated_subnet_id" != "null" ]; then
    41  		cleanup_stale_nics
    42  		echo "$isolated_subnet_id"
    43  		return
    44  	fi
    45  
    46  	# Create a subnet in the default vpc
    47  	vpc_id=$(aws ec2 describe-vpcs | jq -r ".Vpcs[0].VpcId")
    48  	subnet_id=$(aws ec2 create-subnet --vpc-id "${vpc_id}" --cidr-block "172.31.254.0/24" | jq -r ".Subnet.SubnetId")
    49  	if [ -z "${subnet_id}" ] || [ "${subnet_id}" == "null" ]; then
    50  		echo "$(red "failed to create subnet in vpc $vpc_id")" 1>&2
    51  		exit 1
    52  	fi
    53  	echo "${subnet_id}"
    54  }
    55  
    56  setup_nic_for_space_tests() {
    57  	isolated_subnet_id=${1}
    58  	hotplug_nic_id=$(aws ec2 create-network-interface --subnet-id "$isolated_subnet_id" --description="hot-pluggable NIC for space tests" 2>"${TEST_DIR}/create-network-interface-stderr.log" | jq -r '.NetworkInterface.NetworkInterfaceId')
    59  	if [ -z "$hotplug_nic_id" ] || [ "$hotplug_nic_id" == "null" ]; then
    60  		# shellcheck disable=SC2046
    61  		echo $(red "Unable to create extra NIC for space tests; please check that your account has permissions to create NICs. Failed with:") 1>&2
    62  		cat "${TEST_DIR}/create-network-interface-stderr.log" 1>&2
    63  		exit 1
    64  	fi
    65  
    66  	# Add a created_at tag so we can use a background job to clean forgotten NICs
    67  	aws ec2 create-tags --resources "$hotplug_nic_id" --tags Key=created_at,Value="$(date +'%Y-%m-%d')"
    68  
    69  	echo "$hotplug_nic_id"
    70  }
    71  
    72  cleanup_stale_nics() {
    73  	# Each NIC is tagged with a created_at key and the "YYYY-MM-DD" value
    74  	# corresponding to the NIC creation date. Since we don't know whether
    75  	# other tests are running concurrently, we simply iterate the list of
    76  	# custom NIC interfaces, filter *out* the ones with today's date and
    77  	# try to delete anything older in a best effort manner.
    78  
    79  	# TODO(jack-w-shaw) fix this. This:
    80  	# 1) Should use jq
    81  	# 2) Should work. At the moment the created_at tag is not created, so all nics are destroyed
    82  	aws ec2 describe-network-interfaces --filter Name=description,Values="hot-pluggable NIC for space tests" |
    83  		grep 'NetworkInterfaceId\|Value' |
    84  		cut -d'"' -f4 |
    85  		paste - - -d, |
    86  		grep -v "$(date +'%Y-%m-%d')" |
    87  		cut -d',' -f 1 |
    88  		xargs -I % echo 'echo "[!] attempting to remove stale NIC % (will retry later if currently in-use)" 1>&2; aws ec2 delete-network-interface --network-interface-id % 2>/dev/null' |
    89  		sh ||
    90  		true
    91  }