1
1
import azure .storage .blob
2
2
import dotenv
3
+ import glob
3
4
import logging
4
5
import os
5
6
import pytest
6
- import subprocess
7
7
import time
8
8
9
- """
10
- End to end test for the azure functions using docker compose.
11
- To run the test with logging output use the following command:
12
-
13
- pytest --log-cli-level=INFO tests/test_end_to_end.py
14
- """
15
-
16
9
ENV_FILE = os .getenv ("ENV_FILE" , ".env.e2e" )
17
10
18
11
19
12
@pytest .fixture ()
20
13
def setup ():
21
- if not os .getenv ("GITHUB_ACTIONS" ):
22
- pytest .skip ("Skipping end to end test, set GITHUB_ACTIONS env var to run this test" )
23
-
24
14
dotenv .load_dotenv (dotenv_path = ENV_FILE )
25
15
26
16
27
- @pytest .fixture
28
- def docker ():
29
- try :
30
- assert docker_compose_build (), "Error building containers"
31
- assert docker_compose_up ()
32
- yield
33
- finally :
34
- docker_compose_down ()
35
-
36
-
37
- def docker_arglist (command , * args ):
38
- return ['docker' , 'compose' , '--env-file' , ENV_FILE , '--profile' , 'test' , command , * args ]
39
-
40
-
41
- def docker_compose_build ():
42
- logging .info ("Building containers" )
43
- try :
44
- subprocess .run (
45
- docker_arglist ('build' , 'azurite' , 'azurite-setup' , 'notify' , 'process-pilot-data' ),
46
- check = True ,
47
- stderr = subprocess .PIPE ,
48
- stdout = subprocess .PIPE ,
49
- text = True ,
50
- )
51
- return True
52
- except Exception as e :
53
- logging .error (f"Error building containers: { e .stderr } " )
54
- return False
55
-
56
-
57
- def docker_compose_up ():
58
- logging .info ("Starting containers" )
59
- try :
60
- subprocess .Popen (
61
- docker_arglist ('up' , '-d' ),
62
- stderr = subprocess .PIPE ,
63
- stdout = subprocess .PIPE ,
64
- )
65
- return True
66
- except Exception as e :
67
- logging .error (f"Error building containers: { e .stderr } " )
68
- return False
69
-
70
-
71
- def docker_compose_down ():
72
- logging .info ("Stopping containers" )
73
- subprocess .run (
74
- docker_arglist ('down' ),
75
- stdout = subprocess .PIPE ,
76
- stderr = subprocess .PIPE ,
77
- )
78
-
79
-
80
17
def upload_file_to_blob_storage ():
81
18
logging .info ("Uploading file to blob storage" )
82
19
try :
@@ -99,17 +36,17 @@ def upload_file_to_blob_storage():
99
36
return True
100
37
101
38
39
+ def log_dir_for_container (container_name ):
40
+ return f"/tests/end_to_end/log/functions/{ container_name } "
41
+
42
+
102
43
def logs_for_container (container_name ):
103
- result = subprocess .run (
104
- docker_arglist ('logs' , container_name , '--since' , '30s' ),
105
- check = True ,
106
- stderr = subprocess .PIPE ,
107
- stdout = subprocess .PIPE ,
108
- text = True ,
109
- )
110
- logging .debug (f"Logs for { container_name } : { result .stdout } " )
44
+ logs = glob .glob (f"{ log_dir_for_container (container_name )} /*.log" )
45
+
46
+ assert logs , f"No logs found for container { container_name } "
111
47
112
- return result .stdout
48
+ with open (logs [0 ]) as f :
49
+ return f .read ()
113
50
114
51
115
52
def logs_contain_message (container_name , message ):
@@ -126,21 +63,16 @@ def poll_logs_for_message(container_name, message, cycles=20):
126
63
return False
127
64
128
65
129
- def test_end_to_end (setup , docker ):
130
- assert poll_logs_for_message ("azurite-setup" , "Blob containers created" ), (
131
- "Containers not ready" )
132
-
133
- logging .info ("Containers are ready" )
134
-
66
+ def test_end_to_end (setup ):
135
67
assert upload_file_to_blob_storage (), "File upload unsuccessful"
136
68
137
69
assert poll_logs_for_message ("process-pilot-data" , "Trigger Details:" ), (
138
70
"ProcessPilotData function not triggered by file upload" )
139
71
140
- assert logs_contain_message (
72
+ assert poll_logs_for_message (
141
73
"process-pilot-data" , "Executing 'Functions.ProcessPilotData'" ), (
142
74
"ProcessPilotData function not executed" )
143
- assert logs_contain_message (
75
+ assert poll_logs_for_message (
144
76
"notify" , "Executing 'Functions.Notify'" ), (
145
77
"Notify function not executed" )
146
78
assert poll_logs_for_message (
0 commit comments