Merge "Increase test coverage"
This commit is contained in:
commit
3a6332f4de
@ -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()
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
45
pkg/util/tabwriter_test.go
Normal file
45
pkg/util/tabwriter_test.go
Normal 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
3
pkg/util/testdata/incorrect.yaml
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
--
|
||||
testString: incorrectYamlSyntax
|
||||
...
|
35
pkg/util/writefiles_test.go
Normal file
35
pkg/util/writefiles_test.go
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user