Skip to content

Commit 9105274

Browse files
committed
fix: build image only once during tests
Adjust the CircleCI stages so that the container image is built only once and reused for all the tests (per workflow). Allows us to test the same image under a variety of scenarios (integration tests, package manager tests, -anything new-). Additionally, include the commit hash when notifying Slack that a job failed as it is easier to track where a problem comes from. Also, split the test job to run unit tests and integration tests in parallel, to allow for quicker builds.
1 parent 0aad844 commit 9105274

File tree

2 files changed

+111
-26
lines changed

2 files changed

+111
-26
lines changed

.circleci/config.yml

Lines changed: 108 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ main_branches_filter: &main_branches_filter
2525
- master
2626

2727
jobs:
28-
######################## PACKAGE MANAGER REGRESSION TESTS ########################
28+
######################## PR OR MERGE TO STAGING ########################
2929
package_manager_test_apk:
3030
<<: *default_machine_config
3131
steps:
@@ -37,10 +37,18 @@ jobs:
3737
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" &&
3838
nvm install v10 &&
3939
npm install &&
40+
export IMAGE_TAG=$([[ "$CIRCLE_BRANCH" == "staging" ]] && echo "staging-candidate" || echo "discardable") &&
41+
export KUBERNETES_MONITOR_IMAGE_NAME_AND_TAG=snyk/kubernetes-monitor:${IMAGE_TAG}-${CIRCLE_SHA1} &&
42+
docker pull ${KUBERNETES_MONITOR_IMAGE_NAME_AND_TAG} &&
4043
npm run test:apk
4144
- run:
4245
name: Notify Slack on failure
43-
command: ./scripts/slack-notify-failure.sh "apk-tests"
46+
command: |
47+
if [[ "$CIRCLE_BRANCH" == "staging" ]]; then
48+
./scripts/slack-notify-failure.sh "staging-apk-tests-${CIRCLE_SHA1}"
49+
else
50+
echo "Current branch is $CIRCLE_BRANCH so skipping notifying Slack"
51+
fi
4452
when: on_fail
4553

4654
package_manager_test_apt:
@@ -54,11 +62,20 @@ jobs:
5462
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" &&
5563
nvm install v10 &&
5664
npm install &&
65+
export IMAGE_TAG=$([[ "$CIRCLE_BRANCH" == "staging" ]] && echo "staging-candidate" || echo "discardable") &&
66+
export KUBERNETES_MONITOR_IMAGE_NAME_AND_TAG=snyk/kubernetes-monitor:${IMAGE_TAG}-${CIRCLE_SHA1} &&
67+
docker pull ${KUBERNETES_MONITOR_IMAGE_NAME_AND_TAG} &&
5768
npm run test:apt
5869
- run:
5970
name: Notify Slack on failure
60-
command: ./scripts/slack-notify-failure.sh "apt-tests"
71+
command: |
72+
if [[ "$CIRCLE_BRANCH" == "staging" ]]; then
73+
./scripts/slack-notify-failure.sh "staging-apt-tests-${CIRCLE_SHA1}"
74+
else
75+
echo "Current branch is $CIRCLE_BRANCH so skipping notifying Slack"
76+
fi
6177
when: on_fail
78+
6279
package_manager_test_rpm:
6380
<<: *default_machine_config
6481
steps:
@@ -70,51 +87,93 @@ jobs:
7087
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" &&
7188
nvm install v10 &&
7289
npm install &&
90+
export IMAGE_TAG=$([[ "$CIRCLE_BRANCH" == "staging" ]] && echo "staging-candidate" || echo "discardable") &&
91+
export KUBERNETES_MONITOR_IMAGE_NAME_AND_TAG=snyk/kubernetes-monitor:${IMAGE_TAG}-${CIRCLE_SHA1} &&
92+
docker pull ${KUBERNETES_MONITOR_IMAGE_NAME_AND_TAG} &&
7393
npm run test:rpm
7494
- run:
7595
name: Notify Slack on failure
76-
command: ./scripts/slack-notify-failure.sh "rpm-tests"
96+
command: |
97+
if [[ "$CIRCLE_BRANCH" == "staging" ]]; then
98+
./scripts/slack-notify-failure.sh "staging-rpm-tests-${CIRCLE_SHA1}"
99+
else
100+
echo "Current branch is $CIRCLE_BRANCH so skipping notifying Slack"
101+
fi
77102
when: on_fail
78103

