Repository: metal3-io/baremetal-operator
Pull request #1684 (2024-04-18 13:33:07)

🌱 Remove redundant Provisioner from HostFirmwareSettings tests
We already have a test provisioner, one more is unnecessary.

Signed-off-by: Dmitry Tantsur <dtantsur@protonmail.com>


Patch:
diff --git a/pkg/provisioner/fixture/fixture.go b/pkg/provisioner/fixture/fixture.go
index 6b004a31..d0bf7ed7 100644
--- a/pkg/provisioner/fixture/fixture.go
+++ b/pkg/provisioner/fixture/fixture.go
@@ -58,6 +58,11 @@ type fixtureProvisioner struct {
 	state *Fixture
 }
 
+type HostFirmwareSettingsMock struct {
+	Settings metal3api.SettingsMap
+	Schema   map[string]metal3api.SettingSchema
+}
+
 // Fixture contains persistent state for a particular host.
 type Fixture struct {
 	// counter to set the provisioner as ready
@@ -74,6 +79,8 @@ type Fixture struct {
 	validateError string
 
 	customDeploy *metal3api.CustomDeploy
+
+	HostFirmwareSettings HostFirmwareSettingsMock
 }
 
 // NewProvisioner returns a new Fixture Provisioner.
@@ -339,7 +346,7 @@ func (p *fixtureProvisioner) TryInit() (result bool, err error) {
 
 func (p *fixtureProvisioner) GetFirmwareSettings(_ bool) (settings metal3api.SettingsMap, schema map[string]metal3api.SettingSchema, err error) {
 	p.log.Info("getting BIOS settings")
-	return
+	return p.state.HostFirmwareSettings.Settings, p.state.HostFirmwareSettings.Schema, nil
 }
 
 func (p *fixtureProvisioner) AddBMCEventSubscriptionForNode(_ *metal3api.BMCEventSubscription, _ provisioner.HTTPHeaders) (result provisioner.Result, err error) {


Test patch:
diff --git a/controllers/metal3.io/hostfirmwaresettings_test.go b/controllers/metal3.io/hostfirmwaresettings_test.go
index 828df70e..ef54c416 100644
--- a/controllers/metal3.io/hostfirmwaresettings_test.go
+++ b/controllers/metal3.io/hostfirmwaresettings_test.go
@@ -7,7 +7,9 @@ import (
 	"github.com/stretchr/testify/assert"
 
 	metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
+	"github.com/metal3-io/baremetal-operator/pkg/hardwareutils/bmc"
 	"github.com/metal3-io/baremetal-operator/pkg/provisioner"
+	"github.com/metal3-io/baremetal-operator/pkg/provisioner/fixture"
 
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/util/intstr"
@@ -46,82 +48,16 @@ func getTestHFSReconciler(host *metal3api.HostFirmwareSettings) *HostFirmwareSet
 	return reconciler
 }
 
-func getMockProvisioner(settings metal3api.SettingsMap, schema map[string]metal3api.SettingSchema) *hsfMockProvisioner {
-	return &hsfMockProvisioner{
-		Settings: settings,
-		Schema:   schema,
-		Error:    nil,
+func getMockProvisioner(host *metal3api.BareMetalHost, settings metal3api.SettingsMap, schema map[string]metal3api.SettingSchema) provisioner.Provisioner {
+	state := fixture.Fixture{
+		HostFirmwareSettings: fixture.HostFirmwareSettingsMock{
+			Settings: settings,
+			Schema:   schema,
+		},
 	}
-}
-
-type hsfMockProvisioner struct {
-	Settings metal3api.SettingsMap
-	Schema   map[string]metal3api.SettingSchema
-	Error    error
-}
-
-func (m *hsfMockProvisioner) HasCapacity() (result bool, err error) {
-	return
-}
-
-func (m *hsfMockProvisioner) ValidateManagementAccess(_ provisioner.ManagementAccessData, _, _ bool) (result provisioner.Result, provID string, err error) {
-	return
-}
-
-func (m *hsfMockProvisioner) InspectHardware(_ provisioner.InspectData, _, _ bool) (result provisioner.Result, started bool, details *metal3api.HardwareDetails, err error) {
-	return
-}
-
-func (m *hsfMockProvisioner) UpdateHardwareState() (hwState provisioner.HardwareState, err error) {
-	return
-}
-
-func (m *hsfMockProvisioner) Prepare(_ provisioner.PrepareData, _ bool, _ bool) (result provisioner.Result, started bool, err error) {
-	return
-}
-
-func (m *hsfMockProvisioner) Adopt(_ provisioner.AdoptData, _ bool) (result provisioner.Result, err error) {
-	return
-}
-
-func (m *hsfMockProvisioner) Provision(_ provisioner.ProvisionData, _ bool) (result provisioner.Result, err error) {
-	return
-}
-
-func (m *hsfMockProvisioner) Deprovision(_ bool) (result provisioner.Result, err error) {
-	return
-}
-
-func (m *hsfMockProvisioner) Delete() (result provisioner.Result, err error) {
-	return
-}
-
-func (m *hsfMockProvisioner) Detach() (result provisioner.Result, err error) {
-	return
-}
-
-func (m *hsfMockProvisioner) PowerOn(_ bool) (result provisioner.Result, err error) {
-	return
-}
-
-func (m *hsfMockProvisioner) PowerOff(_ metal3api.RebootMode, _ bool) (result provisioner.Result, err error) {
-	return
-}
-
-func (m *hsfMockProvisioner) IsReady() (result bool, err error) {
-	return
-}
-
-func (m *hsfMockProvisioner) GetFirmwareSettings(_ bool) (settings metal3api.SettingsMap, schema map[string]metal3api.SettingSchema, err error) {
-	return m.Settings, m.Schema, m.Error
-}
-
-func (m *hsfMockProvisioner) AddBMCEventSubscriptionForNode(_ *metal3api.BMCEventSubscription, _ provisioner.HTTPHeaders) (result provisioner.Result, err error) {
-	return result, nil
-}
-
-func (m *hsfMockProvisioner) RemoveBMCEventSubscriptionForNode(_ metal3api.BMCEventSubscription) (result provisioner.Result, err error) {
-	return result, nil
+	p, _ := state.NewProvisioner(context.TODO(), provisioner.BuildHostData(*host, bmc.Credentials{}),
+		func(reason, message string) {})
+	return p
 }
 
 func getSchema() *metal3api.FirmwareSchema {
@@ -544,7 +480,6 @@ func TestStoreHostFirmwareSettings(t *testing.T) {
 	for _, tc := range testCases {
 		t.Run(tc.Scenario, func(t *testing.T) {
 			ctx := context.TODO()
-			prov := getMockProvisioner(getCurrentSettings(), getCurrentSchemaSettings())
 
 			tc.ExpectedSettings.TypeMeta = metav1.TypeMeta{
 				Kind:       "HostFirmwareSettings",
@@ -558,6 +493,7 @@ func TestStoreHostFirmwareSettings(t *testing.T) {
 			r := getTestHFSReconciler(hfs)
 			// Create bmh resource needed by hfs reconciler
 			bmh := createBaremetalHost()
+			prov := getMockProvisioner(bmh, getCurrentSettings(), getCurrentSchemaSettings())
 
 			info := &rInfo{
 				log: logf.Log.WithName("controllers").WithName("HostFirmwareSettings"),