|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net" |
| 6 | + "os" |
| 7 | + "reflect" |
| 8 | + "testing" |
| 9 | + |
| 10 | + apiv1 "k8s.io/api/core/v1" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/client-go/kubernetes/fake" |
| 13 | +) |
| 14 | + |
| 15 | +func Test_GetNodeObject(t *testing.T) { |
| 16 | + curHostname, err := os.Hostname() |
| 17 | + if err != nil { |
| 18 | + t.Fatalf("failed to get local hostname: %v", err) |
| 19 | + } |
| 20 | + |
| 21 | + testcases := []struct { |
| 22 | + name string |
| 23 | + envNodeName string |
| 24 | + hostnameOverride string |
| 25 | + existingNode *apiv1.Node |
| 26 | + err error |
| 27 | + }{ |
| 28 | + { |
| 29 | + "node with NODE_NAME exists", |
| 30 | + "test-node", |
| 31 | + "", |
| 32 | + &apiv1.Node{ |
| 33 | + ObjectMeta: metav1.ObjectMeta{ |
| 34 | + Name: "test-node", |
| 35 | + }, |
| 36 | + }, |
| 37 | + nil, |
| 38 | + }, |
| 39 | + { |
| 40 | + "node with hostname overrie exists", |
| 41 | + "something-else", |
| 42 | + "test-node", |
| 43 | + &apiv1.Node{ |
| 44 | + ObjectMeta: metav1.ObjectMeta{ |
| 45 | + Name: "test-node", |
| 46 | + }, |
| 47 | + }, |
| 48 | + nil, |
| 49 | + }, |
| 50 | + { |
| 51 | + "node with current hostname exists", |
| 52 | + "something-else", |
| 53 | + "something-else", |
| 54 | + &apiv1.Node{ |
| 55 | + ObjectMeta: metav1.ObjectMeta{ |
| 56 | + Name: curHostname, |
| 57 | + }, |
| 58 | + }, |
| 59 | + nil, |
| 60 | + }, |
| 61 | + { |
| 62 | + "node with NODE_NAME, hostname override or current hostname does not exists", |
| 63 | + "test-node", |
| 64 | + "", |
| 65 | + &apiv1.Node{ |
| 66 | + ObjectMeta: metav1.ObjectMeta{ |
| 67 | + Name: "another-node", |
| 68 | + }, |
| 69 | + }, |
| 70 | + errors.New("Failed to identify the node by NODE_NAME, hostname or --hostname-override"), |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + for _, testcase := range testcases { |
| 75 | + t.Run(testcase.name, func(t *testing.T) { |
| 76 | + clientset := fake.NewSimpleClientset() |
| 77 | + _, err := clientset.Core().Nodes().Create(testcase.existingNode) |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("failed to create existing nodes for test: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + os.Setenv("NODE_NAME", testcase.envNodeName) |
| 83 | + defer os.Unsetenv("NODE_NAME") |
| 84 | + |
| 85 | + _, err = GetNodeObject(clientset, testcase.hostnameOverride) |
| 86 | + if !reflect.DeepEqual(err, testcase.err) { |
| 87 | + t.Logf("actual error: %v", err) |
| 88 | + t.Logf("expected error: %v", testcase.err) |
| 89 | + t.Error("did not get expected error") |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func Test_GetNodeIP(t *testing.T) { |
| 96 | + testcases := []struct { |
| 97 | + name string |
| 98 | + node *apiv1.Node |
| 99 | + ip net.IP |
| 100 | + err error |
| 101 | + }{ |
| 102 | + { |
| 103 | + "has external and internal IPs", |
| 104 | + &apiv1.Node{ |
| 105 | + ObjectMeta: metav1.ObjectMeta{ |
| 106 | + Name: "test-node", |
| 107 | + }, |
| 108 | + Status: apiv1.NodeStatus{ |
| 109 | + Addresses: []apiv1.NodeAddress{ |
| 110 | + { |
| 111 | + Type: apiv1.NodeInternalIP, |
| 112 | + Address: "10.0.0.1", |
| 113 | + }, |
| 114 | + { |
| 115 | + Type: apiv1.NodeExternalIP, |
| 116 | + Address: "1.1.1.1", |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + net.ParseIP("10.0.0.1"), |
| 122 | + nil, |
| 123 | + }, |
| 124 | + { |
| 125 | + "has only internal IP", |
| 126 | + &apiv1.Node{ |
| 127 | + ObjectMeta: metav1.ObjectMeta{ |
| 128 | + Name: "test-node", |
| 129 | + }, |
| 130 | + Status: apiv1.NodeStatus{ |
| 131 | + Addresses: []apiv1.NodeAddress{ |
| 132 | + { |
| 133 | + Type: apiv1.NodeInternalIP, |
| 134 | + Address: "10.0.0.1", |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + net.ParseIP("10.0.0.1"), |
| 140 | + nil, |
| 141 | + }, |
| 142 | + { |
| 143 | + "has only external IP", |
| 144 | + &apiv1.Node{ |
| 145 | + ObjectMeta: metav1.ObjectMeta{ |
| 146 | + Name: "test-node", |
| 147 | + }, |
| 148 | + Status: apiv1.NodeStatus{ |
| 149 | + Addresses: []apiv1.NodeAddress{ |
| 150 | + { |
| 151 | + Type: apiv1.NodeExternalIP, |
| 152 | + Address: "1.1.1.1", |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + net.ParseIP("1.1.1.1"), |
| 158 | + nil, |
| 159 | + }, |
| 160 | + { |
| 161 | + "has no addresses", |
| 162 | + &apiv1.Node{ |
| 163 | + ObjectMeta: metav1.ObjectMeta{ |
| 164 | + Name: "test-node", |
| 165 | + }, |
| 166 | + Status: apiv1.NodeStatus{ |
| 167 | + Addresses: []apiv1.NodeAddress{}, |
| 168 | + }, |
| 169 | + }, |
| 170 | + nil, |
| 171 | + errors.New("host IP unknown"), |
| 172 | + }, |
| 173 | + } |
| 174 | + |
| 175 | + for _, testcase := range testcases { |
| 176 | + t.Run(testcase.name, func(t *testing.T) { |
| 177 | + ip, err := GetNodeIP(testcase.node) |
| 178 | + if !reflect.DeepEqual(err, testcase.err) { |
| 179 | + t.Logf("actual error: %v", err) |
| 180 | + t.Logf("expected error: %v", testcase.err) |
| 181 | + t.Error("did not get expected error") |
| 182 | + } |
| 183 | + |
| 184 | + if !reflect.DeepEqual(ip, testcase.ip) { |
| 185 | + t.Logf("actual ip: %v", ip) |
| 186 | + t.Logf("expected ip: %v", testcase.ip) |
| 187 | + t.Error("did not get expected node ip") |
| 188 | + } |
| 189 | + }) |
| 190 | + } |
| 191 | +} |
0 commit comments