Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.

Commit 578d9f0

Browse files
authored
chore(tests): add bats tests (xenial) (#619)
* chore: initial sketch using bats tests * chore: jenkinsfile target the main stage * chore: add node_modules to .dockerignore * chore(ci): update the GH workflow * chore(tests): make it possible to run the tests via `docker run` * chore(tests): cleaning up tests and helper functions * chore(tests): extra comments to our node tests
1 parent d990c08 commit 578d9f0

File tree

9 files changed

+137
-6
lines changed

9 files changed

+137
-6
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
tmp/
2+
node_modules

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
steps:
99
- uses: actions/checkout@v2
1010
- name: Build Docker image
11-
run: docker build . --build-arg NF_IMAGE_VERSION=$GITHUB_SHA -t netlify/build:$GITHUB_SHA
11+
run: docker build . --build-arg NF_IMAGE_VERSION=$GITHUB_SHA --target build-image -t netlify/build:$GITHUB_SHA
1212
- uses: vbrown608/whalescale@v1
1313
with:
1414
token: ${{ secrets.GITHUB_TOKEN }}

Dockerfile

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM ubuntu:16.04
1+
FROM ubuntu:16.04 as build-image
22

33
LABEL maintainer Netlify
44

@@ -262,6 +262,7 @@ RUN git clone https://github.com/creationix/nvm.git ~/.nvm && \
262262
git checkout v$NVM_VERSION && \
263263
cd /
264264

265+
# Install node.js, yarn, bower and elm
265266
ENV ELM_VERSION=0.19.0-bugfix6
266267
ENV YARN_VERSION=1.22.10
267268

@@ -499,3 +500,23 @@ ENV NF_IMAGE_VERSION ${NF_IMAGE_VERSION:-latest}
499500

500501
ARG NF_IMAGE_TAG
501502
ENV NF_IMAGE_TAG ${NF_IMAGE_TAG:-latest}
503+
504+
505+
################################################################################
506+
#
507+
# Test stage Dockerfile
508+
#
509+
################################################################################
510+
511+
FROM build-image as build-image-test
512+
513+
USER buildbot
514+
SHELL ["/bin/bash", "-c"]
515+
516+
ADD --chown=buildbot package.json /opt/buildhome/test-env/package.json
517+
RUN cd /opt/buildhome/test-env && . ~/.nvm/nvm.sh && npm i &&\
518+
ln -s /opt/build-bin/run-build-functions.sh /opt/buildhome/test-env/run-build-functions.sh &&\
519+
ln -s /opt/build-bin/build /opt/buildhome/test-env/run-build.sh
520+
ADD --chown=buildbot tests /opt/buildhome/test-env/tests
521+
WORKDIR /opt/buildhome/test-env
522+
CMD . ~/.nvm/nvm.sh && npm test

Jenkinsfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pipeline {
1616
anyOf { branch 'staging' ; branch 'xenial' ; branch 'trusty' ; buildingTag() }
1717
}
1818
steps {
19-
sh "docker build --build-arg NF_IMAGE_VERSION=${env.GIT_COMMIT} --build-arg NF_IMAGE_TAG=${env.BRANCH_NAME} -t netlify/build:${env.BRANCH_NAME} -t netlify/build:${env.GIT_COMMIT} ."
19+
sh "docker build --build-arg NF_IMAGE_VERSION=${env.GIT_COMMIT} --build-arg NF_IMAGE_TAG=${env.BRANCH_NAME} -t netlify/build:${env.BRANCH_NAME} -t netlify/build:${env.GIT_COMMIT} --target build-image ."
2020
}
2121
}
2222

