|
| 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 | +}) |
0 commit comments