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