diff --git a/cmd/bootstrap/bootstrap_isogen.go b/cmd/bootstrap/bootstrap_isogen.go index 90375d9c2..a5ba84a7c 100644 --- a/cmd/bootstrap/bootstrap_isogen.go +++ b/cmd/bootstrap/bootstrap_isogen.go @@ -14,7 +14,7 @@ func NewISOGenCommand(parent *cobra.Command, rootSettings *environment.AirshipCT Use: "isogen", Short: "Generate bootstrap ISO image", RunE: func(cmd *cobra.Command, args []string) error { - return isogen.GenerateBootstrapIso(settings, args, cmd.OutOrStdout()) + return isogen.GenerateBootstrapIso(settings, args) }, } diff --git a/pkg/bootstrap/isogen/command.go b/pkg/bootstrap/isogen/command.go index fe3b0ad3c..f9d159b13 100644 --- a/pkg/bootstrap/isogen/command.go +++ b/pkg/bootstrap/isogen/command.go @@ -3,7 +3,6 @@ package isogen import ( "context" "fmt" - "io" "os" "path/filepath" "strings" @@ -12,6 +11,7 @@ import ( "opendev.org/airship/airshipctl/pkg/container" "opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/errors" + "opendev.org/airship/airshipctl/pkg/log" "opendev.org/airship/airshipctl/pkg/util" "sigs.k8s.io/kustomize/v3/pkg/fs" @@ -22,9 +22,9 @@ const ( ) // GenerateBootstrapIso will generate data for cloud init and start ISO builder container -func GenerateBootstrapIso(settings *Settings, args []string, out io.Writer) error { +func GenerateBootstrapIso(settings *Settings, args []string) error { if settings.IsogenConfigFile == "" { - fmt.Fprintln(out, "Reading config file location from global settings is not supported") + log.Print("Reading config file location from global settings is not supported") return errors.ErrNotImplemented{} } @@ -35,7 +35,7 @@ func GenerateBootstrapIso(settings *Settings, args []string, out io.Writer) erro return err } - if err := verifyInputs(cfg, args, out); err != nil { + if err := verifyInputs(cfg, args); err != nil { return err } @@ -44,7 +44,7 @@ func GenerateBootstrapIso(settings *Settings, args []string, out io.Writer) erro return err } - fmt.Fprintln(out, "Creating ISO builder container") + log.Print("Creating ISO builder container") builder, err := container.NewContainer( &ctx, cfg.Container.ContainerRuntime, cfg.Container.Image) @@ -52,28 +52,28 @@ func GenerateBootstrapIso(settings *Settings, args []string, out io.Writer) erro return err } - err = generateBootstrapIso(docBundle, builder, cfg, out, settings.Debug) + err = generateBootstrapIso(docBundle, builder, cfg, settings.Debug) if err != nil { return err } - fmt.Fprintln(out, "Checking artifacts") + log.Print("Checking artifacts") return verifyArtifacts(cfg) } -func verifyInputs(cfg *Config, args []string, out io.Writer) error { +func verifyInputs(cfg *Config, args []string) error { if len(args) == 0 { - fmt.Fprintln(out, "Specify path to document model. Config param from global settings is not supported") + log.Print("Specify path to document model. Config param from global settings is not supported") return errors.ErrNotImplemented{} } if cfg.Container.Volume == "" { - fmt.Fprintln(out, "Specify volume bind for ISO builder container") + log.Print("Specify volume bind for ISO builder container") return errors.ErrWrongConfig{} } if (cfg.Builder.UserDataFileName == "") || (cfg.Builder.NetworkConfigFileName == "") { - fmt.Fprintln(out, "UserDataFileName or NetworkConfigFileName are not specified in ISO builder config") + log.Print("UserDataFileName or NetworkConfigFileName are not specified in ISO builder config") return errors.ErrWrongConfig{} } @@ -82,7 +82,7 @@ func verifyInputs(cfg *Config, args []string, out io.Writer) error { case len(vols) == 1: cfg.Container.Volume = fmt.Sprintf("%s:%s", vols[0], vols[0]) case len(vols) > 2: - fmt.Fprintln(out, "Bad container volume format. Use hostPath:contPath") + log.Print("Bad container volume format. Use hostPath:contPath") return errors.ErrWrongConfig{} } return nil @@ -113,11 +113,10 @@ func generateBootstrapIso( docBubdle document.Bundle, builder container.Container, cfg *Config, - out io.Writer, debug bool, ) error { cntVol := strings.Split(cfg.Container.Volume, ":")[1] - fmt.Fprintln(out, "Creating cloud-init for ephemeral K8s") + log.Print("Creating cloud-init for ephemeral K8s") userData, netConf, err := cloudinit.GetCloudData(docBubdle, EphemeralClusterAnnotation) if err != nil { return err @@ -131,7 +130,7 @@ func generateBootstrapIso( vols := []string{cfg.Container.Volume} builderCfgLocation := filepath.Join(cntVol, builderConfigFileName) - fmt.Fprintf(out, "Running default container command. Mounted dir: %s\n", vols) + log.Printf("Running default container command. Mounted dir: %s", vols) if err := builder.RunCommand( []string{}, nil, @@ -142,15 +141,12 @@ func generateBootstrapIso( return err } - fmt.Fprintln(out, "ISO successfully built.") - if debug { - fmt.Fprintf( - out, - "Debug flag is set. Container %s stopped but not deleted.\n", - builder.GetId(), - ) - return nil + log.Print("ISO successfully built.") + if !debug { + log.Print("Removing container.") + return builder.RmContainer() } - fmt.Fprintln(out, "Removing container.") - return builder.RmContainer() + + log.Debugf("Debug flag is set. Container %s stopped but not deleted.", builder.GetId()) + return nil } diff --git a/pkg/bootstrap/isogen/command_test.go b/pkg/bootstrap/isogen/command_test.go index a814b6bea..a7af6a802 100644 --- a/pkg/bootstrap/isogen/command_test.go +++ b/pkg/bootstrap/isogen/command_test.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "io" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -11,6 +12,7 @@ import ( "opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/errors" + "opendev.org/airship/airshipctl/pkg/log" "opendev.org/airship/airshipctl/testutil" ) @@ -60,18 +62,18 @@ func TestBootstrapIso(t *testing.T) { }, } expOut := []string{ - "Creating cloud-init for ephemeral K8s\n", - fmt.Sprintf("Running default container command. Mounted dir: [%s]\n", volBind), - "ISO successfully built.\n", - "Debug flag is set. Container TESTID stopped but not deleted.\n", - "Removing container.\n", + "Creating cloud-init for ephemeral K8s", + fmt.Sprintf("Running default container command. Mounted dir: [%s]", volBind), + "ISO successfully built.", + "Debug flag is set. Container TESTID stopped but not deleted.", + "Removing container.", } tests := []struct { builder *mockContainer cfg *Config debug bool - expectedOut string + expectedOut []string expectdErr error }{ { @@ -80,7 +82,7 @@ func TestBootstrapIso(t *testing.T) { }, cfg: testCfg, debug: false, - expectedOut: expOut[0] + expOut[1], + expectedOut: []string{expOut[0], expOut[1]}, expectdErr: testErr, }, { @@ -90,7 +92,7 @@ func TestBootstrapIso(t *testing.T) { }, cfg: testCfg, debug: true, - expectedOut: expOut[0] + expOut[1] + expOut[2] + expOut[3], + expectedOut: []string{expOut[0], expOut[1], expOut[2], expOut[3]}, expectdErr: nil, }, { @@ -101,20 +103,22 @@ func TestBootstrapIso(t *testing.T) { }, cfg: testCfg, debug: false, - expectedOut: expOut[0] + expOut[1] + expOut[2] + expOut[4], + expectedOut: []string{expOut[0], expOut[1], expOut[2], expOut[4]}, expectdErr: testErr, }, } for _, tt := range tests { - actualOut := bytes.NewBufferString("") - actualErr := generateBootstrapIso(bundle, tt.builder, tt.cfg, actualOut, tt.debug) + outBuf := &bytes.Buffer{} + log.Init(tt.debug, outBuf) + actualErr := generateBootstrapIso(bundle, tt.builder, tt.cfg, tt.debug) + actualOut := outBuf.String() - errS := fmt.Sprintf("generateBootstrapIso should have return error %s, got %s", tt.expectdErr, actualErr) - assert.Equal(t, actualErr, tt.expectdErr, errS) + for _, line := range tt.expectedOut { + assert.True(t, strings.Contains(actualOut, line)) + } - errS = fmt.Sprintf("generateBootstrapIso should have print %s, got %s", tt.expectedOut, actualOut) - assert.Equal(t, actualOut.String(), tt.expectedOut, errS) + assert.Equal(t, tt.expectdErr, actualErr) } } @@ -172,7 +176,7 @@ func TestVerifyInputs(t *testing.T) { } for _, tt := range tests { - actualErr := verifyInputs(tt.cfg, tt.args, bytes.NewBufferString("")) + actualErr := verifyInputs(tt.cfg, tt.args) assert.Equal(t, tt.expectedErr, actualErr) }