Added unit test for dochelper, config

* added more negative test cases to dochelper, config to
  increase the test coverage

Change-Id: I3179823f9196157e72009ff998cfb8dd348fee1f
This commit is contained in:
Sirajudeen 2020-07-14 14:55:48 -05:00
parent 37c044a5b7
commit 35d119d3d1
8 changed files with 131 additions and 4 deletions

View File

@ -279,6 +279,27 @@ func TestCurrentContextBootstrapInfo(t *testing.T) {
assert.Equal(t, conf.BootstrapInfo[defaultString], bootstrapInfo) assert.Equal(t, conf.BootstrapInfo[defaultString], bootstrapInfo)
} }
func TestCurrentContextManagementConfig(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
clusterName := "def"
clusterType := "ephemeral"
managementConfig, err := conf.CurrentContextManagementConfig()
require.Error(t, err)
assert.Nil(t, managementConfig)
conf.CurrentContext = currentContextName
conf.Clusters[clusterName].ClusterTypes[clusterType].ManagementConfiguration = defaultString
conf.Contexts[currentContextName].Manifest = defaultString
conf.Contexts[currentContextName].KubeContext().Cluster = clusterName
managementConfig, err = conf.CurrentContextManagementConfig()
require.NoError(t, err)
assert.Equal(t, conf.ManagementConfiguration[defaultString], managementConfig)
}
func TestPurge(t *testing.T) { func TestPurge(t *testing.T) {
conf, cleanup := testutil.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
@ -840,3 +861,27 @@ func TestGetManifests(t *testing.T) {
assert.EqualValues(t, manifests[0].PrimaryRepositoryName, "primary") assert.EqualValues(t, manifests[0].PrimaryRepositoryName, "primary")
} }
func TestModifyManifests(t *testing.T) {
conf, cleanup := testutil.InitConfig(t)
defer cleanup(t)
mo := testutil.DummyManifestOptions()
manifest := conf.AddManifest(mo)
require.NotNil(t, manifest)
mo.TargetPath += stringDelta
err := conf.ModifyManifest(manifest, mo)
require.NoError(t, err)
mo.CommitHash = "11ded0"
mo.Tag = "v1.0"
err = conf.ModifyManifest(manifest, mo)
require.Error(t, err, "Checkout mutually exclusive, use either: commit-hash, branch or tag")
// error scenario
mo.RepoName = "invalid"
mo.URL = ""
err = conf.ModifyManifest(manifest, mo)
require.Error(t, err)
}

View File

@ -28,7 +28,7 @@ func TestDocHelpers(t *testing.T) {
require := require.New(t) require := require.New(t)
assert := assert.New(t) assert := assert.New(t)
fSys := testutil.SetupTestFs(t, "testdata/dochelper") fSys := testutil.SetupTestFs(t, "testdata/dochelper/valid/")
bundle, err := document.NewBundle(fSys, "/") bundle, err := document.NewBundle(fSys, "/")
require.NoError(err, "Building Bundle Failed") require.NoError(err, "Building Bundle Failed")
require.NotNil(bundle) require.NotNil(bundle)
@ -41,7 +41,7 @@ func TestDocHelpers(t *testing.T) {
networkData, err := document.GetBMHNetworkData(doc, bundle) networkData, err := document.GetBMHNetworkData(doc, bundle)
require.NoError(err, "Unexpected error trying to GetBMHNetworkData") require.NoError(err, "Unexpected error trying to GetBMHNetworkData")
assert.Equal(networkData, "some network data") assert.Equal(networkData, "some network data\n")
}) })
t.Run("GetBMHBMCAddress", func(t *testing.T) { t.Run("GetBMHBMCAddress", func(t *testing.T) {
@ -67,3 +67,41 @@ func TestDocHelpers(t *testing.T) {
assert.Equal(bmcPassword, "password") assert.Equal(bmcPassword, "password")
}) })
} }
func TestDocHelpersNegativeCases(t *testing.T) {
require := require.New(t)
fSys := testutil.SetupTestFs(t, "testdata/dochelper/invalid/")
bundle, err := document.NewBundle(fSys, "/")
require.NoError(err, "Building Bundle Failed")
require.NotNil(bundle)
t.Run("GetBMHNetworkData", func(t *testing.T) {
selector := document.NewSelector().ByKind("BareMetalHost")
doc, err := bundle.SelectOne(selector)
require.NoError(err)
_, err = document.GetBMHNetworkData(doc, bundle)
require.Error(err)
})
t.Run("GetBMHBMCAddress", func(t *testing.T) {
// retrieve our single bmh in the dataset
selector := document.NewSelector().ByKind("BareMetalHost")
doc, err := bundle.SelectOne(selector)
require.NoError(err)
_, err = document.GetBMHBMCAddress(doc)
require.Error(err)
})
t.Run("GetBMHBMCCredentials", func(t *testing.T) {
// retrieve our single bmh in the dataset
selector := document.NewSelector().ByKind("BareMetalHost")
doc, err := bundle.SelectOne(selector)
require.NoError(err)
_, _, err = document.GetBMHBMCCredentials(doc, bundle)
require.Error(err)
})
}

View File

@ -0,0 +1,15 @@
---
apiVersion: metal3.io/v1alpha1
kind: BareMetalHost
metadata:
labels:
airshipit.org/ephemeral-node: "true"
name: master-0
spec:
online: true
bootMACAddress: 00:3b:8b:0c:ec:8b
bmc:
credentialsName: master-0-bmc
networkData:
name: master-0-networkdata
namespace: metal3

View File

@ -0,0 +1,3 @@
resources:
- baremetalhost.yaml
- secret.yaml

View File

@ -0,0 +1,26 @@
apiVersion: v1
kind: Secret
metadata:
labels:
airshipit.org/ephemeral-user-data: "true"
name: ephemeral-user-data
type: Opaque
stringData:
userData: cloud-init
---
apiVersion: v1
kind: Secret
metadata:
name: master-0-bmc-invalid
namespace: metal3
type: Opaque
stringData:
username: username
password: password
---
apiVersion: v1
kind: Secret
metadata:
name: master-0-networkdata
namespace: metal3
type: Opaque

View File

@ -24,5 +24,5 @@ metadata:
name: master-0-networkdata name: master-0-networkdata
namespace: metal3 namespace: metal3
type: Opaque type: Opaque
stringData: data:
networkData: some network data networkData: c29tZSBuZXR3b3JrIGRhdGEK