Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize logging with default info #648

Merged
merged 3 commits into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions ctc_lib/container_tool_command_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/GoogleCloudPlatform/runtimes-common/ctc_lib/flags"
"github.com/GoogleCloudPlatform/runtimes-common/ctc_lib/logging"
"github.com/GoogleCloudPlatform/runtimes-common/ctc_lib/types"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -50,11 +51,25 @@ func (ctb *ContainerToolCommandBase) toolName() string {
}

func (ctb *ContainerToolCommandBase) Init() {
cobra.OnInitialize(initConfig, ctb.initLogging)
// Init Logging with info level with colors disabled since initLogging gets called
// only after arguments are parsed correctly.
Log = logging.NewLogger(
viper.GetString(config.LogDirConfigKey),
ctb.Name(),
log.InfoLevel,
false,
)
cobra.OnInitialize(initConfig, ctb.initLogging, ctb.SetSilenceUsage)
ctb.AddFlags()
ctb.AddSubCommands()
}

func (ctb *ContainerToolCommandBase) SetSilenceUsage() {
// Do not display usage when using RunE after args are parsed.
// See https://github.com/spf13/cobra/issues/340 for more information.
ctb.SilenceUsage = true
}

func (ctb *ContainerToolCommandBase) initLogging() {
Log = logging.NewLogger(
viper.GetString(config.LogDirConfigKey),
Expand All @@ -76,10 +91,6 @@ func (ctb *ContainerToolCommandBase) AddSubCommands() {

// Set up Root Command
ctb.Command.SetHelpTemplate(HelpTemplate)

// Donot display usage when using RunE.
// See https://github.com/spf13/cobra/issues/340 for more information.
ctb.SilenceUsage = true
}

func (ctb *ContainerToolCommandBase) AddCommand(command CLIInterface) {
Expand Down
58 changes: 58 additions & 0 deletions ctc_lib/container_tool_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"os/exec"
"reflect"
"strings"
"testing"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -263,3 +264,60 @@ func TestContainerToolCommandOutputInJson(t *testing.T) {
t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expectedObj, actualObj)
}
}

func TestContainerToolCommandLogNotNull(t *testing.T) {
defer SetExitOnError(true)
testCommand := ContainerToolCommand{
ContainerToolCommandBase: &ContainerToolCommandBase{
Command: &cobra.Command{
Use: "loggingError",
Short: "Logging not nil",
},
},
Output: "",
RunO: func(command *cobra.Command, args []string) (interface{}, error) {
return nil, nil
},
}
SetExitOnError(false)
var OutputBuffer bytes.Buffer
testCommand.Command.SetOutput(&OutputBuffer)
testCommand.SetArgs([]string{"--name=Sparks"})
err := ExecuteE(&testCommand)
expectedError := ("unknown flag: --name")
if Log == nil {
t.Errorf("Expected Log to be not nil. Got nil")
}
if !strings.Contains(OutputBuffer.String(), "Usage:") {
t.Error("Expected to contain Usage. However Usage is not displayed")
}
if err.Error() != expectedError {
t.Errorf("Expected Error: \n %q \nGot:\n %q\n", expectedError, err.Error())
}
}

func TestContainerToolCommandDoesNotDisplayUsage(t *testing.T) {
defer SetExitOnError(true)
testCommand := ContainerToolCommand{
ContainerToolCommandBase: &ContainerToolCommandBase{
Command: &cobra.Command{
Use: "fail Command",
},
},
Output: "",
RunO: func(command *cobra.Command, args []string) (interface{}, error) {
return nil, errors.New("Command failed")
},
}
SetExitOnError(false)
var OutputBuffer bytes.Buffer
testCommand.Command.SetOutput(&OutputBuffer)
err := ExecuteE(&testCommand)
expectedError := ("Command failed")
if strings.Contains(OutputBuffer.String(), "Usage:") {
t.Error("Expected to not display usage when Command Fails. However Usage is displayed")
}
if err.Error() != expectedError {
t.Errorf("Expected Error: \n %q \nGot:\n %q\n", expectedError, err.Error())
}
}