From 35d119d3d1cfbfa4d59c309ad2301b6049678042 Mon Sep 17 00:00:00 2001 From: Sirajudeen Date: Tue, 14 Jul 2020 14:55:48 -0500 Subject: [PATCH] Added unit test for dochelper, config * added more negative test cases to dochelper, config to increase the test coverage Change-Id: I3179823f9196157e72009ff998cfb8dd348fee1f --- pkg/config/config_test.go | 45 +++++++++++++++++++ pkg/document/dochelper_test.go | 42 ++++++++++++++++- .../dochelper/invalid/baremetalhost.yaml | 15 +++++++ .../dochelper/invalid/kustomization.yaml | 3 ++ .../testdata/dochelper/invalid/secret.yaml | 26 +++++++++++ .../dochelper/{ => valid}/baremetalhost.yaml | 0 .../dochelper/{ => valid}/kustomization.yaml | 0 .../dochelper/{ => valid}/secret.yaml | 4 +- 8 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 pkg/document/testdata/dochelper/invalid/baremetalhost.yaml create mode 100644 pkg/document/testdata/dochelper/invalid/kustomization.yaml create mode 100644 pkg/document/testdata/dochelper/invalid/secret.yaml rename pkg/document/testdata/dochelper/{ => valid}/baremetalhost.yaml (100%) rename pkg/document/testdata/dochelper/{ => valid}/kustomization.yaml (100%) rename pkg/document/testdata/dochelper/{ => valid}/secret.yaml (90%) diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index e32da7744..fbd251958 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -279,6 +279,27 @@ func TestCurrentContextBootstrapInfo(t *testing.T) { 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) { conf, cleanup := testutil.InitConfig(t) defer cleanup(t) @@ -840,3 +861,27 @@ func TestGetManifests(t *testing.T) { 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) +} diff --git a/pkg/document/dochelper_test.go b/pkg/document/dochelper_test.go index fbed3277f..e03844894 100644 --- a/pkg/document/dochelper_test.go +++ b/pkg/document/dochelper_test.go @@ -28,7 +28,7 @@ func TestDocHelpers(t *testing.T) { require := require.New(t) assert := assert.New(t) - fSys := testutil.SetupTestFs(t, "testdata/dochelper") + fSys := testutil.SetupTestFs(t, "testdata/dochelper/valid/") bundle, err := document.NewBundle(fSys, "/") require.NoError(err, "Building Bundle Failed") require.NotNil(bundle) @@ -41,7 +41,7 @@ func TestDocHelpers(t *testing.T) { networkData, err := document.GetBMHNetworkData(doc, bundle) 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) { @@ -67,3 +67,41 @@ func TestDocHelpers(t *testing.T) { 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) + }) +} diff --git a/pkg/document/testdata/dochelper/invalid/baremetalhost.yaml b/pkg/document/testdata/dochelper/invalid/baremetalhost.yaml new file mode 100644 index 000000000..17af0c21b --- /dev/null +++ b/pkg/document/testdata/dochelper/invalid/baremetalhost.yaml @@ -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 diff --git a/pkg/document/testdata/dochelper/invalid/kustomization.yaml b/pkg/document/testdata/dochelper/invalid/kustomization.yaml new file mode 100644 index 000000000..fe18b33ef --- /dev/null +++ b/pkg/document/testdata/dochelper/invalid/kustomization.yaml @@ -0,0 +1,3 @@ +resources: + - baremetalhost.yaml + - secret.yaml diff --git a/pkg/document/testdata/dochelper/invalid/secret.yaml b/pkg/document/testdata/dochelper/invalid/secret.yaml new file mode 100644 index 000000000..b81f2628e --- /dev/null +++ b/pkg/document/testdata/dochelper/invalid/secret.yaml @@ -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 diff --git a/pkg/document/testdata/dochelper/baremetalhost.yaml b/pkg/document/testdata/dochelper/valid/baremetalhost.yaml similarity index 100% rename from pkg/document/testdata/dochelper/baremetalhost.yaml rename to pkg/document/testdata/dochelper/valid/baremetalhost.yaml diff --git a/pkg/document/testdata/dochelper/kustomization.yaml b/pkg/document/testdata/dochelper/valid/kustomization.yaml similarity index 100% rename from pkg/document/testdata/dochelper/kustomization.yaml rename to pkg/document/testdata/dochelper/valid/kustomization.yaml diff --git a/pkg/document/testdata/dochelper/secret.yaml b/pkg/document/testdata/dochelper/valid/secret.yaml similarity index 90% rename from pkg/document/testdata/dochelper/secret.yaml rename to pkg/document/testdata/dochelper/valid/secret.yaml index 4bafed861..a218442ec 100644 --- a/pkg/document/testdata/dochelper/secret.yaml +++ b/pkg/document/testdata/dochelper/valid/secret.yaml @@ -24,5 +24,5 @@ metadata: name: master-0-networkdata namespace: metal3 type: Opaque -stringData: - networkData: some network data \ No newline at end of file +data: + networkData: c29tZSBuZXR3b3JrIGRhdGEK