Merge "Switch isogen command output to log module"

This commit is contained in:
Zuul 2019-11-15 15:37:48 +00:00 committed by Gerrit Code Review
commit ffcb533d39
3 changed files with 42 additions and 42 deletions

View File

@ -14,7 +14,7 @@ func NewISOGenCommand(parent *cobra.Command, rootSettings *environment.AirshipCT
Use: "isogen", Use: "isogen",
Short: "Generate bootstrap ISO image", Short: "Generate bootstrap ISO image",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return isogen.GenerateBootstrapIso(settings, args, cmd.OutOrStdout()) return isogen.GenerateBootstrapIso(settings, args)
}, },
} }

View File

@ -3,7 +3,6 @@ package isogen
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -12,6 +11,7 @@ import (
"opendev.org/airship/airshipctl/pkg/container" "opendev.org/airship/airshipctl/pkg/container"
"opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/errors" "opendev.org/airship/airshipctl/pkg/errors"
"opendev.org/airship/airshipctl/pkg/log"
"opendev.org/airship/airshipctl/pkg/util" "opendev.org/airship/airshipctl/pkg/util"
"sigs.k8s.io/kustomize/v3/pkg/fs" "sigs.k8s.io/kustomize/v3/pkg/fs"
@ -22,9 +22,9 @@ const (
) )
// GenerateBootstrapIso will generate data for cloud init and start ISO builder container // 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 == "" { 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{} return errors.ErrNotImplemented{}
} }
@ -35,7 +35,7 @@ func GenerateBootstrapIso(settings *Settings, args []string, out io.Writer) erro
return err return err
} }
if err := verifyInputs(cfg, args, out); err != nil { if err := verifyInputs(cfg, args); err != nil {
return err return err
} }
@ -44,7 +44,7 @@ func GenerateBootstrapIso(settings *Settings, args []string, out io.Writer) erro
return err return err
} }
fmt.Fprintln(out, "Creating ISO builder container") log.Print("Creating ISO builder container")
builder, err := container.NewContainer( builder, err := container.NewContainer(
&ctx, cfg.Container.ContainerRuntime, &ctx, cfg.Container.ContainerRuntime,
cfg.Container.Image) cfg.Container.Image)
@ -52,28 +52,28 @@ func GenerateBootstrapIso(settings *Settings, args []string, out io.Writer) erro
return err return err
} }
err = generateBootstrapIso(docBundle, builder, cfg, out, settings.Debug) err = generateBootstrapIso(docBundle, builder, cfg, settings.Debug)
if err != nil { if err != nil {
return err return err
} }
fmt.Fprintln(out, "Checking artifacts") log.Print("Checking artifacts")
return verifyArtifacts(cfg) return verifyArtifacts(cfg)
} }
func verifyInputs(cfg *Config, args []string, out io.Writer) error { func verifyInputs(cfg *Config, args []string) error {
if len(args) == 0 { 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{} return errors.ErrNotImplemented{}
} }
if cfg.Container.Volume == "" { 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{} return errors.ErrWrongConfig{}
} }
if (cfg.Builder.UserDataFileName == "") || (cfg.Builder.NetworkConfigFileName == "") { 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{} return errors.ErrWrongConfig{}
} }
@ -82,7 +82,7 @@ func verifyInputs(cfg *Config, args []string, out io.Writer) error {
case len(vols) == 1: case len(vols) == 1:
cfg.Container.Volume = fmt.Sprintf("%s:%s", vols[0], vols[0]) cfg.Container.Volume = fmt.Sprintf("%s:%s", vols[0], vols[0])
case len(vols) > 2: 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 errors.ErrWrongConfig{}
} }
return nil return nil
@ -113,11 +113,10 @@ func generateBootstrapIso(
docBubdle document.Bundle, docBubdle document.Bundle,
builder container.Container, builder container.Container,
cfg *Config, cfg *Config,
out io.Writer,
debug bool, debug bool,
) error { ) error {
cntVol := strings.Split(cfg.Container.Volume, ":")[1] 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) userData, netConf, err := cloudinit.GetCloudData(docBubdle, EphemeralClusterAnnotation)
if err != nil { if err != nil {
return err return err
@ -131,7 +130,7 @@ func generateBootstrapIso(
vols := []string{cfg.Container.Volume} vols := []string{cfg.Container.Volume}
builderCfgLocation := filepath.Join(cntVol, builderConfigFileName) 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( if err := builder.RunCommand(
[]string{}, []string{},
nil, nil,
@ -142,15 +141,12 @@ func generateBootstrapIso(
return err return err
} }
fmt.Fprintln(out, "ISO successfully built.") log.Print("ISO successfully built.")
if debug { if !debug {
fmt.Fprintf( log.Print("Removing container.")
out, return builder.RmContainer()
"Debug flag is set. Container %s stopped but not deleted.\n",
builder.GetId(),
)
return nil
} }
fmt.Fprintln(out, "Removing container.")
return builder.RmContainer() log.Debugf("Debug flag is set. Container %s stopped but not deleted.", builder.GetId())
return nil
} }

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -11,6 +12,7 @@ import (
"opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/errors" "opendev.org/airship/airshipctl/pkg/errors"
"opendev.org/airship/airshipctl/pkg/log"
"opendev.org/airship/airshipctl/testutil" "opendev.org/airship/airshipctl/testutil"
) )
@ -60,18 +62,18 @@ func TestBootstrapIso(t *testing.T) {
}, },
} }
expOut := []string{ expOut := []string{
"Creating cloud-init for ephemeral K8s\n", "Creating cloud-init for ephemeral K8s",
fmt.Sprintf("Running default container command. Mounted dir: [%s]\n", volBind), fmt.Sprintf("Running default container command. Mounted dir: [%s]", volBind),
"ISO successfully built.\n", "ISO successfully built.",
"Debug flag is set. Container TESTID stopped but not deleted.\n", "Debug flag is set. Container TESTID stopped but not deleted.",
"Removing container.\n", "Removing container.",
} }
tests := []struct { tests := []struct {
builder *mockContainer builder *mockContainer
cfg *Config cfg *Config
debug bool debug bool
expectedOut string expectedOut []string
expectdErr error expectdErr error
}{ }{
{ {
@ -80,7 +82,7 @@ func TestBootstrapIso(t *testing.T) {
}, },
cfg: testCfg, cfg: testCfg,
debug: false, debug: false,
expectedOut: expOut[0] + expOut[1], expectedOut: []string{expOut[0], expOut[1]},
expectdErr: testErr, expectdErr: testErr,
}, },
{ {
@ -90,7 +92,7 @@ func TestBootstrapIso(t *testing.T) {
}, },
cfg: testCfg, cfg: testCfg,
debug: true, debug: true,
expectedOut: expOut[0] + expOut[1] + expOut[2] + expOut[3], expectedOut: []string{expOut[0], expOut[1], expOut[2], expOut[3]},
expectdErr: nil, expectdErr: nil,
}, },
{ {
@ -101,20 +103,22 @@ func TestBootstrapIso(t *testing.T) {
}, },
cfg: testCfg, cfg: testCfg,
debug: false, debug: false,
expectedOut: expOut[0] + expOut[1] + expOut[2] + expOut[4], expectedOut: []string{expOut[0], expOut[1], expOut[2], expOut[4]},
expectdErr: testErr, expectdErr: testErr,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
actualOut := bytes.NewBufferString("") outBuf := &bytes.Buffer{}
actualErr := generateBootstrapIso(bundle, tt.builder, tt.cfg, actualOut, tt.debug) 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) for _, line := range tt.expectedOut {
assert.Equal(t, actualErr, tt.expectdErr, errS) assert.True(t, strings.Contains(actualOut, line))
}
errS = fmt.Sprintf("generateBootstrapIso should have print %s, got %s", tt.expectedOut, actualOut) assert.Equal(t, tt.expectdErr, actualErr)
assert.Equal(t, actualOut.String(), tt.expectedOut, errS)
} }
} }
@ -172,7 +176,7 @@ func TestVerifyInputs(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
actualErr := verifyInputs(tt.cfg, tt.args, bytes.NewBufferString("")) actualErr := verifyInputs(tt.cfg, tt.args)
assert.Equal(t, tt.expectedErr, actualErr) assert.Equal(t, tt.expectedErr, actualErr)
} }