Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit 86f87e2

Browse files
authored
Provision resources for service introspection test (#196)
1 parent d61c19e commit 86f87e2

File tree

3 files changed

+107
-4
lines changed

3 files changed

+107
-4
lines changed

tests/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ If you wish to run these tests locally, skip ahead to
107107
- AZDO_ORG (e.g. `epicstuff`)
108108
- AZDO_PAT (e.g. Personal Access Token with **read/write/manage** access to
109109
AZDO_PROJECT) <-- 🔒
110-
- AZ_RESOURCE_GROUP - The name of an Azure resource group (for
111-
`introspection-validations.sh`)
110+
- AZ_RESOURCE_GROUP - The name of an Azure resource group
111+
- AZ_STORAGE_ACCOUNT - The name of an Azure storage account
112112
- SP_APP_ID (e.g Service Principal App Id)
113113
- SP_PASS (e.g Service Principal Password) <-- 🔒
114114
- SP_TENANT (e.g Service Principal Tenant Id)
@@ -163,7 +163,8 @@ pipeline. Instead run these steps:
163163
export SP_PASS=<b>REPLACE_ME</b>
164164
export SP_APP_ID=<b>REPLACE_ME</b>
165165
export SP_TENANT=<b>REPLACE_ME</b>
166-
export AZ_SUBSCRIPTION_ID=<b>REPLACE_ME</b>
166+
export AZ_RESOURCE_GROUP=<b>REPLACE_ME</b>
167+
export AZ_STORAGE_ACCOUNT=<b>REPLACE_ME</b>
167168
</pre>
168169
4. Navigate to this directory in shell
169170
5. RUN --> `$ sh validations.sh`

tests/functions.sh

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,22 @@ function variable_group_exists () {
150150
fi
151151
}
152152

153+
function variable_group_variable_create () {
154+
id=$1
155+
org=$2
156+
p=$3
157+
n=$4
158+
v=$5
159+
s=$6
160+
161+
echo "Create variable '$n' in variable group"
162+
if [ "$s" = "secret" ]; then
163+
result=$(az pipelines variable-group variable create --id $id --org $org -p $p --name $n --value $v --secret)
164+
else
165+
result=$(az pipelines variable-group variable create --id $id --org $org -p $p --name $n --value $v)
166+
fi
167+
}
168+
153169
function storage_account_exists () {
154170
sa_name=$1
155171
rg=$2
@@ -168,6 +184,10 @@ function storage_account_exists () {
168184
if [ "$action" == "fail" ]; then
169185
exit 1
170186
fi
187+
if [ "$action" == "create" ]; then
188+
echo "Create storage account '$sa_name'"
189+
az storage account create -n $sa_name -g $rg
190+
fi
171191
fi
172192
}
173193

@@ -185,6 +205,67 @@ function storage_account_table_exists () {
185205
if [ "$action" == "fail" ]; then
186206
exit 1
187207
fi
208+
if [ "$action" == "create" ]; then
209+
echo "Create table $t"
210+
az storage table create -n $t --account-name $sa_name
211+
total_wait_seconds=20
212+
start=0
213+
wait_seconds=5
214+
while [ $start -lt $total_wait_seconds ]; do
215+
sat_result=$(az storage table exists -n $t --account-name $sa_name)
216+
sat_exists=$(echo $sat_result | jq '.exists | . == true')
217+
if [ "$sat_exists" = "true" ]; then
218+
echo "The table '$t' was created"
219+
break
220+
fi
221+
echo "Wait $wait_seconds seconds..."
222+
sleep $wait_seconds
223+
az storage table create -n $t --account-name $sa_name
224+
start=$((start + wait_seconds))
225+
done
226+
if [ "$sat_exists" != "true" ]; then
227+
echo "The table '$t' could not be created"
228+
exit 1
229+
fi
230+
fi
231+
fi
232+
}
233+
234+
function storage_account_cors_enabled () {
235+
sa_name=$1
236+
action=$2
237+
cors_enabled_result=$(az storage cors list --services t --account-name $sa_name | jq '.[] | select((.Service=="table") and (.AllowedMethods=="GET") and (.AllowedOrigins=="http://localhost:4040")) != null')
238+
239+
if [ "$cors_enabled_result" = "true" ]; then
240+
echo "The storage account '$sa_name' has cors enabled"
241+
else
242+
echo "The storage account '$sa_name' does not have cors enabled"
243+
if [ "$action" == "fail" ]; then
244+
exit 1
245+
fi
246+
if [ "$action" == "enable" ]; then
247+
echo "Enable cors in storage account '$sa_name'"
248+
az storage cors add --methods "GET" --origins "http://localhost:4040" --services t --allowed-headers "*" --exposed-headers "*" --account-name $sa_name
249+
fi
250+
if [ "$action" == "wait" ]; then
251+
total_wait_seconds=25
252+
start=0
253+
wait_seconds=5
254+
while [ $start -lt $total_wait_seconds ]; do
255+
cors_enabled_result=$(az storage cors list --services t --account-name $sa_name | jq '.[] | select((.Service=="table") and (.AllowedMethods=="GET") and (.AllowedOrigins=="http://localhost:4040")) != null')
256+
if [ "$cors_enabled_result" = "true" ]; then
257+
echo "The storage account '$sa_name' has cors enabled"
258+
break
259+
fi
260+
echo "Wait $wait_seconds seconds..."
261+
sleep $wait_seconds
262+
start=$((start + wait_seconds))
263+
done
264+
if [ "$cors_enabled_result" != "true" ]; then
265+
echo "The storage account '$sa_name' does not have cors enabled"
266+
exit 1
267+
fi
268+
fi
188269
fi
189270
}
190271

tests/validations.sh

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@ TEST_WORKSPACE="$(pwd)/spk-env"
1515
[ ! -z "$SP_APP_ID" ] || { echo "Provide SP_APP_ID"; exit 1;}
1616
[ ! -z "$SP_PASS" ] || { echo "Provide SP_PASS"; exit 1;}
1717
[ ! -z "$SP_TENANT" ] || { echo "Provide SP_TENANT"; exit 1;}
18+
[ ! -z "$AZ_RESOURCE_GROUP" ] || { echo "Provide AZ_RESOURCE_GROUP"; exit 1;}
19+
[ ! -z "$AZ_STORAGE_ACCOUNT" ] || { echo "Provide AZ_STORAGE_ACCOUNT"; exit 1;}
1820
AZDO_ORG_URL="${AZDO_ORG_URL:-"https://dev.azure.com/$AZDO_ORG"}"
1921

20-
2122
echo "TEST_WORKSPACE: $TEST_WORKSPACE"
2223
echo "SPK_LOCATION: $SPK_LOCATION"
2324
echo "AZDO_PROJECT: $AZDO_PROJECT"
2425
echo "AZDO_ORG: $AZDO_ORG"
2526
echo "AZDO_ORG_URL: $AZDO_ORG_URL"
2627
echo "ACR_NAME: $ACR_NAME"
28+
echo "AZ_RESOURCE_GROUP: $AZ_RESOURCE_GROUP"
29+
echo "AZ_STORAGE_ACCOUNT: $AZ_STORAGE_ACCOUNT"
2730

2831
branchName=myFeatureBranch
2932
FrontEnd=fabrikam.acme.frontend
@@ -50,6 +53,17 @@ fi
5053

5154
cd $TEST_WORKSPACE
5255

56+
# Introspection Storage Account Setup
57+
sat_name=fabrikamdeployments
58+
sa_partition_key="integration-test"
59+
60+
storage_account_exists $AZ_STORAGE_ACCOUNT $AZ_RESOURCE_GROUP "fail"
61+
storage_account_cors_enabled $AZ_STORAGE_ACCOUNT "enable"
62+
storage_account_cors_enabled $AZ_STORAGE_ACCOUNT "wait"
63+
storage_account_table_exists $sat_name $AZ_STORAGE_ACCOUNT "create"
64+
storage_account_table_exists $sat_name $AZ_STORAGE_ACCOUNT "fail"
65+
sa_access_key=$(az storage account keys list -n $AZ_STORAGE_ACCOUNT -g $AZ_RESOURCE_GROUP | jq '.[0].value')
66+
5367
# Manifest Repo Setup ------------------
5468
mkdir $manifests_dir
5569
cd $manifests_dir
@@ -153,6 +167,13 @@ spk project create-variable-group $vg_name -r $ACR_NAME -d $hld_repo_url -u $SP_
153167
# Verify the variable group was created. Fail if not
154168
variable_group_exists $AZDO_ORG_URL $AZDO_PROJECT $vg_name "fail"
155169

170+
# Add introspection variables to variable group
171+
variable_group_id=$(az pipelines variable-group list --org $AZDO_ORG_URL -p $AZDO_PROJECT | jq '.[] | select(.name=="fabrikam-vg") | .id')
172+
variable_group_variable_create $variable_group_id $AZDO_ORG_URL $AZDO_PROJECT "ACCOUNT_KEY" $sa_access_key "secret"
173+
variable_group_variable_create $variable_group_id $AZDO_ORG_URL $AZDO_PROJECT "ACCOUNT_NAME" $AZ_STORAGE_ACCOUNT
174+
variable_group_variable_create $variable_group_id $AZDO_ORG_URL $AZDO_PROJECT "PARTITION_KEY" $sa_partition_key
175+
variable_group_variable_create $variable_group_id $AZDO_ORG_URL $AZDO_PROJECT "TABLE_NAME" $sat_name
176+
156177
spk service create $FrontEnd -d $services_dir >> $TEST_WORKSPACE/log.txt
157178
directory_to_check="$services_full_dir/$FrontEnd"
158179
file_we_expect=(".gitignore" "azure-pipelines.yaml" "Dockerfile" )

0 commit comments

Comments
 (0)