/
command_line_handler_test.go
772 lines (644 loc) · 27 KB
/
command_line_handler_test.go
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
// Copyright (C) 2016-Present Pivotal Software, Inc. All rights reserved.
// This program and the accompanying materials are made available under
// the terms of the under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package integration_tests_test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os/exec"
"gopkg.in/yaml.v2"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
"github.com/pivotal-cf/on-demand-services-sdk/bosh"
"github.com/pivotal-cf/on-demand-services-sdk/integration_tests/testharness/testvariables"
"github.com/pivotal-cf/on-demand-services-sdk/serviceadapter"
)
var (
stdout *bytes.Buffer
stderr *bytes.Buffer
operationFails string
exitCode int
doNotImplementInterfaces bool
expectedServiceDeployment = serviceadapter.ServiceDeployment{
DeploymentName: "service-instance-deployment",
Releases: serviceadapter.ServiceReleases{
{
Name: "release-name",
Version: "release-version",
Jobs: []string{"job_one", "job_two"},
},
},
Stemcells: []serviceadapter.Stemcell{{
OS: "BeOS",
Version: "2",
}},
}
expectedCurrentPlan = serviceadapter.Plan{
InstanceGroups: []serviceadapter.InstanceGroup{{
Name: "example-server",
VMType: "small",
PersistentDiskType: "ten",
Networks: []string{"example-network"},
AZs: []string{"example-az"},
Instances: 1,
Lifecycle: "errand",
}},
Properties: serviceadapter.Properties{"example": "property"},
}
expectedRequestParams = map[string]interface{}{"key": "foo", "bar": "baz"}
expectedResultantBoshManifest = bosh.BoshManifest{Name: "deployment-name",
Releases: []bosh.Release{
{
Name: "a-release",
Version: "latest",
},
},
Stemcells: []bosh.Stemcell{
{
Alias: "greatest",
OS: "Windows",
Version: "3.1",
},
},
InstanceGroups: []bosh.InstanceGroup{
{
Name: "Test",
Properties: map[string]interface{}{
"parseSymbols": "yes%[===",
},
},
},
}
expectedPreviousPlan = serviceadapter.Plan{
InstanceGroups: []serviceadapter.InstanceGroup{{
Name: "another-example-server",
VMType: "small",
PersistentDiskType: "ten",
Networks: []string{"example-network"},
AZs: []string{"example-az"},
Instances: 1,
Lifecycle: "errand",
}},
Properties: serviceadapter.Properties{"example": "property"},
}
expectedPlan = expectedPreviousPlan
expectedPreviousManifest = bosh.BoshManifest{Name: "another-deployment-name",
Releases: []bosh.Release{
{
Name: "a-release",
Version: "latest",
},
},
InstanceGroups: []bosh.InstanceGroup{},
Stemcells: []bosh.Stemcell{
{
Alias: "greatest",
OS: "Windows",
Version: "3.1",
},
}}
expectedBindingID = "bindingId"
expectedBoshVMs = bosh.BoshVMs{"kafka": []string{"a", "b"}}
expectedManifest = expectedPreviousManifest
expectedUnbindingRequestParams = serviceadapter.RequestParameters{"unbinding_param": "unbinding_value"}
expectedSecretsMap = serviceadapter.ODBManagedSecrets{"foo": "bar"}
)
var _ = Describe("Command line handler", func() {
BeforeEach(func() {
doNotImplementInterfaces = false
stdout = new(bytes.Buffer)
stderr = new(bytes.Buffer)
operationFails = ""
})
It("logs and exits with 1 if called without args", func() {
exitCode = startPassingCommandAndGetExitCode([]string{})
Expect(exitCode).To(Equal(1))
Expect(stderr.String()).To(Equal("[odb-sdk] the following commands are supported: create-binding, dashboard-url, delete-binding, generate-manifest, generate-plan-schemas\n"))
})
It("logs and exits with 1 if called with a non-existing subcommand", func() {
exitCode = startPassingCommandAndGetExitCode([]string{"non-existing-subcommand"})
Expect(exitCode).To(Equal(1))
Expect(stderr.String()).To(ContainSubstring(`[odb-sdk] unknown subcommand: non-existing-subcommand. The following commands are supported: create-binding, dashboard-url, delete-binding, generate-manifest, generate-plan-schemas`))
})
Describe("generate-manifest subcommand", func() {
Describe("with positional arguments", func() {
It("succeeds without optional parameters", func() {
exitCode = startPassingCommandAndGetExitCode([]string{"generate-manifest",
toJson(expectedServiceDeployment),
toJson(expectedCurrentPlan),
toJson(expectedRequestParams),
"",
"null",
})
Expect(exitCode).To(Equal(0))
Expect(stdout.String()).To(Equal(toYaml(expectedResultantBoshManifest)))
})
It("generate-manifest exits with 10", func() {
exitCode = startEmptyImplementationCommandAndGetExitCode([]string{"generate-manifest", toJson(expectedServiceDeployment), toJson(expectedCurrentPlan), toJson(expectedRequestParams), "", "null"})
Expect(exitCode).To(Equal(10))
})
It("succeeds with optional parameters", func() {
exitCode = startPassingCommandAndGetExitCode([]string{
"generate-manifest",
toJson(expectedServiceDeployment),
toJson(expectedCurrentPlan),
toJson(expectedRequestParams),
toYaml(expectedPreviousManifest),
toJson(expectedPreviousPlan),
})
Expect(exitCode).To(Equal(0))
Expect(stdout.String()).To(Equal(toYaml(expectedResultantBoshManifest)))
})
It("logs and exits with 1 when an argument is missing", func() {
exitCode = startPassingCommandAndGetExitCode([]string{"generate-manifest", "foo"})
Expect(exitCode).To(Equal(1))
Expect(stderr.String()).To(MatchRegexp(
`Missing arguments for generate-manifest. Usage: testharness-\d* generate-manifest <service-deployment-JSON> <plan-JSON> <request-params-JSON> <previous-manifest-YAML> <previous-plan-JSON>`))
})
It("exits 1 and logs when a generic error occurs", func() {
exitCode = startFailingCommandAndGetExitCode([]string{"generate-manifest",
toJson(expectedServiceDeployment),
toJson(expectedCurrentPlan),
toJson(expectedRequestParams),
"",
"null",
}, "true")
Expect(exitCode).To(Equal(1))
Expect(stdout.String()).To(Equal("some message to the user"))
})
})
Describe("with arguments passed in on standard input", func() {
It("errors when nothing is sent to stdin", func() {
exitCode = startCommandWithNoStdinAndGetExitCode([]string{"generate-manifest"})
Expect(exitCode).To(Equal(1))
Expect(stderr.String()).To(ContainSubstring("expecting parameters to be passed via stdin"))
})
It("succeeds", func() {
rawInputParams := serviceadapter.InputParams{
GenerateManifest: serviceadapter.GenerateManifestJSONParams{
ServiceDeployment: toJson(expectedServiceDeployment),
Plan: toJson(expectedCurrentPlan),
PreviousPlan: toJson(expectedPreviousPlan),
RequestParameters: toJson(expectedRequestParams),
PreviousManifest: "",
},
}
exitCode = startCommandWithStdinAndGetExitCode([]string{"generate-manifest"},
toJson(rawInputParams),
)
Expect(exitCode).To(Equal(0))
var output serviceadapter.MarshalledGenerateManifest
Expect(json.Unmarshal(stdout.Bytes(), &output)).To(Succeed())
Expect(output.Manifest).To(Equal(toYaml(expectedResultantBoshManifest)))
})
It("generate-manifest exits with 10 when command not implemented", func() {
exitCode = startEmptyImplementationCommandAndGetExitCode([]string{"generate-manifest"})
Expect(exitCode).To(Equal(10))
})
It("succeeds with optional parameters", func() {
rawInputParams := serviceadapter.InputParams{
GenerateManifest: serviceadapter.GenerateManifestJSONParams{
ServiceDeployment: toJson(expectedServiceDeployment),
Plan: toJson(expectedCurrentPlan),
PreviousPlan: toJson(expectedPreviousPlan),
RequestParameters: toJson(expectedRequestParams),
PreviousManifest: toYaml(expectedPreviousManifest),
PreviousSecrets: toJson(expectedSecretsMap),
},
}
exitCode = startCommandWithStdinAndGetExitCode([]string{"generate-manifest"},
toJson(rawInputParams),
)
Expect(exitCode).To(Equal(0))
var output serviceadapter.MarshalledGenerateManifest
Expect(json.Unmarshal(stdout.Bytes(), &output)).To(Succeed())
Expect(output.Manifest).To(Equal(toYaml(expectedResultantBoshManifest)))
})
It("logs and exits with 1 when a required field param is missing", func() {
rawInputParams := serviceadapter.InputParams{
GenerateManifest: serviceadapter.GenerateManifestJSONParams{
ServiceDeployment: toJson(expectedServiceDeployment),
Plan: "{}",
PreviousPlan: toJson(expectedPreviousPlan),
RequestParameters: toJson(expectedRequestParams),
PreviousManifest: toYaml(expectedPreviousManifest),
},
}
exitCode = startCommandWithStdinAndGetExitCode([]string{"generate-manifest"},
toJson(rawInputParams),
)
Expect(exitCode).To(Equal(1))
Expect(stderr.String()).To(ContainSubstring(
"validating service plan: Key: 'Plan.InstanceGroups' Error:Field validation for 'InstanceGroups' failed on the 'required' tag"))
})
It("exits 1 and logs when a generic error occurs", func() {
rawInputParams := serviceadapter.InputParams{
GenerateManifest: serviceadapter.GenerateManifestJSONParams{
ServiceDeployment: toJson(expectedServiceDeployment),
Plan: toJson(expectedCurrentPlan),
PreviousPlan: toJson(expectedPreviousPlan),
RequestParameters: toJson(expectedRequestParams),
PreviousManifest: toYaml(expectedPreviousManifest),
},
}
exitCode = startFailingCommandWithStdinAndGetExitCode(
[]string{"generate-manifest"},
toJson(rawInputParams),
"true",
)
Expect(exitCode).To(Equal(1))
Expect(stdout.String()).To(Equal("some message to the user"))
})
})
})
Describe("create-binding subcommand", func() {
Describe("with positional arguments", func() {
It("succeeds", func() {
exitCode = startPassingCommandAndGetExitCode([]string{"create-binding", expectedBindingID, toJson(expectedBoshVMs), toYaml(expectedManifest), toJson(expectedRequestParams)})
Expect(exitCode).To(Equal(0))
Expect(stdout.String()).To(MatchJSON(toJson(testvariables.SuccessfulBinding)))
})
It("create-binding exits with 10", func() {
exitCode = startEmptyImplementationCommandAndGetExitCode([]string{"create-binding", toJson(expectedServiceDeployment), toJson(expectedCurrentPlan), toJson(expectedRequestParams), "", "null"})
Expect(exitCode).To(Equal(10))
})
It("logs and exits with 1 when an argument is missing", func() {
exitCode = startPassingCommandAndGetExitCode([]string{"create-binding", "arg1"})
Expect(exitCode).To(Equal(1))
Expect(stderr.String()).To(MatchRegexp(
`Missing arguments for create-binding. Usage: testharness-\d* create-binding <binding-ID> <bosh-VMs-JSON> <manifest-YAML> <request-params-JSON>`,
))
})
It("exits with 49 when a binding already exists", func() {
exitCode = startFailingCommandAndGetExitCode([]string{"create-binding", expectedBindingID, toJson(expectedBoshVMs), toYaml(expectedManifest), toJson(expectedRequestParams)}, testvariables.ErrBindingAlreadyExists)
Expect(exitCode).To(Equal(49))
})
It("exits with 42 when app_guid is not provided", func() {
exitCode = startFailingCommandAndGetExitCode([]string{"create-binding", expectedBindingID, toJson(expectedBoshVMs), toYaml(expectedManifest), toJson(expectedRequestParams)}, testvariables.ErrAppGuidNotProvided)
Expect(exitCode).To(Equal(42))
})
It("exits with 1 when an error occurs", func() {
exitCode = startFailingCommandAndGetExitCode([]string{"create-binding", expectedBindingID, toJson(expectedBoshVMs), toYaml(expectedManifest), toJson(expectedRequestParams)}, "true")
Expect(exitCode).To(Equal(1))
Expect(stdout.String()).To(Equal("An internal error occured."))
})
})
Describe("with arguments passed through standard input", func() {
var rawInputParams serviceadapter.InputParams
BeforeEach(func() {
rawInputParams = serviceadapter.InputParams{
CreateBinding: serviceadapter.CreateBindingJSONParams{
BoshVms: toJson(expectedBoshVMs),
BindingId: expectedBindingID,
RequestParameters: toJson(expectedRequestParams),
Manifest: toYaml(expectedManifest),
},
}
})
It("errors when stdin is empty", func() {
exitCode = startCommandWithNoStdinAndGetExitCode([]string{"create-binding"})
Expect(exitCode).To(Equal(1))
Expect(stderr.String()).To(ContainSubstring("expecting parameters to be passed via stdin"))
})
It("succeeds", func() {
exitCode = startCommandWithStdinAndGetExitCode([]string{"create-binding"},
toJson(rawInputParams),
)
Expect(exitCode).To(Equal(0))
Expect(stdout.String()).To(MatchJSON(toJson(testvariables.SuccessfulBinding)))
})
It("exits with 10 if command is not implemented", func() {
exitCode = startEmptyImplementationCommandAndGetExitCode([]string{"create-binding"})
Expect(exitCode).To(Equal(10))
})
It("exits with 49 when a binding already exists", func() {
exitCode = startFailingCommandWithStdinAndGetExitCode([]string{"create-binding"}, toJson(rawInputParams), testvariables.ErrBindingAlreadyExists)
Expect(exitCode).To(Equal(49))
})
It("exits with 42 when app_guid is not provided", func() {
exitCode = startFailingCommandWithStdinAndGetExitCode([]string{"create-binding"}, toJson(rawInputParams), testvariables.ErrAppGuidNotProvided)
Expect(exitCode).To(Equal(42))
})
It("exits with 1 when an error occurs", func() {
exitCode = startFailingCommandWithStdinAndGetExitCode([]string{"create-binding"}, toJson(rawInputParams), "true")
Expect(exitCode).To(Equal(1))
Expect(stdout.String()).To(Equal("An internal error occured."))
})
})
})
Describe("delete-binding subcommand", func() {
Describe("with positional arguments", func() {
It("succeeds", func() {
exitCode = startPassingCommandAndGetExitCode([]string{"delete-binding", expectedBindingID, toJson(expectedBoshVMs), toYaml(expectedManifest), toJson(expectedUnbindingRequestParams)})
Expect(exitCode).To(Equal(0))
Expect(stdout.String()).To(BeEmpty())
})
It("delete-binding exits with 10", func() {
exitCode = startEmptyImplementationCommandAndGetExitCode([]string{"delete-binding", toJson(expectedServiceDeployment), toJson(expectedCurrentPlan), toJson(expectedRequestParams), "", "null"})
Expect(exitCode).To(Equal(10))
})
It("logs and exits with 1 when an argument is missing", func() {
exitCode = startPassingCommandAndGetExitCode([]string{"delete-binding", "arg1"})
Expect(exitCode).To(Equal(1))
Expect(stderr.String()).To(MatchRegexp(
`Missing arguments for delete-binding. Usage: testharness-\d* delete-binding <binding-ID> <bosh-VMs-JSON> <manifest-YAML> <request-params-JSON>`,
))
})
It("exits with 41 when the binding is not found", func() {
exitCode = startFailingCommandAndGetExitCode([]string{"delete-binding", expectedBindingID, toJson(expectedBoshVMs), toYaml(expectedManifest), toJson(expectedUnbindingRequestParams)}, testvariables.ErrBindingNotFound)
Expect(exitCode).To(Equal(41))
Expect(stdout.String()).To(ContainSubstring("binding not found"))
})
It("exits with a failure when a generic error occurs", func() {
exitCode = startFailingCommandAndGetExitCode([]string{"delete-binding", expectedBindingID, toJson(expectedBoshVMs), toYaml(expectedManifest), toJson(expectedUnbindingRequestParams)}, "true")
Expect(exitCode).To(Equal(1))
Expect(stdout.String()).To(Equal("An error occurred"))
})
})
Describe("with arguments passed via stdin", func() {
var rawInputParams serviceadapter.InputParams
BeforeEach(func() {
rawInputParams = serviceadapter.InputParams{
DeleteBinding: serviceadapter.DeleteBindingJSONParams{
BoshVms: toJson(expectedBoshVMs),
BindingId: expectedBindingID,
RequestParameters: toJson(expectedRequestParams),
Manifest: toYaml(expectedManifest),
},
}
})
It("fails when stdin is empty", func() {
exitCode = startCommandWithNoStdinAndGetExitCode([]string{"delete-binding"})
Expect(exitCode).To(Equal(1))
Expect(stderr.String()).To(MatchRegexp("expecting parameters to be passed via stdin"))
})
It("succeeds", func() {
exitCode = startCommandWithStdinAndGetExitCode([]string{"delete-binding"},
toJson(rawInputParams),
)
Expect(exitCode).To(Equal(0))
Expect(stdout.String()).To(BeEmpty())
})
It("delete-binding exits with 10 when command is not implemented", func() {
exitCode = startEmptyImplementationCommandAndGetExitCode([]string{"delete-binding"})
Expect(exitCode).To(Equal(10))
})
It("exits with 41 when the binding is not found", func() {
exitCode = startFailingCommandWithStdinAndGetExitCode([]string{"delete-binding"}, toJson(rawInputParams), testvariables.ErrBindingNotFound)
Expect(exitCode).To(Equal(41))
Expect(stdout.String()).To(ContainSubstring("binding not found"))
})
It("exits with a failure when a generic error occurs", func() {
exitCode = startFailingCommandWithStdinAndGetExitCode([]string{"delete-binding"}, toJson(rawInputParams), "true")
Expect(exitCode).To(Equal(1))
Expect(stdout.String()).To(Equal("An error occurred"))
})
})
})
Describe("dashboard-url subcommand", func() {
Describe("with positional arguments", func() {
It("succeeds", func() {
exitCode = startPassingCommandAndGetExitCode([]string{"dashboard-url", "instance-identifier", toJson(expectedPlan), toYaml(expectedManifest)})
Expect(exitCode).To(Equal(0))
Expect(stdout.Bytes()).To(MatchJSON(`{ "dashboard_url": "http://dashboard.com"}`))
})
It("dashboard-url exits with 10", func() {
exitCode = startEmptyImplementationCommandAndGetExitCode([]string{"dashboard-url", "id", toJson(expectedCurrentPlan), "null"})
Expect(exitCode).To(Equal(10))
})
It("logs and exits with 1 when an argument is missing", func() {
exitCode = startPassingCommandAndGetExitCode([]string{"dashboard-url", "foo"})
Expect(exitCode).To(Equal(1))
Expect(stderr.String()).To(MatchRegexp(
`Missing arguments for dashboard-url. Usage: testharness-\d* dashboard-url <instance-ID> <plan-JSON> <manifest-YAML>`,
))
})
It("exits with 1 if a generic error occurs", func() {
exitCode = startFailingCommandAndGetExitCode([]string{"dashboard-url", "instance-identifier", toJson(expectedPlan), toYaml(expectedManifest)}, "true")
Expect(exitCode).To(Equal(1))
Expect(stdout.String()).To(Equal("An error occurred"))
})
})
Describe("with arguments passed via stdin", func() {
It("fails when nothing is sent through stdin", func() {
exitCode = startCommandWithNoStdinAndGetExitCode([]string{"dashboard-url"})
Expect(exitCode).To(Equal(1))
Expect(stderr.String()).To(ContainSubstring("expecting parameters to be passed via stdin"))
})
It("succeeds", func() {
rawInputParams := serviceadapter.InputParams{
DashboardUrl: serviceadapter.DashboardUrlJSONParams{
InstanceId: "some-id",
Plan: toJson(expectedCurrentPlan),
Manifest: toYaml(expectedManifest),
},
}
exitCode = startCommandWithStdinAndGetExitCode([]string{"dashboard-url"},
toJson(rawInputParams),
)
Expect(exitCode).To(Equal(0))
Expect(stdout.Bytes()).To(MatchJSON(`{ "dashboard_url": "http://dashboard.com"}`))
})
It("dashboard-url exits with 10", func() {
exitCode = startEmptyImplementationCommandAndGetExitCode([]string{"dashboard-url"})
Expect(exitCode).To(Equal(10))
})
It("exits with 1 if a generic error occurs", func() {
rawInputParams := serviceadapter.InputParams{
DashboardUrl: serviceadapter.DashboardUrlJSONParams{
InstanceId: "some-id",
Plan: toJson(expectedCurrentPlan),
Manifest: toYaml(expectedManifest),
},
}
exitCode = startFailingCommandWithStdinAndGetExitCode(
[]string{"dashboard-url"},
toJson(rawInputParams),
"true",
)
Expect(exitCode).To(Equal(1))
Expect(stdout.String()).To(Equal("An error occurred"))
})
})
})
Describe("generate-plan-schemas subcommand", func() {
Describe("with positional arguments", func() {
It("returns 0 and output the schema for a plan", func() {
exitCode = startPassingCommandAndGetExitCode([]string{"generate-plan-schemas", "--plan-json", toJson(expectedPlan)})
schemas := serviceadapter.JSONSchemas{
Parameters: map[string]interface{}{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": map[string]interface{}{
"billing-account": map[string]interface{}{
"description": "Billing account number used to charge use of shared fake server.",
"type": "string",
},
},
},
}
expectedPlanSchema := serviceadapter.PlanSchema{
ServiceInstance: serviceadapter.ServiceInstanceSchema{
Create: schemas,
Update: schemas,
},
ServiceBinding: serviceadapter.ServiceBindingSchema{
Create: schemas,
},
}
Expect(exitCode).To(Equal(0))
Expect(stdout.Bytes()).To(MatchJSON(toJson(expectedPlanSchema)))
})
It("returns 1 if an error occurred while parsing the CLI args", func() {
exitCode = startPassingCommandAndGetExitCode([]string{"generate-plan-schemas", "-foo"})
Expect(exitCode).To(Equal(1))
Expect(stderr.String()).To(ContainSubstring(
"flag provided but not defined: -foo",
))
})
It("returns 1 if an error occurred while generating the schema", func() {
exitCode = startFailingCommandAndGetExitCode([]string{
"generate-plan-schemas", "--plan-json", toJson(expectedPlan),
}, "true")
Expect(exitCode).To(Equal(1))
Expect(stderr.String()).To(MatchRegexp(`\[odb-sdk\] An error occurred`))
})
It("returns 10 (not implemented) when the command is not implement", func() {
exitCode = startEmptyImplementationCommandAndGetExitCode([]string{"generate-plan-schemas", "--plan-json", toJson(expectedCurrentPlan)})
Expect(exitCode).To(Equal(10))
})
})
Describe("with arguments passed via stdin", func() {
It("fails when nothing is provided in the stdin", func() {
exitCode = startCommandWithNoStdinAndGetExitCode([]string{"generate-plan-schemas"})
Expect(exitCode).To(Equal(1))
Expect(stderr.String()).To(ContainSubstring("expecting parameters to be passed via stdin"))
})
It("returns 0 and output the schema for a plan", func() {
rawInputParams := serviceadapter.InputParams{
GeneratePlanSchemas: serviceadapter.GeneratePlanSchemasJSONParams{
Plan: toJson(expectedCurrentPlan),
},
}
exitCode = startCommandWithStdinAndGetExitCode([]string{"generate-plan-schemas"},
toJson(rawInputParams),
)
schemas := serviceadapter.JSONSchemas{
Parameters: map[string]interface{}{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": map[string]interface{}{
"billing-account": map[string]interface{}{
"description": "Billing account number used to charge use of shared fake server.",
"type": "string",
},
},
},
}
expectedPlanSchema := serviceadapter.PlanSchema{
ServiceInstance: serviceadapter.ServiceInstanceSchema{
Create: schemas,
Update: schemas,
},
ServiceBinding: serviceadapter.ServiceBindingSchema{
Create: schemas,
},
}
Expect(exitCode).To(Equal(0))
Expect(stdout.Bytes()).To(MatchJSON(toJson(expectedPlanSchema)))
})
It("returns 1 if an error occurred while generating the schema", func() {
rawInputParams := serviceadapter.InputParams{
GeneratePlanSchemas: serviceadapter.GeneratePlanSchemasJSONParams{
Plan: toJson(expectedCurrentPlan),
},
}
exitCode = startFailingCommandWithStdinAndGetExitCode(
[]string{"generate-plan-schemas"},
toJson(rawInputParams),
"true",
)
Expect(exitCode).To(Equal(1))
Expect(stderr.String()).To(MatchRegexp(`\[odb-sdk\] An error occurred`))
})
It("returns 10 (not implemented) when the command is not implement", func() {
exitCode = startEmptyImplementationCommandAndGetExitCode([]string{"generate-plan-schemas"})
Expect(exitCode).To(Equal(10))
})
})
})
})
func startEmptyImplementationCommandAndGetExitCode(args []string) int {
doNotImplementInterfaces = true
operationFails = ""
return startCommandAndGetExitCode(args)
}
func startFailingCommandAndGetExitCode(args []string, errMsg string) int {
operationFails = errMsg
return startCommandAndGetExitCode(args)
}
func startPassingCommandAndGetExitCode(args []string) int {
operationFails = ""
return startCommandAndGetExitCode(args)
}
func startCommandAndGetExitCode(args []string) int {
cmd := exec.Command(adapterBin, args...)
cmd.Env = resetCommandEnv()
runningAdapter, err := gexec.Start(cmd, io.MultiWriter(GinkgoWriter, stdout), io.MultiWriter(GinkgoWriter, stderr))
Expect(err).NotTo(HaveOccurred())
Eventually(runningAdapter, time.Second*2).Should(gexec.Exit())
return runningAdapter.ExitCode()
}
func startFailingCommandWithStdinAndGetExitCode(args []string, stdin, errMsg string) int {
operationFails = errMsg
return startCommandWithStdinAndGetExitCode(args, stdin)
}
func startCommandWithNoStdinAndGetExitCode(args []string) int {
cmd := exec.Command(adapterBin, args...)
cmd.Env = resetCommandEnv()
runningAdapter, err := gexec.Start(cmd, io.MultiWriter(GinkgoWriter, stdout), io.MultiWriter(GinkgoWriter, stderr))
Expect(err).NotTo(HaveOccurred())
Eventually(runningAdapter, time.Second*3).Should(gexec.Exit())
return runningAdapter.ExitCode()
}
func startCommandWithStdinAndGetExitCode(args []string, stdin string) int {
cmd := exec.Command(adapterBin, args...)
cmd.Env = resetCommandEnv()
pipe, err := cmd.StdinPipe()
Expect(err).NotTo(HaveOccurred())
_, err = io.WriteString(pipe, stdin)
Expect(err).NotTo(HaveOccurred())
pipe.Close()
runningAdapter, err := gexec.Start(cmd, io.MultiWriter(GinkgoWriter, stdout), io.MultiWriter(GinkgoWriter, stderr))
Expect(err).NotTo(HaveOccurred())
Eventually(runningAdapter).Should(gexec.Exit())
return runningAdapter.ExitCode()
}
func resetCommandEnv() []string {
return []string{
fmt.Sprintf("%s=%s", testvariables.OperationFailsKey, operationFails),
fmt.Sprintf("%s=%t", testvariables.DoNotImplementInterfacesKey, doNotImplementInterfaces),
}
}
func toYaml(obj interface{}) string {
str, err := yaml.Marshal(obj)
Expect(err).NotTo(HaveOccurred())
return string(str)
}
func toJson(obj interface{}) string {
str, err := json.Marshal(obj)
Expect(err).NotTo(HaveOccurred())
return string(str)
}