Skip to content

Commit

Permalink
bmh: added list unit tests (#403)
Browse files Browse the repository at this point in the history
Co-authored-by: nkononov <[email protected]>
  • Loading branch information
kononovn and kononovn authored May 8, 2024
1 parent 4c09725 commit 01688e1
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/bmh/baremetalhost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,13 @@ func buildBareMetalHostTestClientWithDummyObject(state ...bmhv1alpha1.Provisioni
})
}

func buildDummyBmHost(state bmhv1alpha1.ProvisioningState) []runtime.Object {
func buildDummyBmHost(
state bmhv1alpha1.ProvisioningState, operationalStatus ...bmhv1alpha1.OperationalStatus) []runtime.Object {
operState := bmhv1alpha1.OperationalStatusOK
if len(operationalStatus) > 0 {
operState = operationalStatus[0]
}

return append([]runtime.Object{}, &bmhv1alpha1.BareMetalHost{
Spec: bmhv1alpha1.BareMetalHostSpec{
BMC: bmhv1alpha1.BMCDetails{
Expand All @@ -1044,7 +1050,7 @@ func buildDummyBmHost(state bmhv1alpha1.ProvisioningState) []runtime.Object {
Namespace: defaultBmHostNsName,
},
Status: bmhv1alpha1.BareMetalHostStatus{
OperationalStatus: bmhv1alpha1.OperationalStatusOK,
OperationalStatus: operState,
PoweredOn: true,
Provisioning: bmhv1alpha1.ProvisionStatus{
State: state,
Expand Down
6 changes: 6 additions & 0 deletions pkg/bmh/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ const (

// List returns bareMetalHosts inventory in the given namespace.
func List(apiClient *clients.Settings, nsname string, options ...goclient.ListOptions) ([]*BmhBuilder, error) {
if apiClient == nil {
glog.V(100).Infof("BareMetalHosts 'apiClient' parameter can not be empty")

return nil, fmt.Errorf("failed to list bareMetalHosts, 'apiClient' parameter is empty")
}

if nsname == "" {
glog.V(100).Infof("bareMetalHost 'nsname' parameter can not be empty")

Expand Down
156 changes: 156 additions & 0 deletions pkg/bmh/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package bmh

import (
"context"
"fmt"
"testing"
"time"

bmhv1alpha1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/labels"
goclient "sigs.k8s.io/controller-runtime/pkg/client"
)

func TestBareMetalHostList(t *testing.T) {
testCases := []struct {
BareMetalHosts []*BmhBuilder
nsName string
listOptions []goclient.ListOptions
expectedError error
client bool
}{
{

BareMetalHosts: []*BmhBuilder{buildValidBmHostBuilder(buildBareMetalHostTestClientWithDummyObject())},
nsName: "test-namespace",
expectedError: nil,
client: true,
},
{
BareMetalHosts: []*BmhBuilder{buildValidBmHostBuilder(buildBareMetalHostTestClientWithDummyObject())},
nsName: "",
expectedError: fmt.Errorf("failed to list bareMetalHosts, 'nsname' parameter is empty"),
client: true,
},
{
BareMetalHosts: []*BmhBuilder{buildValidBmHostBuilder(buildBareMetalHostTestClientWithDummyObject())},
nsName: "test-namespace",
listOptions: []goclient.ListOptions{{LabelSelector: labels.NewSelector()}},
client: true,
expectedError: nil,
},
{
BareMetalHosts: []*BmhBuilder{buildValidBmHostBuilder(buildBareMetalHostTestClientWithDummyObject())},
nsName: "test-namespace",
listOptions: []goclient.ListOptions{{LabelSelector: labels.NewSelector()}, {LabelSelector: labels.NewSelector()}},
expectedError: fmt.Errorf("error: more than one ListOptions was passed"),
client: true,
},
{
BareMetalHosts: []*BmhBuilder{buildValidBmHostBuilder(buildBareMetalHostTestClientWithDummyObject())},
nsName: "test-namespace",
expectedError: fmt.Errorf("failed to list bareMetalHosts, 'apiClient' parameter is empty"),
client: false,
},
}
for _, testCase := range testCases {
var testSettings *clients.Settings

if testCase.client {
testSettings = clients.GetTestClients(clients.TestClientParams{
K8sMockObjects: buildDummyBmHost(bmhv1alpha1.StateProvisioned),
})
}

bmhBuilders, err := List(testSettings, testCase.nsName, testCase.listOptions...)
assert.Equal(t, err, testCase.expectedError)

if testCase.expectedError == nil && len(testCase.listOptions) == 0 {
assert.Equal(t, len(testCase.BareMetalHosts), len(bmhBuilders))
}
}
}

func TestBareMetalWaitForAllBareMetalHostsInGoodOperationalState(t *testing.T) {
testCases := []struct {
BareMetalHosts []*BmhBuilder
nsName string
listOptions []goclient.ListOptions
operationalState bmhv1alpha1.OperationalStatus
expectedError error
client bool
expectedStatus bool
}{
{
BareMetalHosts: []*BmhBuilder{buildValidBmHostBuilder(buildBareMetalHostTestClientWithDummyObject())},
nsName: "test-namespace",
operationalState: bmhv1alpha1.OperationalStatusOK,
listOptions: nil,
expectedError: nil,
expectedStatus: true,
client: true,
},
{
BareMetalHosts: []*BmhBuilder{buildValidBmHostBuilder(buildBareMetalHostTestClientWithDummyObject())},
nsName: "test-namespace",
operationalState: bmhv1alpha1.OperationalStatusDelayed,
expectedError: context.DeadlineExceeded,
listOptions: nil,
expectedStatus: false,
client: true,
},
{
BareMetalHosts: []*BmhBuilder{buildValidBmHostBuilder(buildBareMetalHostTestClientWithDummyObject())},
nsName: "",
operationalState: bmhv1alpha1.OperationalStatusOK,
expectedError: fmt.Errorf("failed to list bareMetalHosts, 'nsname' parameter is empty"),
expectedStatus: false,
listOptions: nil,
client: true,
},
{
BareMetalHosts: []*BmhBuilder{buildValidBmHostBuilder(buildBareMetalHostTestClientWithDummyObject())},
nsName: "test-namespace",
operationalState: bmhv1alpha1.OperationalStatusOK,
expectedError: fmt.Errorf("failed to list bareMetalHosts, 'apiClient' parameter is empty"),
expectedStatus: false,
listOptions: nil,
client: false,
},
{
BareMetalHosts: []*BmhBuilder{buildValidBmHostBuilder(buildBareMetalHostTestClientWithDummyObject())},
nsName: "test-namespace",
operationalState: bmhv1alpha1.OperationalStatusOK,
expectedError: nil,
expectedStatus: true,
listOptions: []goclient.ListOptions{{LabelSelector: labels.NewSelector()}},
client: true,
},
{
BareMetalHosts: []*BmhBuilder{buildValidBmHostBuilder(buildBareMetalHostTestClientWithDummyObject())},
nsName: "test-namespace",
operationalState: bmhv1alpha1.OperationalStatusOK,
expectedError: fmt.Errorf("error: more than one ListOptions was passed"),
expectedStatus: false,
listOptions: []goclient.ListOptions{
{LabelSelector: labels.NewSelector()}, {LabelSelector: labels.NewSelector()}},
client: true,
},
}
for _, testCase := range testCases {
var testSettings *clients.Settings

if testCase.client {
testSettings = clients.GetTestClients(clients.TestClientParams{
K8sMockObjects: buildDummyBmHost(bmhv1alpha1.StateProvisioned, testCase.operationalState),
})
}

status, err := WaitForAllBareMetalHostsInGoodOperationalState(
testSettings, testCase.nsName, 1*time.Second, testCase.listOptions...)
assert.Equal(t, err, testCase.expectedError)
assert.Equal(t, status, testCase.expectedStatus)
}
}

0 comments on commit 01688e1

Please sign in to comment.