e2e/cli: port logging to use hclog
This commit is contained in:
parent
bba732b2c3
commit
77018c6774
|
@ -0,0 +1,22 @@
|
|||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
"github.com/mitchellh/cli"
|
||||
)
|
||||
|
||||
type Meta struct {
|
||||
Ui cli.Ui
|
||||
logger hclog.Logger
|
||||
|
||||
verbose bool
|
||||
}
|
||||
|
||||
func (m *Meta) FlagSet(n string) *flag.FlagSet {
|
||||
f := flag.NewFlagSet(n, flag.ContinueOnError)
|
||||
|
||||
f.BoolVar(&m.verbose, "v", false, "Toggle verbose output")
|
||||
return f
|
||||
}
|
|
@ -4,11 +4,9 @@ import (
|
|||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
|
@ -16,6 +14,7 @@ import (
|
|||
"strings"
|
||||
|
||||
getter "github.com/hashicorp/go-getter"
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
"github.com/hashicorp/nomad/helper/discover"
|
||||
"github.com/mitchellh/cli"
|
||||
)
|
||||
|
@ -24,11 +23,18 @@ func init() {
|
|||
getter.Getters["file"].(*getter.FileGetter).Copy = true
|
||||
}
|
||||
|
||||
func ProvisionCommandFactory() (cli.Command, error) {
|
||||
return &Provision{}, nil
|
||||
func ProvisionCommandFactory(ui cli.Ui, logger hclog.Logger) cli.CommandFactory {
|
||||
return func() (cli.Command, error) {
|
||||
meta := Meta{
|
||||
Ui: ui,
|
||||
logger: logger,
|
||||
}
|
||||
return &Provision{Meta: meta}, nil
|
||||
}
|
||||
}
|
||||
|
||||
type Provision struct {
|
||||
Meta
|
||||
}
|
||||
|
||||
func (c *Provision) Help() string {
|
||||
|
@ -75,33 +81,38 @@ func (c *Provision) Run(args []string) int {
|
|||
var nomadBinary string
|
||||
var destroy bool
|
||||
var tfPath string
|
||||
cmdFlags := flag.NewFlagSet("provision", flag.ContinueOnError)
|
||||
cmdFlags.Usage = func() { log.Println(c.Help()) }
|
||||
cmdFlags := c.FlagSet("provision")
|
||||
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
|
||||
cmdFlags.StringVar(&envPath, "env-path", "./environments/", "Path to e2e environment terraform configs")
|
||||
cmdFlags.StringVar(&nomadBinary, "nomad-binary", "", "")
|
||||
cmdFlags.BoolVar(&destroy, "destroy", false, "")
|
||||
cmdFlags.StringVar(&tfPath, "tf-path", "", "")
|
||||
|
||||
if err := cmdFlags.Parse(args); err != nil {
|
||||
log.Fatalf("failed to parse flags: %v", err)
|
||||
c.logger.Error("failed to parse flags:", "error", err)
|
||||
return 1
|
||||
}
|
||||
if c.verbose {
|
||||
c.logger.SetLevel(hclog.Debug)
|
||||
}
|
||||
|
||||
args = cmdFlags.Args()
|
||||
if len(args) != 2 {
|
||||
log.Fatalf("expected 2 args (provider and environment), but got: %v", args)
|
||||
c.logger.Error("expected 2 args (provider and environment)", "args", args)
|
||||
}
|
||||
|
||||
env, err := newEnv(envPath, args[0], args[1], tfPath)
|
||||
env, err := newEnv(envPath, args[0], args[1], tfPath, c.logger)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
c.logger.Error("failed to build environment", "error", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
if destroy {
|
||||
if err := env.destroy(); err != nil {
|
||||
log.Fatal(err)
|
||||
c.logger.Error("failed to destroy environment", "error", err)
|
||||
return 1
|
||||
}
|
||||
log.Println("Environment successfully destroyed")
|
||||
c.logger.Debug("environment successfully destroyed")
|
||||
return 0
|
||||
}
|
||||
|
||||
|
@ -111,7 +122,8 @@ func (c *Provision) Run(args []string) int {
|
|||
|
||||
results, err := env.provision(nomadPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
c.logger.Error("", "error", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
fmt.Printf(strings.TrimSpace(`
|
||||
|
@ -149,6 +161,7 @@ type environment struct {
|
|||
tf string
|
||||
tfPath string
|
||||
tfState string
|
||||
logger hclog.Logger
|
||||
}
|
||||
|
||||
type envResults struct {
|
||||
|
@ -157,16 +170,18 @@ type envResults struct {
|
|||
vaultAddr string
|
||||
}
|
||||
|
||||
func newEnv(envPath, provider, name, tfStatePath string) (*environment, error) {
|
||||
func newEnv(envPath, provider, name, tfStatePath string, logger hclog.Logger) (*environment, error) {
|
||||
// Make sure terraform is on the PATH
|
||||
tf, err := exec.LookPath("terraform")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to lookup terraform binary: %v", err)
|
||||
}
|
||||
|
||||
logger = logger.Named("provision").With("provider", provider, "name", name)
|
||||
|
||||
// set the path to the terraform module
|
||||
tfPath := path.Join(envPath, provider, name)
|
||||
log.Printf("[DEBUG] provision: using tf path %s", tfPath)
|
||||
logger.Debug("using tf path", "path", tfPath)
|
||||
if _, err := os.Stat(tfPath); os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("failed to lookup terraform configuration dir %s: %v", tfPath, err)
|
||||
}
|
||||
|
@ -181,13 +196,14 @@ func newEnv(envPath, provider, name, tfStatePath string) (*environment, error) {
|
|||
tf: tf,
|
||||
tfPath: tfPath,
|
||||
tfState: tfState,
|
||||
logger: logger,
|
||||
}
|
||||
return env, nil
|
||||
}
|
||||
|
||||
// envsFromGlob allows for the discovery of multiple environments using globs (*).
|
||||
// ex. aws/* for all environments in aws.
|
||||
func envsFromGlob(envPath, glob, tfStatePath string) ([]*environment, error) {
|
||||
func envsFromGlob(envPath, glob, tfStatePath string, logger hclog.Logger) ([]*environment, error) {
|
||||
results, err := filepath.Glob(filepath.Join(envPath, glob))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -199,7 +215,7 @@ func envsFromGlob(envPath, glob, tfStatePath string) ([]*environment, error) {
|
|||
elems := strings.Split(p, "/")
|
||||
name := elems[len(elems)-1]
|
||||
provider := elems[len(elems)-2]
|
||||
env, err := newEnv(envPath, provider, name, tfStatePath)
|
||||
env, err := newEnv(envPath, provider, name, tfStatePath, logger)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -234,8 +250,8 @@ func (env *environment) provision(nomadPath string) (*envResults, error) {
|
|||
|
||||
// Run 'terraform apply'
|
||||
cmd.Start()
|
||||
go tfLog("tf.stderr", stderr)
|
||||
go tfLog("tf.stdout", stdout)
|
||||
go tfLog(env.logger.Named("tf.stderr"), stderr)
|
||||
go tfLog(env.logger.Named("tf.stdout"), stdout)
|
||||
|
||||
err = cmd.Wait()
|
||||
if err != nil {
|
||||
|
@ -285,8 +301,8 @@ func (env *environment) destroy() error {
|
|||
|
||||
// Run 'terraform destroy'
|
||||
cmd.Start()
|
||||
go tfLog("tf.stderr", stderr)
|
||||
go tfLog("tf.stdout", stdout)
|
||||
go tfLog(env.logger.Named("tf.stderr"), stderr)
|
||||
go tfLog(env.logger.Named("tf.stdout"), stdout)
|
||||
|
||||
err = cmd.Wait()
|
||||
if err != nil {
|
||||
|
@ -296,14 +312,14 @@ func (env *environment) destroy() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func tfLog(prefix string, r io.ReadCloser) {
|
||||
func tfLog(logger hclog.Logger, r io.ReadCloser) {
|
||||
defer r.Close()
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
log.Printf("[DEBUG] provision.%s: %s", prefix, scanner.Text())
|
||||
logger.Debug(scanner.Text())
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Printf("[WARN] provision.%s: %v", err)
|
||||
logger.Error("scan error", "error", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,24 +1,29 @@
|
|||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
capi "github.com/hashicorp/consul/api"
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
vapi "github.com/hashicorp/vault/api"
|
||||
"github.com/mitchellh/cli"
|
||||
)
|
||||
|
||||
func RunCommandFactory() (cli.Command, error) {
|
||||
return &Run{}, nil
|
||||
func RunCommandFactory(ui cli.Ui, logger hclog.Logger) cli.CommandFactory {
|
||||
return func() (cli.Command, error) {
|
||||
meta := Meta{
|
||||
Ui: ui,
|
||||
logger: logger,
|
||||
}
|
||||
return &Run{Meta: meta}, nil
|
||||
}
|
||||
}
|
||||
|
||||
type Run struct {
|
||||
Meta
|
||||
}
|
||||
|
||||
func (c *Run) Help() string {
|
||||
|
@ -38,40 +43,43 @@ func (c *Run) Run(args []string) int {
|
|||
var tfPath string
|
||||
var slow bool
|
||||
var run string
|
||||
var verbose bool
|
||||
cmdFlags := flag.NewFlagSet("run", flag.ContinueOnError)
|
||||
cmdFlags.Usage = func() { log.Println(c.Help()) }
|
||||
cmdFlags := c.FlagSet("run")
|
||||
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
|
||||
cmdFlags.StringVar(&envPath, "env-path", "./environments/", "Path to e2e environment terraform configs")
|
||||
cmdFlags.StringVar(&nomadBinary, "nomad-binary", "", "")
|
||||
cmdFlags.StringVar(&tfPath, "tf-path", "", "")
|
||||
cmdFlags.StringVar(&run, "run", "", "Regex to target specific test suites/cases")
|
||||
cmdFlags.BoolVar(&slow, "slow", false, "Toggle slow running suites")
|
||||
cmdFlags.BoolVar(&verbose, "v", false, "Toggle verbose output")
|
||||
|
||||
if err := cmdFlags.Parse(args); err != nil {
|
||||
log.Fatalf("failed to parse flags: %v", err)
|
||||
c.logger.Error("failed to parse flags", "error", err)
|
||||
return 1
|
||||
}
|
||||
if c.verbose {
|
||||
c.logger.SetLevel(hclog.Debug)
|
||||
}
|
||||
|
||||
args = cmdFlags.Args()
|
||||
|
||||
if len(args) == 0 {
|
||||
log.Println("No environments specified, running test suite locally...")
|
||||
c.logger.Info("no environments specified, running test suite locally")
|
||||
var report *TestReport
|
||||
var err error
|
||||
if report, err = c.run(&runOpts{
|
||||
slow: slow,
|
||||
verbose: verbose,
|
||||
verbose: c.verbose,
|
||||
}); err != nil {
|
||||
log.Fatalf("failed to run test suite: %v", err)
|
||||
c.logger.Error("failed to run test suite", "error", err)
|
||||
return 1
|
||||
}
|
||||
if report.TotalFailedTests == 0 {
|
||||
log.Println("PASSED!")
|
||||
if verbose {
|
||||
log.Println(report.Summary())
|
||||
c.Ui.Output("PASSED!")
|
||||
if c.verbose {
|
||||
c.Ui.Output(report.Summary())
|
||||
}
|
||||
} else {
|
||||
log.Println("***FAILED***")
|
||||
log.Println(report.Summary())
|
||||
c.Ui.Output("***FAILED***")
|
||||
c.Ui.Output(report.Summary())
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
@ -79,11 +87,13 @@ func (c *Run) Run(args []string) int {
|
|||
environments := []*environment{}
|
||||
for _, e := range args {
|
||||
if len(strings.Split(e, "/")) != 2 {
|
||||
log.Fatalf("argument %s should be formated as <provider>/<environment>", e)
|
||||
c.logger.Error("argument should be formated as <provider>/<environment>", "args", e)
|
||||
return 1
|
||||
}
|
||||
envs, err := envsFromGlob(envPath, e, tfPath)
|
||||
envs, err := envsFromGlob(envPath, e, tfPath, c.logger)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to build environment %s: %v", e, err)
|
||||
c.logger.Error("failed to build environment", "environment", e, "error", err)
|
||||
return 1
|
||||
}
|
||||
environments = append(environments, envs...)
|
||||
|
||||
|
@ -93,22 +103,25 @@ func (c *Run) Run(args []string) int {
|
|||
nomadPath, err := fetchBinary(nomadBinary)
|
||||
defer os.RemoveAll(nomadPath)
|
||||
if err != nil {
|
||||
log.Fatal("failed to fetch nomad binary: %v", err)
|
||||
c.logger.Error("failed to fetch nomad binary", "error", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
log.Printf("Running tests against %d environments...", envCount)
|
||||
c.logger.Debug("starting tests", "totalEnvironments", envCount)
|
||||
for i, env := range environments {
|
||||
log.Printf("[%d/%d] provisioning %s environment on %s provider", i+1, envCount, env.name, env.provider)
|
||||
logger := c.logger.With("name", env.name, "provider", env.provider)
|
||||
logger.Debug("provisioning environment")
|
||||
results, err := env.provision(nomadPath)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to provision environment %s/%s: %v", env.provider, env.name, err)
|
||||
logger.Error("failed to provision environment", "error", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
opts := &runOpts{
|
||||
provider: env.provider,
|
||||
env: env.name,
|
||||
slow: slow,
|
||||
verbose: verbose,
|
||||
verbose: c.verbose,
|
||||
nomadAddr: results.nomadAddr,
|
||||
consulAddr: results.consulAddr,
|
||||
vaultAddr: results.vaultAddr,
|
||||
|
@ -116,17 +129,18 @@ func (c *Run) Run(args []string) int {
|
|||
|
||||
var report *TestReport
|
||||
if report, err = c.run(opts); err != nil {
|
||||
log.Printf("failed to run tests against environment %s/%s: %v", env.provider, env.name, err)
|
||||
logger.Error("failed to run tests against environment", "error", err)
|
||||
return 1
|
||||
}
|
||||
if report.TotalFailedTests == 0 {
|
||||
log.Printf("[%d/%d] %s/%s: PASSED!\n", i, envCount, env.provider, env.name)
|
||||
if verbose {
|
||||
log.Printf("[%d/%d] %s/%s: %s", i, envCount, env.provider, env.name, report.Summary())
|
||||
|
||||
c.Ui.Output(fmt.Sprintf("[%d/%d] %s/%s: PASSED!\n", i+1, envCount, env.provider, env.name))
|
||||
if c.verbose {
|
||||
c.Ui.Output(fmt.Sprintf("[%d/%d] %s/%s: %s", i+1, envCount, env.provider, env.name, report.Summary()))
|
||||
}
|
||||
} else {
|
||||
log.Printf("[%d/%d] %s/%s: ***FAILED***\n", i, envCount, env.provider, env.name)
|
||||
log.Printf("[%d/%d] %s/%s: %s", i, envCount, env.provider, env.name, report.Summary())
|
||||
c.Ui.Output(fmt.Sprintf("[%d/%d] %s/%s: ***FAILED***\n", i+1, envCount, env.provider, env.name))
|
||||
c.Ui.Output(fmt.Sprintf("[%d/%d] %s/%s: %s", i+1, envCount, env.provider, env.name, report.Summary()))
|
||||
}
|
||||
}
|
||||
return 0
|
||||
|
@ -151,13 +165,8 @@ func (c *Run) run(opts *runOpts) (*TestReport, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
var logger io.Writer
|
||||
if opts.verbose {
|
||||
logger = os.Stdout
|
||||
}
|
||||
|
||||
dec := NewDecoder(out)
|
||||
report, err := dec.Decode(logger)
|
||||
report, err := dec.Decode(c.logger.Named("run.gotest"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/fatih/color"
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
)
|
||||
|
||||
type EventDecoder struct {
|
||||
|
@ -79,7 +80,7 @@ func NewDecoder(r io.Reader) *EventDecoder {
|
|||
}
|
||||
}
|
||||
|
||||
func (d *EventDecoder) Decode(logger io.Writer) (*TestReport, error) {
|
||||
func (d *EventDecoder) Decode(logger hclog.Logger) (*TestReport, error) {
|
||||
for d.dec.More() {
|
||||
var e TestEvent
|
||||
err := d.dec.Decode(&e)
|
||||
|
@ -88,8 +89,8 @@ func (d *EventDecoder) Decode(logger io.Writer) (*TestReport, error) {
|
|||
}
|
||||
|
||||
d.report.record(&e)
|
||||
if logger != nil {
|
||||
logger.Write([]byte(e.Output))
|
||||
if logger != nil && e.Output != "" {
|
||||
logger.Debug(strings.TrimRight(e.Output, "\n"))
|
||||
}
|
||||
}
|
||||
return d.report, nil
|
||||
|
|
|
@ -1,26 +1,36 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
"github.com/hashicorp/nomad/e2e/cli/command"
|
||||
"github.com/mitchellh/cli"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.SetPrefix("@@@ ==> ")
|
||||
|
||||
ui := &cli.BasicUi{
|
||||
Reader: os.Stdin,
|
||||
Writer: os.Stdout,
|
||||
ErrorWriter: os.Stderr,
|
||||
}
|
||||
|
||||
logger := hclog.New(&hclog.LoggerOptions{
|
||||
Name: "nomad-e2e",
|
||||
Output: &cli.UiWriter{ui},
|
||||
})
|
||||
|
||||
c := cli.NewCLI("nomad-e2e", "0.0.1")
|
||||
c.Args = os.Args[1:]
|
||||
c.Commands = map[string]cli.CommandFactory{
|
||||
"provision": command.ProvisionCommandFactory,
|
||||
"run": command.RunCommandFactory,
|
||||
"provision": command.ProvisionCommandFactory(ui, logger),
|
||||
"run": command.RunCommandFactory(ui, logger),
|
||||
}
|
||||
|
||||
exitStatus, err := c.Run()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
logger.Error("command exited with non-zero status", "status", exitStatus, "error", err)
|
||||
}
|
||||
os.Exit(exitStatus)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue