Skip to content

Commit

Permalink
feat(clusterupdate): add --label flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal-Leszczynski committed Jul 23, 2024
1 parent 845aaca commit 979475b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
6 changes: 6 additions & 0 deletions pkg/command/cluster/clusterupdate/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type command struct {

cluster string
name string
label flag.Label
host string
port int64
authToken string
Expand Down Expand Up @@ -55,6 +56,7 @@ func (cmd *command) init() {
w := flag.Wrap(cmd.Flags())
w.Cluster(&cmd.cluster)
w.Unwrap().StringVarP(&cmd.name, "name", "n", "", "")
w.Unwrap().Var(&cmd.label, "label", "")
w.Unwrap().StringVar(&cmd.host, "host", "", "")
w.Unwrap().Int64Var(&cmd.port, "port", 10001, "")
w.Unwrap().StringVar(&cmd.authToken, "auth-token", "", "")
Expand All @@ -79,6 +81,10 @@ func (cmd *command) run() error {
cluster.Name = cmd.name
ok = true
}
if cmd.Flags().Changed("label") {
cluster.Labels = cmd.label.ApplyDiff(cluster.Labels)
ok = true
}
if cmd.Flags().Changed("host") {
cluster.Host = cmd.host
ok = true
Expand Down
75 changes: 65 additions & 10 deletions pkg/command/cluster/clusterupdate/cmd_integration_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package clusterupdate
import (
"bytes"
"context"
"maps"
"os/exec"
"testing"

Expand All @@ -26,19 +27,32 @@ func TestSctoolClusterUpdateIntegrationAPITest(t *testing.T) {
for _, tc := range []struct {
name string
args []string
initialCluster *models.Cluster
expectedCluster *models.Cluster
}{
{
name: "update cluster, no-changes",
args: []string{"cluster", "update", "--auth-token", authToken},
initialCluster: &models.Cluster{
Labels: map[string]string{
"k1": "v1",
},
ForceTLSDisabled: true,
},
expectedCluster: &models.Cluster{
ForceTLSDisabled: true,
ForceNonSslSessionPort: false,
Labels: map[string]string{
"k1": "v1",
},
ForceTLSDisabled: true,
},
},
{
name: "update cluster, force TLS enabled",
args: []string{"cluster", "update", "--force-non-ssl-session-port"},
initialCluster: &models.Cluster{
ForceTLSDisabled: true,
ForceNonSslSessionPort: false,
},
expectedCluster: &models.Cluster{
ForceTLSDisabled: true,
ForceNonSslSessionPort: true,
Expand All @@ -47,6 +61,10 @@ func TestSctoolClusterUpdateIntegrationAPITest(t *testing.T) {
{
name: "update cluster, force TLS disabled",
args: []string{"cluster", "update", "--force-tls-disabled=false"},
initialCluster: &models.Cluster{
ForceTLSDisabled: true,
ForceNonSslSessionPort: false,
},
expectedCluster: &models.Cluster{
ForceTLSDisabled: false,
ForceNonSslSessionPort: false,
Expand All @@ -55,25 +73,59 @@ func TestSctoolClusterUpdateIntegrationAPITest(t *testing.T) {
{
name: "update cluster, clean TLS flag",
args: []string{"cluster", "update", "--force-tls-disabled=false", "--force-non-ssl-session-port"},
initialCluster: &models.Cluster{
ForceTLSDisabled: true,
ForceNonSslSessionPort: false,
},
expectedCluster: &models.Cluster{
ForceTLSDisabled: false,
ForceNonSslSessionPort: true,
},
},
{
name: "update cluster, add labels",
args: []string{"cluster", "update", "--label", "k1-,k2=v22,k4=v4"},
initialCluster: &models.Cluster{
Labels: map[string]string{
"k1": "v1",
"k2": "v2",
"k3": "v3",
},
},
expectedCluster: &models.Cluster{
Labels: map[string]string{
"k2": "v22",
"k3": "v3",
"k4": "v4",
},
},
},
} {
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(), &models.Cluster{
AuthToken: authToken,
Host: clusterIntroHost,
Password: testPass,
Username: testUsername,
ForceTLSDisabled: true,
})

fillCluster := func(c *models.Cluster) {
if c.AuthToken == "" {
c.AuthToken = authToken
}
if c.Host == "" {
c.Host = clusterIntroHost
}
if c.Username == "" {
c.Username = testUsername
}
if c.Password == "" {
c.Password = testPass
}
}
fillCluster(tc.initialCluster)
fillCluster(tc.expectedCluster)

clusterID, err := client.CreateCluster(context.Background(), tc.initialCluster)
if err != nil {
t.Fatalf("Unable to create cluster for further updates, err = {%v}", err)
}
Expand All @@ -90,7 +142,7 @@ func TestSctoolClusterUpdateIntegrationAPITest(t *testing.T) {
}

defer func() {
if err != client.DeleteCluster(context.Background(), clusterID) {
if err := client.DeleteCluster(context.Background(), clusterID); err != nil {
t.Logf("Failed to delete cluster, err = {%v}", err)
}
}()
Expand All @@ -112,6 +164,9 @@ func TestSctoolClusterUpdateIntegrationAPITest(t *testing.T) {
t.Fatalf("ForceNonSslPort mismatch {%v} != {%v}, output={%v}", c.ForceNonSslSessionPort,
tc.expectedCluster.ForceNonSslSessionPort, string(output))
}
if !maps.Equal(c.Labels, tc.expectedCluster.Labels) {
t.Fatalf("Labels mismatch {%v} != {%v}", c.Labels, tc.expectedCluster.Labels)
}
})
}

Expand Down

0 comments on commit 979475b

Please sign in to comment.