@@ -25,7 +25,7 @@ pipeline {
2525
anyOf { buildingTag() }
2626
}
2727
steps {
28-
sh "docker build --build-arg NF_IMAGE_VERSION=${env.GIT_COMMIT} --build-arg NF_IMAGE_TAG=${env.BRANCH_NAME} --squash -t netlify/build:${env.BRANCH_NAME}-squash ."
28+
sh "docker build --build-arg NF_IMAGE_VERSION=${env.GIT_COMMIT} --build-arg NF_IMAGE_TAG=${env.BRANCH_NAME} --squash -t netlify/build:${env.BRANCH_NAME}-squash --target build-image ."
2929
}
3030
}
3131

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"description": "This repository contains the tools to make the build image Netlify uses to build a site from git (for continuous deployment)",
55
"main": "index.js",
66
"private": "true",
7-
"scripts": {},
7+
"scripts": {
8+
"test": "bats --recursive --timing --tap tests"
9+
},
810
"repository": {
911
"type": "git",
1012
"url": "git+https://github.com/netlify/build-image.git"
@@ -15,5 +17,11 @@
1517
"url": "https://github.com/netlify/build-image/issues"
1618
},
1719
"homepage": "https://github.com/netlify/build-image#readme",
18-
"devDependencies": {}
20+
"devDependencies": {
21+
"bats": "^1.3.0-alpha.0",
22+
"bats-assert": "^2.0.0",
23+
"bats-support": "^0.3.0",
24+
"bats-file": "git+https://github.com/bats-core/bats-file.git#v0.3.0"
25+
},
26+
"dependencies": {}
1927
}

tests/helpers.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
# Sets a given fixture as the project to be used as the target repository for our build-image scripts.
4+
# It expects a fixture name which should be present in a relative dir named `./fixtures` from the bats test executing it.
5+
# It sets the global `NETLIFY_BUILD_BASE`, `NETLIFY_CACHE_DIR` and `NETLIFY_REPO_DIR` variables based on the provided directory.
6+
7+
# Arguments:
8+
# $1 - fixture name
9+
# $2 - path to use as the repo base (ideally a temporary dir)
10+
set_fixture_as_repo() {
11+
local fixture="$1"
12+
local tmp_dir="$2"
13+
NETLIFY_BUILD_BASE=$tmp_dir
14+
NETLIFY_CACHE_DIR="$NETLIFY_BUILD_BASE/cache"
15+
NETLIFY_REPO_DIR="$NETLIFY_BUILD_BASE/repo"
16+
rm -rf "$NETLIFY_REPO_DIR"
17+
cp -r "$BATS_TEST_DIRNAME/fixtures/$fixture" "$NETLIFY_REPO_DIR"
18+
19+
# Change to the repo dir
20+
cd "$NETLIFY_REPO_DIR" || exit 1
21+
}
22+
23+
# Setups a tmp dir to be used for test purposes.
24+
setup_tmp_dir() {
25+
mktemp -d
26+
}

tests/node/base.bats

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bats
2+
3+
load "../helpers.sh"
4+
5+
load '../../node_modules/bats-support/load'
6+
load '../../node_modules/bats-assert/load'
7+
8+
9+
NODE_VERSION=12.18.0
10+
11+
#Note: These binaries are accessible because we source `~/.nvm/nvm.sh` before running the `bats` tests
12+
13+
@test 'node version ${NODE_VERSION} is installed and available at startup' {
14+
run node --version
15+
assert_success
16+
assert_output --partial $NODE_VERSION
17+
}
18+
19+
@test 'grunt-cli is installed and available at startup' {
20+
run grunt --version
21+
assert_success
22+
}
23+
24+
@test 'bower is installed and available at startup' {
25+
run bower --version
26+
assert_success
27+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "simple-node-project",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "UNLICENSED",
11+
"dependencies": {
12+
"@netlify/plugins-list": "^3.6.0"
13+
}
14+
}

tests/node/yarn.bats

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bats
2+
3+
load "../helpers.sh"
4+
5+
load '../../node_modules/bats-support/load'
6+
load '../../node_modules/bats-assert/load'
7+
load '../../node_modules/bats-file/load'
8+
9+
YARN_CACHE_DIR=/opt/buildhome/.yarn_cache
10+
11+
setup() {
12+
TMP_DIR=$(setup_tmp_dir)
13+
set_fixture_as_repo 'simple-node' "$TMP_DIR"
14+
15+
# Load functions
16+
load '../../run-build-functions.sh'
17+
}
18+
19+
teardown() {
20+
rm -rf "$TMP_DIR"
21+
# Return to original dir
22+
cd - || return
23+
}
24+
25+
@test 'run_yarn sets up new yarn version if different from the one installed, installs deps and creates cache dir' {
26+
local newYarnVersion=1.21.0
27+
run run_yarn $newYarnVersion
28+
assert_success
29+
assert_output --partial "Installing yarn at version $newYarnVersion"
30+
assert_dir_exist $YARN_CACHE_DIR
31+
32+
# The cache dir is actually being used
33+
assert_dir_exist "$YARN_CACHE_DIR/v6"
34+
}

0 commit comments

Comments
 (0)