Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Shitrit <[email protected]>
  • Loading branch information
mshitrit committed Nov 28, 2023
1 parent b201de9 commit 979b8e5
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

machinev1beta1 "github.com/openshift/api/machine/v1beta1"

"github.com/medik8s/self-node-remediation/api/v1alpha1"
"github.com/medik8s/self-node-remediation/controllers"
"github.com/medik8s/self-node-remediation/controllers/tests/shared"
Expand Down Expand Up @@ -70,6 +72,7 @@ var _ = Describe("SNR Controller", func() {
Expect(k8sClient.Update(context.Background(), getNode(shared.PeerNodeName)))
time.Sleep(time.Second * 2)
deleteRemediations()
clearEvents()
verifyCleanState()
})

Expand Down Expand Up @@ -320,6 +323,88 @@ var _ = Describe("SNR Controller", func() {
})
})

Context("Remediation has a Machine Owner Ref", func() {
var machine *machinev1beta1.Machine
var machineName = "test-machine"
BeforeEach(func() {
snr.OwnerReferences = []metav1.OwnerReference{{Name: machineName, Kind: "Machine", APIVersion: "machine.openshift.io/v1beta1", UID: "12345"}}
})

When("A machine exist that matches the remediation OwnerRef machine", func() {
var machineStatus *machinev1beta1.MachineStatus

JustBeforeEach(func() {
Expect(k8sClient.Create(context.Background(), machine)).To(Succeed())
DeferCleanup(func() {
savedMachine := &machinev1beta1.Machine{}
Expect(k8sClient.Get(context.Background(), client.ObjectKeyFromObject(machine), savedMachine)).To(Succeed())
Expect(k8sClient.Delete(context.Background(), savedMachine))
})

if machineStatus != nil {
time.Sleep(time.Second)
savedMachine := &machinev1beta1.Machine{}
Expect(k8sClient.Get(context.Background(), client.ObjectKeyFromObject(machine), savedMachine)).To(Succeed())
savedMachine.Status = *machineStatus
Expect(k8sClient.Status().Update(context.Background(), savedMachine)).To(Succeed())
}

})
BeforeEach(func() {
machine = &machinev1beta1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: machineName,
Namespace: snrNamespace,
},
}
})
When("the correct NodeRef is set in the machine statusThe", func() {

BeforeEach(func() {
machineStatus = &machinev1beta1.MachineStatus{
NodeRef: &v1.ObjectReference{Name: shared.UnhealthyNodeName},
}
DeferCleanup(func() {
machineStatus = nil
})
})
It("Node is found and set with Unschedulable taint", func() {
time.Sleep(time.Second)
verifyEvent("Normal", "MarkUnschedulable", "Remediation process - unhealthy node marked as unschedulable")

})
})
When("the wrong NodeRef is set in the machine statusThe", func() {
BeforeEach(func() {
machineStatus = &machinev1beta1.MachineStatus{
NodeRef: &v1.ObjectReference{Name: "made-up-non-existing-node"},
}
DeferCleanup(func() {
machineStatus = nil
})
})
When("NHC isn't set as owner in the remediation", func() {
It("Node is not found", func() {
time.Sleep(time.Second)
verifyNoEvent("Normal", "MarkUnschedulable", "Remediation process - unhealthy node marked as unschedulable")
verifyTypeConditions(snr.Name, metav1.ConditionFalse, metav1.ConditionFalse, "RemediationFinishedNodeNotFound")
verifyEvent("Normal", "RemediationStopped", "couldn't find node matching remediation")
})
})
When("NHC isn set as owner in the remediation", func() {
BeforeEach(func() {
snr.OwnerReferences = append(snr.OwnerReferences, metav1.OwnerReference{Name: "nhc", Kind: "NodeHealthCheck", APIVersion: "remediation.medik8s.io/v1alpha1", UID: "12345"})
})
It("Node is found and set with Unschedulable taint", func() {
time.Sleep(time.Second)
verifyEvent("Normal", "MarkUnschedulable", "Remediation process - unhealthy node marked as unschedulable")

})
})
})
})

})
})

Context("Unhealthy node without api-server access", func() {
Expand Down Expand Up @@ -814,6 +899,17 @@ func verifyNodesAreEqual(expected *v1.Node, actual *v1.Node) {
Expect(reflect.DeepEqual(expected.Labels, actual.Labels)).To(BeTrue())
}

func clearEvents() {
for {
select {
case _ = <-fakeRecorder.Events:

default:
return
}
}
}

func verifyEvent(eventType, reason, message string) {
isEventMatch := isEventOccurred(eventType, reason, message)
ExpectWithOffset(1, isEventMatch).To(BeTrue())
Expand Down
28 changes: 20 additions & 8 deletions controllers/tests/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import (
. "github.com/onsi/gomega"

v1 "k8s.io/api/core/v1"

//+kubebuilder:scaffold:imports
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -35,14 +38,15 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

machinev1beta1 "github.com/openshift/api/machine/v1beta1"

selfnoderemediationv1alpha1 "github.com/medik8s/self-node-remediation/api/v1alpha1"
"github.com/medik8s/self-node-remediation/controllers"
"github.com/medik8s/self-node-remediation/controllers/tests/shared"
"github.com/medik8s/self-node-remediation/pkg/apicheck"
"github.com/medik8s/self-node-remediation/pkg/peers"
"github.com/medik8s/self-node-remediation/pkg/reboot"
"github.com/medik8s/self-node-remediation/pkg/watchdog"
//+kubebuilder:scaffold:imports
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
Expand Down Expand Up @@ -74,23 +78,32 @@ func TestAPIs(t *testing.T) {
var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

testScheme := runtime.NewScheme()

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("../../..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,
CRDInstallOptions: envtest.CRDInstallOptions{
Scheme: testScheme,
Paths: []string{
filepath.Join("../../..", "vendor", "github.com", "openshift", "api", "machine", "v1beta1"),
filepath.Join("../../..", "config", "crd", "bases"),
},
ErrorIfPathMissing: true,
},
}

cfg, err := testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

err = selfnoderemediationv1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
Expect(scheme.AddToScheme(testScheme)).To(Succeed())
Expect(machinev1beta1.Install(testScheme)).To(Succeed())
Expect(selfnoderemediationv1alpha1.AddToScheme(testScheme)).To(Succeed())

//+kubebuilder:scaffold:scheme

k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
Scheme: testScheme,
MetricsBindAddress: "0",
})
Expect(err).ToNot(HaveOccurred())
Expand All @@ -107,7 +120,6 @@ var _ = BeforeSuite(func() {
Name: shared.Namespace,
},
}

Expect(k8sClient.Create(context.Background(), nsToCreate)).To(Succeed())
dummyDog = watchdog.NewFake(true)
err = k8sManager.Add(dummyDog)
Expand All @@ -120,7 +132,7 @@ var _ = BeforeSuite(func() {
Client: k8sManager.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("self-node-remediation-config-controller"),
InstallFileFolder: "../../../install/",
Scheme: scheme.Scheme,
Scheme: testScheme,
Namespace: shared.Namespace,
ManagerSafeTimeCalculator: mockManagerCalculator,
}).SetupWithManager(k8sManager)
Expand Down

0 comments on commit 979b8e5

Please sign in to comment.