Skip to content

Commit 2b5784d

Browse files
Merge pull request #1615 from NeerajNagure/NeerajNagure/unit-test-coverage-improvement-#1574
🌱Added unit tests for baremetalHostTypes and bmceventSubscriptionController
2 parents b9c00f0 + e8fa5d9 commit 2b5784d

File tree

2 files changed

+298
-0
lines changed

2 files changed

+298
-0
lines changed

apis/metal3.io/v1alpha1/baremetalhost_types_test.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,159 @@ func TestBootMode(t *testing.T) {
510510
})
511511
}
512512
}
513+
514+
func TestSetOperationalStatus(t *testing.T) {
515+
for _, tc := range []struct {
516+
Scenario string
517+
Host BareMetalHost
518+
StatusArg OperationalStatus
519+
Expected bool
520+
}{
521+
{
522+
Scenario: "Host is partially configured and operationalStatus is ok",
523+
Host: BareMetalHost{
524+
Status: BareMetalHostStatus{
525+
OperationalStatus: "OK",
526+
},
527+
},
528+
StatusArg: "discovered",
529+
Expected: true,
530+
},
531+
{
532+
Scenario: "Host is detached and operationalStatus is also detached",
533+
Host: BareMetalHost{
534+
Status: BareMetalHostStatus{
535+
OperationalStatus: "detached",
536+
},
537+
},
538+
StatusArg: "detached",
539+
Expected: false,
540+
},
541+
{
542+
Scenario: "Host has an error and operationalStatus also has an error",
543+
Host: BareMetalHost{
544+
Status: BareMetalHostStatus{
545+
OperationalStatus: "error",
546+
},
547+
},
548+
StatusArg: "error",
549+
Expected: false,
550+
},
551+
{
552+
Scenario: "Host is delayed and operationalStatus is not defined",
553+
Host: BareMetalHost{
554+
Status: BareMetalHostStatus{},
555+
},
556+
StatusArg: "delayed",
557+
Expected: true,
558+
},
559+
} {
560+
t.Run(tc.Scenario, func(t *testing.T) {
561+
actual := tc.Host.SetOperationalStatus(tc.StatusArg)
562+
if tc.Expected && !actual {
563+
t.Error("Expected a change in operational status")
564+
}
565+
if !tc.Expected && actual {
566+
t.Error("Did not expect a change in operational status")
567+
}
568+
})
569+
}
570+
}
571+
572+
func TestSetHardwareProfile(t *testing.T) {
573+
for _, tc := range []struct {
574+
Scenario string
575+
Host BareMetalHost
576+
name string
577+
Expected bool
578+
}{
579+
{
580+
Scenario: "Host hardware profile and name is same",
581+
Host: BareMetalHost{
582+
Status: BareMetalHostStatus{
583+
HardwareProfile: "test",
584+
},
585+
},
586+
name: "test",
587+
Expected: false,
588+
},
589+
{
590+
Scenario: "Host hardware profile and name are different",
591+
Host: BareMetalHost{
592+
Status: BareMetalHostStatus{
593+
HardwareProfile: "test",
594+
},
595+
},
596+
name: "newtest",
597+
Expected: true,
598+
},
599+
{
600+
Scenario: "Host hardware profile is not defined",
601+
Host: BareMetalHost{
602+
Status: BareMetalHostStatus{},
603+
},
604+
name: "test",
605+
Expected: true,
606+
},
607+
} {
608+
t.Run(tc.Scenario, func(t *testing.T) {
609+
actual := tc.Host.SetHardwareProfile(tc.name)
610+
if tc.Expected && !actual {
611+
t.Error("Expected a change in hardware profile")
612+
}
613+
if !tc.Expected && actual {
614+
t.Error("Did not expect a change in hardware profile")
615+
}
616+
})
617+
}
618+
}
619+
620+
func TestHasBMCDetails(t *testing.T) {
621+
for _, tc := range []struct {
622+
Scenario string
623+
Host BareMetalHost
624+
Expected bool
625+
}{
626+
{
627+
Scenario: "Only host bmc address is set",
628+
Host: BareMetalHost{
629+
Spec: BareMetalHostSpec{
630+
BMC: BMCDetails{
631+
Address: "http://127.0.0.1/",
632+
},
633+
},
634+
},
635+
Expected: true,
636+
},
637+
{
638+
Scenario: "Only host credentialsName is set",
639+
Host: BareMetalHost{
640+
Spec: BareMetalHostSpec{
641+
BMC: BMCDetails{
642+
CredentialsName: "test",
643+
},
644+
},
645+
},
646+
Expected: true,
647+
},
648+
{
649+
Scenario: "Neither host credentialsName nor address is set",
650+
Host: BareMetalHost{
651+
Spec: BareMetalHostSpec{
652+
BMC: BMCDetails{},
653+
},
654+
},
655+
Expected: false,
656+
},
657+
} {
658+
t.Run(tc.Scenario, func(t *testing.T) {
659+
actual := tc.Host.HasBMCDetails()
660+
if tc.Expected && !actual {
661+
t.Error("Expected BMC details exist")
662+
}
663+
if !tc.Expected && actual {
664+
t.Error("Did not expect that BMC details exist")
665+
}
666+
})
667+
}
668+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package controllers
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
corev1 "k8s.io/api/core/v1"
8+
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/apimachinery/pkg/runtime"
11+
"k8s.io/apimachinery/pkg/types"
12+
13+
ctrl "sigs.k8s.io/controller-runtime"
14+
"sigs.k8s.io/controller-runtime/pkg/client"
15+
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
16+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
17+
18+
metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
19+
"github.com/metal3-io/baremetal-operator/pkg/provisioner/fixture"
20+
"github.com/metal3-io/baremetal-operator/pkg/utils"
21+
)
22+
23+
func newBMCTestReconcilerWithFixture(fix *fixture.Fixture, initObjs ...runtime.Object) *BMCEventSubscriptionReconciler {
24+
clientBuilder := fakeclient.NewClientBuilder().WithRuntimeObjects(initObjs...)
25+
for _, v := range initObjs {
26+
clientBuilder = clientBuilder.WithStatusSubresource(v.(client.Object))
27+
}
28+
c := clientBuilder.Build()
29+
// Add a default secret that can be used by most subscriptions.
30+
bmcSecret := newBMCCredsSecret(defaultSecretName, "User", "Pass")
31+
c.Create(context.TODO(), bmcSecret)
32+
33+
return &BMCEventSubscriptionReconciler{
34+
Client: c,
35+
ProvisionerFactory: fix,
36+
Log: ctrl.Log.WithName("controllers").WithName("BMCEventSubscription"),
37+
APIReader: c,
38+
}
39+
}
40+
41+
type BMCDoneFunc func(subscription *metal3api.BMCEventSubscription, result reconcile.Result) bool
42+
43+
func newBMCTestReconciler(initObjs ...runtime.Object) *BMCEventSubscriptionReconciler {
44+
fix := fixture.Fixture{}
45+
return newBMCTestReconcilerWithFixture(&fix, initObjs...)
46+
}
47+
48+
func newBMCRequest(subscription *metal3api.BMCEventSubscription) ctrl.Request {
49+
namespacedName := types.NamespacedName{
50+
Namespace: subscription.ObjectMeta.Namespace,
51+
Name: subscription.ObjectMeta.Name,
52+
}
53+
return ctrl.Request{NamespacedName: namespacedName}
54+
}
55+
56+
func newSubscription(name string, spec *metal3api.BMCEventSubscriptionSpec) *metal3api.BMCEventSubscription {
57+
return &metal3api.BMCEventSubscription{
58+
TypeMeta: metav1.TypeMeta{
59+
Kind: "BareMetalHost",
60+
APIVersion: "metal3.io/v1alpha1",
61+
}, ObjectMeta: metav1.ObjectMeta{
62+
Name: name,
63+
Namespace: namespace,
64+
},
65+
Spec: *spec,
66+
}
67+
}
68+
69+
func newDefaultNamedSubscription(t *testing.T, name string) *metal3api.BMCEventSubscription {
70+
t.Helper()
71+
spec := &metal3api.BMCEventSubscriptionSpec{
72+
HostName: t.Name(),
73+
Destination: "user destination",
74+
Context: "user context",
75+
HTTPHeadersRef: &corev1.SecretReference{
76+
Name: defaultSecretName,
77+
Namespace: namespace,
78+
},
79+
}
80+
t.Logf("newNamedSubscription(%s)", name)
81+
subscription := newSubscription(name, spec)
82+
return subscription
83+
}
84+
85+
func newDefaultSubscription(t *testing.T) *metal3api.BMCEventSubscription {
86+
t.Helper()
87+
return newDefaultNamedSubscription(t, t.Name())
88+
}
89+
90+
func HostWithProvisioningID(t *testing.T, host *metal3api.BareMetalHost) *metal3api.BareMetalHost {
91+
t.Helper()
92+
host.Status.Provisioning.ID = "made-up-id"
93+
return host
94+
}
95+
96+
func TestBMCAddFinalizers(t *testing.T) {
97+
host := newDefaultHost(t)
98+
subscription := newDefaultSubscription(t)
99+
r := newBMCTestReconciler(subscription, host)
100+
err := r.addFinalizer(context.Background(), subscription)
101+
if err != nil {
102+
t.Error(err)
103+
}
104+
t.Logf("subscription finalizers: %v", subscription.Finalizers)
105+
if !utils.StringInList(subscription.Finalizers, metal3api.BMCEventSubscriptionFinalizer) {
106+
t.Error("Expected finalizers to be added")
107+
}
108+
}
109+
110+
func TestBMCGetProvisioner(t *testing.T) {
111+
host := newDefaultHost(t)
112+
subscription := newDefaultSubscription(t)
113+
request := newBMCRequest(subscription)
114+
r := newBMCTestReconciler(subscription, host)
115+
for _, tc := range []struct {
116+
Scenario string
117+
Host *metal3api.BareMetalHost
118+
Expected bool
119+
}{
120+
{
121+
Scenario: "No provisioning id is provided",
122+
Host: host,
123+
Expected: true,
124+
},
125+
{
126+
Scenario: "Provisioning id is provided",
127+
Host: HostWithProvisioningID(t, host),
128+
Expected: true,
129+
},
130+
} {
131+
t.Run(tc.Scenario, func(t *testing.T) {
132+
prov, actual, err := r.getProvisioner(context.Background(), request, tc.Host)
133+
if err != nil {
134+
t.Error(err)
135+
}
136+
t.Log("Provisioner Details:", prov)
137+
if tc.Expected && !actual {
138+
t.Error("Expected a ready provisioner")
139+
}
140+
})
141+
}
142+
}

0 commit comments

Comments
 (0)