forked from logilab/saemref-formula
/
run-tests.py
executable file
·135 lines (111 loc) · 4.28 KB
/
run-tests.py
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
#!/usr/bin/env python
import click
import os
import subprocess
BASEDIR = os.path.abspath(os.path.dirname(__file__))
_formula = "saemref"
def get_http_proxy():
return os.environ.get('HTTP_PROXY', os.environ.get('http_proxy'))
def get_tag(image, salt=False):
tag = "{0}-formula:{1}".format(_formula, image)
if salt:
return tag + "_salted"
else:
return tag
def image_exists(image):
# docker image accept tags only in version >= 1.8...
# so workaround with docker history
rc = subprocess.call("docker history -q {0} > /dev/null".format(image), shell=True)
if rc == 0:
return True
elif rc == 1:
return False
else:
raise RuntimeError("Cannot test if image exists")
image_option = click.argument("image", type=click.Choice(["centos7", "jessie"]))
salt_option = click.option('--salt', is_flag=True, help="Run salt highstate")
@click.group()
def cli():
pass
@cli.command(help="Build a container")
@image_option
@salt_option
@click.option('--tag', default=None, help="Custom tag name for the built docker image")
@click.option('--file-root', type=click.Path(exists=True), default='test/salt')
@click.option('--pillar-root', type=click.Path(exists=True), default='test/pillar')
@click.option('--log-level', type=click.Choice(['debug', 'info', 'warning', 'error']),
default='info')
def build(image, salt, tag, file_root, pillar_root, log_level):
dockerfile = "test/{0}.Dockerfile".format(image)
if salt:
dockerfile_content = open(dockerfile, "r").read()
dockerfile_content += (
"\n"
"ADD test/minion.conf /etc/salt/minion.d/minion.conf\n"
"ADD %(file_root)s /srv/salt\n"
"ADD %(pillar_root)s /srv/pillar\n"
"ADD %(formula)s /srv/formula/%(formula)s\n"
"RUN salt-call --hard-crash --retcode-passthrough -l %(log_level)s state.highstate\n"
) % {
"file_root": file_root,
"pillar_root": pillar_root,
"formula": _formula,
"log_level": log_level,
}
dockerfile = os.path.join("test", "{0}_salted.Dockerfile".format(image))
with open(dockerfile, "wb") as fd:
fd.write(dockerfile_content.encode())
if tag is None:
tag = get_tag(image, salt)
args = ["docker", "build"]
http_proxy = get_http_proxy()
if http_proxy:
args.extend(["--build-arg", "http_proxy={0}".format(http_proxy)])
args.extend(["-t", tag, "-f", dockerfile, "."])
subprocess.check_call(args)
@cli.command(
help="Build a salted (highstate) container and run tests on it",
context_settings={"allow_extra_args": True},
)
@click.pass_context
@image_option
def test(ctx, image):
tag = get_tag(image, True)
if not image_exists(tag):
ctx.invoke(build, image=image, salt=True, log_level='debug')
postgres_tag = get_tag("postgres", False)
if not image_exists(postgres_tag):
ctx.invoke(build, image="postgres", salt=False)
import pytest
ctx.exit(pytest.main(["--docker-image", tag, "--postgres-image", postgres_tag] + ctx.args))
@cli.command(help="Run a container and spawn an interactive shell inside")
@click.pass_context
@image_option
@salt_option
def dev(ctx, image, salt):
tag = get_tag(image, salt)
if not image_exists(tag):
ctx.invoke(build, image=image, salt=salt)
cmd = [
"docker", "run", "-d", "--hostname", image,
"-v", "{0}/test/minion.conf:/etc/salt/minion.d/minion.conf".format(BASEDIR),
"-v", "{0}/test/salt:/srv/salt".format(BASEDIR),
"-v", "{0}/test/pillar:/srv/pillar".format(BASEDIR),
"-v", "{0}/{1}:/srv/formula/{1}".format(BASEDIR, _formula),
]
http_proxy = get_http_proxy()
if http_proxy:
cmd.extend(["--env", "http_proxy={0}".format(http_proxy)])
if image in ("centos7", "jessie"):
# Systemd require privileged container
cmd.append("--privileged")
cmd.append(tag)
# Run the container default CMD as pid 1 (init system)
docker_id = subprocess.check_output(cmd).strip()
try:
# Spawn a interactive shell in the container
subprocess.call(["docker", "exec", "-it", docker_id, "/bin/bash"])
finally:
subprocess.call(["docker", "rm", "-f", docker_id])
if __name__ == "__main__":
cli()