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

Commit 5974a24

Browse files
committed
Build atomic app base and app image to run functional tests on.
1 parent 9d9e8a6 commit 5974a24

File tree

5 files changed

+59
-38
lines changed

5 files changed

+59
-38
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ test:
2121
functional-test:
2222
pip install -qr requirements.txt
2323
pip install -qr test-requirements.txt
24+
./tests/functional/scripts/atomic.sh install
25+
./tests/functional/scripts/prepare.sh install
26+
$(DOCKER) build -t atomicapp:build .
2427
$(PYTHON) -m pytest tests/functional/ -vv --cov atomicapp
2528

2629
.PHONY: image

tests/functional/base.py

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import distutils.dir_util
12
import os
3+
import re
24
import logging
35
import logging.config
4-
import re
56
import time
67
import anymarkup
78
import datetime
@@ -51,12 +52,13 @@ class BaseProviderTestSuite(unittest.TestCase):
5152
NULECULE_LIB_REPO = 'https://github.com/projectatomic/nulecule-library'
5253
NULECULE_LIB_PATH = os.path.join(os.path.dirname(__file__),
5354
'nulecule-library')
55+
BUILD_DIR = os.path.join(os.path.dirname(__file__), 'build')
5456
PROVIDER = None
5557

5658
@classmethod
5759
def setUpClass(cls):
5860
cls.fetch_nulecule_lib()
59-
cls.build_image()
61+
cls.build()
6062

