Merge pull request #10380 from daehyeok/profile_list_light
Add -l/--light option for profile list command.pull/10442/head
commit
a3609d3778
|
@ -41,6 +41,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var output string
|
var output string
|
||||||
|
var isLight bool
|
||||||
|
|
||||||
var profileListCmd = &cobra.Command{
|
var profileListCmd = &cobra.Command{
|
||||||
Use: "list",
|
Use: "list",
|
||||||
|
@ -58,8 +59,18 @@ var profileListCmd = &cobra.Command{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func listProfiles() (validProfiles, invalidProfiles []*config.Profile, err error) {
|
||||||
|
if isLight {
|
||||||
|
validProfiles, err = config.ListValidProfiles()
|
||||||
|
} else {
|
||||||
|
validProfiles, invalidProfiles, err = config.ListProfiles()
|
||||||
|
}
|
||||||
|
|
||||||
|
return validProfiles, invalidProfiles, err
|
||||||
|
}
|
||||||
|
|
||||||
func printProfilesTable() {
|
func printProfilesTable() {
|
||||||
validProfiles, invalidProfiles, err := config.ListProfiles()
|
validProfiles, invalidProfiles, err := listProfiles()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Warningf("error loading profiles: %v", err)
|
klog.Warningf("error loading profiles: %v", err)
|
||||||
|
@ -75,6 +86,13 @@ func printProfilesTable() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateProfilesStatus(profiles []*config.Profile) {
|
func updateProfilesStatus(profiles []*config.Profile) {
|
||||||
|
if isLight {
|
||||||
|
for _, p := range profiles {
|
||||||
|
p.Status = "Skipped"
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
api, err := machine.NewAPIClient()
|
api, err := machine.NewAPIClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("failed to get machine api client %v", err)
|
klog.Errorf("failed to get machine api client %v", err)
|
||||||
|
@ -168,7 +186,7 @@ func warnInvalidProfiles(invalidProfiles []*config.Profile) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func printProfilesJSON() {
|
func printProfilesJSON() {
|
||||||
validProfiles, invalidProfiles, err := config.ListProfiles()
|
validProfiles, invalidProfiles, err := listProfiles()
|
||||||
|
|
||||||
updateProfilesStatus(validProfiles)
|
updateProfilesStatus(validProfiles)
|
||||||
|
|
||||||
|
@ -195,5 +213,6 @@ func profilesOrDefault(profiles []*config.Profile) []*config.Profile {
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
profileListCmd.Flags().StringVarP(&output, "output", "o", "table", "The output format. One of 'json', 'table'")
|
profileListCmd.Flags().StringVarP(&output, "output", "o", "table", "The output format. One of 'json', 'table'")
|
||||||
|
profileListCmd.Flags().BoolVarP(&isLight, "light", "l", false, "If true, returns list of profiles faster by skipping validating the status of the cluster.")
|
||||||
ProfileCmd.AddCommand(profileListCmd)
|
ProfileCmd.AddCommand(profileListCmd)
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,6 +89,7 @@ minikube profile list [flags]
|
||||||
### Options
|
### Options
|
||||||
|
|
||||||
```
|
```
|
||||||
|
-l, --light If true, returns list of profiles faster by skipping validating the status of the cluster.
|
||||||
-o, --output string The output format. One of 'json', 'table' (default "table")
|
-o, --output string The output format. One of 'json', 'table' (default "table")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -811,50 +811,98 @@ func validateProfileCmd(ctx context.Context, t *testing.T, profile string) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("profile_list", func(t *testing.T) {
|
t.Run("profile_list", func(t *testing.T) {
|
||||||
|
// helper function to run command then, return target profile line from table output.
|
||||||
|
extractrofileListFunc := func(rr *RunResult) string {
|
||||||
|
listLines := strings.Split(strings.TrimSpace(rr.Stdout.String()), "\n")
|
||||||
|
for i := 3; i < (len(listLines) - 1); i++ {
|
||||||
|
profileLine := listLines[i]
|
||||||
|
if strings.Contains(profileLine, profile) {
|
||||||
|
return profileLine
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// List profiles
|
// List profiles
|
||||||
|
start := time.Now()
|
||||||
rr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list"))
|
rr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list"))
|
||||||
|
elapsed := time.Since(start)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to list profiles: args %q : %v", rr.Command(), err)
|
t.Errorf("failed to list profiles: args %q : %v", rr.Command(), err)
|
||||||
}
|
}
|
||||||
|
t.Logf("Took %q to run %q", elapsed, rr.Command())
|
||||||
|
|
||||||
// Table output
|
profileLine := extractrofileListFunc(rr)
|
||||||
listLines := strings.Split(strings.TrimSpace(rr.Stdout.String()), "\n")
|
if profileLine == "" {
|
||||||
profileExists := false
|
|
||||||
for i := 3; i < (len(listLines) - 1); i++ {
|
|
||||||
profileLine := listLines[i]
|
|
||||||
if strings.Contains(profileLine, profile) {
|
|
||||||
profileExists = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !profileExists {
|
|
||||||
t.Errorf("expected 'profile list' output to include %q but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command())
|
t.Errorf("expected 'profile list' output to include %q but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// List profiles with light option.
|
||||||
|
start = time.Now()
|
||||||
|
lrr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list", "-l"))
|
||||||
|
lightElapsed := time.Since(start)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to list profiles: args %q : %v", lrr.Command(), err)
|
||||||
|
}
|
||||||
|
t.Logf("Took %q to run %q", lightElapsed, lrr.Command())
|
||||||
|
|
||||||
|
profileLine = extractrofileListFunc(lrr)
|
||||||
|
if profileLine == "" || !strings.Contains(profileLine, "Skipped") {
|
||||||
|
t.Errorf("expected 'profile list' output to include %q with 'Skipped' status but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command())
|
||||||
|
}
|
||||||
|
|
||||||
|
if lightElapsed > 3*time.Second {
|
||||||
|
t.Errorf("expected running time of '%q' is less than 3 seconds. Took %q ", lrr.Command(), lightElapsed)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("profile_json_output", func(t *testing.T) {
|
t.Run("profile_json_output", func(t *testing.T) {
|
||||||
// Json output
|
// helper function to run command then, return target profile object from json output.
|
||||||
rr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list", "--output", "json"))
|
extractProfileObjFunc := func(rr *RunResult) *config.Profile {
|
||||||
|
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)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, profileObject := range jsonObject["valid"] {
|
||||||
|
if profileObject.Name == profile {
|
||||||
|
return &profileObject
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
rr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list", "-o", "json"))
|
||||||
|
elapsed := time.Since(start)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to list profiles with json format. args %q: %v", rr.Command(), err)
|
t.Errorf("failed to list profiles with json format. args %q: %v", rr.Command(), err)
|
||||||
}
|
}
|
||||||
var jsonObject map[string][]map[string]interface{}
|
t.Logf("Took %q to run %q", elapsed, rr.Command())
|
||||||
err = json.Unmarshal(rr.Stdout.Bytes(), &jsonObject)
|
|
||||||
if err != nil {
|
pr := extractProfileObjFunc(rr)
|
||||||
t.Errorf("failed to decode json from profile list: args %q: %v", rr.Command(), err)
|
if pr == nil {
|
||||||
}
|
|
||||||
validProfiles := jsonObject["valid"]
|
|
||||||
profileExists := false
|
|
||||||
for _, profileObject := range validProfiles {
|
|
||||||
if profileObject["Name"] == profile {
|
|
||||||
profileExists = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !profileExists {
|
|
||||||
t.Errorf("expected the json of 'profile list' to include %q but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command())
|
t.Errorf("expected the json of 'profile list' to include %q but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
start = time.Now()
|
||||||
|
lrr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list", "-o", "json", "--light"))
|
||||||
|
lightElapsed := time.Since(start)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to list profiles with json format. args %q: %v", lrr.Command(), err)
|
||||||
|
}
|
||||||
|
t.Logf("Took %q to run %q", lightElapsed, lrr.Command())
|
||||||
|
|
||||||
|
pr = extractProfileObjFunc(lrr)
|
||||||
|
if pr == nil || pr.Status != "Skipped" {
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
|
||||||
|
if lightElapsed > 3*time.Second {
|
||||||
|
t.Errorf("expected running time of '%q' is less than 3 seconds. Took %q ", lrr.Command(), lightElapsed)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue