From d630b5523595edada36e5fb58a44e16ad14d2b22 Mon Sep 17 00:00:00 2001 From: "Yasin, Siraj (SY495P)" Date: Wed, 18 Mar 2020 16:07:23 -0500 Subject: [PATCH] Increase test coverage * Few more minor changes to increase test coverage Change-Id: Id34a33732d3201637027cb3eb512a245acbd77b5 --- cmd/config/set_cluster_test.go | 26 ++++++++++++ ...onfig-cmd-set-cluster-too-many-args.golden | 37 +++++++++++++++++ .../config-cmd-set-cluster-with-help.golden | 40 +++++++++++++++++++ ...onfig-cmd-set-cluster-with-no-flags.golden | 37 +++++++++++++++++ pkg/util/configreader_test.go | 27 ++++++++----- pkg/util/writefiles_test.go | 6 +++ 6 files changed, 162 insertions(+), 11 deletions(-) create mode 100644 cmd/config/testdata/TestConfigSetClusterGoldenOutput/config-cmd-set-cluster-too-many-args.golden create mode 100644 cmd/config/testdata/TestConfigSetClusterGoldenOutput/config-cmd-set-cluster-with-help.golden create mode 100644 cmd/config/testdata/TestConfigSetClusterGoldenOutput/config-cmd-set-cluster-with-no-flags.golden diff --git a/cmd/config/set_cluster_test.go b/cmd/config/set_cluster_test.go index 34a61b5e4..f112efc47 100644 --- a/cmd/config/set_cluster_test.go +++ b/cmd/config/set_cluster_test.go @@ -46,6 +46,32 @@ const ( testCluster = "my_new-cluster" ) +func TestConfigSetCluster(t *testing.T) { + cmdTests := []*testutil.CmdTest{ + { + Name: "config-cmd-set-cluster-with-help", + CmdLine: "--help", + Cmd: cmd.NewSetClusterCommand(nil), + }, + { + Name: "config-cmd-set-cluster-with-no-flags", + CmdLine: "", + Cmd: cmd.NewSetClusterCommand(nil), + Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 0), + }, + { + Name: "config-cmd-set-cluster-too-many-args", + CmdLine: "arg1 arg2", + Cmd: cmd.NewSetClusterCommand(nil), + Error: fmt.Errorf("accepts at most %d arg(s), received %d", 1, 2), + }, + } + + for _, tt := range cmdTests { + testutil.RunTest(t, tt) + } +} + func TestSetClusterWithCAFile(t *testing.T) { given, cleanupGiven := testutil.InitConfig(t) defer cleanupGiven(t) diff --git a/cmd/config/testdata/TestConfigSetClusterGoldenOutput/config-cmd-set-cluster-too-many-args.golden b/cmd/config/testdata/TestConfigSetClusterGoldenOutput/config-cmd-set-cluster-too-many-args.golden new file mode 100644 index 000000000..d851aeac9 --- /dev/null +++ b/cmd/config/testdata/TestConfigSetClusterGoldenOutput/config-cmd-set-cluster-too-many-args.golden @@ -0,0 +1,37 @@ +Error: accepts 1 arg(s), received 2 +Usage: + set-cluster NAME [flags] + +Examples: + +# Set the server field on the ephemeral exampleCluster +airshipctl config set-cluster exampleCluster \ + --cluster-type=ephemeral \ + --server=https://1.2.3.4 + +# Embed certificate authority data for the target exampleCluster +airshipctl config set-cluster exampleCluster \ + --cluster-type=target \ + --client-certificate-authority=$HOME/.airship/ca/kubernetes.ca.crt \ + --embed-certs + +# Disable certificate checking for the target exampleCluster +airshipctl config set-cluster exampleCluster + --cluster-type=target \ + --insecure-skip-tls-verify + +# Configure client certificate for the target exampleCluster +airshipctl config set-cluster exampleCluster \ + --cluster-type=target \ + --embed-certs \ + --client-certificate=$HOME/.airship/cert_file + + +Flags: + --certificate-authority string path to a certificate authority + --cluster-type string the type of the cluster to add or modify + --embed-certs if set, embed the client certificate/key into the cluster + -h, --help help for set-cluster + --insecure-skip-tls-verify if set, disable certificate checking (default true) + --server string server to use for the cluster + diff --git a/cmd/config/testdata/TestConfigSetClusterGoldenOutput/config-cmd-set-cluster-with-help.golden b/cmd/config/testdata/TestConfigSetClusterGoldenOutput/config-cmd-set-cluster-with-help.golden new file mode 100644 index 000000000..150a66e4b --- /dev/null +++ b/cmd/config/testdata/TestConfigSetClusterGoldenOutput/config-cmd-set-cluster-with-help.golden @@ -0,0 +1,40 @@ +Create or modify a cluster in the airshipctl config files. + +Since a cluster can be either "ephemeral" or "target", you must specify +cluster-type when managing clusters. + +Usage: + set-cluster NAME [flags] + +Examples: + +# Set the server field on the ephemeral exampleCluster +airshipctl config set-cluster exampleCluster \ + --cluster-type=ephemeral \ + --server=https://1.2.3.4 + +# Embed certificate authority data for the target exampleCluster +airshipctl config set-cluster exampleCluster \ + --cluster-type=target \ + --client-certificate-authority=$HOME/.airship/ca/kubernetes.ca.crt \ + --embed-certs + +# Disable certificate checking for the target exampleCluster +airshipctl config set-cluster exampleCluster + --cluster-type=target \ + --insecure-skip-tls-verify + +# Configure client certificate for the target exampleCluster +airshipctl config set-cluster exampleCluster \ + --cluster-type=target \ + --embed-certs \ + --client-certificate=$HOME/.airship/cert_file + + +Flags: + --certificate-authority string path to a certificate authority + --cluster-type string the type of the cluster to add or modify + --embed-certs if set, embed the client certificate/key into the cluster + -h, --help help for set-cluster + --insecure-skip-tls-verify if set, disable certificate checking (default true) + --server string server to use for the cluster diff --git a/cmd/config/testdata/TestConfigSetClusterGoldenOutput/config-cmd-set-cluster-with-no-flags.golden b/cmd/config/testdata/TestConfigSetClusterGoldenOutput/config-cmd-set-cluster-with-no-flags.golden new file mode 100644 index 000000000..fac64e838 --- /dev/null +++ b/cmd/config/testdata/TestConfigSetClusterGoldenOutput/config-cmd-set-cluster-with-no-flags.golden @@ -0,0 +1,37 @@ +Error: accepts 1 arg(s), received 0 +Usage: + set-cluster NAME [flags] + +Examples: + +# Set the server field on the ephemeral exampleCluster +airshipctl config set-cluster exampleCluster \ + --cluster-type=ephemeral \ + --server=https://1.2.3.4 + +# Embed certificate authority data for the target exampleCluster +airshipctl config set-cluster exampleCluster \ + --cluster-type=target \ + --client-certificate-authority=$HOME/.airship/ca/kubernetes.ca.crt \ + --embed-certs + +# Disable certificate checking for the target exampleCluster +airshipctl config set-cluster exampleCluster + --cluster-type=target \ + --insecure-skip-tls-verify + +# Configure client certificate for the target exampleCluster +airshipctl config set-cluster exampleCluster \ + --cluster-type=target \ + --embed-certs \ + --client-certificate=$HOME/.airship/cert_file + + +Flags: + --certificate-authority string path to a certificate authority + --cluster-type string the type of the cluster to add or modify + --embed-certs if set, embed the client certificate/key into the cluster + -h, --help help for set-cluster + --insecure-skip-tls-verify if set, disable certificate checking (default true) + --server string server to use for the cluster + diff --git a/pkg/util/configreader_test.go b/pkg/util/configreader_test.go index ba79b6054..565ba50ec 100644 --- a/pkg/util/configreader_test.go +++ b/pkg/util/configreader_test.go @@ -18,25 +18,30 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "opendev.org/airship/airshipctl/pkg/util" ) func TestReadYAMLFile(t *testing.T) { assert := assert.New(t) - require := require.New(t) var actual map[string]interface{} - err := util.ReadYAMLFile("testdata/test.yaml", &actual) - require.NoError(err, "Error while reading YAML") - actualString := actual["testString"] - expectedString := "test" - assert.Equal(expectedString, actualString) + tests := []struct { + testFile string + isError bool + }{ + {"testdata/test.yaml", false}, + {"testdata/incorrect.yaml", true}, + {"testdata/notfound.yaml", true}, + } - // test using an incorrect yaml - err = util.ReadYAMLFile("testdata/incorrect.yaml", &actual) - expectedString = "error converting YAML to JSON" - require.Contains(err.Error(), expectedString) + for _, test := range tests { + err := util.ReadYAMLFile(test.testFile, &actual) + if test.isError { + assert.Error(err) + } else { + assert.NoError(err) + } + } } diff --git a/pkg/util/writefiles_test.go b/pkg/util/writefiles_test.go index 514ebfb80..64f154eba 100644 --- a/pkg/util/writefiles_test.go +++ b/pkg/util/writefiles_test.go @@ -46,4 +46,10 @@ func TestWriteFiles(t *testing.T) { // check if files are readable _, err = ioutil.ReadFile(testFile1) assert.NoError(t, err) + + // test to fail WriteFiles with NonExistent Dir Path + testFile3 := filepath.Join("NonExistentDir", "testFile3") + fls[testFile3] = dummyData + err = util.WriteFiles(fls, 0600) + assert.Error(t, err) }