-
Notifications
You must be signed in to change notification settings - Fork 7
/
test
executable file
·185 lines (140 loc) · 6.19 KB
/
test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# DON'T REMOVE FROM test script.
CYAN='\033[0;36m'
BCYAN='\033[1;36m'
NC='\033[0m' # No Color
DIVIDER=$(printf %"$(tput cols)"s | tr " " "*")
DIVIDER+="\n"
function get_project_id() {
local __resultvar=$1
VALUE=$(gcloud config get-value project | xargs)
eval $__resultvar="'$VALUE'"
}
function get_project_number() {
local __resultvar=$1
local PRO=$2
VALUE=$(gcloud projects list --filter="project_id=$PRO" --format="value(PROJECT_NUMBER)" | xargs)
eval $__resultvar="'$VALUE'"
}
# DISPLAY HELPERS
function section_open() {
section_description=$1
printf "$DIVIDER"
printf "${CYAN}$section_description${NC} \n"
printf "$DIVIDER"
}
function section_close() {
printf "$DIVIDER"
printf "${CYAN}$section_description ${BCYAN}- done${NC}\n"
printf "\n\n"
}
function evalTest() {
local command=$1
local expected=$2
local ERR=""
got=$(eval $command 2>errFile)
ERR=$(<errFile)
if [ ${#ERR} -gt 0 ]; then
if [ "$expected" = "EXPECTERROR" ]; then
printf "ok \n"
return
fi
printf "expecting no error, got error='$ERR' \n"
exit 1
fi
if [ "$got" != "$expected" ]; then
printf "expecting: '$expected' got: '$got' \n"
exit 1
fi
printf "$expected is ok\n"
}
# END DON'T REMOVE FROM test script.
get_project_id PROJECT
gcloud services enable cloudresourcemanager.googleapis.com --project=$PROJECT
get_project_number PROJECT_NUMBER $PROJECT
ZONE=us-central1-a
BASENAME=costsentry
REGION=us-central1
BUDGETAMOUNT=0
BUDGETCURRENCY=USD
LABEL=$BASENAME
LOCATION=US
SAFUNCTIONSUSER=$BASENAME-functions-sa
SAFUNCTIONS="$SAFUNCTIONSUSER@$PROJECT_NUMBER.iam.gserviceaccount.com"
gcloud config set project ${PROJECT}
# If you are setting up your own test billing account, you have to put the word deploystack somewhere in the display name.
BA=$(gcloud beta billing accounts list --format="value(ACCOUNT_ID)" --filter="displayName~Deploystack" | xargs)
echo "project: $PROJECT"
echo "account: $BA"
echo "basename=\"${BASENAME}\"" > terraform.tfvars
echo "billing_account=\"${BA}\"" >> terraform.tfvars
echo "label=\"${LABEL}\"" >> terraform.tfvars
echo "project_id=\"${PROJECT}\"" >> terraform.tfvars
echo "project_number=\"${PROJECT_NUMBER}\"" >> terraform.tfvars
echo "region=\"${REGION}\"" >> terraform.tfvars
echo "zone=\"${ZONE}\"" >> terraform.tfvars
echo "location=\"${LOCATION}\"" >> terraform.tfvars
echo "budgetamount=\"${BUDGETAMOUNT}\"" >> terraform.tfvars
terraform init
terraform apply -auto-approve -var-file=terraform.tfvars
section_open "Testing Cloud Function is deployed"
evalTest 'gcloud functions describe costsentry --format="value(name)"' "projects/$PROJECT/locations/$REGION/functions/costsentry"
section_close
section_open "Testing Service Account exists"
echo "gcloud iam service-accounts list --filter=\"email=$SAFUNCTIONS\" --format=\"value(email)\""
evalTest "gcloud iam service-accounts list --filter=\"email=$SAFUNCTIONS\" --format=\"value(email)\"" $SAFUNCTIONS
section_close
section_open "Testing Budget exists"
BUDGETNAME=$(gcloud beta billing budgets list --format="value(NAME)" --billing-account $BA --filter="displayName:$BASENAME-budget" | xargs)
evalTest "gcloud beta billing budgets describe $BUDGETNAME --format=\"value(name)\" --billing-account $BA" $BUDGETNAME
section_close
section_open "Testing Compute Instance creation"
evalTest "gcloud compute instances describe $BASENAME-example --zone $ZONE --format=\"value(name)\"" $BASENAME-example
section_close
section_open "Testing Pub/Sub"
evalTest "gcloud pubsub topics describe $BASENAME-billing-channel --format=\"value(name)\"" projects/$PROJECT/topics/$BASENAME-billing-channel
section_close
section_open "Testing Cloud Storage"
evalTest "gsutil ls gs://$PROJECT-function-deployer" "gs://$PROJECT-function-deployer/index.zip"
section_close
section_open "Testing Compute Instance RUNNING"
evalTest "gcloud compute instances describe $BASENAME-example --zone $ZONE --format=\"value(STATUS)\"" RUNNING
section_close
section_open "Test Cloud Run Services"
evalTest 'gcloud run services describe $BASENAME-run-service --region=$REGION --format="value(name)"' "$BASENAME-run-service"
evalTest 'gcloud run services get-iam-policy $BASENAME-run-service --region=$REGION --format="value(bindings)"' "{'members': ['allUsers'], 'role': 'roles/run.invoker'}"
section_close
./simulate
section_open "Testing Compute Instance RUNNING"
evalTest "gcloud compute instances describe $BASENAME-example --zone $ZONE --format=\"value(STATUS)\"" STOPPING
section_close
section_open "Test Cloud Run Services EXIST BUT UNSHARED"
evalTest 'gcloud run services describe $BASENAME-run-service --region=$REGION --format="value(name)"' "$BASENAME-run-service"
evalTest 'gcloud run services get-iam-policy $BASENAME-run-service --region=$REGION --format="value(bindings)"' ""
section_close
terraform destroy -auto-approve -var-file=terraform.tfvars
section_open "Testing Pub/Sub deleted"
evalTest "gcloud pubsub topics describe $BASENAME-billing-channel --format=\"value(name)\"" "EXPECTERROR"
section_close
section_open "Testing Compute Instance deleted"
evalTest "gcloud compute instances describe $BASENAME-example --zone $ZONE --format=\"value(name)\"" "EXPECTERROR"
section_close
section_open "Testing Budget deleted"
evalTest "gcloud beta billing budgets describe $BUDGETNAME --format=\"value(name)\" --billing-account $BA" "EXPECTERROR"
section_close
section_open "Testing Service Account deleted"
evalTest "gcloud iam service-accounts list --format=\"value(email)\" | grep $SAFUNCTIONSUSER" ""
section_close
section_open "Testing Cloud Storage deleted"
evalTest "gsutil ls gs://$PROJECT-function-deployer" "EXPECTERROR"
section_close
section_open "Testing Cloud Function deleted"
evalTest 'gcloud functions describe costsentry --format="value(name)"' "EXPECTERROR"
section_close
section_open "Test Cloud Run Service deleted"
evalTest 'gcloud run services describe $BASENAME-run-service --region=$REGION --format="value(name)"' "EXPECTERROR"
section_close
rm terraform.tfvars
printf "$DIVIDER"
printf "CONGRATS!!!!!!! \n"
printf "You got the end the of your test with everything working. \n"
printf "$DIVIDER"