Skip to content

Commit a3609d3

Browse files
authored
Merge pull request #10380 from daehyeok/profile_list_light
Add -l/--light option for profile list command.
2 parents 984b079 + f457bde commit a3609d3

File tree

3 files changed

+96
-28
lines changed

3 files changed

+96
-28
lines changed

cmd/minikube/cmd/config/profile_list.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
)
4242

4343
var output string
44+
var isLight bool
4445

4546
var profileListCmd = &cobra.Command{
4647
Use: "list",
@@ -58,8 +59,18 @@ var profileListCmd = &cobra.Command{
5859
},
5960
}
6061

62+
func listProfiles() (validProfiles, invalidProfiles []*config.Profile, err error) {
63+
if isLight {
64+
validProfiles, err = config.ListValidProfiles()
65+
} else {
66+
validProfiles, invalidProfiles, err = config.ListProfiles()
67+
}
68+
69+
return validProfiles, invalidProfiles, err
70+
}
71+
6172
func printProfilesTable() {
62-
validProfiles, invalidProfiles, err := config.ListProfiles()
73+
validProfiles, invalidProfiles, err := listProfiles()
6374

6475
if err != nil {
6576
klog.Warningf("error loading profiles: %v", err)
@@ -75,6 +86,13 @@ func printProfilesTable() {
7586
}
7687

7788
func updateProfilesStatus(profiles []*config.Profile) {
89+
if isLight {
90+
for _, p := range profiles {
91+
p.Status = "Skipped"
92+
}
93+
return
94+
}
95+
7896
api, err := machine.NewAPIClient()
7997
if err != nil {
8098
klog.Errorf("failed to get machine api client %v", err)
@@ -168,7 +186,7 @@ func warnInvalidProfiles(invalidProfiles []*config.Profile) {
168186
}
169187

170188
func printProfilesJSON() {
171-
validProfiles, invalidProfiles, err := config.ListProfiles()
189+
validProfiles, invalidProfiles, err := listProfiles()
172190

173191
updateProfilesStatus(validProfiles)
174192

@@ -195,5 +213,6 @@ func profilesOrDefault(profiles []*config.Profile) []*config.Profile {
195213

196214
func init() {
197215
profileListCmd.Flags().StringVarP(&output, "output", "o", "table", "The output format. One of 'json', 'table'")
216+
profileListCmd.Flags().BoolVarP(&isLight, "light", "l", false, "If true, returns list of profiles faster by skipping validating the status of the cluster.")
198217
ProfileCmd.AddCommand(profileListCmd)
199218
}

site/content/en/docs/commands/profile.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ minikube profile list [flags]
8989
### Options
9090

9191
```
92+
-l, --light If true, returns list of profiles faster by skipping validating the status of the cluster.
9293
-o, --output string The output format. One of 'json', 'table' (default "table")
9394
```
9495

test/integration/functional_test.go

+74-26
Original file line numberDiff line numberDiff line change
@@ -811,50 +811,98 @@ func validateProfileCmd(ctx context.Context, t *testing.T, profile string) {
811811
})
812812

813813
t.Run("profile_list", func(t *testing.T) {
814+
// helper function to run command then, return target profile line from table output.
815+
extractrofileListFunc := func(rr *RunResult) string {
816+
listLines := strings.Split(strings.TrimSpace(rr.Stdout.String()), "\n")
817+
for i := 3; i < (len(listLines) - 1); i++ {
818+
profileLine := listLines[i]
819+
if strings.Contains(profileLine, profile) {
820+
return profileLine
821+
}
822+
}
823+
return ""
824+
}
825+
814826
// List profiles
827+
start := time.Now()
815828
rr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list"))
829+
elapsed := time.Since(start)
816830
if err != nil {
817831
t.Errorf("failed to list profiles: args %q : %v", rr.Command(), err)
818832
}
833+
t.Logf("Took %q to run %q", elapsed, rr.Command())
819834

820-
// Table output
821-
listLines := strings.Split(strings.TrimSpace(rr.Stdout.String()), "\n")
822-
profileExists := false
823-
for i := 3; i < (len(listLines) - 1); i++ {
824-
profileLine := listLines[i]
825-
if strings.Contains(profileLine, profile) {
826-
profileExists = true
827-
break
828-
}
829-
}
830-
if !profileExists {
835+
profileLine := extractrofileListFunc(rr)
836+
if profileLine == "" {
831837
t.Errorf("expected 'profile list' output to include %q but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command())
832838
}
839+
840+
// List profiles with light option.
841+
start = time.Now()
842+
lrr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list", "-l"))
843+
lightElapsed := time.Since(start)
844+
if err != nil {
845+
t.Errorf("failed to list profiles: args %q : %v", lrr.Command(), err)
846+
}
847+
t.Logf("Took %q to run %q", lightElapsed, lrr.Command())
848+
849+
profileLine = extractrofileListFunc(lrr)
850+
if profileLine == "" || !strings.Contains(profileLine, "Skipped") {
851+
t.Errorf("expected 'profile list' output to include %q with 'Skipped' status but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command())
852+
}
853+
854+
if lightElapsed > 3*time.Second {
855+
t.Errorf("expected running time of '%q' is less than 3 seconds. Took %q ", lrr.Command(), lightElapsed)
856+
}
833857
})
834858

835859
t.Run("profile_json_output", func(t *testing.T) {
836-
// Json output
837-
rr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list", "--output", "json"))
860+
// helper function to run command then, return target profile object from json output.
861+
extractProfileObjFunc := func(rr *RunResult) *config.Profile {
862+
var jsonObject map[string][]config.Profile
863+
err := json.Unmarshal(rr.Stdout.Bytes(), &jsonObject)
864+
if err != nil {
865+
t.Errorf("failed to decode json from profile list: args %q: %v", rr.Command(), err)
866+
return nil
867+
}
868+
869+
for _, profileObject := range jsonObject["valid"] {
870+
if profileObject.Name == profile {
871+
return &profileObject
872+
}
873+
}
874+
return nil
875+
}
876+
877+
start := time.Now()
878+
rr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list", "-o", "json"))
879+
elapsed := time.Since(start)
838880
if err != nil {
839881
t.Errorf("failed to list profiles with json format. args %q: %v", rr.Command(), err)
840882
}
841-
var jsonObject map[string][]map[string]interface{}
842-
err = json.Unmarshal(rr.Stdout.Bytes(), &jsonObject)
843-
if err != nil {
844-
t.Errorf("failed to decode json from profile list: args %q: %v", rr.Command(), err)
883+
t.Logf("Took %q to run %q", elapsed, rr.Command())
884+
885+
pr := extractProfileObjFunc(rr)
886+
if pr == nil {
887+
t.Errorf("expected the json of 'profile list' to include %q but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command())
845888
}
846-
validProfiles := jsonObject["valid"]
847-
profileExists := false
848-
for _, profileObject := range validProfiles {
849-
if profileObject["Name"] == profile {
850-
profileExists = true
851-
break
852-
}
889+
890+
start = time.Now()
891+
lrr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list", "-o", "json", "--light"))
892+
lightElapsed := time.Since(start)
893+
if err != nil {
894+
t.Errorf("failed to list profiles with json format. args %q: %v", lrr.Command(), err)
853895
}
854-
if !profileExists {
855-
t.Errorf("expected the json of 'profile list' to include %q but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command())
896+
t.Logf("Took %q to run %q", lightElapsed, lrr.Command())
897+
898+
pr = extractProfileObjFunc(lrr)
899+
if pr == nil || pr.Status != "Skipped" {
900+
t.Errorf("expected the json of 'profile list' to include 'Skipped' status for %q but got *%q*. args: %q", profile, lrr.Stdout.String(), lrr.Command())
856901
}
857902

903+
if lightElapsed > 3*time.Second {
904+
t.Errorf("expected running time of '%q' is less than 3 seconds. Took %q ", lrr.Command(), lightElapsed)
905+
}
858906
})
859907
}
860908

0 commit comments

Comments
 (0)