Skip to content

Commit

Permalink
Remove cluster's subnode from ListProfiles result.
Browse files Browse the repository at this point in the history
Fix prolbem which ListProfiles return subnodes in Mult-Node clusters as
a part of inValidPs.
  • Loading branch information
daehyeok authored and Daehyeok Mun committed Jan 17, 2021
1 parent 31aa5c1 commit 772590c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
22 changes: 20 additions & 2 deletions pkg/minikube/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ func ListProfiles(miniHome ...string) (validPs []*Profile, inValidPs []*Profile,
if err == nil {
pDirs = append(pDirs, cs...)
}
pDirs = removeDupes(pDirs)
for _, n := range pDirs {

nodeNames := map[string]bool{}
for _, n := range removeDupes(pDirs) {
p, err := LoadProfile(n, miniHome...)
if err != nil {
inValidPs = append(inValidPs, p)
Expand All @@ -219,7 +220,13 @@ func ListProfiles(miniHome ...string) (validPs []*Profile, inValidPs []*Profile,
continue
}
validPs = append(validPs, p)

for _, child := range p.Config.Nodes {
nodeNames[MachineName(*p.Config, child)] = true
}
}

inValidPs = removeChildNodes(inValidPs, nodeNames)
return validPs, inValidPs, nil
}

Expand All @@ -243,6 +250,17 @@ func removeDupes(profiles []string) []string {
return result
}

func removeChildNodes(inValidPs []*Profile, nodeNames map[string]bool) []*Profile {
ps := []*Profile{}
for _, p := range inValidPs {
if _, ok := nodeNames[p.Name]; !ok {
ps = append(ps, p)
}
}

return ps
}

// LoadProfile loads type Profile based on its name
func LoadProfile(name string, miniHome ...string) (*Profile, error) {
cfg, err := DefaultLoader.LoadConfigFromFile(name, miniHome...)
Expand Down
42 changes: 42 additions & 0 deletions test/integration/multinode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ package integration

import (
"context"
"encoding/json"
"fmt"
"os/exec"
"strings"
"testing"

"k8s.io/minikube/pkg/minikube/config"
)

func TestMultiNode(t *testing.T) {
Expand All @@ -43,6 +46,7 @@ func TestMultiNode(t *testing.T) {
}{
{"FreshStart2Nodes", validateMultiNodeStart},
{"AddNode", validateAddNodeToMultiNode},
{"ProfileList", validateProfileListWithMultiNode},
{"StopNode", validateStopRunningNode},
{"StartAfterStop", validateStartNodeAfterStop},
{"DeleteNode", validateDeleteNodeFromMultiNode},
Expand Down Expand Up @@ -109,6 +113,44 @@ func validateAddNodeToMultiNode(ctx context.Context, t *testing.T, profile strin
}
}

func validateProfileListWithMultiNode(ctx context.Context, t *testing.T, profile string) {
rr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list", "--output", "json"))
if err != nil {
t.Errorf("failed to list profiles with json format. args %q: %v", rr.Command(), err)
}

var jsonObject map[string][]config.Profile
err = json.Unmarshal(rr.Stdout.Bytes(), &jsonObject)
if err != nil {
t.Errorf("failed to decode json from profile list: args %q: %v", rr.Command(), err)
}

validProfiles := jsonObject["valid"]
var profileObject *config.Profile
for _, obj := range validProfiles {
if obj.Name == profile {
profileObject = &obj
break
}
}

if profileObject == nil {
t.Errorf("expected the json of 'profile list' to include %q but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command())
} else if expected, numNodes := 3, len(profileObject.Config.Nodes); expected != numNodes {
t.Errorf("expected profile %q in json of 'profile list' include %d nodes but have %d nodes. got *%q*. args: %q", profile, expected, numNodes, rr.Stdout.String(), rr.Command())
}

if invalidPs, ok := jsonObject["invalid"]; ok {
for _, ps := range invalidPs {
if strings.Contains(ps.Name, profile) {
t.Errorf("expected the json of 'profile list' to not include profile or node in invalid profile but got *%q*. args: %q", rr.Stdout.String(), rr.Command())
}
}

}

}

func validateStopRunningNode(ctx context.Context, t *testing.T, profile string) {
// Run minikube node stop on that node
rr, err := Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "node", "stop", ThirdNodeName))
Expand Down

0 comments on commit 772590c

Please sign in to comment.