@@ -30,16 +30,44 @@ def build_tests(self, project, files_or_dirs):
30
30
self .__build_tests (project , [test .name for test in tests_to_run ])
31
31
32
32
def __build_tests (self , project , goals , post_build = '' ):
33
+ if project .target .test_image and project .target .test_dockerfile :
34
+ print ('warning: both "test_image" and "test_dockerfile" options are specified, will use "test_image"' )
33
35
if project .target .test_image :
34
- self .__build_using_image (project , project .target .test_image , goals , post_build )
36
+ self .__build_using_image (
37
+ project ,
38
+ project .target .test_image ,
39
+ goals ,
40
+ post_build
41
+ )
42
+ elif project .target .test_dockerfile :
43
+ self .__build_using_dockerfile (
44
+ project ,
45
+ project .target .test_dockerfile ,
46
+ self .__test_image_name (project ),
47
+ goals ,
48
+ post_build
49
+ )
35
50
else :
36
51
self .__build_goal (goals )
37
52
38
53
def __build (self , project , goals , post_build = '' ):
54
+ if project .target .image and project .target .dockerfile :
55
+ print ('warning: both "image" and "dockerfile" options are specified, will use "image"' )
39
56
if project .target .image :
40
- self .__build_using_image (project , project .target .image , goals , post_build )
57
+ self .__build_using_image (
58
+ project ,
59
+ project .target .image ,
60
+ goals ,
61
+ post_build
62
+ )
41
63
elif project .target .dockerfile :
42
- self .__build_using_dockerfile (project , project .target .dockerfile , goals , post_build )
64
+ self .__build_using_dockerfile (
65
+ project ,
66
+ project .target .dockerfile ,
67
+ self .__build_image_name (project ),
68
+ goals ,
69
+ post_build
70
+ )
43
71
else :
44
72
self .__build_goal (goals )
45
73
@@ -61,10 +89,9 @@ def __build_using_image(self, project, image_name, goals, post_build):
61
89
raise DockerImageNotFound (image_name )
62
90
self .__build_inside_container (client , project , image_name , goals , post_build )
63
91
64
- def __build_using_dockerfile (self , project , dockerfile , goals , post_build ):
92
+ def __build_using_dockerfile (self , project , dockerfile , image_name , goals , post_build ):
65
93
client = docker .from_env ()
66
94
print (f'building image from { dockerfile } ' )
67
- image_name = f'{ project .name } _cpm_build'
68
95
with open (dockerfile , 'rb' ) as fileobj :
69
96
client .images .build (path = '.' , fileobj = fileobj , tag = image_name )
70
97
self .__build_inside_container (client , project , image_name , goals , post_build )
@@ -92,10 +119,28 @@ def __build_inside_container(self, client, project, image_name, goals, post_buil
92
119
raise BuildError
93
120
container .remove ()
94
121
122
+ def __build_image_name (self , project ):
123
+ return f'{ project .name } _cpm_build'
124
+
125
+ def __test_image_name (self , project ):
126
+ return f'{ project .name } _cpm_test'
127
+
95
128
def run_tests (self , project , files_or_dirs , test_args = ()):
96
129
tests_to_run = project .test .test_suites if not files_or_dirs else self .tests_from_args (project , files_or_dirs )
97
130
if project .target .test_image :
98
- test_results = self .__run_tests_inside_container (project , tests_to_run , test_args )
131
+ test_results = self .__run_tests_using_image (
132
+ project ,
133
+ project .target .test_image ,
134
+ tests_to_run ,
135
+ test_args
136
+ )
137
+ elif project .target .test_dockerfile :
138
+ test_results = self .__run_tests_using_dockerfile (
139
+ project ,
140
+ self .__test_image_name (project ),
141
+ tests_to_run ,
142
+ test_args
143
+ )
99
144
else :
100
145
test_results = [self .run_test (test .name , test_args ) for test in tests_to_run ]
101
146
if any (result != 0 for result in test_results ):
@@ -109,9 +154,8 @@ def run_test(self, executable, test_args):
109
154
print (f'{ executable } failed with { result .returncode } ({ signal .Signals (- result .returncode ).name } )' )
110
155
return result .returncode
111
156
112
- def __run_tests_inside_container (self , project , tests_to_run , test_args ):
157
+ def __run_tests_using_image (self , project , image_name , tests_to_run , test_args ):
113
158
client = docker .from_env ()
114
- image_name = project .target .test_image
115
159
try :
116
160
client .images .pull (image_name )
117
161
except docker .errors .ImageNotFound :
@@ -120,6 +164,10 @@ def __run_tests_inside_container(self, project, tests_to_run, test_args):
120
164
raise DockerImageNotFound (image_name )
121
165
return [self .__run_test_inside_container (project , client , image_name , test .name , test_args ) for test in tests_to_run ]
122
166
167
+ def __run_tests_using_dockerfile (self , project , image_name , tests_to_run , test_args ):
168
+ client = docker .from_env ()
169
+ return [self .__run_test_inside_container (project , client , image_name , test .name , test_args ) for test in tests_to_run ]
170
+
123
171
def __run_test_inside_container (self , project , client , image_name , executable , test_args ):
124
172
container = client .containers .run (
125
173
image_name ,
@@ -129,6 +177,8 @@ def __run_test_inside_container(self, project, client, image_name, executable, t
129
177
user = f'{ os .getuid ()} :{ os .getgid ()} ' ,
130
178
detach = True
131
179
)
180
+ for log in container .logs (stream = True ):
181
+ sys .stdout .write (log .decode ())
132
182
exit_code = container .wait ()
133
183
result = exit_code ['StatusCode' ]
134
184
container .remove ()
0 commit comments