12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ # To run the tests:
16
+ # nox -s "lint(sample='./dataflow/run_template')"
17
+ # nox -s "py27(sample='./dataflow/run_template')"
18
+ # nox -s "py36(sample='./dataflow/run_template')"
19
+
15
20
import flask
16
21
import json
17
22
import os
18
23
import pytest
19
- import subprocess as sp
20
24
import time
21
25
22
26
from datetime import datetime
27
+ from googleapiclient .discovery import build
28
+ from googleapiclient .errors import HttpError
23
29
from werkzeug .urls import url_encode
24
30
25
31
import main
26
32
27
33
PROJECT = os .environ ['GCLOUD_PROJECT' ]
28
34
BUCKET = os .environ ['CLOUD_STORAGE_BUCKET' ]
29
35
30
- # Wait time until a job can be cancelled, as a best effort.
31
- # If it fails to be cancelled, the job will run for ~8 minutes.
32
- WAIT_TIME = 5 # seconds
36
+ dataflow = build ('dataflow' , 'v1b3' )
33
37
34
38
# Create a fake "app" for generating test request contexts.
35
39
@pytest .fixture (scope = "module" )
36
40
def app ():
37
41
return flask .Flask (__name__ )
38
42
39
43
40
- def test_run_template_empty_args (app ):
44
+ def test_run_template_python_empty_args (app ):
45
+ project = PROJECT
46
+ job = datetime .now ().strftime ('test_run_template_python-%Y%m%d-%H%M%S' )
47
+ template = 'gs://dataflow-templates/latest/Word_Count'
48
+ with pytest .raises (HttpError ):
49
+ main .run (project , job , template )
50
+
51
+
52
+ def test_run_template_python (app ):
53
+ project = PROJECT
54
+ job = datetime .now ().strftime ('test_run_template_python-%Y%m%d-%H%M%S' )
55
+ template = 'gs://dataflow-templates/latest/Word_Count'
56
+ parameters = {
57
+ 'inputFile' : 'gs://apache-beam-samples/shakespeare/kinglear.txt' ,
58
+ 'output' : 'gs://{}/dataflow/wordcount/outputs' .format (BUCKET ),
59
+ }
60
+ res = main .run (project , job , template , parameters )
61
+ dataflow_jobs_cancel (res ['job' ]['id' ])
62
+
63
+
64
+ def test_run_template_http_empty_args (app ):
41
65
with app .test_request_context ():
42
66
with pytest .raises (KeyError ):
43
67
main .run_template (flask .request )
44
68
45
69
46
- def test_run_template_url (app ):
70
+ def test_run_template_http_url (app ):
47
71
args = {
48
72
'project' : PROJECT ,
49
73
'job' : datetime .now ().strftime ('test_run_template_url-%Y%m%d-%H%M%S' ),
@@ -54,12 +78,10 @@ def test_run_template_url(app):
54
78
with app .test_request_context ('/?' + url_encode (args )):
55
79
res = main .run_template (flask .request )
56
80
data = json .loads (res )
57
- job_id = data ['job' ]['id' ]
58
- time .sleep (WAIT_TIME )
59
- assert sp .call (['gcloud' , 'dataflow' , 'jobs' , 'cancel' , job_id ]) == 0
81
+ dataflow_jobs_cancel (data ['job' ]['id' ])
60
82
61
83
62
- def test_run_template_data (app ):
84
+ def test_run_template_http_data (app ):
63
85
args = {
64
86
'project' : PROJECT ,
65
87
'job' : datetime .now ().strftime ('test_run_template_data-%Y%m%d-%H%M%S' ),
@@ -70,12 +92,10 @@ def test_run_template_data(app):
70
92
with app .test_request_context (data = args ):
71
93
res = main .run_template (flask .request )
72
94
data = json .loads (res )
73
- job_id = data ['job' ]['id' ]
74
- time .sleep (WAIT_TIME )
75
- assert sp .call (['gcloud' , 'dataflow' , 'jobs' , 'cancel' , job_id ]) == 0
95
+ dataflow_jobs_cancel (data ['job' ]['id' ])
76
96
77
97
78
- def test_run_template_json (app ):
98
+ def test_run_template_http_json (app ):
79
99
args = {
80
100
'project' : PROJECT ,
81
101
'job' : datetime .now ().strftime ('test_run_template_json-%Y%m%d-%H%M%S' ),
@@ -86,6 +106,16 @@ def test_run_template_json(app):
86
106
with app .test_request_context (json = args ):
87
107
res = main .run_template (flask .request )
88
108
data = json .loads (res )
89
- job_id = data ['job' ]['id' ]
90
- time .sleep (WAIT_TIME )
91
- assert sp .call (['gcloud' , 'dataflow' , 'jobs' , 'cancel' , job_id ]) == 0
109
+ dataflow_jobs_cancel (data ['job' ]['id' ])
110
+
111
+
112
+ def dataflow_jobs_cancel (job_id ):
113
+ # Wait time until a job can be cancelled, as a best effort.
114
+ # If it fails to be cancelled, the job will run for ~8 minutes.
115
+ time .sleep (5 ) # seconds
116
+ request = dataflow .projects ().jobs ().update (
117
+ projectId = PROJECT ,
118
+ jobId = job_id ,
119
+ body = {'requestedState' : 'JOB_STATE_CANCELLED' }
120
+ )
121
+ request .execute ()
0 commit comments