6163
@classmethod
6264
def tearDownClass(cls):
@@ -85,8 +87,11 @@ def deploy(self, app_spec, answers):
8587
Returns:
8688
Path of the deployed dir.
8789
"""
88-
destination = tempfile.mkdtemp()
89-
answers_path = self.get_tmp_answers_file(answers)
90+
destination = self.BUILD_DIR
91+
answers_path = os.path.join(self.BUILD_DIR, 'answers.conf')
92+
anymarkup.serialize_file(answers,
93+
answers_path,
94+
format='ini')
9095
cmd = (
9196
'atomic run {app_spec} -a {answers} --provider={provider} '
9297
'--destination={dest}').format(
@@ -132,13 +137,29 @@ def fetch_nulecule_lib(cls):
132137
'git pull origin master', shell=True)
133138

134139
@classmethod
135-
def build_image(cls):
140+
def build(cls):
136141
app_dir = os.path.join(cls.NULECULE_LIB_PATH, cls.APP_DIR_NAME)
142+
143+
build_dir = cls.BUILD_DIR
144+
145+
try:
146+
os.rmdir(build_dir)
147+
except:
148+
pass
149+
150+
distutils.dir_util.copy_tree(app_dir, build_dir)
151+
152+
with open(os.path.join(build_dir, 'Dockerfile')) as f:
153+
s = f.read()
154+
155+
with open(os.path.join(build_dir, 'Dockerfile'), 'w') as f:
156+
f.write(re.sub('FROM.*', 'FROM atomicapp:build', s))
157+
137158
cls.image_name = '{}-{}'.format(
138159
cls.APP_DIR_NAME, uuid.uuid1().hex[:8])
139160
subprocess.check_call(
140-
'cd {app_dir}; docker build -t {image_name} .'.format(
141-
app_dir=app_dir, image_name=cls.image_name),
161+
'docker build -t {image_name} {path}'.format(
162+
image_name=cls.image_name, path=build_dir),
142163
stdin=False, stderr=False,
143164
shell=True)
144165

@@ -164,18 +185,25 @@ def tearDown(self):
164185
print cmd
165186
subprocess.check_output(cmd)
166187

167-
def assertContainerRunning(self, name):
168-
containers = self._get_containers()
169-
for _id, container in containers.items():
170-
if container['names'] == name:
171-
return True
188+
def assertContainerRunning(self, name, timeout=1):
189+
start = datetime.datetime.now()
190+
while (datetime.datetime.now() - start).total_seconds() <= timeout:
191+
containers = self._get_containers()
192+
for _id, container in containers.items():
193+
if container['names'] == name:
194+
return True
172195
raise AssertionError('Container: %s not running.' % name)
173196

174-
def assertContainerNotRunning(self, name):
175-
containers = self._get_containers()
176-
for _id, container in containers.items():
177-
if container.get('name') == name:
178-
raise AssertionError('Container: %s is running' % name)
197+
def assertContainerNotRunning(self, name, timeout=1):
198+
start = datetime.datetime.now()
199+
while (datetime.datetime.now() - start).total_seconds() <= timeout:
200+
container_running = False
201+
containers = self._get_containers()
202+
for _id, container in containers.items():
203+
if container.get('name') == name:
204+
container_running = True
205+
if container_running:
206+
raise AssertionError('Container: %s is running' % name)
179207
return True
180208

181209
def get_initial_state(self):
@@ -217,6 +245,7 @@ class KubernetesProviderTestSuite(BaseProviderTestSuite):
217245

218246
@classmethod
219247
def setUpClass(cls):
248+
super(KubernetesProviderTestSuite, cls).setUpClass()
220249
logger.debug('setUpClass...')
221250
logger.debug('Stopping existing kubernetes instance, if any...')
222251
kubernetes.stop()
@@ -393,6 +422,7 @@ class OpenshiftProviderTestSuite(BaseProviderTestSuite):
393422

394423
@classmethod
395424
def setUpClass(cls):
425+
super(OpenshiftProviderTestSuite, cls).setUpClass()
396426
openshift.stop()
397427
openshift.start()
398428
cls.answers = anymarkup.parse(openshift.answers(), 'ini')

tests/functional/test_docker_provider.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class TestWordpress(DockerProviderTestSuite):
1010
'general': {
1111
'namespace': 'default'
1212
},
13-
'mariadb-atomicapp': {
13+
'mariadb-centos7-atomicapp:mariadb-atomicapp': {
1414
'db_user': 'foo',
1515
'db_pass': 'foo',
1616
'db_name': 'foo'
@@ -26,10 +26,10 @@ def test_wordpress_lifecycle(self):
2626
app_spec = self.image_name
2727
workdir = self.deploy(app_spec, self.answers)
2828

29-
self.assertContainerRunning('wordpress-atomicapp')
30-
self.assertContainerRunning('mariadb-atomicapp-app')
29+
self.assertContainerRunning('wordpress-atomicapp', timeout=10)
30+
self.assertContainerRunning('mariadb-atomicapp-app', timeout=10)
3131

3232
self.undeploy(self.image_name, workdir)
3333

34-
self.assertContainerNotRunning('wordpress-atomicapp')
35-
self.assertContainerNotRunning('mariadb-atomicapp-app')
34+
self.assertContainerNotRunning('wordpress-atomicapp', timeout=10)
35+
self.assertContainerNotRunning('mariadb-atomicapp-app', timeout=10)

tests/functional/test_kubernetes_provider.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import absolute_import
22

3-
import os
4-
53
from .base import KubernetesProviderTestSuite
64

75

@@ -26,13 +24,8 @@ def setUp(self):
2624
}
2725
})
2826

29-
def _run(self):
30-
app_spec = self.image_name
31-
return self.deploy(app_spec, self.answers)
32-
3327
def test_wordpress_lifecycle(self):
34-
app_spec = os.path.join(
35-
self.nulecule_lib, 'wordpress-centos7-atomicapp')
28+
app_spec = self.image_name
3629
workdir = self.deploy(app_spec, self.answers)
3730

3831
self.assertPod('wordpress', status='Running', timeout=360)

tests/functional/test_openshift_provider.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import absolute_import
22

33
import logging
4-
import os
54

65
from .base import OpenshiftProviderTestSuite
76

@@ -29,21 +28,17 @@ def setUp(self):
2928
}
3029
})
3130

32-
def _run(self):
33-
app_spec = os.path.join(
34-
self.nulecule_lib, 'wordpress-centos7-atomicapp')
35-
return self.deploy(app_spec, self.answers)
36-
3731
def test_wordpress_lifecycle(self):
38-
workdir = self._run()
32+
app_spec = self.image_name
33+
workdir = self.deploy(app_spec, self.answers)
3934

4035
self.assertPod('wordpress', status='ContainerCreating', timeout=120)
4136
self.assertPod('mariadb', status='Running', timeout=120)
4237

4338
self.assertService('wpfrontend', timeout=120)
4439
self.assertService('mariadb', timeout=120)
4540

46-
self.undeploy(workdir)
41+
self.undeploy(app_spec, workdir)
4742

4843
self.assertPod('wordpress', exists=False, timeout=120)
4944
self.assertPod('mariadb', exists=False, timeout=120)

0 commit comments

Comments
 (0)