tq 0.2.1 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,161 +0,0 @@
1
- require_relative './helper'
2
- require_relative '../lib/tq/app'
3
- require_relative '../lib/tq/shell'
4
-
5
- def setup_test_logger!
6
- TestUtils.setup_logger( File.basename(__FILE__,'.rb') )
7
- end
8
-
9
- class ShellTests < Minitest::Spec
10
-
11
- # for installed app auth
12
- CLIENT_SECRETS_FILE = File.expand_path(
13
- '../config/secrets/test/client_secrets.json', File.dirname(__FILE__)
14
- )
15
- CREDENTIALS_FILE = File.expand_path(
16
- "../config/secrets/test/#{File.basename(__FILE__,'.rb')}-oauth2.json",
17
- File.dirname(__FILE__)
18
- )
19
-
20
- SERVICE_ISSUER_FILE = File.expand_path('../config/secrets/test/issuer', File.dirname(__FILE__))
21
- SERVICE_P12_FILE = File.expand_path('../config/secrets/test/client.p12', File.dirname(__FILE__))
22
-
23
- # task queue constants
24
- TASKQUEUE_APP_CONFIG =
25
- File.expand_path("../config/secrets/test/#{File.basename(__FILE__,'.rb')}" +
26
- "-config.json", File.dirname(__FILE__))
27
-
28
- TASKQUEUE_LEASE_SECS = 2
29
-
30
- class EchoWorker
31
-
32
- def initialize(stdin, stdout, stderr, env)
33
- @stdin = stdin
34
- @env = env
35
- @logger = env['logger']
36
- end
37
-
38
- def call(task)
39
- @logger.info("EchoWorker") { "Received task #{task.id}" }
40
- @logger.debug("EchoWorker") { "Task payload: #{task.payload.inspect}" }
41
- @logger.debug("EchoWorker") { "Env: #{@env.inspect}" }
42
- task.finish!
43
- end
44
-
45
- end
46
-
47
- def logger
48
- @logger ||= TestUtils.current_logger
49
- end
50
-
51
- def queue_helper(project,queue)
52
- TestUtils::QueueHelper.new(project,queue).auth_files(CLIENT_SECRETS_FILE, CREDENTIALS_FILE)
53
- end
54
-
55
- def app_config
56
- @app_config ||= JSON.load( File.open(TASKQUEUE_APP_CONFIG) )
57
- end
58
-
59
- def app_project_id
60
- app_config['project']
61
- end
62
-
63
- def app_stdin_name
64
- app_config['stdin']['name']
65
- end
66
-
67
- def app_stdin_num_tasks
68
- app_config['stdin']['num_tasks']
69
- end
70
-
71
- def populate_queue!(tasks)
72
- q = queue_helper(app_project_id, app_stdin_name)
73
- tasks.each do |task| q.push!(task) end
74
- end
75
-
76
- def clear_queue!
77
- queue_helper(app_project_id, app_stdin_name).clear!
78
- end
79
-
80
- def tasks_on_queue
81
- clear_queue!
82
- end
83
-
84
- def assert_tasks_on_queue(exp)
85
- assert_equal exp, n = tasks_on_queue.length,
86
- "Expected #{exp} tasks on input queue, was #{n}"
87
- end
88
-
89
- def shell_args
90
- [ "--auth-secrets", CLIENT_SECRETS_FILE,
91
- "--auth-store", CREDENTIALS_FILE,
92
- "--config", TASKQUEUE_APP_CONFIG
93
- ]
94
- end
95
-
96
- def shell_args_service
97
- [ "--service-auth-issuer", SERVICE_ISSUER_FILE,
98
- "--service-auth-p12", SERVICE_P12_FILE,
99
- "--config", TASKQUEUE_APP_CONFIG
100
- ]
101
- end
102
-
103
- def setup
104
- sleep TASKQUEUE_LEASE_SECS+1
105
- clear_queue!
106
- @app = TQ::App.new('test_app_shell/0.0.0', EchoWorker)
107
- end
108
-
109
- it 'should execute and complete app-specified number of tasks on input queue' do
110
- exps = [
111
- { 'What is your name?' => 'Sir Lancelot',
112
- 'What is your quest?' => 'To seek the holy grail',
113
- 'What is your favorite color?' => 'blue' },
114
- { 'What is your name?' => 'Sir Robin',
115
- 'What is your quest?' => 'To seek the holy grail',
116
- 'What is the capital of Assyria?' => nil },
117
- { 'What is your name?' => 'Galahad',
118
- 'What is your quest?' => 'To seek the grail',
119
- 'What is your favorite color?' => ['blue','yellow'] },
120
- { 'What is your name?' => 'Arthur',
121
- 'What is your quest?' => 'To seek the holy grail',
122
- 'What is the air-speed velocity of an unladen swallow?' => 'African or European swallow?' }
123
- ]
124
-
125
- populate_queue! exps
126
-
127
- TQ::Shell.new(@app, logger).call( shell_args )
128
-
129
- assert_tasks_on_queue(exps.length - app_stdin_num_tasks)
130
-
131
- end
132
-
133
- it 'should execute using service auth' do
134
- exps = [
135
- { 'What is your name?' => 'Sir Lancelot',
136
- 'What is your quest?' => 'To seek the holy grail',
137
- 'What is your favorite color?' => 'blue' },
138
- { 'What is your name?' => 'Sir Robin',
139
- 'What is your quest?' => 'To seek the holy grail',
140
- 'What is the capital of Assyria?' => nil },
141
- { 'What is your name?' => 'Galahad',
142
- 'What is your quest?' => 'To seek the grail',
143
- 'What is your favorite color?' => ['blue','yellow'] },
144
- { 'What is your name?' => 'Arthur',
145
- 'What is your quest?' => 'To seek the holy grail',
146
- 'What is the air-speed velocity of an unladen swallow?' => 'African or European swallow?' }
147
- ]
148
-
149
- populate_queue! exps
150
-
151
- TQ::Shell.new(@app, logger).call( shell_args_service )
152
-
153
- assert_tasks_on_queue(exps.length - app_stdin_num_tasks)
154
-
155
- end
156
-
157
- end
158
-
159
-
160
- setup_test_logger!
161
-