79-
######################## PR TO STAGING ########################
80-
test_discardable:
104+
build_image:
81105
<<: *default_machine_config
82106
steps:
83107
- checkout
84108
- run:
85-
name: TEST DISCARDABLE
109+
name: BUILD IMAGE
110+
command: |
111+
docker login --username ${DOCKERHUB_USER} --password ${DOCKERHUB_PASSWORD} &&
112+
export IMAGE_TAG=$([[ "$CIRCLE_BRANCH" == "staging" ]] && echo "staging-candidate" || echo "discardable") &&
113+
IMAGE_NAME_CANDIDATE=snyk/kubernetes-monitor:${IMAGE_TAG}-${CIRCLE_SHA1} &&
114+
./scripts/build-image.sh ${IMAGE_NAME_CANDIDATE} &&
115+
docker push ${IMAGE_NAME_CANDIDATE}
116+
- run:
117+
name: Notify Slack on failure
118+
command: |
119+
if [[ "$CIRCLE_BRANCH" == "staging" ]]; then
120+
./scripts/slack-notify-failure.sh "staging-build-image-${CIRCLE_SHA1}"
121+
else
122+
echo "Current branch is $CIRCLE_BRANCH so skipping notifying Slack"
123+
fi
124+
when: on_fail
125+
126+
unit_tests:
127+
<<: *default_machine_config
128+
steps:
129+
- checkout
130+
- run:
131+
name: UNIT TESTS
86132
command: |
87133
export NVM_DIR="/opt/circleci/.nvm" &&
88134
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" &&
89135
nvm install v10 &&
90136
npm install &&
91-
npm test
137+
npm run lint &&
138+
npm run build &&
139+
npm run test:unit
140+
- run:
141+
name: Notify Slack on failure
142+
command: |
143+
if [[ "$CIRCLE_BRANCH" == "staging" ]]; then
144+
./scripts/slack-notify-failure.sh "staging-unit-tests-${CIRCLE_SHA1}"
145+
else
146+
echo "Current branch is $CIRCLE_BRANCH so skipping notifying Slack"
147+
fi
148+
when: on_fail
92149

93-
######################## MERGE TO STAGING ########################
94-
test_and_build:
150+
integration_tests:
95151
<<: *default_machine_config
96152
steps:
97153
- checkout
98154
- run:
99-
name: TEST AND BUILD
155+
name: INTEGRATION TESTS
100156
command: |
101157
export NVM_DIR="/opt/circleci/.nvm" &&
102158
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" &&
103159
nvm install v10 &&
104160
npm install &&
105-
npm run lint &&
106-
npm run build &&
107-
npm run test:unit &&
108161
docker login --username ${DOCKERHUB_USER} --password ${DOCKERHUB_PASSWORD} &&
109-
IMAGE_NAME_CANDIDATE=snyk/kubernetes-monitor:staging-candidate-${CIRCLE_SHA1} &&
110-
./scripts/build-image.sh ${IMAGE_NAME_CANDIDATE} &&
111-
docker push ${IMAGE_NAME_CANDIDATE} &&
112-
./scripts/slack-notify-push.sh ${IMAGE_NAME_CANDIDATE} &&
113-
KUBERNETES_MONITOR_IMAGE_NAME_AND_TAG=${IMAGE_NAME_CANDIDATE} npm run test:integration
162+
export IMAGE_TAG=$([[ "$CIRCLE_BRANCH" == "staging" ]] && echo "staging-candidate" || echo "discardable") &&
163+
export KUBERNETES_MONITOR_IMAGE_NAME_AND_TAG=snyk/kubernetes-monitor:${IMAGE_TAG}-${CIRCLE_SHA1} &&
164+
docker pull ${KUBERNETES_MONITOR_IMAGE_NAME_AND_TAG} &&
165+
npm run test:integration
114166
- run:
115167
name: Notify Slack on failure
116-
command: ./scripts/slack-notify-failure.sh "staging-test"
168+
command: |
169+
if [[ "$CIRCLE_BRANCH" == "staging" ]]; then
170+
./scripts/slack-notify-failure.sh "staging-integration-tests-${CIRCLE_SHA1}"
171+
else
172+
echo "Current branch is $CIRCLE_BRANCH so skipping notifying Slack"
173+
fi
117174
when: on_fail
175+
176+
######################## MERGE TO STAGING ########################
118177
tag_and_push:
119178
<<: *default_container_config
120179
steps:
@@ -213,28 +272,54 @@ workflows:
213272
version: 2
214273
PR_TO_STAGING:
215274
jobs:
216-
- test_discardable:
275+
- build_image:
276+
<<: *main_branches_filter
277+
- unit_tests:
278+
<<: *main_branches_filter
279+
- integration_tests:
280+
requires:
281+
- build_image
217282
<<: *main_branches_filter
218283
- package_manager_test_apk:
284+
requires:
285+
- build_image
219286
<<: *main_branches_filter
220287
- package_manager_test_apt:
288+
requires:
289+
- build_image
221290
<<: *main_branches_filter
222291
- package_manager_test_rpm:
292+
requires:
293+
- build_image
223294
<<: *main_branches_filter
224295

