|
| 1 | +package hosts |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + serverscom "github.com/serverscom/serverscom-go-client/pkg" |
| 10 | + "github.com/serverscom/srvctl/cmd/testutils" |
| 11 | + "github.com/serverscom/srvctl/internal/mocks" |
| 12 | + "go.uber.org/mock/gomock" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + featuresFixtureBasePath = filepath.Join("..", "..", "..", "testdata", "entities", "hosts", "features") |
| 17 | +) |
| 18 | + |
| 19 | +func TestListDSFeaturesCmd(t *testing.T) { |
| 20 | + testFeature1 := serverscom.DedicatedServerFeature{ |
| 21 | + Name: "disaggregated_public_ports", |
| 22 | + Status: "deactivated", |
| 23 | + } |
| 24 | + testFeature2 := serverscom.DedicatedServerFeature{ |
| 25 | + Name: "no_public_network", |
| 26 | + Status: "unavailable", |
| 27 | + } |
| 28 | + |
| 29 | + testCases := []struct { |
| 30 | + name string |
| 31 | + output string |
| 32 | + args []string |
| 33 | + expectedOutput []byte |
| 34 | + expectError bool |
| 35 | + configureMock func(*mocks.MockCollection[serverscom.DedicatedServerFeature]) |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "list all ds features", |
| 39 | + output: "json", |
| 40 | + args: []string{"-A", testServerID}, |
| 41 | + expectedOutput: testutils.ReadFixture(filepath.Join(featuresFixtureBasePath, "list_all.json")), |
| 42 | + configureMock: func(mock *mocks.MockCollection[serverscom.DedicatedServerFeature]) { |
| 43 | + mock.EXPECT(). |
| 44 | + Collect(gomock.Any()). |
| 45 | + Return([]serverscom.DedicatedServerFeature{ |
| 46 | + testFeature1, |
| 47 | + testFeature2, |
| 48 | + }, nil) |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "list ds features", |
| 53 | + output: "json", |
| 54 | + args: []string{testServerID}, |
| 55 | + expectedOutput: testutils.ReadFixture(filepath.Join(featuresFixtureBasePath, "list.json")), |
| 56 | + configureMock: func(mock *mocks.MockCollection[serverscom.DedicatedServerFeature]) { |
| 57 | + mock.EXPECT(). |
| 58 | + List(gomock.Any()). |
| 59 | + Return([]serverscom.DedicatedServerFeature{ |
| 60 | + testFeature1, |
| 61 | + }, nil) |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "list ds features with template", |
| 66 | + args: []string{testServerID, "--template", "{{range .}}Name: {{.Name}} Status: {{.Status}}\n{{end}}"}, |
| 67 | + expectedOutput: testutils.ReadFixture(filepath.Join(featuresFixtureBasePath, "list_template.txt")), |
| 68 | + configureMock: func(mock *mocks.MockCollection[serverscom.DedicatedServerFeature]) { |
| 69 | + mock.EXPECT(). |
| 70 | + List(gomock.Any()). |
| 71 | + Return([]serverscom.DedicatedServerFeature{ |
| 72 | + testFeature1, |
| 73 | + testFeature2, |
| 74 | + }, nil) |
| 75 | + }, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "list ds features with page-view", |
| 79 | + args: []string{testServerID, "--page-view"}, |
| 80 | + expectedOutput: testutils.ReadFixture(filepath.Join(featuresFixtureBasePath, "list_pageview.txt")), |
| 81 | + configureMock: func(mock *mocks.MockCollection[serverscom.DedicatedServerFeature]) { |
| 82 | + mock.EXPECT(). |
| 83 | + List(gomock.Any()). |
| 84 | + Return([]serverscom.DedicatedServerFeature{ |
| 85 | + testFeature1, |
| 86 | + testFeature2, |
| 87 | + }, nil) |
| 88 | + }, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "list ds features with error", |
| 92 | + args: []string{testServerID}, |
| 93 | + expectError: true, |
| 94 | + configureMock: func(mock *mocks.MockCollection[serverscom.DedicatedServerFeature]) { |
| 95 | + mock.EXPECT(). |
| 96 | + List(gomock.Any()). |
| 97 | + Return(nil, errors.New("some error")) |
| 98 | + }, |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + mockCtrl := gomock.NewController(t) |
| 103 | + defer mockCtrl.Finish() |
| 104 | + hostService := mocks.NewMockHostsService(mockCtrl) |
| 105 | + collection := mocks.NewMockCollection[serverscom.DedicatedServerFeature](mockCtrl) |
| 106 | + |
| 107 | + hostService.EXPECT().DedicatedServerFeatures(gomock.Any()).Return(collection).AnyTimes() |
| 108 | + collection.EXPECT().SetParam(gomock.Any(), gomock.Any()).Return(collection).AnyTimes() |
| 109 | + |
| 110 | + scClient := serverscom.NewClientWithEndpoint("", "") |
| 111 | + scClient.Hosts = hostService |
| 112 | + |
| 113 | + for _, tc := range testCases { |
| 114 | + t.Run(tc.name, func(t *testing.T) { |
| 115 | + g := NewWithT(t) |
| 116 | + if tc.configureMock != nil { |
| 117 | + tc.configureMock(collection) |
| 118 | + } |
| 119 | + testCmdContext := testutils.NewTestCmdContext(scClient) |
| 120 | + cmd := NewCmd(testCmdContext) |
| 121 | + |
| 122 | + args := append([]string{"hosts", "ds", "list-features"}, tc.args...) |
| 123 | + if tc.output != "" { |
| 124 | + args = append(args, "--output", tc.output) |
| 125 | + } |
| 126 | + builder := testutils.NewTestCommandBuilder(). |
| 127 | + WithCommand(cmd). |
| 128 | + WithArgs(args) |
| 129 | + |
| 130 | + c := builder.Build() |
| 131 | + err := c.Execute() |
| 132 | + if tc.expectError { |
| 133 | + g.Expect(err).To(HaveOccurred()) |
| 134 | + } else { |
| 135 | + g.Expect(err).To(BeNil()) |
| 136 | + g.Expect(builder.GetOutput()).To(BeEquivalentTo(string(tc.expectedOutput))) |
| 137 | + } |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
0 commit comments