diff --git a/pkg/inventory/baremetal/baremetal_test.go b/pkg/inventory/baremetal/baremetal_test.go index 9f3f83643..cf354effa 100644 --- a/pkg/inventory/baremetal/baremetal_test.go +++ b/pkg/inventory/baremetal/baremetal_test.go @@ -16,16 +16,120 @@ package baremetal import ( "context" + "errors" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/document" "opendev.org/airship/airshipctl/pkg/inventory/ifc" + testdoc "opendev.org/airship/airshipctl/testutil/document" ) +const ( + kind = "BareMetalHost" + bmhMaster0 = `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: + address: redfish+http://nolocalhost:32201/redfish/v1/Systems/ephemeral + credentialsName: master-0-bmc-secret +` + master0BmcSec = `apiVersion: v1 +kind: Secret +metadata: + labels: + name: master-0-bmc-secret +type: Opaque +data: + username: YWRtaW4= + password: cGFzc3dvcmQ= +` + bmhMaster1 = `apiVersion: metal3.io/v1alpha1 +kind: BareMetalHost +metadata: + labels: + host-group: "control-plane" + name: master-1 +spec: + online: true + bootMACAddress: 00:3b:8b:0c:ec:8b + bmc: + address: redfish+http://nolocalhost:8888/redfish/v1/Systems/node-master-1 + credentialsName: master-1-bmc-secret +` + bmhMaster2 = `apiVersion: metal3.io/v1alpha1 +kind: BareMetalHost +metadata: + labels: + host-group: "control-plane" + name: master-2 +spec: + online: true + bootMACAddress: 00:3b:8b:0c:ec:8b + bmc: + address: redfish+http://nolocalhost:8888/redfish/v1/Systems/node-master-2 + credentialsName: master-1-bmc-secret +` + bmhNoCreds = `apiVersion: v1 +kind: Secret +metadata: + labels: + airshipit.org/ephemeral-node: "true" + name: master-1-bmc-secret +type: Opaque +data: + username: YWRtaW4= + password: cGFzc3dvcmQ= +` +) + +func getMaster0Docs(t *testing.T) []document.Document { + docCfgs := []string{ + bmhMaster0, + } + return buildTestDocs(t, docCfgs) +} + +func getControlPlaneDocs(t *testing.T) []document.Document { + docCfgs := []string{ + bmhMaster1, + bmhMaster2, + } + return buildTestDocs(t, docCfgs) +} + +func getNoCredsDocs(t *testing.T) []document.Document { + docCfgs := []string{ + bmhNoCreds, + } + return buildTestDocs(t, docCfgs) +} + +func getNoSuchHostDocs(t *testing.T) []document.Document { + docCfgs := []string{} + return buildTestDocs(t, docCfgs) +} + +func buildTestDocs(t *testing.T, docCfgs []string) []document.Document { + allDocs := make([]document.Document, len(docCfgs)) + for i, cfg := range docCfgs { + doc, err := document.NewDocumentFromBytes([]byte(cfg)) + require.NoError(t, err) + allDocs[i] = doc + } + return allDocs +} + func TestSelect(t *testing.T) { tests := []struct { name, remoteDriver, expectedErr string @@ -65,7 +169,7 @@ func TestSelect(t *testing.T) { }, } - bundle := testBundle(t) + bundle := testSelectBundle(t) for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { @@ -102,7 +206,7 @@ func TestSelectOne(t *testing.T) { }, } - bundle := testBundle(t) + bundle := testSelectOneBundle(t) for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { @@ -150,7 +254,7 @@ func TestRunAction(t *testing.T) { }, } - bundle := testBundle(t) + bundle := testSelectBundle(t) for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { @@ -215,9 +319,50 @@ func TestAction(t *testing.T) { } } -func testBundle(t *testing.T) document.Bundle { +func testSelectBundle(t *testing.T) document.Bundle { t.Helper() - bundle, err := document.NewBundleByPath("testdata") + bundle := &testdoc.MockBundle{} + secDoc, err := document.NewDocumentFromBytes([]byte(master0BmcSec)) require.NoError(t, err) + + bundle.On("SelectOne", mock.Anything). + Return(secDoc, nil) + bundle.On("Select", mock.MatchedBy(func(selector document.Selector) bool { + return selector.Kind == kind && selector.Name == "master-0" + })).Return(getMaster0Docs(t), nil) + bundle.On("Select", mock.MatchedBy(func(selector document.Selector) bool { + return selector.Kind == kind && selector.LabelSelector == "host-group=control-plane" + })).Return(getControlPlaneDocs(t), nil) + bundle.On("Select", mock.MatchedBy(func(selector document.Selector) bool { + return selector.Kind == kind && selector.Name == "no-creds" + })).Return(getNoCredsDocs(t), nil) + bundle.On("Select", mock.MatchedBy(func(selector document.Selector) bool { + return selector.Kind == kind && selector.Name == "no such host" + })).Return(getNoSuchHostDocs(t), nil) + bundle.On("Select", mock.MatchedBy(func(selector document.Selector) bool { + return selector.Kind == kind && selector.Name == "does not exist" + })).Return(getNoSuchHostDocs(t), nil) + + return bundle +} + +func testSelectOneBundle(t *testing.T) document.Bundle { + t.Helper() + bundle := &testdoc.MockBundle{} + secDoc, err := document.NewDocumentFromBytes([]byte(master0BmcSec)) + require.NoError(t, err) + bmhMaster0Doc, err := document.NewDocumentFromBytes([]byte(bmhMaster0)) + require.NoError(t, err) + + bundle.On("SelectOne", mock.MatchedBy(func(selector document.Selector) bool { + return selector.Kind == kind && selector.Name == "master-0" + })).Return(bmhMaster0Doc, nil) + bundle.On("SelectOne", mock.MatchedBy(func(selector document.Selector) bool { + return selector.Kind == "Secret" && selector.Name == "master-0-bmc-secret" + })).Return(secDoc, nil) + bundle.On("SelectOne", mock.MatchedBy(func(selector document.Selector) bool { + return selector.Kind == kind && selector.LabelSelector == "host-group=control-plane" + })).Return(nil, errors.New("found more than one document")) + return bundle } diff --git a/pkg/inventory/baremetal/testdata/hosts.yaml b/pkg/inventory/baremetal/testdata/hosts.yaml deleted file mode 100644 index 3f01ddbdf..000000000 --- a/pkg/inventory/baremetal/testdata/hosts.yaml +++ /dev/null @@ -1,71 +0,0 @@ ---- -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: - address: redfish+http://nolocalhost:32201/redfish/v1/Systems/ephemeral - credentialsName: master-0-bmc-secret ---- -apiVersion: v1 -kind: Secret -metadata: - labels: - name: master-0-bmc-secret -type: Opaque -data: - username: YWRtaW4= - password: cGFzc3dvcmQ= ---- -apiVersion: metal3.io/v1alpha1 -kind: BareMetalHost -metadata: - labels: - host-group: "control-plane" - name: master-1 -spec: - online: true - bootMACAddress: 00:3b:8b:0c:ec:8b - bmc: - address: redfish+http://nolocalhost:8888/redfish/v1/Systems/node-master-1 - credentialsName: master-1-bmc-secret ---- -apiVersion: metal3.io/v1alpha1 -kind: BareMetalHost -metadata: - labels: - host-group: "control-plane" - name: master-2 -spec: - online: true - bootMACAddress: 00:3b:8b:0c:ec:8b - bmc: - address: redfish+http://nolocalhost:8888/redfish/v1/Systems/node-master-2 - credentialsName: master-1-bmc-secret ---- -apiVersion: v1 -kind: Secret -metadata: - labels: - airshipit.org/ephemeral-node: "true" - name: master-1-bmc-secret -type: Opaque -data: - username: YWRtaW4= - password: cGFzc3dvcmQ= ---- -apiVersion: metal3.io/v1alpha1 -kind: BareMetalHost -metadata: - name: no-creds -spec: - online: true - bootMACAddress: 00:3b:8b:0c:ec:8b - bmc: - address: redfish+http://nolocalhost:8888/redfish/v1/Systems/test-node -... diff --git a/pkg/inventory/baremetal/testdata/kustomization.yaml b/pkg/inventory/baremetal/testdata/kustomization.yaml deleted file mode 100644 index 70de5c65b..000000000 --- a/pkg/inventory/baremetal/testdata/kustomization.yaml +++ /dev/null @@ -1,2 +0,0 @@ -resources: - - hosts.yaml