Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cmd/entities/hosts/features.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package hosts

import (
"log"

serverscom "github.com/serverscom/serverscom-go-client/pkg"
"github.com/serverscom/srvctl/cmd/base"
"github.com/spf13/cobra"
)

func newListDSFeaturesCmd(cmdContext *base.CmdContext) *cobra.Command {
factory := func(verbose bool, args ...string) serverscom.Collection[serverscom.DedicatedServerFeature] {
scClient := cmdContext.GetClient().SetVerbose(verbose).GetScClient()
if len(args) == 0 {
log.Fatal("Missing dedicated server ID")
}
id := args[0]
return scClient.Hosts.DedicatedServerFeatures(id)
}

opts := &base.BaseListOptions[serverscom.DedicatedServerFeature]{}

return base.NewListCmd("list-features <id>", "Dedicated server features", factory, cmdContext, opts)
}
140 changes: 140 additions & 0 deletions cmd/entities/hosts/features_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package hosts

import (
"errors"
"path/filepath"
"testing"

. "github.com/onsi/gomega"
serverscom "github.com/serverscom/serverscom-go-client/pkg"
"github.com/serverscom/srvctl/cmd/testutils"
"github.com/serverscom/srvctl/internal/mocks"
"go.uber.org/mock/gomock"
)

var (
featuresFixtureBasePath = filepath.Join("..", "..", "..", "testdata", "entities", "hosts", "features")
)

func TestListDSFeaturesCmd(t *testing.T) {
testFeature1 := serverscom.DedicatedServerFeature{
Name: "disaggregated_public_ports",
Status: "deactivated",
}
testFeature2 := serverscom.DedicatedServerFeature{
Name: "no_public_network",
Status: "unavailable",
}

testCases := []struct {
name string
output string
args []string
expectedOutput []byte
expectError bool
configureMock func(*mocks.MockCollection[serverscom.DedicatedServerFeature])
}{
{
name: "list all ds features",
output: "json",
args: []string{"-A", testServerID},
expectedOutput: testutils.ReadFixture(filepath.Join(featuresFixtureBasePath, "list_all.json")),
configureMock: func(mock *mocks.MockCollection[serverscom.DedicatedServerFeature]) {
mock.EXPECT().
Collect(gomock.Any()).
Return([]serverscom.DedicatedServerFeature{
testFeature1,
testFeature2,
}, nil)
},
},
{
name: "list ds features",
output: "json",
args: []string{testServerID},
expectedOutput: testutils.ReadFixture(filepath.Join(featuresFixtureBasePath, "list.json")),
configureMock: func(mock *mocks.MockCollection[serverscom.DedicatedServerFeature]) {
mock.EXPECT().
List(gomock.Any()).
Return([]serverscom.DedicatedServerFeature{
testFeature1,
}, nil)
},
},
{
name: "list ds features with template",
args: []string{testServerID, "--template", "{{range .}}Name: {{.Name}} Status: {{.Status}}\n{{end}}"},
expectedOutput: testutils.ReadFixture(filepath.Join(featuresFixtureBasePath, "list_template.txt")),
configureMock: func(mock *mocks.MockCollection[serverscom.DedicatedServerFeature]) {
mock.EXPECT().
List(gomock.Any()).
Return([]serverscom.DedicatedServerFeature{
testFeature1,
testFeature2,
}, nil)
},
},
{
name: "list ds features with page-view",
args: []string{testServerID, "--page-view"},
expectedOutput: testutils.ReadFixture(filepath.Join(featuresFixtureBasePath, "list_pageview.txt")),
configureMock: func(mock *mocks.MockCollection[serverscom.DedicatedServerFeature]) {
mock.EXPECT().
List(gomock.Any()).
Return([]serverscom.DedicatedServerFeature{
testFeature1,
testFeature2,
}, nil)
},
},
{
name: "list ds features with error",
args: []string{testServerID},
expectError: true,
configureMock: func(mock *mocks.MockCollection[serverscom.DedicatedServerFeature]) {
mock.EXPECT().
List(gomock.Any()).
Return(nil, errors.New("some error"))
},
},
}

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
hostService := mocks.NewMockHostsService(mockCtrl)
collection := mocks.NewMockCollection[serverscom.DedicatedServerFeature](mockCtrl)

hostService.EXPECT().DedicatedServerFeatures(gomock.Any()).Return(collection).AnyTimes()
collection.EXPECT().SetParam(gomock.Any(), gomock.Any()).Return(collection).AnyTimes()

scClient := serverscom.NewClientWithEndpoint("", "")
scClient.Hosts = hostService

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
if tc.configureMock != nil {
tc.configureMock(collection)
}
testCmdContext := testutils.NewTestCmdContext(scClient)
cmd := NewCmd(testCmdContext)

args := append([]string{"hosts", "ds", "list-features"}, tc.args...)
if tc.output != "" {
args = append(args, "--output", tc.output)
}
builder := testutils.NewTestCommandBuilder().
WithCommand(cmd).
WithArgs(args)

c := builder.Build()
err := c.Execute()
if tc.expectError {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).To(BeNil())
g.Expect(builder.GetOutput()).To(BeEquivalentTo(string(tc.expectedOutput)))
}
})
}
}
6 changes: 6 additions & 0 deletions cmd/entities/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func NewCmd(cmdContext *base.CmdContext) *cobra.Command {
newAddDSNetworkCmd,
newDeleteDSNetworkCmd,
newListDSCmd,
newListDSServicesCmd,
newListDSFeaturesCmd,
newGetDSOOBCredsCmd,
},
},
{
Expand Down Expand Up @@ -90,6 +93,9 @@ func NewCmd(cmdContext *base.CmdContext) *cobra.Command {
newUpdateSBMCmd,
newSBMReleaseCmd,
newListSBMCmd,
newListSBMPTRCmd,
newCreateSBMPTRCmd,
newDeleteSBMPTRCmd,
},
},
}
Expand Down
Loading
Loading