Skip to content

Commit

Permalink
Merge pull request #159 from reetasingh/feat/validatecommon
Browse files Browse the repository at this point in the history
refactor: have a common method for setting defaults and move spinner call to root.go
  • Loading branch information
k8s-ci-robot authored Feb 21, 2024
2 parents 2340d59 + 53475aa commit c5a232b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
10 changes: 7 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,26 @@ var rootCmd = &cobra.Command{
client := client.NewClient()
config, clientSet := service.Init(viper.GetString("kubeconfig"))
client.ClientSet = clientSet
common.PrintInfo(client.ClientSet, config)
common.SetDefaults(client.ClientSet, config)
if cleanup {
common.SetDefaultNamespace()
service.Cleanup(client.ClientSet)
} else if listImages {
service.PrintListImages(client.ClientSet)
} else {
if err := common.ValidateArgs(); err != nil {
if err := common.ValidateConformanceArgs(); err != nil {
log.Fatal(err)
}
spinner := common.NewSpinner(os.Stdout)

service.RunE2E(client.ClientSet)
spinner.Start()
// PrintE2ELogs is a long running method
client.PrintE2ELogs()
spinner.Stop()
client.FetchFiles(config, clientSet, viper.GetString("output-dir"))
client.FetchExitCode()
service.Cleanup(client.ClientSet)
spinner.Stop()
}
log.Println("Exiting with code: ", client.ExitCode)
os.Exit(client.ExitCode)
Expand Down
29 changes: 10 additions & 19 deletions pkg/common/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ import (
"sigs.k8s.io/hydrophone/pkg/log"
)

// PrintInfo prints the information about the cluster
func PrintInfo(clientSet *kubernetes.Clientset, config *rest.Config) {
spinner := NewSpinner(os.Stdout)
spinner.Start()

// SetDefaults sets the default values for various configuration options used in the application.
// Finally, it logs the API endpoint, server version, namespace, conformance image, and busybox image.
func SetDefaults(clientSet *kubernetes.Clientset, config *rest.Config) {
time.Sleep(2 * time.Second)
serverVersion, err := clientSet.ServerVersion()
if err != nil {
Expand All @@ -50,24 +48,20 @@ func PrintInfo(clientSet *kubernetes.Clientset, config *rest.Config) {
if viper.Get("busybox-image") == "" {
viper.Set("busybox-image", busyboxImage)
}

log.PrintfAPI("API endpoint : %s", config.Host)
log.Printf("Server version : %#v", *serverVersion)
}

func SetDefaultNamespace() {
if viper.Get("namespace") == "" {
viper.Set("namespace", DefaultNamespace)
}
log.Printf("API endpoint : %s", config.Host)
log.Printf("Server version : %#v", *serverVersion)
log.Printf("Using namespace : '%s'", viper.Get("namespace"))
log.Printf("Using conformance image : '%s'", viper.Get("conformance-image"))
log.Printf("Using busybox image : '%s'", viper.Get("busybox-image"))
}

// ValidateArgs validates the arguments passed to the program
// ValidateConformanceArgs validates the arguments passed to the program
// and creates the output directory if it doesn't exist
func ValidateConformanceArgs() error {

func ValidateArgs() error {
if viper.Get("namespace") == "" {
viper.Set("namespace", DefaultNamespace)
}
if viper.Get("focus") == "" {
viper.Set("focus", "\\[Conformance\\]")
}
Expand All @@ -89,9 +83,6 @@ func ValidateArgs() error {
}
}

log.Printf("Using namespace : '%s'", viper.Get("namespace"))
log.Printf("Using conformance image : '%s'", viper.Get("conformance-image"))
log.Printf("Using busybox image : '%s'", viper.Get("busybox-image"))
log.Printf("Test framework will start '%d' threads and use verbosity '%d'",
viper.Get("parallel"), viper.Get("verbosity"))

Expand Down
4 changes: 2 additions & 2 deletions pkg/common/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestValidateArgs(t *testing.T) {
func TestValidateConformanceArgs(t *testing.T) {
// Create a temporary output directory
tempDir := t.TempDir()
viper.Set("output-dir", tempDir)
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestValidateArgs(t *testing.T) {
viper.Set("extra-args", tc.extraArgs)

// Call the function under test
err := ValidateArgs()
err := ValidateConformanceArgs()
if tc.wantErr {
assert.EqualError(t, err, tc.expectedErr)
} else {
Expand Down
6 changes: 0 additions & 6 deletions pkg/log/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ func Printf(format string, v ...any) {
slog.Info(fmt.Sprintf(format, v...))
}

// Print logs for API
func PrintfAPI(format string, v ...interface{}) {
fmt.Print("\n")
slog.Info(fmt.Sprintf(format, v...))
}

// Println logs an info message from the given arguments.
func Println(v ...any) {
slog.Info(fmt.Sprint(v...))
Expand Down

0 comments on commit c5a232b

Please sign in to comment.