-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathspec.rb
314 lines (293 loc) · 7.29 KB
/
spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
require 'net/http'
require 'rspec'
require 'yaml'
require 'oj'
Oj.default_options = { mode: :strict, indent: 2, symbol_keys: true }
class GetPoster
attr_accessor :ip
def initialize(ip)
@ip = ip
end
def get(path)
uri = URI("http://#{@ip}:3000#{path}")
req = Net::HTTP::Get.new(uri)
Net::HTTP.start(uri.hostname, uri.port) { |h|
h.request(req)
}
end
def post(content, type)
uri = URI("http://#{@ip}:3000/graphql")
req = Net::HTTP::Post.new(uri)
req['Accept-Encoding'] = '*'
req['Content-Type'] = type
req.body = content
Net::HTTP.start(uri.hostname, uri.port) { |h|
h.request(req)
}
end
end
class TestClient < GetPoster
def initialize(name)
@name = name
@cid = `docker run -e LOCAL_STATE_STORE='true' -td #{@name}`.strip
# Dual purpose, get the IP address in the container for the server and
# detect when the container is available. That avoids using a simple sleep
# which sets up a race condition.
remote_ip = nil
20.times do
remote_ip = `docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' #{@cid}`.strip
break if nil != remote_ip && 0 < remote_ip.size()
sleep 1
end
raise StandardError.new("failed to start docker for #{@name}") if remote_ip.nil? || remote_ip.empty?
error = nil
20.times do
begin
uri = URI("http://#{remote_ip}:3000")
content = Net::HTTP.get(uri)
error = nil
break if nil != content
rescue Exception => e
error = e
sleep 1
end
end
raise error unless error.nil?
@ip = `docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' #{@cid}`.strip
end
def stop
`docker stop #{@cid}`unless @cid.nil?
end
def logs
puts `docker logs #{@cid}`unless @cid.nil?
end
end
$local = nil
if ENV["LOCAL_FRAMEWORK"].nil?
if ENV["FRAMEWORK"].nil?
$targets = []
else
$targets = ENV["FRAMEWORK"].split(',')
end
else
$local = ENV["LOCAL_FRAMEWORK"]
end
root = File.expand_path('../frameworks', __FILE__)
$query = '/graphql?query={artists{name,origin,songs{name,duration,likes}},__schema{types{name,kind,fields{name}}}}'
def check_tree(path, exp, act)
if act.nil? && !exp.nil?
describe "value at #{path} should be #{exp}, not nil" do
it "should match" do
expect(act).to eq exp
end
end
return
end
case exp
when Hash
exp.each { |k,v| check_tree("#{path}.#{k}", v, act[k]) }
when Array
# If elements are Hashs then use name to match otherwise assume order is
# important.
if 0 < exp.size
if exp[0].is_a?(Hash)
exp.each_with_index { |v,i|
name = v[:name]
p2 = "#{path}.@name=#{name}"
found = false
act.each { |av|
if name == av[:name]
check_tree(p2, v, av)
found = true
break
end
}
describe "value at #{p2}" do
it "should not be nil" do
expect(found).to eq true
end
end
}
else
exp.each_with_index { |v,i| check_tree("#{path}.#{i}", v, act[i]) }
end
end
when String
describe "value at #{path} should be #{exp}" do
it "should match" do
expect(act.strip).to eq exp
end
end
else
describe "value at #{path} should be #{exp}" do
it "should match" do
expect(act).to eq exp
end
end
end
end
$expect_result = {
data: {
artists: [
{name: 'Fazerdaze', origin: ['Morningside', 'Auckland', 'New Zealand'],
songs: [
{name: 'Jennifer', duration: 240, likes: 0},
{name: 'Lucky Girl', duration: 170, likes: 0},
{name: 'Friends', duration: 194, likes: 0},
{name: 'Reel', duration: 193, likes: 0}
]
},
{name: 'Viagra Boys', origin: ['Stockholm', 'Sweden'],
songs: [
{name: 'Down In The Basement', duration: 216, likes: 0},
{name: 'Frogstrap', duration: 195, likes: 0},
{name: 'Worms', duration: 208, likes: 0},
{name: 'Amphetanarchy', duration: 346, likes: 0}
]
}
],
__schema: {
types: [
{
name: '__EnumValue',
fields: [
{name: 'name'},
{name: 'description'},
{name: 'isDeprecated'},
{name: 'deprecationReason'}
]
},
{name: 'Int'},
{name: '__DirectiveLocation'},
{name: '__Schema',
fields: [
{name: 'types'},
{name: 'queryType'},
{name: 'mutationType'},
{name: 'subscriptionType'},
{name: 'directives'}
]
},
{name: 'Mutation',
fields: [
{name: 'like'}
]
},
{name: 'Date'},
{name: 'Boolean'},
{name: 'String'},
{name: 'Song',
fields: [
{name: 'name'},
{name: 'artist'},
{name: 'duration'},
{name: 'release'},
{name: 'likes'}
]
},
{name: 'Artist',
fields: [
{name: 'name'},
{name: 'songs'},
{name: 'origin'}
]
},
{name: '__Directive',
fields: [
{name: 'name'},
{name: 'description'},
{name: 'locations'},
{name: 'args'}
]
},
{name: '__Field',
fields: [
{name: 'name'},
{name: 'description'},
{name: 'args'},
{name: 'type'},
{name: 'isDeprecated'},
{name: 'deprecationReason'}
]
},
{name: '__TypeKind'},
{name: 'Query',
fields: [
{name: 'artist'},
{name: 'artists'}
]
},
{name: '__InputValue',
fields: [
{name: 'name'},
{name: 'description'},
{name: 'type'},
{name: 'defaultValue'}
]
},
{name: '__Type',
fields: [
{name: 'kind'},
{name: 'name'},
{name: 'description'},
{name: 'fields'},
{name: 'interfaces'},
{name: 'possibleTypes'},
{name: 'enumValues'},
{name: 'inputFields'},
{name: 'ofType'}
]
}
]
}
}
}
def run_test(base, client)
puts "Testing #{base}"
describe "#{base} get on /" do
res = client.get('/')
it 'should return a status code of 200' do
expect(res.code).to eq '200'
end
it 'should return an empty body' do
expect(res.body).to eq ''
end
end
describe "#{base} get on #{$query}" do
res = client.get($query)
it 'should return a status code of 200' do
expect(res.code).to eq '200'
end
result = Oj.load(res.body)
check_tree('result', $expect_result, result)
end
describe "#{base} post on /graphql" do
10.times { |i|
res = client.post(%|mutation{like(artist:"Fazerdaze",song:"Jennifer"){likes}}|, 'application/graphql')
it 'should return a status code of 200' do
expect(res.code).to eq '200'
end
it %|should return {"data":{"like":{"likes":#{i+1}}}}| do
expect(res.body.strip).to eq %|{"data":{"like":{"likes":#{i+1}}}}|
end
}
end
end
if $local.nil?
Dir.glob(root + '/*').each { |dir|
base = File.basename(dir)
info = YAML.load(File.read(dir + "/info.yml"))
if $targets.nil? || 0 == $targets.size || $targets.include?(info['name']) || $targets.include?(info['language'])
begin
tc = TestClient.new(base)
run_test(base, tc)
ensure
tc.logs if ENV["VERBOSE"] == 'true'
tc.stop
end
end
}
else
c = GetPoster.new('127.0.0.1')
run_test($local, c)
end