|
| 1 | +package k8s |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + corev1 "k8s.io/api/core/v1" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/client-go/kubernetes/fake" |
| 12 | +) |
| 13 | + |
| 14 | +// newNodeForPatch creates a minimal node and registers it with a fresh fake |
| 15 | +// clientset, returning both. Using fake.NewSimpleClientset means patch calls |
| 16 | +// go through the object tracker and work out of the box. |
| 17 | +func newNodeForPatch(name string, annotations, labels map[string]string) (*corev1.Node, *fake.Clientset) { |
| 18 | + node := &corev1.Node{ |
| 19 | + ObjectMeta: metav1.ObjectMeta{ |
| 20 | + Name: name, |
| 21 | + Annotations: annotations, |
| 22 | + Labels: labels, |
| 23 | + }, |
| 24 | + } |
| 25 | + client := fake.NewSimpleClientset(node) |
| 26 | + return node, client |
| 27 | +} |
| 28 | + |
| 29 | +// TestAddAnnotationToNode_WithExistingAnnotations verifies that |
| 30 | +// AddAnnotationToNode merges into an existing annotations map without |
| 31 | +// clobbering other entries. |
| 32 | +func TestAddAnnotationToNode_WithExistingAnnotations(t *testing.T) { |
| 33 | + node, client := newNodeForPatch("test-node", |
| 34 | + map[string]string{"existing-key": "existing-value"}, |
| 35 | + nil, |
| 36 | + ) |
| 37 | + |
| 38 | + err := AddAnnotationToNode(node.Name, "new-key", "new-value", client) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + got, err := client.CoreV1().Nodes().Get(context.TODO(), node.Name, metav1.GetOptions{}) |
| 42 | + require.NoError(t, err) |
| 43 | + assert.Equal(t, "existing-value", got.Annotations["existing-key"], "existing annotation should be preserved") |
| 44 | + assert.Equal(t, "new-value", got.Annotations["new-key"], "new annotation should be set") |
| 45 | +} |
| 46 | + |
| 47 | +// TestAddAnnotationToNode_WithNilAnnotations verifies that AddAnnotationToNode |
| 48 | +// succeeds even when the node's annotations map is nil. This was the bug with |
| 49 | +// the old JSON Patch "add" approach: adding a key to a nil map via JSON Patch |
| 50 | +// fails at the apiserver level. |
| 51 | +func TestAddAnnotationToNode_WithNilAnnotations(t *testing.T) { |
| 52 | + node, client := newNodeForPatch("test-node", nil, nil) |
| 53 | + |
| 54 | + err := AddAnnotationToNode(node.Name, "some-key", "some-value", client) |
| 55 | + require.NoError(t, err) |
| 56 | + |
| 57 | + got, err := client.CoreV1().Nodes().Get(context.TODO(), node.Name, metav1.GetOptions{}) |
| 58 | + require.NoError(t, err) |
| 59 | + assert.Equal(t, "some-value", got.Annotations["some-key"]) |
| 60 | +} |
| 61 | + |
| 62 | +// TestAddAnnotationToNode_OverwritesExistingValue verifies that patching an |
| 63 | +// annotation that already exists updates its value. |
| 64 | +func TestAddAnnotationToNode_OverwritesExistingValue(t *testing.T) { |
| 65 | + node, client := newNodeForPatch("test-node", |
| 66 | + map[string]string{"my-key": "old-value"}, |
| 67 | + nil, |
| 68 | + ) |
| 69 | + |
| 70 | + err := AddAnnotationToNode(node.Name, "my-key", "new-value", client) |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + got, err := client.CoreV1().Nodes().Get(context.TODO(), node.Name, metav1.GetOptions{}) |
| 74 | + require.NoError(t, err) |
| 75 | + assert.Equal(t, "new-value", got.Annotations["my-key"]) |
| 76 | +} |
| 77 | + |
| 78 | +// TestAddLabelToNode_WithExistingLabels verifies that AddLabelToNode merges |
| 79 | +// into an existing labels map without clobbering other entries. |
| 80 | +func TestAddLabelToNode_WithExistingLabels(t *testing.T) { |
| 81 | + node, client := newNodeForPatch("test-node", |
| 82 | + nil, |
| 83 | + map[string]string{"existing-label": "existing-value"}, |
| 84 | + ) |
| 85 | + |
| 86 | + err := AddLabelToNode(node.Name, "new-label", "new-value", client) |
| 87 | + require.NoError(t, err) |
| 88 | + |
| 89 | + got, err := client.CoreV1().Nodes().Get(context.TODO(), node.Name, metav1.GetOptions{}) |
| 90 | + require.NoError(t, err) |
| 91 | + assert.Equal(t, "existing-value", got.Labels["existing-label"], "existing label should be preserved") |
| 92 | + assert.Equal(t, "new-value", got.Labels["new-label"], "new label should be set") |
| 93 | +} |
| 94 | + |
| 95 | +// TestAddLabelToNode_WithNilLabels verifies that AddLabelToNode succeeds even |
| 96 | +// when the node has no labels. Mirrors the nil-annotations case. |
| 97 | +func TestAddLabelToNode_WithNilLabels(t *testing.T) { |
| 98 | + node, client := newNodeForPatch("test-node", nil, nil) |
| 99 | + |
| 100 | + err := AddLabelToNode(node.Name, "role", "worker", client) |
| 101 | + require.NoError(t, err) |
| 102 | + |
| 103 | + got, err := client.CoreV1().Nodes().Get(context.TODO(), node.Name, metav1.GetOptions{}) |
| 104 | + require.NoError(t, err) |
| 105 | + assert.Equal(t, "worker", got.Labels["role"]) |
| 106 | +} |
| 107 | + |
| 108 | +// TestAddAnnotationAndLabelToNode_Independent verifies that patching |
| 109 | +// annotations and labels independently doesn't interfere. |
| 110 | +func TestAddAnnotationAndLabelToNode_Independent(t *testing.T) { |
| 111 | + node, client := newNodeForPatch("test-node", nil, nil) |
| 112 | + |
| 113 | + require.NoError(t, AddAnnotationToNode(node.Name, "ann", "ann-val", client)) |
| 114 | + require.NoError(t, AddLabelToNode(node.Name, "lbl", "lbl-val", client)) |
| 115 | + |
| 116 | + got, err := client.CoreV1().Nodes().Get(context.TODO(), node.Name, metav1.GetOptions{}) |
| 117 | + require.NoError(t, err) |
| 118 | + assert.Equal(t, "ann-val", got.Annotations["ann"]) |
| 119 | + assert.Equal(t, "lbl-val", got.Labels["lbl"]) |
| 120 | +} |
| 121 | + |
| 122 | +// TestAddAnnotationToNode_NodeNotFound verifies that an error is returned when |
| 123 | +// the target node doesn't exist. |
| 124 | +func TestAddAnnotationToNode_NodeNotFound(t *testing.T) { |
| 125 | + client := fake.NewSimpleClientset() // empty — no nodes registered |
| 126 | + err := AddAnnotationToNode("does-not-exist", "k", "v", client) |
| 127 | + assert.Error(t, err) |
| 128 | +} |
| 129 | + |
| 130 | +// TestAddLabelToNode_NodeNotFound verifies that an error is returned when the |
| 131 | +// target node doesn't exist. |
| 132 | +func TestAddLabelToNode_NodeNotFound(t *testing.T) { |
| 133 | + client := fake.NewSimpleClientset() |
| 134 | + err := AddLabelToNode("does-not-exist", "k", "v", client) |
| 135 | + assert.Error(t, err) |
| 136 | +} |
0 commit comments