From f12446afdf4f99960d1d4f120420c7064b5403ae Mon Sep 17 00:00:00 2001 From: "Yasin, Siraj (SY495P)" Date: Sat, 14 Mar 2020 21:02:55 -0500 Subject: [PATCH] Increase test coverage Added test cases: WriteFiles TabWriter NewRedfishRemoteDirectClient Updated: ReadYAMLFile => new test case with invalid yaml file Change-Id: I06c6f2eefd1c1c1659e0bf3c08fbe28628c80725 --- pkg/remote/redfish/redfish_test.go | 43 ++++++++++++++++++++++++++++ pkg/util/configreader_test.go | 5 ++++ pkg/util/tabwriter_test.go | 45 ++++++++++++++++++++++++++++++ pkg/util/testdata/incorrect.yaml | 3 ++ pkg/util/writefiles_test.go | 35 +++++++++++++++++++++++ 5 files changed, 131 insertions(+) create mode 100644 pkg/util/tabwriter_test.go create mode 100644 pkg/util/testdata/incorrect.yaml create mode 100644 pkg/util/writefiles_test.go diff --git a/pkg/remote/redfish/redfish_test.go b/pkg/remote/redfish/redfish_test.go index d3e957ce9..364068765 100644 --- a/pkg/remote/redfish/redfish_test.go +++ b/pkg/remote/redfish/redfish_test.go @@ -240,6 +240,49 @@ func getTestSystem() redfishClient.ComputerSystem { } } +func TestNewRedfishRemoteDirectClient(t *testing.T) { + m := &redfishMocks.RedfishAPI{} + defer m.AssertExpectations(t) + + _, err := NewRedfishRemoteDirectClient( + context.Background(), + defaultURL, + computerSystemID, + "/tmp/test.iso", + ) + assert.NoError(t, err) + + // Test with empty remote URL + _, err = NewRedfishRemoteDirectClient( + context.Background(), + "", + computerSystemID, + "/tmp/test.iso", + ) + expectedError := "missing configuration: redfish remote url empty" + assert.EqualError(t, err, expectedError) + + // Test with empty ephemeral NodeID + _, err = NewRedfishRemoteDirectClient( + context.Background(), + defaultURL, + "", + "/tmp/test.iso", + ) + expectedError = "missing configuration: redfish ephemeral node id empty" + assert.EqualError(t, err, expectedError) + + // Test with empty Iso Path + _, err = NewRedfishRemoteDirectClient( + context.Background(), + defaultURL, + computerSystemID, + "", + ) + expectedError = "missing configuration: redfish ephemeral node iso Path empty" + assert.EqualError(t, err, expectedError) +} + func getDefaultRedfishRemoteDirectObj(t *testing.T, api redfishAPI.RedfishAPI) RemoteDirect { t.Helper() diff --git a/pkg/util/configreader_test.go b/pkg/util/configreader_test.go index 3a2839484..b037fde97 100644 --- a/pkg/util/configreader_test.go +++ b/pkg/util/configreader_test.go @@ -20,4 +20,9 @@ func TestReadYAMLFile(t *testing.T) { actualString := actual["testString"] expectedString := "test" assert.Equal(expectedString, actualString) + + // test using an incorrect yaml + err = util.ReadYAMLFile("testdata/incorrect.yaml", &actual) + expectedString = "error converting YAML to JSON" + require.Contains(err.Error(), expectedString) } diff --git a/pkg/util/tabwriter_test.go b/pkg/util/tabwriter_test.go new file mode 100644 index 000000000..9b788cfc3 --- /dev/null +++ b/pkg/util/tabwriter_test.go @@ -0,0 +1,45 @@ +package util_test + +import ( + "bytes" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + + "opendev.org/airship/airshipctl/pkg/util" +) + +func TestNewTabWriter(t *testing.T) { + var tests = []struct { + testname, src, expected string + }{ + { + "empty-string", + "", + "\n", + }, + { + "newline-test", + "\n", + "\n\n", + }, + { + "format-string", + "airshipctl\tconfig\tinit", + "airshipctl config init\n", + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.testname, func(t *testing.T) { + var buf bytes.Buffer + out := util.NewTabWriter(&buf) + fmt.Fprintln(out, tt.src) + err := out.Flush() + assert.NoError(t, err) + assert.Equal(t, tt.expected, buf.String()) + }) + } +} diff --git a/pkg/util/testdata/incorrect.yaml b/pkg/util/testdata/incorrect.yaml new file mode 100644 index 000000000..23840f08c --- /dev/null +++ b/pkg/util/testdata/incorrect.yaml @@ -0,0 +1,3 @@ +-- +testString: incorrectYamlSyntax +... diff --git a/pkg/util/writefiles_test.go b/pkg/util/writefiles_test.go new file mode 100644 index 000000000..71f6defea --- /dev/null +++ b/pkg/util/writefiles_test.go @@ -0,0 +1,35 @@ +package util_test + +import ( + "io/ioutil" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + + "opendev.org/airship/airshipctl/pkg/util" + "opendev.org/airship/airshipctl/testutil" +) + +func TestWriteFiles(t *testing.T) { + testDir, cleanup := testutil.TempDir(t, "test-dir") + defer cleanup(t) + + fls := make(map[string][]byte) + dummyData := []byte("") + testFile1 := filepath.Join(testDir, "testFile1") + testFile2 := filepath.Join(testDir, "testFile2") + fls[testFile1] = dummyData + fls[testFile2] = dummyData + err := util.WriteFiles(fls, 0600) + + assert.NoError(t, err) + + // check if all files are created + assert.FileExists(t, testFile1) + assert.FileExists(t, testFile2) + + // check if files are readable + _, err = ioutil.ReadFile(testFile1) + assert.NoError(t, err) +}