Merge "Increase test coverage"

This commit is contained in:
Zuul 2020-03-17 15:01:18 +00:00 committed by Gerrit Code Review
commit 3a6332f4de
5 changed files with 131 additions and 0 deletions

View File

@ -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 { func getDefaultRedfishRemoteDirectObj(t *testing.T, api redfishAPI.RedfishAPI) RemoteDirect {
t.Helper() t.Helper()

View File

@ -20,4 +20,9 @@ func TestReadYAMLFile(t *testing.T) {
actualString := actual["testString"] actualString := actual["testString"]
expectedString := "test" expectedString := "test"
assert.Equal(expectedString, actualString) 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)
} }

View File

@ -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())
})
}
}

3
pkg/util/testdata/incorrect.yaml vendored Normal file
View File

@ -0,0 +1,3 @@
--
testString: incorrectYamlSyntax
...

View File

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