-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
167 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package linode | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/linode/linode-cloud-controller-manager/cloud/annotations" | ||
"github.com/linode/linode-cloud-controller-manager/cloud/linode/client/mocks" | ||
"github.com/linode/linodego" | ||
"github.com/stretchr/testify/assert" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/fake" | ||
"k8s.io/client-go/util/workqueue" | ||
) | ||
|
||
func TestNodeController_processNext(t *testing.T) { | ||
// Mock dependencies | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
client := mocks.NewMockClient(ctrl) | ||
kubeClient := fake.NewSimpleClientset() | ||
queue := workqueue.NewTypedDelayingQueueWithConfig(workqueue.TypedDelayingQueueConfig[any]{Name: "testQueue"}) | ||
node := &v1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
Labels: map[string]string{}, | ||
Annotations: map[string]string{}, | ||
}, | ||
Spec: v1.NodeSpec{ProviderID: "linode://111"}, | ||
} | ||
|
||
_, err := kubeClient.CoreV1().Nodes().Create(context.TODO(), node, metav1.CreateOptions{}) | ||
assert.NoError(t, err, "expected no error during node creation") | ||
|
||
controller := &nodeController{ | ||
kubeclient: kubeClient, | ||
instances: newInstances(client), | ||
queue: queue, | ||
metadataLastUpdate: make(map[string]time.Time), | ||
ttl: defaultMetadataTTL, | ||
} | ||
|
||
t.Run("should return no error", func(t *testing.T) { | ||
queue.Add(node) | ||
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{}, errors.New("lookup failed")) | ||
result := controller.processNext() | ||
assert.True(t, result, "processNext should return true") | ||
if queue.Len() != 0 { | ||
t.Errorf("expected queue to be empty, got %d items", queue.Len()) | ||
} | ||
}) | ||
|
||
t.Run("should return no error if node exists", func(t *testing.T) { | ||
queue.Add(node) | ||
publicIP := net.ParseIP("172.234.31.123") | ||
privateIP := net.ParseIP("192.168.159.135") | ||
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{ | ||
{ID: 111, Label: "test", IPv4: []*net.IP{&publicIP, &privateIP}, HostUUID: "111"}, | ||
}, nil) | ||
result := controller.processNext() | ||
assert.True(t, result, "processNext should return true") | ||
if queue.Len() != 0 { | ||
t.Errorf("expected queue to be empty, got %d items", queue.Len()) | ||
} | ||
}) | ||
|
||
t.Run("should return error and requeue when it gets 429 from linode API", func(t *testing.T) { | ||
queue = workqueue.NewTypedDelayingQueueWithConfig(workqueue.TypedDelayingQueueConfig[any]{Name: "testQueue1"}) | ||
queue.Add(node) | ||
controller.queue = queue | ||
client := mocks.NewMockClient(ctrl) | ||
controller.instances = newInstances(client) | ||
defaultRetryInterval = 1 * time.Nanosecond | ||
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{}, &linodego.Error{Code: http.StatusTooManyRequests, Message: "Too many requests"}) | ||
result := controller.processNext() | ||
time.Sleep(1 * time.Second) | ||
assert.True(t, result, "processNext should return true") | ||
if queue.Len() == 0 { | ||
t.Errorf("expected queue to not be empty, got it empty") | ||
} | ||
}) | ||
|
||
t.Run("should return error and requeue when it gets error >= 500 from linode API", func(t *testing.T) { | ||
queue = workqueue.NewTypedDelayingQueueWithConfig(workqueue.TypedDelayingQueueConfig[any]{Name: "testQueue2"}) | ||
queue.Add(node) | ||
controller.queue = queue | ||
client := mocks.NewMockClient(ctrl) | ||
controller.instances = newInstances(client) | ||
defaultRetryInterval = 1 * time.Nanosecond | ||
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{}, &linodego.Error{Code: http.StatusInternalServerError, Message: "Too many requests"}) | ||
result := controller.processNext() | ||
time.Sleep(1 * time.Second) | ||
assert.True(t, result, "processNext should return true") | ||
if queue.Len() == 0 { | ||
t.Errorf("expected queue to not be empty, got it empty") | ||
} | ||
}) | ||
} | ||
|
||
func TestNodeController_handleNode(t *testing.T) { | ||
// Mock dependencies | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
client := mocks.NewMockClient(ctrl) | ||
kubeClient := fake.NewSimpleClientset() | ||
node := &v1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-node", | ||
Labels: map[string]string{}, | ||
Annotations: map[string]string{}, | ||
}, | ||
Spec: v1.NodeSpec{ProviderID: "linode://123"}, | ||
} | ||
_, err := kubeClient.CoreV1().Nodes().Create(context.TODO(), node, metav1.CreateOptions{}) | ||
assert.NoError(t, err, "expected no error during node creation") | ||
|
||
instCache := newInstances(client) | ||
nodeCtrl := newNodeController(kubeClient, client, nil, instCache) | ||
|
||
// Test: Successful metadata update | ||
publicIP := net.ParseIP("172.234.31.123") | ||
privateIP := net.ParseIP("192.168.159.135") | ||
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{ | ||
{ID: 123, Label: "test-node", IPv4: []*net.IP{&publicIP, &privateIP}, HostUUID: "123"}, | ||
}, nil) | ||
err = nodeCtrl.handleNode(context.TODO(), node) | ||
assert.NoError(t, err, "expected no error during handleNode") | ||
|
||
// Check metadataLastUpdate | ||
lastUpdate := nodeCtrl.LastMetadataUpdate("test-node") | ||
if time.Since(lastUpdate) > 5*time.Second { | ||
t.Errorf("metadataLastUpdate was not updated correctly") | ||
} | ||
|
||
// Annotations set, no update needed as ttl not reached | ||
node.Labels[annotations.AnnLinodeHostUUID] = "123" | ||
node.Annotations[annotations.AnnLinodeNodePrivateIP] = privateIP.String() | ||
err = nodeCtrl.handleNode(context.TODO(), node) | ||
assert.NoError(t, err, "expected no error during handleNode") | ||
|
||
// Lookup failure for linode instance | ||
client = mocks.NewMockClient(ctrl) | ||
nodeCtrl.instances = newInstances(client) | ||
nodeCtrl.metadataLastUpdate["test-node"] = time.Now().Add(-2 * nodeCtrl.ttl) | ||
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{}, errors.New("lookup failed")) | ||
err = nodeCtrl.handleNode(context.TODO(), node) | ||
assert.Error(t, err, "expected error during handleNode, got nil") | ||
|
||
// All fields already set | ||
client = mocks.NewMockClient(ctrl) | ||
nodeCtrl.instances = newInstances(client) | ||
nodeCtrl.metadataLastUpdate["test-node"] = time.Now().Add(-2 * nodeCtrl.ttl) | ||
client.EXPECT().ListInstances(gomock.Any(), nil).Times(1).Return([]linodego.Instance{ | ||
{ID: 123, Label: "test-node", IPv4: []*net.IP{&publicIP, &privateIP}, HostUUID: "123"}, | ||
}, nil) | ||
err = nodeCtrl.handleNode(context.TODO(), node) | ||
assert.NoError(t, err, "expected no error during handleNode") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters