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

Commit 9956892

Browse files
authored
Extending smoke test validations for builds and pull requests (#149)
1 parent 5a958af commit 9956892

File tree

3 files changed

+89
-8
lines changed

3 files changed

+89
-8
lines changed

smoke-test-pipeline.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ stages:
6262
export SPK_LOCATION="$(pwd)/spk-linux"
6363
export ACCESS_TOKEN_SECRET=$(AZDO_PAT)
6464
export SP_PASS=$(SP_PASS)
65+
export SP_TENANT=$(SP_TENANT)
6566
6667
# Include dependent scripts
6768
. ./functions.sh
@@ -73,4 +74,9 @@ stages:
7374
status=$?
7475
[ $status -eq 0 ] && echo "Test Done!" || (echo "Test had issues" && exit 1)
7576
#failOnStderr: true
76-
displayName: "Run Tests"
77+
displayName: "Run Tests"
78+
- task: PublishPipelineArtifact@1
79+
inputs:
80+
path: $(System.DefaultWorkingDirectory)/spk-env/log.txt
81+
artifact: test_logs
82+
condition: always()

tests/functions.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,65 @@ function hld_pipeline_exists () {
180180
az pipelines delete --id "$pipeline_id" --yes --org $1 --p $2
181181
fi
182182
}
183+
184+
function verify_pipeline_with_poll () {
185+
local pipeline_name=$3
186+
poll_timeout=$4
187+
poll_interval=$5
188+
end=$((SECONDS+$poll_timeout))
189+
loop_result="unknown"
190+
191+
echo "Attempting to verify that the pipeline build for $pipeline_name is successful..."
192+
pipeline_result=$(az pipelines build definition show --name $pipeline_name --org $AZDO_ORG_URL --p $AZDO_PROJECT)
193+
pipeline_id=$(tr '"\""' '"\\"' <<< "$pipeline_result" | jq .id)
194+
echo "$pipeline_name has pipeline id of $pipeline_id"
195+
196+
while [ $SECONDS -lt $end ]; do
197+
pipeline_builds=$(az pipelines build list --definition-ids $pipeline_id --org $1 --p $2)
198+
199+
# We expect only 1 build right now
200+
build_count=$(tr '"\""' '"\\"' <<< "$pipeline_builds" | jq '. | length')
201+
if [ "$build_count" != "1" ]; then
202+
echo "Expected 1 build for pipeline id $pipeline_id but found $build_count"
203+
exit 1
204+
fi
205+
206+
# We use grep because of string matching issues
207+
echo "Get the build status for build..."
208+
pipeline_status=$(tr '"\""' '"\\"' <<< "$pipeline_builds" | jq .[0].status)
209+
echo "pipeline_status this iteration --> $pipeline_status"
210+
if [ "$(echo $pipeline_status | grep 'completed')" != "" ]; then
211+
pipeline_result=$(tr '"\""' '"\\"' <<< "$pipeline_builds" | jq .[0].result)
212+
if [ "$(echo $pipeline_result | grep 'succeeded')" != "" ]; then
213+
echo "Successful build for pipeline id $pipeline_id!"
214+
loop_result=$pipeline_result
215+
break
216+
else
217+
echo "Expected successful build for pipeline id $pipeline_id but result is $pipeline_result"
218+
exit 1
219+
fi
220+
else
221+
echo "Pipeline Id $pipeline_id status is $pipeline_status. Sleeping for $poll_interval seconds"
222+
sleep $poll_interval
223+
fi
224+
done
225+
if [ "$loop_result" = "unknown" ]; then
226+
echo "Polling the build timed out after $poll_timeout seconds!"
227+
exit 1
228+
fi
229+
}
230+
231+
function approve_pull_request () {
232+
all_prs=$(az repos pr list --org $1 --p $2)
233+
pr_title=$3
234+
pr_exists=$(tr '"\""' '"\\"' <<< "$all_prs" | jq -r --arg pr_title $pr_title '.[] | select(.title == $pr_title) | != null')
235+
if [ "$pr_exists" != "true" ]; then
236+
echo "PR for '$pr_title' not found"
237+
exit 1
238+
fi
239+
pull_request_id=$(tr '"\""' '"\\"' <<< "$all_prs" | jq -r --arg pr_title $pr_title '.[] | select(.title == $pr_title) | .pullRequestId')
240+
echo "Found pull request id $pull_request_id for '$pr_title'"
241+
approve_result=$(az repos pr update --id $pull_request_id --auto-complete true -—org $1 --p $2)
242+
echo "PR $pull_request_id approved"
243+
# TODO verify actually successful
244+
}

tests/validations.sh

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ spk hld install-manifest-pipeline -o $AZDO_ORG -d $AZDO_PROJECT -p $ACCESS_TOKEN
129129

130130
# Verify the pipeline was created
131131
pipeline_created=$(az pipelines show --name $hld_dir-to-$manifests_dir --org $AZDO_ORG_URL --p $AZDO_PROJECT)
132-
# TODO: Verify the pipeline run was successful
132+
133+
# Verify the pipeline run was successful
134+
verify_pipeline_with_poll $AZDO_ORG_URL $AZDO_PROJECT $hld_dir-to-$manifests_dir 180 15
133135

134136
# App Code Mono Repo set up
135137
mkdir $mono_repo_dir
@@ -186,12 +188,17 @@ git push -u origin --all
186188
# First we should check what pipelines exist. If there is a pipeline with the same name we should delete it
187189
pipeline_exists $AZDO_ORG_URL $AZDO_PROJECT $FrontEnd
188190

191+
pipeline_name="$FrontEnd-pipeline"
192+
189193
# Create a pipeline since the code exists in remote repo
190-
spk service install-build-pipeline -o $AZDO_ORG -r $mono_repo_dir -u $remote_repo_url -d $AZDO_PROJECT -l $services_dir -p $ACCESS_TOKEN_SECRET -v $FrontEnd >> $TEST_WORKSPACE/log.txt
194+
spk service install-build-pipeline -o $AZDO_ORG -r $mono_repo_dir -u $remote_repo_url -d $AZDO_PROJECT -l $services_dir -p $ACCESS_TOKEN_SECRET -n $pipeline_name -v $FrontEnd >> $TEST_WORKSPACE/log.txt
191195

192196
# Verify the pipeline was created
193-
pipeline_created=$(az pipelines show --name $FrontEnd-pipeline --org $AZDO_ORG_URL --p $AZDO_PROJECT)
194-
# TODO: Verify the pipeline run was successful
197+
pipeline_created=$(az pipelines show --name $pipeline_name --org $AZDO_ORG_URL --p $AZDO_PROJECT)
198+
199+
# Verify the pipeline run was successful
200+
verify_pipeline_with_poll $AZDO_ORG_URL $AZDO_PROJECT $pipeline_name 180 25
201+
# TODO approve the PR this build creates on the HLD
195202

196203
# Start creating a service revision
197204
git branch $branchName
@@ -202,7 +209,13 @@ git commit -m "Adding my new file"
202209
git push --set-upstream origin $branchName
203210

204211
# Create a PR for the change
205-
spk service create-revision -t "Automated Test PR" -d "Adding my new file" --org-name $AZDO_ORG --personal-access-token $ACCESS_TOKEN_SECRET --remote-url $remote_repo_url >> $TEST_WORKSPACE/log.txt
212+
current_time=$(date +"%Y-%m-%d-%H-%M-%S")
213+
pr_title="Automated Test PR $current_time"
214+
echo "Creating pull request: '$pr_title'"
215+
spk service create-revision -t $pr_title -d "Adding my new file" --org-name $AZDO_ORG --personal-access-token "$ACCESS_TOKEN_SECRET" --remote-url $remote_repo_url >> $TEST_WORKSPACE/log.txt
216+
217+
echo "Attempting to approve pull request: '$pr_title'"
218+
# Get the id of the pr created and set the PR to be approved
219+
approve_pull_request $AZDO_ORG_URL $AZDO_PROJECT $pr_title
220+
206221

207-
# TODO: Get the id of the pr created and set the PR to be approved
208-
# az repos pr update --id --bypass-policy --auto-complete

0 commit comments

Comments
 (0)