Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱Add tests for reduce reconcilement predicate methods (PR 1359) #1373

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions controllers/hcloudmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controllers

import (
"testing"
"time"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
Expand All @@ -37,6 +38,188 @@ import (
"github.com/syself/cluster-api-provider-hetzner/pkg/utils"
)

func TestIgnoreInsignificantHCloudMachineStatusUpdates(t *testing.T) {
logger := klog.Background()
predicate := IgnoreInsignificantHCloudMachineStatusUpdates(logger)

testCases := []struct {
name string
oldObj *infrav1.HCloudMachine
newObj *infrav1.HCloudMachine
expected bool
}{
{
name: "No significant changes",
oldObj: &infrav1.HCloudMachine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: "default",
},
Status: infrav1.HCloudMachineStatus{
Ready: true,
},
},
newObj: &infrav1.HCloudMachine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: "default",
ResourceVersion: "2",
},
Status: infrav1.HCloudMachineStatus{
Ready: true,
},
},
expected: false,
},
{
name: "Significant changes in spec",
oldObj: &infrav1.HCloudMachine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: "default",
},
Spec: infrav1.HCloudMachineSpec{
Type: "cx11",
},
},
newObj: &infrav1.HCloudMachine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: "default",
},
Spec: infrav1.HCloudMachineSpec{
Type: "cx21",
},
},
expected: true,
},
{
name: "Empty status in new object",
oldObj: &infrav1.HCloudMachine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: "default",
},
Status: infrav1.HCloudMachineStatus{
InstanceState: stPtr(hcloud.ServerStatusRunning),
},
},
newObj: &infrav1.HCloudMachine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: "default",
},
Status: infrav1.HCloudMachineStatus{},
},
expected: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
updateEvent := event.UpdateEvent{
ObjectOld: tc.oldObj,
ObjectNew: tc.newObj,
}
result := predicate.Update(updateEvent)
if result != tc.expected {
t.Errorf("Expected %v, but got %v", tc.expected, result)
}
})
}
}
func stPtr(h hcloud.ServerStatus) *hcloud.ServerStatus {
return &h
}
func TestIgnoreInsignificantMachineStatusUpdates(t *testing.T) {
logger := klog.Background()
predicate := IgnoreInsignificantMachineStatusUpdates(logger)

testCases := []struct {
name string
oldObj *clusterv1.Machine
newObj *clusterv1.Machine
expected bool
}{
{
name: "No significant changes",
oldObj: &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: "default",
},
Status: clusterv1.MachineStatus{},
},
newObj: &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: "default",
ResourceVersion: "2",
},
Status: clusterv1.MachineStatus{},
},
expected: false,
},
{
name: "Significant changes in spec",
oldObj: &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: "default",
},
Spec: clusterv1.MachineSpec{
ClusterName: "old-cluster",
},
},
newObj: &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: "default",
},
Spec: clusterv1.MachineSpec{
ClusterName: "new-cluster",
},
},
expected: true,
},
{
name: "Changes only in status",
oldObj: &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: "default",
},
Status: clusterv1.MachineStatus{
Phase: "Pending",
},
},
newObj: &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: "default",
},
Status: clusterv1.MachineStatus{
Phase: "Running",
},
},
expected: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
updateEvent := event.UpdateEvent{
ObjectOld: tc.oldObj,
ObjectNew: tc.newObj,
}
result := predicate.Update(updateEvent)
if result != tc.expected {
t.Errorf("Expected %v, but got %v", tc.expected, result)
}
})
}
}

var _ = Describe("HCloudMachineReconciler", func() {
var (
capiCluster *clusterv1.Cluster
Expand Down
Loading
Loading