Skip to content

Commit eeb18ce

Browse files
Rearranged the commands/order/place_tests, reduced runtime by 50% (30s -> 15s)
1 parent de43f28 commit eeb18ce

1 file changed

Lines changed: 100 additions & 130 deletions

File tree

plugin/commands/order/place_test.go

Lines changed: 100 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package order_test
22

33
import (
4-
"errors"
5-
6-
"fmt"
7-
8-
. "github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/matchers"
94
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
105

116
. "github.com/onsi/ginkgo/v2"
@@ -19,23 +14,28 @@ import (
1914
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/order"
2015
)
2116

17+
var TESTMAP = map[string]interface{}{
18+
"SoftLayer_Container_Product_Order_Virtual_Guest": &datatypes.Container_Product_Order_Virtual_Guest{},
19+
"SoftLayer_Container_Product_Order_Network_Subnet": &datatypes.Container_Product_Order_Network_Subnet{},
20+
"SoftLayer_Container_Product_Order_Hardware_Server": &datatypes.Container_Product_Order_Hardware_Server{},
21+
"SoftLayer_Container_Product_Order_Network_Storage_AsAService": &datatypes.Container_Product_Order_Network_Storage_AsAService{},
22+
}
23+
2224
var _ = Describe("Place", func() {
2325
var (
24-
fakeUI *terminal.FakeUI
25-
cliCommand *order.PlaceCommand
26-
fakeSession *session.Session
27-
slCommand *metadata.SoftlayerCommand
28-
OrderManager managers.OrderManager
29-
fakeOrderManager *testhelpers.FakeOrderManager
30-
fakeHandler *testhelpers.FakeTransportHandler
26+
fakeUI *terminal.FakeUI
27+
cliCommand *order.PlaceCommand
28+
fakeSession *session.Session
29+
slCommand *metadata.SoftlayerCommand
30+
OrderManager managers.OrderManager
31+
fakeHandler *testhelpers.FakeTransportHandler
3132
)
3233
BeforeEach(func() {
3334
filenames := []string{"getDatacenters_1"}
3435
fakeUI = terminal.NewFakeUI()
3536
fakeSession = testhelpers.NewFakeSoftlayerSession(filenames)
3637
fakeHandler = testhelpers.GetSessionHandler(fakeSession)
3738
OrderManager = managers.NewOrderManager(fakeSession)
38-
fakeOrderManager = new(testhelpers.FakeOrderManager)
3939
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
4040
cliCommand = order.NewPlaceCommand(slCommand)
4141
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
@@ -47,202 +47,172 @@ var _ = Describe("Place", func() {
4747
fakeHandler.ClearErrors()
4848
})
4949

50-
Describe("order verify", func() {
51-
for k, _ := range order.TYPEMAP {
52-
Context("successfully"+k, func() {
50+
Describe("Order Tests", func() {
51+
for k, _ := range TESTMAP {
52+
Context("Happy Path for ComplexType="+k, func() {
5353

5454
k := k
55-
It("return no error with three arguments", func() {
56-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC", "--complex-type", k, "--billing=hourly", "--verify")
55+
It("Verify Basic Order Happy Path", func() {
56+
err := testhelpers.RunCobraCommand(
57+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC",
58+
"--complex-type", k, "--billing=hourly", "--verify")
5759
Expect(err).NotTo(HaveOccurred())
58-
fmt.Println(fakeUI.Outputs())
59-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"4_PORTABLE_PUBLIC_IP_ADDRESSES"}))
60+
Expect(fakeUI.Outputs()).To(ContainSubstring("4_PORTABLE_PUBLIC_IP_ADDRESSES"))
6061
})
61-
62-
It("return no error with more of three arguments", func() {
63-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC", "--complex-type", k, "--billing=hourly", "--verify")
62+
It("Verify Basic Order Happy Path --output=json", func() {
63+
err := testhelpers.RunCobraCommand(
64+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC",
65+
"--complex-type", k, "--billing=monthly", "--verify", "--output=json")
6466
Expect(err).NotTo(HaveOccurred())
65-
fmt.Println(fakeUI.Outputs())
66-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"4_PORTABLE_PUBLIC_IP_ADDRESSES"}))
67+
Expect(fakeUI.Outputs()).To(ContainSubstring("4_PORTABLE_PUBLIC_IP_ADDRESSES"))
6768
})
6869

69-
})
70-
}
71-
72-
for k, _ := range order.TYPEMAP {
73-
Context("successfully "+k, func() {
74-
75-
k := k
76-
It("return in json format with three arguments", func() {
77-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC", "--complex-type", k, "--billing=monthly", "--verify", "--output=json")
70+
It("Verify 2 Item Order Happy Path", func() {
71+
err := testhelpers.RunCobraCommand(
72+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC",
73+
"--complex-type", k, "--billing=hourly", "--verify")
7874
Expect(err).NotTo(HaveOccurred())
7975
Expect(fakeUI.Outputs()).To(ContainSubstring("4_PORTABLE_PUBLIC_IP_ADDRESSES"))
8076
})
81-
82-
It("return in json format with more of three arguments", func() {
83-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC", "--complex-type", k, "--billing=monthly", "--verify", "--output=json")
77+
It("Verify 2 Item Order Happy Path --output=json", func() {
78+
err := testhelpers.RunCobraCommand(
79+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC",
80+
"--complex-type", k, "--billing=monthly", "--verify", "--output=json")
8481
Expect(err).NotTo(HaveOccurred())
8582
Expect(fakeUI.Outputs()).To(ContainSubstring("4_PORTABLE_PUBLIC_IP_ADDRESSES"))
8683
})
84+
It("Place Basic Order Happy Path", func() {
85+
err := testhelpers.RunCobraCommand(
86+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC",
87+
"--complex-type", k, "-f")
88+
Expect(err).NotTo(HaveOccurred())
89+
Expect(fakeUI.Outputs()).To(ContainSubstring("11493593"))
90+
})
91+
92+
It("Place Basic Order Happy Path 2 Items", func() {
93+
err := testhelpers.RunCobraCommand(
94+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC",
95+
"--complex-type", k, "-f")
96+
Expect(err).NotTo(HaveOccurred())
97+
Expect(fakeUI.Outputs()).To(ContainSubstring("11493593"))
98+
})
8799

88100
})
89101
}
90102

91-
Context("Return error", func() {
92-
BeforeEach(func() {
93-
fakeOrderManager.VerifyPlaceOrderReturns(datatypes.Container_Product_Order{}, errors.New("This command requires three arguments."))
94-
})
103+
Context("Handle CLI Errors", func() {
95104
It("Arguments is not set", func() {
96105
err := testhelpers.RunCobraCommand(cliCommand.Command, "--verify")
97106
Expect(err).To(HaveOccurred())
98107
Expect(err.Error()).To(ContainSubstring("requires at least 3 arg(s), only received 0"))
99108
})
100-
})
101-
102-
Context("Return error", func() {
103-
BeforeEach(func() {
104-
fakeOrderManager.VerifyPlaceOrderReturns(datatypes.Container_Product_Order{}, errors.New("--billing can only be either hourly or monthly."))
105-
})
106109
It("Billing flag is set with an invalid value with three arguments", func() {
107-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC", "--verify", "--billing=invalid")
110+
err := testhelpers.RunCobraCommand(
111+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC",
112+
"--verify", "--billing=invalid")
108113
Expect(err).To(HaveOccurred())
109114
Expect(err.Error()).To(ContainSubstring("--billing can only be either hourly or monthly."))
110115
})
111-
112116
It("Billing flag is set with an invalid value with more of three arguments", func() {
113-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC", "--verify", "--billing=invalid")
117+
err := testhelpers.RunCobraCommand(
118+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC",
119+
"--verify", "--billing=invalid")
114120
Expect(err).To(HaveOccurred())
115121
Expect(err.Error()).To(ContainSubstring("--billing can only be either hourly or monthly."))
116122
})
117-
})
118-
119-
Context("Return error", func() {
120-
BeforeEach(func() {
121-
fakeOrderManager.VerifyPlaceOrderReturns(datatypes.Container_Product_Order{}, errors.New("Incorrect complex type"))
122-
})
123123
It("Complex type is set with an invalid value with three arguments", func() {
124-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC", "--verify", "--complex-type=invalid")
124+
err := testhelpers.RunCobraCommand(
125+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC",
126+
"--verify", "--complex-type=invalid")
125127
Expect(err).To(HaveOccurred())
126128
Expect(err.Error()).To(ContainSubstring("Incorrect complex type"))
127129
})
128130

129131
It("Complex type is set with an invalid value with more of three arguments", func() {
130-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC", "--verify", "--complex-type=invalid")
132+
err := testhelpers.RunCobraCommand(
133+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC",
134+
"--verify", "--complex-type=invalid")
131135
Expect(err).To(HaveOccurred())
132136
Expect(err.Error()).To(ContainSubstring("Incorrect complex type"))
133137
})
134-
})
135-
136-
Context("Return error", func() {
137-
BeforeEach(func() {
138-
fakeOrderManager.VerifyPlaceOrderReturns(datatypes.Container_Product_Order{}, errors.New("failed reading file"))
139-
})
140138
It("Extras is set with an invalid file with three arguments", func() {
141-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC", "--verify", "--extras=@invalid", "--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest")
139+
err := testhelpers.RunCobraCommand(
140+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC", "--verify",
141+
"--extras=@invalid", "--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest")
142142
Expect(err).To(HaveOccurred())
143143
Expect(err.Error()).To(ContainSubstring("failed reading file"))
144144
})
145145

146146
It("Extras is set with an invalid file with more of three arguments", func() {
147-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC", "--verify", "--extras=@invalid", "--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest")
147+
err := testhelpers.RunCobraCommand(
148+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC", "--verify",
149+
"--extras=@invalid", "--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest")
148150
Expect(err).To(HaveOccurred())
149151
Expect(err.Error()).To(ContainSubstring("failed reading file"))
150152
})
151-
})
152-
153-
Context("Return error", func() {
154-
BeforeEach(func() {
155-
fakeOrderManager.VerifyPlaceOrderReturns(datatypes.Container_Product_Order{}, errors.New("Unable to unmarshal extras json:"))
156-
})
157153
It("Extras is set with an invalid value with arguments", func() {
158-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC", "--verify", "--extras=invalid", "--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest")
154+
err := testhelpers.RunCobraCommand(
155+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC", "--verify",
156+
"--extras=invalid", "--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest")
159157
Expect(err).To(HaveOccurred())
160158
Expect(err.Error()).To(ContainSubstring("Unable to unmarshal extras json:"))
161159
})
162-
163160
It("Extras is set with an invalid value with more of three arguments", func() {
164-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC", "--verify", "--extras=invalid", "--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest")
161+
err := testhelpers.RunCobraCommand(
162+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC", "--verify",
163+
"--extras=invalid", "--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest")
165164
Expect(err).To(HaveOccurred())
166165
Expect(err.Error()).To(ContainSubstring("Unable to unmarshal extras json:"))
167166
})
168-
})
169-
170-
Context("Return error", func() {
171-
BeforeEach(func() {
172-
fakeOrderManager.VerifyPlaceOrderReturns(datatypes.Container_Product_Order{}, errors.New("Invalid output format, only JSON is supported now."))
173-
})
174167
It("Invalid output is set with three arguments", func() {
175-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC", "--verify", "--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest", "--output=xml")
168+
err := testhelpers.RunCobraCommand(
169+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC", "--verify",
170+
"--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest", "--output=xml")
176171
Expect(err).To(HaveOccurred())
177172
Expect(err.Error()).To(ContainSubstring("Invalid output format, only JSON is supported now."))
178173
})
179174

180175
It("Invalid output is set with more of three arguments", func() {
181-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC", "--verify", "--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest", "--output=xml")
176+
err := testhelpers.RunCobraCommand(
177+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC", "--verify",
178+
"--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest", "--output=xml")
182179
Expect(err).To(HaveOccurred())
183180
Expect(err.Error()).To(ContainSubstring("Invalid output format, only JSON is supported now."))
184181
})
185182
})
186-
})
187-
188-
Describe("order create", func() {
189-
for k, _ := range order.TYPEMAP {
190-
Context("successfully"+k, func() {
191-
192-
k := k
193-
It("return no error with three arguments", func() {
194-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC", "--complex-type", k, "-f")
195-
Expect(err).NotTo(HaveOccurred())
196-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"11493593"}))
197-
})
198-
199-
It("return no error with more of three arguments", func() {
200-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC", "--complex-type", k, "-f")
201-
Expect(err).NotTo(HaveOccurred())
202-
Expect(fakeUI.Outputs()).To(ContainSubstrings([]string{"11493593"}))
203-
})
204-
205-
})
206-
}
183+
Context("Handle User Input", func() {
207184

208-
for k, _ := range order.TYPEMAP {
209-
Context("successfully "+k, func() {
210-
211-
k := k
212-
It("return in json format with three arguments", func() {
213-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13,EVAULT_100_GB", "CITRIX_VDC", "--complex-type", k, "-f", "--output=json")
214-
Expect(err).NotTo(HaveOccurred())
215-
Expect(fakeUI.Outputs()).To(ContainSubstring("11493593"))
216-
})
217-
218-
It("return in json format with more of three arguments", func() {
219-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC", "--complex-type", k, "-f", "--output=json")
220-
Expect(err).NotTo(HaveOccurred())
221-
Expect(fakeUI.Outputs()).To(ContainSubstring("11493593"))
222-
})
223-
224-
})
225-
}
226-
227-
Context("Return No error", func() {
228-
BeforeEach(func() {
229-
fakeUI.Inputs("No")
230-
})
231185
It("Aborted place order with three arguments", func() {
232-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC", "--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest")
186+
fakeUI.Inputs("No")
187+
err := testhelpers.RunCobraCommand(
188+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC",
189+
"--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest")
233190
Expect(err).NotTo(HaveOccurred())
234191
Expect(fakeUI.Outputs()).To(ContainSubstring("This action will incur charges on your account. Continue?"))
235192
Expect(fakeUI.Outputs()).To(ContainSubstring("Aborted."))
236193
})
237194

238195
It("Aborted place order with more of three arguments", func() {
239-
err := testhelpers.RunCobraCommand(cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC", "--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest")
196+
fakeUI.Inputs("No")
197+
err := testhelpers.RunCobraCommand(
198+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB", "CITRIX_VDC",
199+
"--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest")
240200
Expect(err).NotTo(HaveOccurred())
241201
Expect(fakeUI.Outputs()).To(ContainSubstring("This action will incur charges on your account. Continue?"))
242202
Expect(fakeUI.Outputs()).To(ContainSubstring("Aborted."))
243203
})
204+
It("Accepted Order", func() {
205+
fakeUI.Inputs("Yes")
206+
err := testhelpers.RunCobraCommand(
207+
cliCommand.Command, "CLOUD_SERVER", "dal13", "EVAULT_100_GB,CITRIX_VDC",
208+
"--complex-type=SoftLayer_Container_Product_Order_Virtual_Guest")
209+
Expect(err).NotTo(HaveOccurred())
210+
Expect(fakeUI.Outputs()).To(ContainSubstring("This action will incur charges on your account. Continue?"))
211+
Expect(fakeUI.Outputs()).To(ContainSubstring("11493593"))
212+
})
244213
})
245214
})
215+
246216
Describe("softlayer-cli/issues/863", func() {
247217
BeforeEach(func() {
248218
fakeHandler.ClearApiCallLogs()
@@ -267,7 +237,7 @@ var _ = Describe("Place", func() {
267237
// fmt.Printf(callLog[8].String())
268238
Expect(callLog[8].String()).To(ContainSubstring(
269239
`"prices":[{"id":899},{"id":21},{"id":204637},` +
270-
`{"id":314142},{"id":55},{"id":57},{"id":58},{"id":420},{"id":905},{"id":22505}]`,
240+
`{"id":314142},{"id":55},{"id":57},{"id":58},{"id":420},{"id":905},{"id":22505}]`,
271241
))
272242
})
273243
})

0 commit comments

Comments
 (0)