Skip to content

Commit ae373af

Browse files
authored
Merge pull request #40 from meganlkm/test-harness
implement test harness
2 parents b10b9ec + 0762cce commit ae373af

File tree

10 files changed

+247
-1
lines changed

10 files changed

+247
-1
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.egg
2+
*.egg-info/
3+
*.py[cod]
4+
.env
5+
.pytest_cache/
6+
__pycache__/
7+
Dockerfile.*_*
8+
ignore/
9+
.venv/
10+
tmp/

.travis.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
env:
2+
matrix:
3+
- OS_ID: centos_master_2017.7.2
4+
- OS_ID: debian_master_2017.7.2
5+
- OS_ID: opensuse_master_2017.7.2
6+
- OS_ID: ubuntu_master_2016.11.3
7+
- OS_ID: ubuntu_master_2017.7.2
8+
9+
sudo: required
10+
11+
language: python
12+
13+
services:
14+
- docker
15+
16+
before_install:
17+
- pip install Jinja2
18+
- python ${TRAVIS_BUILD_DIR}/tools/filltmpl.py node ${OS_ID}
19+
20+
install:
21+
- docker build --force-rm -t "node:salt-testing-${OS_ID}" -f "Dockerfile.${OS_ID}" .
22+
23+
script:
24+
- ./tools/run-tests.sh node ${OS_ID}

Makefile

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
FORMULA_NAME = "node"
2+
PWD = $(shell pwd)
3+
4+
# ---------------------------------------------------------------
5+
define render_dockerfile
6+
python $(PWD)/tools/filltmpl.py $(FORMULA_NAME) $(1)
7+
endef
8+
9+
define docker_build
10+
docker build --force-rm -t $(FORMULA_NAME):salt-testing-$(1) -f Dockerfile.$(1) .
11+
endef
12+
13+
define docker_run_local
14+
docker run --rm -v $(PWD):/opt/$(FORMULA_NAME)-formula --env=STAGE=TEST -h salt-testing-$(1) --name salt-testing-$(1) -it $(FORMULA_NAME):salt-testing-$(1) /bin/bash
15+
endef
16+
17+
define run_tests
18+
./tools/run-tests.sh $(FORMULA_NAME) $(1)
19+
endef
20+
21+
# --- convenience functions -------------------------------------
22+
define build_thing
23+
$(call render_dockerfile,$(1)) && $(call docker_build,$(1))
24+
endef
25+
26+
define run_local_tests
27+
$(call build_thing,$(1)) && $(call run_tests,$(1))
28+
endef
29+
30+
define run_local
31+
$(call build_thing,$(1)) && $(call docker_run_local,$(1))
32+
endef
33+
34+
# ---------------------------------------------------------------
35+
setup:
36+
pip install Jinja2
37+
38+
clean:
39+
find . -name '*.pyc' -exec rm '{}' ';'
40+
rm -rf Dockerfile.*
41+
# delete pytest caches...
42+
# rm -rf tests/pytests/*/.pytest_cache
43+
# rm -rf tests/pytests/*/__pycache__
44+
rm -rf tests/pytests/apply-all-tests/.pytest_cache
45+
rm -rf tests/pytests/apply-all-tests/__pycache__
46+
47+
# --- centos_master_2017.7.2 ------------------------------------
48+
test-centos_master_2017.7.2: clean
49+
$(call run_local_tests,centos_master_2017.7.2)
50+
51+
local-centos_master_2017.7.2: clean
52+
$(call run_local,centos_master_2017.7.2)
53+
54+
# --- debian_master_2017.7.2 ------------------------------------
55+
test-debian_master_2017.7.2: clean
56+
$(call run_local_tests,debian_master_2017.7.2)
57+
58+
local-debian_master_2017.7.2: clean
59+
$(call run_local,debian_master_2017.7.2)
60+
61+
# --- opensuse_master_2017.7.2 ------------------------------------
62+
test-opensuse_master_2017.7.2: clean
63+
$(call run_local_tests,opensuse_master_2017.7.2)
64+
65+
local-opensuse_master_2017.7.2: clean
66+
$(call run_local,opensuse_master_2017.7.2)
67+
68+
# --- ubuntu_master_2016.11.3 ------------------------------------
69+
test-ubuntu_master_2016.11.3: clean
70+
$(call run_local_tests,ubuntu_master_2016.11.3)
71+
72+
local-ubuntu_master_2016.11.3: clean
73+
$(call run_local,ubuntu_master_2016.11.3)
74+
75+
# --- ubuntu_master_2017.7.2 ------------------------------------
76+
test-ubuntu_master_2017.7.2: clean
77+
$(call run_local_tests,ubuntu_master_2017.7.2)
78+
79+
local-ubuntu_master_2017.7.2: clean
80+
$(call run_local,ubuntu_master_2017.7.2)

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,54 @@ The checksum for the *node-v…-linux-x64.tar.gz* file has to be provided.
4141
When building from sources, only newer node versions with npm included are supported (Versions 0.9.x and newer should
4242
be safe). Also, the executable will be called `node`.
4343

