Skip to content

Commit df517d1

Browse files
Added tests for search feature
1 parent 21220b2 commit df517d1

13 files changed

Lines changed: 556 additions & 7 deletions

File tree

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,22 @@ Check testhelpers/fake_softlayer_session.go for all the fields that get recorded
159159

160160
### Fake Managers
161161

162-
CLI calls to manager functions need an entry in `bluemix-cli\bluemix\slplugin\testhelpers\fake_manager.go`
162+
CLI calls to manager functions need an entry in `plugin\testhelpers\fake_manager.go`
163+
Managers have a fake/test interface that is autogenerate with a program called [couterfieter](https://github.com/maxbrunsfeld/counterfeiter)
163164

165+
```
166+
go generate ./...
167+
```
164168

165-
Managers have a fake/test interface that is autogenerate with a program called [couterfieter](https://github.com/maxbrunsfeld/counterfeiter)
169+
170+
each manager and defined interface should have this line in it to be automatically generated. After the imports, before any interfaces
166171

167172
```
168-
# From /github.ibm.com/SoftLayer/softlayer-cli
169-
cd plugin/managers
170-
counterfeiter.exe -o ../testhelpers/fake_storage_manager.go . StorageManager
173+
//counterfeiter:generate -o ../testhelpers/ . <Whatever>Manager
171174
```
172175

176+
177+
173178
If you want to use the real manager but fixture API data, just initialize the manager like this in the CLI test
174179

175180
(filenames here is optional of course)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ require (
3434
github.com/mattn/go-colorable v0.1.8 // indirect
3535
github.com/mattn/go-isatty v0.0.12 // indirect
3636
github.com/mattn/go-runewidth v0.0.12 // indirect
37+
github.com/maxbrunsfeld/counterfeiter/v6 v6.8.1 // indirect
3738
github.com/nicksnyder/go-i18n/v2 v2.2.0 // indirect
3839
github.com/pelletier/go-toml v1.2.0 // indirect
3940
github.com/pmezard/go-difflib v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX
4848
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
4949
github.com/mattn/go-runewidth v0.0.12 h1:Y41i/hVW3Pgwr8gV+J23B9YEY0zxjptBuCWEaxmAOow=
5050
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
51+
github.com/maxbrunsfeld/counterfeiter/v6 v6.8.1 h1:NicmruxkeqHjDv03SfSxqmaLuisddudfP3h5wdXFbhM=
52+
github.com/maxbrunsfeld/counterfeiter/v6 v6.8.1/go.mod h1:eyp4DdUJAKkr9tvxR3jWhw2mDK7CWABMG5r9uyaKC7I=
5153
github.com/miekg/dns v1.1.50 h1:DQUfb9uc6smULcREF09Uc+/Gd46YWqJd5DbpPE9xkcA=
5254
github.com/miekg/dns v1.1.50/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
5355
github.com/nicksnyder/go-i18n v1.10.1 h1:isfg77E/aCD7+0lD/D00ebR2MV5vgeQ276WYyDaCRQc=

plugin/commands/search/search.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func parseVirtual_Guest(resource datatypes.Virtual_Guest) string {
105105
return fmt.Sprintf(T("ID") + ": %d\n" + T("FQDN") + ": %s\n", *resource.Id, *resource.FullyQualifiedDomainName)
106106
}
107107
func parseEvent_Log(resource datatypes.Event_Log) string {
108-
return fmt.Sprintf(T("ID") + ": %d\n" + T("Event") + ": %s\n", *resource.TraceId, *resource.EventName)
108+
return fmt.Sprintf(T("ID") + ": %s\n" + T("Event") + ": %s\n", *resource.TraceId, *resource.EventName)
109109
}
110110
func parseVirtual_DedicatedHost(resource datatypes.Virtual_DedicatedHost) string {
111111
return fmt.Sprintf(T("ID") + ": %d\n" + T("Name") + ": %s\n", *resource.Id, *resource.Name)
@@ -120,7 +120,7 @@ func parseNetwork_Subnet_IpAddress(resource datatypes.Network_Subnet_IpAddress)
120120
return fmt.Sprintf(T("ID") + ": %d\n" + T("Ip Address") + ": %s\n", *resource.Id, *resource.IpAddress)
121121
}
122122
func parseNetwork_Vlan(resource datatypes.Network_Vlan) string {
123-
return fmt.Sprintf(T("ID") + ": %d\n" + T("VLAN") + ": %s\n", *resource.Id, *resource.VlanNumber)
123+
return fmt.Sprintf(T("ID") + ": %d\n" + T("VLAN") + ": %d\n", *resource.Id, *resource.VlanNumber)
124124
}
125125
func parseNetwork_Vlan_Firewall(resource datatypes.Network_Vlan_Firewall) string {
126126
return fmt.Sprintf(T("ID") + ": %d\n" + T("Ip Address") + ": %s\n", *resource.Id, *resource.PrimaryIpAddress)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package search_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
9+
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
10+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/search"
11+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
12+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
13+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/utils"
14+
)
15+
16+
func TestManagers(t *testing.T) {
17+
RegisterFailHandler(Fail)
18+
RunSpecs(t, "SEarch Suite")
19+
}
20+
21+
var availableCommands = []string{
22+
"types",
23+
}
24+
25+
// This test suite exists to make sure commands don't get accidently removed from the actionBindings
26+
var _ = Describe("Test search commands", func() {
27+
fakeUI := terminal.NewFakeUI()
28+
fakeSession := testhelpers.NewFakeSoftlayerSession([]string{"advancedSearch-allTypes"})
29+
slMeta := metadata.NewSoftlayerCommand(fakeUI, fakeSession)
30+
cliCommand := search.SetupCobraCommands(slMeta)
31+
cliCommand.PersistentFlags().Var(slMeta.OutputFlag, "output", "--output=JSON for json output.")
32+
// fakeSearchManager := new(testhelpers.FakeSearchManager)
33+
34+
Context("New commands testable", func() {
35+
commands := search.SetupCobraCommands(slMeta)
36+
37+
var arrayCommands = []string{}
38+
for _, command := range commands.Commands() {
39+
commandName := command.Name()
40+
arrayCommands = append(arrayCommands, commandName)
41+
It("available commands "+commands.Name(), func() {
42+
available := false
43+
if utils.StringInSlice(commandName, availableCommands) != -1 {
44+
available = true
45+
}
46+
Expect(available).To(BeTrue(), commandName+" not found in array available Commands")
47+
})
48+
}
49+
for _, command := range availableCommands {
50+
commandName := command
51+
It("ibmcloud sl "+commands.Name(), func() {
52+
available := false
53+
if utils.StringInSlice(commandName, arrayCommands) != -1 {
54+
available = true
55+
}
56+
Expect(available).To(BeTrue(), commandName + " not found in ibmcloud sl " + commands.Name())
57+
})
58+
}
59+
})
60+
61+
Context("Seach Namespace", func() {
62+
It("Search Name Space", func() {
63+
Expect(search.SearchNamespace().ParentName).To(ContainSubstring("sl"))
64+
Expect(search.SearchNamespace().Name).To(ContainSubstring("search"))
65+
Expect(search.SearchNamespace().Description).To(ContainSubstring("Perform a query against the SoftLayer search database."))
66+
})
67+
})
68+
69+
var fakeHandler *testhelpers.FakeTransportHandler
70+
BeforeEach(func() {
71+
fakeHandler = testhelpers.GetSessionHandler(fakeSession)
72+
})
73+
// Search command is a bit special is an actual command, not a command group like most others.
74+
Context("Search Command tests", func() {
75+
It("Basic Search Command", func() {
76+
err := testhelpers.RunCobraCommand(cliCommand)
77+
Expect(err).NotTo(HaveOccurred())
78+
})
79+
It("Basic Search API Error", func() {
80+
fakeHandler.AddApiError("SoftLayer_Search", "advancedSearch", 500, "Search Error")
81+
err := testhelpers.RunCobraCommand(cliCommand)
82+
Expect(err).To(HaveOccurred())
83+
Expect(err.Error()).To(ContainSubstring("Search Error: Search Error (HTTP 500)"))
84+
})
85+
It("Basic Search Command with query", func() {
86+
err := testhelpers.RunCobraCommand(cliCommand , "-q", `"_objectTpye:SoftLayer_Virtual_Guest test.com`)
87+
Expect(err).NotTo(HaveOccurred())
88+
Expect(fakeUI.Outputs()).To(ContainSubstring("SoftLayer_Network_Vlan"))
89+
Expect(fakeUI.Outputs()).To(ContainSubstring("VLAN |match| ID: 675037"))
90+
})
91+
It("Basic Search Command with JSON output", func() {
92+
err := testhelpers.RunCobraCommand(cliCommand, "--output=JSON", "-q", `"test.com"`)
93+
Expect(err).NotTo(HaveOccurred())
94+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"resourceType": "SoftLayer_Ticket"`))
95+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"id": 85346218,`))
96+
})
97+
})
98+
AfterEach(func() {
99+
fakeHandler.ClearApiCallLogs()
100+
fakeHandler.ClearErrors()
101+
})
102+
})
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package search_test
2+
3+
import (
4+
5+
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
9+
"github.com/softlayer/softlayer-go/session"
10+
11+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/search"
12+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
13+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
14+
)
15+
16+
var _ = Describe("search types", func() {
17+
var (
18+
fakeUI *terminal.FakeUI
19+
cliCommand *search.SearchTypesCommand
20+
fakeSession *session.Session
21+
slCommand *metadata.SoftlayerCommand
22+
fakeHandler *testhelpers.FakeTransportHandler
23+
// fakeSearchManager *testhelpers.FakeSearchManager
24+
)
25+
BeforeEach(func() {
26+
fakeUI = terminal.NewFakeUI()
27+
fakeSession = testhelpers.NewFakeSoftlayerSession(nil)
28+
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
29+
cliCommand = search.NewSearchTypesCommand(slCommand)
30+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
31+
// fakeSearchManager = new(testhelpers.FakeSearchManager)
32+
// cliCommand.SearchManager = fakeSearchManager
33+
fakeHandler = testhelpers.GetSessionHandler(fakeSession)
34+
})
35+
36+
Describe("search types tests", func() {
37+
Context("Basic usage", func() {
38+
It("sl search types", func() {
39+
err := testhelpers.RunCobraCommand(cliCommand.Command)
40+
Expect(err).NotTo(HaveOccurred())
41+
Expect(fakeUI.Outputs()).To(ContainSubstring("SoftLayer_Network_Vlan"))
42+
Expect(fakeUI.Outputs()).To(ContainSubstring("accountId true integer"))
43+
})
44+
It("sl search types JSON", func() {
45+
err := testhelpers.RunCobraCommand(cliCommand.Command, "--output=JSON")
46+
Expect(err).NotTo(HaveOccurred())
47+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"name": "SoftLayer_Hardware"`))
48+
Expect(fakeUI.Outputs()).To(ContainSubstring(`"name": "accountId",`))
49+
})
50+
It("API errors", func() {
51+
fakeHandler.AddApiError("SoftLayer_Search", "getObjectTypes", 500, "Search Error")
52+
err := testhelpers.RunCobraCommand(cliCommand.Command)
53+
Expect(err).To(HaveOccurred())
54+
Expect(err.Error()).To(ContainSubstring("Search Error: Search Error (HTTP 500)"))
55+
})
56+
})
57+
})
58+
AfterEach(func() {
59+
fakeHandler.ClearApiCallLogs()
60+
fakeHandler.ClearErrors()
61+
})
62+
})

plugin/managers/managers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package managers
2+
3+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate

plugin/managers/search.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/softlayer/softlayer-go/session"
77
)
88

9+
//counterfeiter:generate -o ../testhelpers/ . SearchManager
910
type SearchManager interface {
1011
AdvancedSearch(mask string, params string) ([]datatypes.Container_Search_Result, error)
1112
GetTypes() ([]datatypes.Container_Search_ObjectType, error)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
[
2+
{
3+
"matchedTerms": ["prwetwasdvxvvb"],
4+
"relevanceScore": "5.4415264",
5+
"resource": {
6+
"id": 999,
7+
"fullyQualifiedDomainName": "mex01.fcr01.858"
8+
},
9+
"resourceType": "SoftLayer_Virtual_Guest"
10+
},
11+
{
12+
"matchedTerms": ["prsdfsdfsdf sdf "],
13+
"relevanceScore": "5.4415264",
14+
"resource": {
15+
"EventName": "Test Name",
16+
"traceId": "aaabbb-999-000"
17+
},
18+
"resourceType": "SoftLayer_Event_Log"
19+
},
20+
{
21+
"matchedTerms": ["xxxxxxxxxx"],
22+
"relevanceScore": "5.4415264",
23+
"resource": {
24+
"Name": "mex01.fcr01.858",
25+
"Id": 858
26+
},
27+
"resourceType": "SoftLayer_Virtual_DedicatedHost"
28+
},
29+
{
30+
"matchedTerms": ["dfg dfg |"],
31+
"relevanceScore": "5.4415264",
32+
"resource": {
33+
"fullyQualifiedDomainName": "a.b.com",
34+
"Id": 1122334455
35+
},
36+
"resourceType": "SoftLayer_Hardware"
37+
},
38+
{
39+
"matchedTerms": ["asdasd"],
40+
"relevanceScore": "5.4415264",
41+
"resource": {
42+
"Name": "sdfsdfmex01.fcr01.858",
43+
"Id": 851238
44+
},
45+
"resourceType": "SoftLayer_Network_Application_Delivery_Controller"
46+
},
47+
{
48+
"matchedTerms": ["IP |123.2.2.2|"],
49+
"relevanceScore": "5.4415264",
50+
"resource": {
51+
"IpAddress": "1.2.3.4",
52+
"Id": 123125346858
53+
},
54+
"resourceType": "SoftLayer_Network_Subnet_IpAddress"
55+
},
56+
{
57+
"matchedTerms": ["VLAN |match|"],
58+
"relevanceScore": "5.4415264",
59+
"resource": {
60+
"fullyQualifiedName": "mex01.fcr01.858",
61+
"vlanNumber": 858,
62+
"Id": 675037
63+
},
64+
"resourceType": "SoftLayer_Network_Vlan"
65+
},
66+
{
67+
"matchedTerms": ["VLAN_|Firewall|"],
68+
"relevanceScore": "5.4415264",
69+
"resource": {
70+
"primaryIpAddress": "2.3.4.5",
71+
"Id": 83468258
72+
},
73+
"resourceType": "SoftLayer_Network_Vlan_Firewall"
74+
},
75+
{
76+
"matchedTerms": ["A test |ticket|"],
77+
"relevanceScore": "5.4415264",
78+
"resource": {
79+
"Title": "Test Ticket",
80+
"Id": 85346218
81+
},
82+
"resourceType": "SoftLayer_Ticket"
83+
}
84+
]

0 commit comments

Comments
 (0)