Skip to content

Commit

Permalink
[testing] Enable config of log format for bootstrap monitor (#3467)
Browse files Browse the repository at this point in the history
  • Loading branch information
marun authored Oct 16, 2024
1 parent 1201f26 commit e5ca053
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
24 changes: 19 additions & 5 deletions tests/fixture/bootstrapmonitor/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/spf13/cobra"
"go.uber.org/zap"

"github.com/ava-labs/avalanchego/tests/fixture/bootstrapmonitor"
"github.com/ava-labs/avalanchego/utils/logging"
Expand All @@ -31,6 +30,7 @@ func main() {
podName string
nodeContainerName string
dataDir string
rawLogFormat string
)
rootCmd := &cobra.Command{
Use: commandName,
Expand All @@ -40,6 +40,7 @@ func main() {
rootCmd.PersistentFlags().StringVar(&podName, "pod-name", os.Getenv("POD_NAME"), "The name of the pod")
rootCmd.PersistentFlags().StringVar(&nodeContainerName, "node-container-name", "", "The name of the node container in the pod")
rootCmd.PersistentFlags().StringVar(&dataDir, "data-dir", "", "The path of the data directory used for the bootstrap job")
rootCmd.PersistentFlags().StringVar(&rawLogFormat, "log-format", "auto", "The structure of log format. Defaults to 'auto' which formats terminal-like logs, when the output is a terminal. Otherwise, should be one of {auto, plain, colors, json}")

versionCmd := &cobra.Command{
Use: "version",
Expand All @@ -55,16 +56,17 @@ func main() {
}
rootCmd.AddCommand(versionCmd)

// Use avalanchego logger for consistency
log := logging.NewLogger("", logging.NewWrappedCore(logging.Verbo, os.Stdout, logging.Plain.ConsoleEncoder()))

initCmd := &cobra.Command{
Use: "init",
Short: "Initialize a new bootstrap test",
RunE: func(*cobra.Command, []string) error {
if err := checkArgs(namespace, podName, nodeContainerName, dataDir); err != nil {
return err
}
log, err := newLogger(rawLogFormat)
if err != nil {
return err
}
return bootstrapmonitor.InitBootstrapTest(log, namespace, podName, nodeContainerName, dataDir)
},
}
Expand All @@ -87,6 +89,10 @@ func main() {
if imageCheckInterval <= 0 {
return errors.New("--image-check-interval must be greater than 0")
}
log, err := newLogger(rawLogFormat)
if err != nil {
return err
}
return bootstrapmonitor.WaitForCompletion(log, namespace, podName, nodeContainerName, dataDir, healthCheckInterval, imageCheckInterval)
},
}
Expand All @@ -95,7 +101,6 @@ func main() {
rootCmd.AddCommand(waitCmd)

if err := rootCmd.Execute(); err != nil {
log.Error(commandName+" failed", zap.Error(err))
os.Exit(1)
}
os.Exit(0)
Expand All @@ -116,3 +121,12 @@ func checkArgs(namespace string, podName string, nodeContainerName string, dataD
}
return nil
}

func newLogger(rawLogFormat string) (logging.Logger, error) {
writeCloser := os.Stdout
logFormat, err := logging.ToFormat(rawLogFormat, writeCloser.Fd())
if err != nil {
return nil, err
}
return logging.NewLogger("", logging.NewWrappedCore(logging.Verbo, writeCloser, logFormat.ConsoleEncoder())), nil
}
2 changes: 2 additions & 0 deletions tests/fixture/bootstrapmonitor/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ func createBootstrapTester(tc tests.TestContext, clientset *kubernetes.Clientset
"init",
"--node-container-name=" + nodeContainerName,
"--data-dir=" + dataDir,
"--log-format=json",
})
initContainer.VolumeMounts = []corev1.VolumeMount{
{
Expand All @@ -481,6 +482,7 @@ func createBootstrapTester(tc tests.TestContext, clientset *kubernetes.Clientset
"--data-dir=" + dataDir,
"--health-check-interval=1s",
"--image-check-interval=1s",
"--log-format=json",
})
monitorContainer.VolumeMounts = []corev1.VolumeMount{
{
Expand Down

0 comments on commit e5ca053

Please sign in to comment.