44-
If you need to install older versions, where the executable is called `nodejs`, use
44+
If you need to install older versions, where the executable is called `nodejs`, use
4545
[previous commits](https://github.com/saltstack-formulas/node-formula/commit/bcc649588c162686c4dbde486da840ccd060edf6)
4646
of this formula.
4747

4848
## Installing from packages
4949

5050
See the [example pillar file](https://github.com/saltstack-formulas/node-formula/blob/master/pillar.example).
51+
52+
53+
## <a name='testing'></a> Running Tests
54+
55+
This test runner was implemented using the [formula-test-harness](https://github.com/intuitivetechnologygroup/formula-test-harness) project.
56+
57+
58+
Tests will be run on the following base images:
59+
60+
* `simplyadrian/allsalt:centos_master_2017.7.2`
61+
* `simplyadrian/allsalt:debian_master_2017.7.2`
62+
* `simplyadrian/allsalt:opensuse_master_2017.7.2`
63+
* `simplyadrian/allsalt:ubuntu_master_2016.11.3`
64+
* `simplyadrian/allsalt:ubuntu_master_2017.7.2`
65+
66+
##### Start a virtualenv
67+
68+
```bash
69+
pip install -U virtualenv
70+
virtualenv .venv
71+
source .venv/bin/activate
72+
```
73+
74+
##### Install local requirements
75+
76+
```bash
77+
make setup
78+
```
79+
80+
##### Run tests
81+
82+
* `make test-centos_master_2017.7.2`
83+
* `make test-debian_master_2017.7.2`
84+
* `make test-opensuse_master_2017.7.2`
85+
* `make test-ubuntu_master_2016.11.3`
86+
* `make test-ubuntu_master_2017.7.2`
87+
88+
### <a name='run-containers'></a> Run Containers
89+
90+
* `make local-centos_master_2017.7.2`
91+
* `make local-debian_master_2017.7.2`
92+
* `make local-opensuse_master_2017.7.2`
93+
* `make local-ubuntu_master_2016.11.3`
94+
* `make local-ubuntu_master_2017.7.2`

tests/pytests/apply-all-tests/__init__.py

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from subprocess import check_output
2+
from unittest import TestCase
3+
4+
5+
class ApplyStateTest(TestCase):
6+
7+
def test_000_apply(self):
8+
state_apply_response = check_output(["salt-call", "--local", "state.apply"])
9+
print('')
10+
print('-' * 50)
11+
print('state_apply_response:')
12+
print(state_apply_response)
13+
print('-' * 50)
14+
print('')
15+
16+
state_apply_response = state_apply_response.split('\n')
17+
summary = state_apply_response[-8:]
18+
failed = 0
19+
for line in summary:
20+
if line.startswith('Failed:'):
21+
failed = int(line.split(':').pop().strip())
22+
23+
self.assertEqual(failed, 0)

tests/srv/salt/top.sls

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
base:
2+
'*':
3+
- node

tools/filltmpl.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
import sys
3+
4+
from jinja2 import Template
5+
6+
# base/tests
7+
dir_path = os.path.dirname(os.path.realpath(__file__))
8+
9+
# base
10+
base_path = os.path.dirname(dir_path)
11+
12+
13+
if __name__ == '__main__':
14+
formula_name = sys.argv[1]
15+
image_tag = sys.argv[2]
16+
17+
template = Template(
18+
open(os.path.join(dir_path, 'templates', 'Dockerfile.j2')).read()
19+
)
20+
21+
dockerfile = template.render({
22+
'formula_name': formula_name,
23+
'image_tag': image_tag
24+
})
25+
26+
with open(os.path.join(base_path, 'Dockerfile.{}'.format(image_tag)), 'w') as fh:
27+
fh.write(dockerfile)

tools/run-tests.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
set -ev
3+
4+
test -z $2 && echo "Usage: ${0} FORMULA_NAME OS_ID" && exit 1
5+
export FORMULA_NAME=$1
6+
export OS_ID=$2
7+
8+
9+
function docker-run-pytest() {
10+
docker run --rm \
11+
-v "$@":/opt/tests \
12+
--env=STAGE=TEST \
13+
-h "salt-testing-${OS_ID}" \
14+
--name "salt-testing-${OS_ID}" \
15+
-it ${FORMULA_NAME}:"salt-testing-${OS_ID}" \
16+
pytest -sv /opt/tests
17+
}
18+
19+
for i in $(find $PWD/tests/pytests/* -maxdepth 0 -type d); do
20+
docker-run-pytest $i;
21+
done

tools/templates/Dockerfile.j2

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM simplyadrian/allsalt:{{ image_tag }}
2+
3+
{% if 'debian' in image_tag or 'ubuntu' in image_tag -%}
4+
RUN apt-get update && \
5+
apt-get install -y python-pip
6+
{% endif %}
7+
8+
RUN pip install pytest && \
9+
sed -i "s/#master: salt/master: localhost/g" /etc/salt/minion
10+
11+
ADD tests/srv /srv
12+
ADD {{ formula_name }} /srv/salt/{{ formula_name }}
13+
14+
WORKDIR /srv/salt

0 commit comments

Comments
 (0)