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

Tink-cli unit tests #366

Merged
merged 1 commit into from
Dec 3, 2020
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
11 changes: 6 additions & 5 deletions cmd/tink-cli/cmd/docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
)

func Test_docsCmd(t *testing.T) {
subCommand := "docs"
type args struct {
name string
}
Expand All @@ -31,7 +32,7 @@ func Test_docsCmd(t *testing.T) {
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
root.SetArgs([]string{"docs"})
root.SetArgs([]string{subCommand})
if err := root.Execute(); err == nil {
t.Error("expected an error")
}
Expand All @@ -44,7 +45,7 @@ func Test_docsCmd(t *testing.T) {
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
out := &bytes.Buffer{}
root.SetArgs([]string{"docs", "--help"})
root.SetArgs([]string{subCommand, "--help"})
root.SetOutput(out)
if err := root.Execute(); err != nil {
t.Error(err)
Expand All @@ -66,7 +67,7 @@ func Test_docsCmd(t *testing.T) {
defer os.RemoveAll(dir)

root := c.Root()
root.SetArgs([]string{"docs", "markdown", "--path", dir})
root.SetArgs([]string{subCommand, "markdown", "--path", dir})

if err := root.Execute(); err != nil {
t.Error(err)
Expand Down Expand Up @@ -96,7 +97,7 @@ func Test_docsCmd(t *testing.T) {
defer os.RemoveAll(dir)

root := c.Root()
root.SetArgs([]string{"docs", "man", "--path", dir})
root.SetArgs([]string{subCommand, "man", "--path", dir})

if err := root.Execute(); err != nil {
t.Error(err)
Expand All @@ -120,7 +121,7 @@ func Test_docsCmd(t *testing.T) {
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
root.SetArgs([]string{"docs", "invalid"})
root.SetArgs([]string{subCommand, "invalid"})
if err := root.Execute(); err == nil {
t.Error("expected error")
}
Expand Down
167 changes: 167 additions & 0 deletions cmd/tink-cli/cmd/hardware_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package cmd

import (
"bytes"
"strings"
"testing"

"github.com/spf13/cobra"
)

func Test_hardwareCmd(t *testing.T) {
subCommand := "hardware"
type args struct {
name string
}
tests := []struct {
name string
args args
want *cobra.Command
cmdFunc func(*testing.T, *cobra.Command)
}{
{
name: "NoArgs",
args: args{name: testCommand},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
root.SetArgs([]string{subCommand})
if err := root.Execute(); err != nil {
t.Logf("%+v", root.Args)
t.Error("expected an error")
}
},
},
{
name: "ID",
args: args{name: testCommand},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
out := &bytes.Buffer{}
root.SetArgs([]string{subCommand, "id", "-h"})
root.SetOutput(out)
if err := root.Execute(); err != nil {
t.Error(err)
}
if !strings.Contains(out.String(), "get hardware by id") {
t.Error("expected output should include get hardware by id")
}
},
},
{
name: "List",
args: args{name: testCommand},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
out := &bytes.Buffer{}
root.SetArgs([]string{subCommand, "list", "-h"})
root.SetOutput(out)
if err := root.Execute(); err != nil {
t.Error(err)
}
if !strings.Contains(out.String(), "list all known hardware") {
t.Error("expected output should include list all known hardware")
}
},
},
{
name: "IP",
args: args{name: testCommand},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
out := &bytes.Buffer{}
root.SetArgs([]string{subCommand, "ip", "-h"})
root.SetOutput(out)
if err := root.Execute(); err != nil {
t.Error(err)
}
if !strings.Contains(out.String(), "get hardware by any associated ip") {
t.Error("expected output should include get hardware by any associated ip")
}
},
},
{
name: "MAC",
args: args{name: testCommand},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
out := &bytes.Buffer{}
root.SetArgs([]string{subCommand, "mac", "-h"})
root.SetOutput(out)
if err := root.Execute(); err != nil {
t.Error(err)
}
if !strings.Contains(out.String(), "get hardware by any associated mac") {
t.Error("expected output should include get hardware by any associated mac")
}
},
},
{
name: "Delete",
args: args{name: testCommand},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
out := &bytes.Buffer{}
root.SetArgs([]string{subCommand, "delete", "-h"})
root.SetOutput(out)
if err := root.Execute(); err != nil {
t.Error(err)
}
if !strings.Contains(out.String(), "delete hardware by id") {
t.Error("expected output should include delete hardware by id")
}
},
},
{
name: "Watch",
args: args{name: testCommand},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
out := &bytes.Buffer{}
root.SetArgs([]string{subCommand, "watch", "-h"})
root.SetOutput(out)
if err := root.Execute(); err != nil {
t.Error(err)
}
if !strings.Contains(out.String(), "register to watch an id for any changes") {
t.Error("expected output should include register to watch an id for any changes")
}
},
},
{
name: "Push",
args: args{name: testCommand},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
out := &bytes.Buffer{}
root.SetArgs([]string{subCommand, "push", "-h"})
root.SetOutput(out)
if err := root.Execute(); err != nil {
t.Error(err)
}
if !strings.Contains(out.String(), "push new hardware to tink") {
t.Error("expected output should include push new hardware to tink")
}
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rootCmd := &cobra.Command{
Use: testCommand,
Run: func(_ *cobra.Command, _ []string) {},
Version: "test",
}
rootCmd.AddCommand(hardwareCmd)
tt.cmdFunc(t, rootCmd)
})
}
}
133 changes: 133 additions & 0 deletions cmd/tink-cli/cmd/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package cmd

import (
"bytes"
"strings"
"testing"

"github.com/spf13/cobra"
)

func Test_templateCmd(t *testing.T) {
subCommand := "template"
type args struct {
name string
}
tests := []struct {
name string
args args
want *cobra.Command
cmdFunc func(*testing.T, *cobra.Command)
}{
{
name: "NoArgs",
args: args{name: testCommand},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
root.SetArgs([]string{subCommand})
if err := root.Execute(); err != nil {
t.Logf("%+v", root.Args)
t.Error("expected an error")
}
},
},
{
name: "List",
args: args{name: testCommand},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
out := &bytes.Buffer{}
root.SetArgs([]string{subCommand, "list", "-h"})
root.SetOutput(out)
if err := root.Execute(); err != nil {
t.Error(err)
}
if !strings.Contains(out.String(), "list all saved templates") {
t.Error("expected output should include list all saved templates")
}
},
},
{
name: "Create",
args: args{name: testCommand},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
out := &bytes.Buffer{}
root.SetArgs([]string{subCommand, "create", "-h"})
root.SetOutput(out)
if err := root.Execute(); err != nil {
t.Error(err)
}
if !strings.Contains(out.String(), "create a workflow template") {
t.Error("expected output should include create a workflow template")
}
},
},
{
name: "Delete",
args: args{name: testCommand},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
out := &bytes.Buffer{}
root.SetArgs([]string{subCommand, "delete", "-h"})
root.SetOutput(out)
if err := root.Execute(); err != nil {
t.Error(err)
}
if !strings.Contains(out.String(), "delete a template") {
t.Error("expected output should include delete a template")
}
},
},
{
name: "Get",
args: args{name: testCommand},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
out := &bytes.Buffer{}
root.SetArgs([]string{subCommand, "get", "-h"})
root.SetOutput(out)
if err := root.Execute(); err != nil {
t.Error(err)
}
if !strings.Contains(out.String(), "get a template") {
t.Error("expected output should include get a template")
}
},
},
{
name: "Update",
args: args{name: testCommand},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
out := &bytes.Buffer{}
root.SetArgs([]string{subCommand, "update", "-h"})
root.SetOutput(out)
if err := root.Execute(); err != nil {
t.Error(err)
}
if !strings.Contains(out.String(), "update a template") {
t.Error("expected output should include update a template")
}
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rootCmd := &cobra.Command{
Use: testCommand,
Run: func(_ *cobra.Command, _ []string) {},
Version: "test",
}
rootCmd.AddCommand(templateCmd)
tt.cmdFunc(t, rootCmd)
})
}
}
Loading