Increase test coverage

* Few more minor changes to increase test coverage

Change-Id: Id34a33732d3201637027cb3eb512a245acbd77b5
This commit is contained in:
Yasin, Siraj (SY495P) 2020-03-18 16:07:23 -05:00
parent a37a8800cc
commit d630b55235
6 changed files with 162 additions and 11 deletions

View File

@ -46,6 +46,32 @@ const (
testCluster = "my_new-cluster" 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) { func TestSetClusterWithCAFile(t *testing.T) {
given, cleanupGiven := testutil.InitConfig(t) given, cleanupGiven := testutil.InitConfig(t)
defer cleanupGiven(t) defer cleanupGiven(t)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -18,25 +18,30 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"opendev.org/airship/airshipctl/pkg/util" "opendev.org/airship/airshipctl/pkg/util"
) )
func TestReadYAMLFile(t *testing.T) { func TestReadYAMLFile(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
require := require.New(t)
var actual map[string]interface{} var actual map[string]interface{}
err := util.ReadYAMLFile("testdata/test.yaml", &actual)
require.NoError(err, "Error while reading YAML")
actualString := actual["testString"] tests := []struct {
expectedString := "test" testFile string
assert.Equal(expectedString, actualString) isError bool
}{
{"testdata/test.yaml", false},
{"testdata/incorrect.yaml", true},
{"testdata/notfound.yaml", true},
}
// test using an incorrect yaml for _, test := range tests {
err = util.ReadYAMLFile("testdata/incorrect.yaml", &actual) err := util.ReadYAMLFile(test.testFile, &actual)
expectedString = "error converting YAML to JSON" if test.isError {
require.Contains(err.Error(), expectedString) assert.Error(err)
} else {
assert.NoError(err)
}
}
} }

View File

@ -46,4 +46,10 @@ func TestWriteFiles(t *testing.T) {
// check if files are readable // check if files are readable
_, err = ioutil.ReadFile(testFile1) _, err = ioutil.ReadFile(testFile1)
assert.NoError(t, err) 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)
} }