Use bundle mock for inventory tests

Removed testdata directory for the baremetal tests and use a mock bundle for tests as needed. Any necessary data were added as constants to the test file so that the tests would behave identically.

Relates-To: #464
Relates-To: #465
Change-Id: I38aa8619ba79b958a81323c4ee6cc638e07dcc7b
This commit is contained in:
Herrera, Josh (jh813b) 2021-08-25 14:22:06 -07:00 committed by Josh Herrera
parent bc704b736d
commit d3c07d0b55
3 changed files with 150 additions and 78 deletions

View File

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

View File

@ -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
...

View File

@ -1,2 +0,0 @@
resources:
- hosts.yaml