1
+ import distutils .dir_util
1
2
import os
3
+ import re
2
4
import logging
3
5
import logging .config
4
- import re
5
6
import time
6
7
import anymarkup
7
8
import datetime
@@ -51,12 +52,13 @@ class BaseProviderTestSuite(unittest.TestCase):
51
52
NULECULE_LIB_REPO = 'https://github.com/projectatomic/nulecule-library'
52
53
NULECULE_LIB_PATH = os .path .join (os .path .dirname (__file__ ),
53
54
'nulecule-library' )
55
+ BUILD_DIR = os .path .join (os .path .dirname (__file__ ), 'build' )
54
56
PROVIDER = None
55
57
56
58
@classmethod
57
59
def setUpClass (cls ):
58
60
cls .fetch_nulecule_lib ()
59
- cls .build_image ()
61
+ cls .build ()
60
62
61
63
@classmethod
62
64
def tearDownClass (cls ):
@@ -85,8 +87,11 @@ def deploy(self, app_spec, answers):
85
87
Returns:
86
88
Path of the deployed dir.
87
89
"""
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' )
90
95
cmd = (
91
96
'atomic run {app_spec} -a {answers} --provider={provider} '
92
97
'--destination={dest}' ).format (
@@ -132,13 +137,29 @@ def fetch_nulecule_lib(cls):
132
137
'git pull origin master' , shell = True )
133
138
134
139
@classmethod
135
- def build_image (cls ):
140
+ def build (cls ):
136
141
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
+
137
158
cls .image_name = '{}-{}' .format (
138
159
cls .APP_DIR_NAME , uuid .uuid1 ().hex [:8 ])
139
160
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 ),
142
163
stdin = False , stderr = False ,
143
164
shell = True )
144
165
@@ -164,18 +185,25 @@ def tearDown(self):
164
185
print cmd
165
186
subprocess .check_output (cmd )
166
187
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
172
195
raise AssertionError ('Container: %s not running.' % name )
173
196
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 )
179
207
return True
180
208
181
209
def get_initial_state (self ):
@@ -217,6 +245,7 @@ class KubernetesProviderTestSuite(BaseProviderTestSuite):
217
245
218
246
@classmethod
219
247
def setUpClass (cls ):
248
+ super (KubernetesProviderTestSuite , cls ).setUpClass ()
220
249
logger .debug ('setUpClass...' )
221
250
logger .debug ('Stopping existing kubernetes instance, if any...' )
222
251
kubernetes .stop ()
@@ -393,6 +422,7 @@ class OpenshiftProviderTestSuite(BaseProviderTestSuite):
393
422
394
423
@classmethod
395
424
def setUpClass (cls ):
425
+ super (OpenshiftProviderTestSuite , cls ).setUpClass ()
396
426
openshift .stop ()
397
427
openshift .start ()
398
428
cls .answers = anymarkup .parse (openshift .answers (), 'ini' )
0 commit comments