Skip to content

Commit

Permalink
chore(influx): add test coverage for root influx cmd
Browse files Browse the repository at this point in the history
this verifies things like token/host and all the root lvl flags are being
set appropriately.
  • Loading branch information
jsteenb2 committed Mar 13, 2020
1 parent 4b8a71b commit 5211e55
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 5 deletions.
27 changes: 22 additions & 5 deletions cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ func newHTTPClient() (*httpc.Client, error) {
type (
cobraRunEFn func(cmd *cobra.Command, args []string) error

cobraRuneEMiddleware func(fn cobraRunEFn) cobraRunEFn
cobraRunEMiddleware func(fn cobraRunEFn) cobraRunEFn

genericCLIOptFn func(*genericCLIOpts)
)

type genericCLIOpts struct {
in io.Reader
w io.Writer
runEWrapFn cobraRuneEMiddleware
in io.Reader
w io.Writer
errW io.Writer

runEWrapFn cobraRunEMiddleware
}

func (o genericCLIOpts) newCmd(use string, runE func(*cobra.Command, []string) error) *cobra.Command {
Expand All @@ -76,6 +78,8 @@ func (o genericCLIOpts) newCmd(use string, runE func(*cobra.Command, []string) e
cmd.RunE = o.runEWrapFn(runE)
}
cmd.SetOut(o.w)
cmd.SetIn(o.in)
cmd.SetErr(o.errW)
return cmd
}

Expand All @@ -95,6 +99,18 @@ func out(w io.Writer) genericCLIOptFn {
}
}

func err(w io.Writer) genericCLIOptFn {
return func(o *genericCLIOpts) {
o.errW = w
}
}

func runEMiddlware(mw cobraRunEMiddleware) genericCLIOptFn {
return func(o *genericCLIOpts) {
o.runEWrapFn = mw
}
}

type globalFlags struct {
config.Config
local bool
Expand All @@ -115,6 +131,7 @@ func newInfluxCmdBuilder(optFns ...genericCLIOptFn) *cmdInfluxBuilder {
opt := genericCLIOpts{
in: os.Stdin,
w: os.Stdout,
errW: os.Stderr,
runEWrapFn: checkSetupRunEMiddleware(&flags),
}
for _, optFn := range optFns {
Expand Down Expand Up @@ -298,7 +315,7 @@ func checkSetup(host string, skipVerify bool) error {
return nil
}

func checkSetupRunEMiddleware(f *globalFlags) cobraRuneEMiddleware {
func checkSetupRunEMiddleware(f *globalFlags) cobraRunEMiddleware {
return func(fn cobraRunEFn) cobraRunEFn {
return func(cmd *cobra.Command, args []string) error {
err := fn(cmd, args)
Expand Down
94 changes: 94 additions & 0 deletions cmd/influx/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package main

import (
"bytes"
"io/ioutil"
"testing"

"github.com/influxdata/influxdb/cmd/influx/config"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_influx_cmd(t *testing.T) {
tests := []struct {
name string
args []string
envVars map[string]string
expected globalFlags
}{
{
name: "all full length flags set",
args: []string{"--token=TOKEN", "--host=HOST", "--local=true", "--skip-verify=true"},
envVars: envVarsZeroMap,
expected: globalFlags{
Config: config.Config{
Token: "TOKEN",
Host: "HOST",
},
skipVerify: true,
local: true,
},
},
{
name: "token p flag set",
args: []string{"-t=TOKEN", "--host=HOST", "--local=true", "--skip-verify=true"},
envVars: envVarsZeroMap,
expected: globalFlags{
Config: config.Config{
Token: "TOKEN",
Host: "HOST",
},
skipVerify: true,
local: true,
},
},
{
name: "env vars set",
args: []string{"--local=true", "--skip-verify=true"},
envVars: map[string]string{
"INFLUX_TOKEN": "TOKEN",
"INFLUX_HOST": "HOST",
},
expected: globalFlags{
Config: config.Config{
Token: "TOKEN",
Host: "HOST",
},
skipVerify: true,
local: true,
},
},
}

for _, tt := range tests {
fn := func(t *testing.T) {
defer addEnvVars(t, tt.envVars)()

builder := newInfluxCmdBuilder(
in(new(bytes.Buffer)),
out(ioutil.Discard),
err(ioutil.Discard),
runEMiddlware(func(fn cobraRunEFn) cobraRunEFn { return fn }),
)

flagCapture := new(globalFlags)
influxCmd := builder.cmd(func(f *globalFlags, opt genericCLIOpts) *cobra.Command {
flagCapture = f
return &cobra.Command{Use: "foo"}
})

influxCmd.SetArgs(append([]string{"foo"}, tt.args...))

require.NoError(t, influxCmd.Execute())

assert.Equal(t, tt.expected.Host, flagCapture.Host)
assert.Equal(t, tt.expected.Token, flagCapture.Token)
assert.Equal(t, tt.expected.local, flagCapture.local)
assert.Equal(t, tt.expected.skipVerify, flagCapture.skipVerify)
}

t.Run(tt.name, fn)
}
}

0 comments on commit 5211e55

Please sign in to comment.