Skip to content

Commit

Permalink
feat(managerclient): render labels in ClusterSlice
Browse files Browse the repository at this point in the history
So that labels are visible in the output of 'sctool cluster list'.
  • Loading branch information
Michal-Leszczynski committed Jul 23, 2024
1 parent 979475b commit 8c115cb
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
95 changes: 95 additions & 0 deletions pkg/command/cluster/clusterlist/cmd_integration_api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (C) 2024 ScyllaDB

//go:build all || api_integration
// +build all api_integration

package clusterlist

import (
"bytes"
"context"
"os/exec"
"regexp"
"strings"
"testing"

"github.com/scylladb/scylla-manager/v3/pkg/managerclient"
"github.com/scylladb/scylla-manager/v3/swagger/gen/scylla-manager/models"
)

const (
authToken = "token"
clusterIntroHost = "192.168.200.11"
)

func TestSctoolClusterListIntegrationAPITest(t *testing.T) {
for _, tc := range []struct {
name string
createCluster *models.Cluster
updateCluster *models.Cluster
args []string
expectedOutputPattern string
}{
{
name: "update cluster with label",
createCluster: &models.Cluster{
AuthToken: authToken,
Host: clusterIntroHost,
Labels: map[string]string{
"k1": "v1",
},
},
updateCluster: &models.Cluster{
AuthToken: authToken,
Host: clusterIntroHost,
Labels: map[string]string{
"k2": "v2",
},
},
args: []string{"cluster", "list"},
expectedOutputPattern: `<cluster_id> *\| *\| *k2=v2`,
},
} {
t.Run(tc.name, func(t *testing.T) {
// given
client, err := managerclient.NewClient("http://localhost:5080/api/v1")
if err != nil {
t.Fatalf("Unable to create managerclient to consume managet HTTP API, err = {%v}", err)
}

clusterID, err := client.CreateCluster(context.Background(), tc.createCluster)
if err != nil {
t.Fatalf("Unable to create cluster for further listing, err = {%v}", err)
}
if tc.updateCluster != nil {
uc := tc.updateCluster
uc.ID = clusterID
if err := client.UpdateCluster(context.Background(), uc); err != nil {
t.Fatalf("Unable to update cluster for further listing, err = {%v}", err)
}
}

cmd := exec.Command("./sctool.api-tests", tc.args...)
var stderr bytes.Buffer
cmd.Stderr = &stderr
cmd.Dir = "/scylla-manager"

output, err := cmd.Output()
if err != nil {
t.Fatalf("Unable to list clusters with sctool cluster list, err = {%v}, stderr = {%v}", err, stderr.String())
}

defer func() {
if err := client.DeleteCluster(context.Background(), clusterID); err != nil {
t.Logf("Failed to delete cluster, err = {%v}", err)
}
}()

re := regexp.MustCompile(strings.ReplaceAll(tc.expectedOutputPattern, "<cluster_id>", clusterID))
if !re.Match(output) {
t.Fatalf("Expected to get pattern {%v}, got {%s}", tc.expectedOutputPattern, string(output))
}
})
}

}
4 changes: 2 additions & 2 deletions pkg/managerclient/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type ClusterSlice []*models.Cluster

// Render renders ClusterSlice in a tabular format.
func (cs ClusterSlice) Render(w io.Writer) error {
t := table.New("ID", "Name", "Port", "CQL credentials")
t := table.New("ID", "Name", "Labels", "Port", "CQL credentials")
for _, c := range cs {
p := "default"
if c.Port != 0 {
Expand All @@ -50,7 +50,7 @@ func (cs ClusterSlice) Render(w io.Writer) error {
if c.Username != "" && c.Password != "" {
creds = "set"
}
t.AddRow(c.ID, c.Name, p, creds)
t.AddRow(c.ID, c.Name, formatLabels(c.Labels), p, creds)
}
if _, err := w.Write([]byte(t.String())); err != nil {
return err
Expand Down

0 comments on commit 8c115cb

Please sign in to comment.