225296
MERGE_TO_STAGING:
226297
jobs:
227-
- test_and_build:
298+
- build_image:
299+
<<: *staging_branch_only_filter
300+
- unit_tests:
301+
<<: *staging_branch_only_filter
302+
- integration_tests:
303+
requires:
304+
- build_image
228305
<<: *staging_branch_only_filter
229306
- package_manager_test_apk:
307+
requires:
308+
- build_image
230309
<<: *staging_branch_only_filter
231310
- package_manager_test_apt:
311+
requires:
312+
- build_image
232313
<<: *staging_branch_only_filter
233314
- package_manager_test_rpm:
315+
requires:
316+
- build_image
234317
<<: *staging_branch_only_filter
235318
- tag_and_push:
236319
requires:
237-
- test_and_build
320+
- build_image
321+
- unit_tests
322+
- integration_tests
238323
- package_manager_test_apk
239324
- package_manager_test_apt
240325
- package_manager_test_rpm

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"test:integration:eks": "TEST_PLATFORM=eks CREATE_CLUSTER=false ./tap test/integration/kubernetes.test.ts -R spec --timeout=1200",
1212
"test:coverage": "npm run test:unit -- --coverage",
1313
"test:watch": "tsc-watch --onSuccess 'npm run test:unit'",
14-
"test:apk": "./scripts/build-image.sh && TEST_PLATFORM=kind CREATE_CLUSTER=true PACKAGE_MANAGER=apk ./tap test/integration/package-manager.test.ts -R spec --timeout=7200",
15-
"test:apt": "./scripts/build-image.sh && TEST_PLATFORM=kind CREATE_CLUSTER=true PACKAGE_MANAGER=apt ./tap test/integration/package-manager.test.ts -R spec --timeout=7200",
16-
"test:rpm": "./scripts/build-image.sh && TEST_PLATFORM=kind CREATE_CLUSTER=true PACKAGE_MANAGER=rpm ./tap test/integration/package-manager.test.ts -R spec --timeout=7200",
14+
"test:apk": "TEST_PLATFORM=kind CREATE_CLUSTER=true PACKAGE_MANAGER=apk ./tap test/integration/package-manager.test.ts -R spec --timeout=7200",
15+
"test:apt": "TEST_PLATFORM=kind CREATE_CLUSTER=true PACKAGE_MANAGER=apt ./tap test/integration/package-manager.test.ts -R spec --timeout=7200",
16+
"test:rpm": "TEST_PLATFORM=kind CREATE_CLUSTER=true PACKAGE_MANAGER=rpm ./tap test/integration/package-manager.test.ts -R spec --timeout=7200",
1717
"start": "bin/start",
1818
"prepare": "npm run build",
1919
"build": "tsc",

0 commit comments

Comments
 (0)