kubelet_network_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package kubelet
  14. import (
  15. "net"
  16. "reflect"
  17. "strings"
  18. "testing"
  19. "k8s.io/kubernetes/pkg/api"
  20. "k8s.io/kubernetes/pkg/util/bandwidth"
  21. )
  22. func TestNodeIPParam(t *testing.T) {
  23. testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
  24. kubelet := testKubelet.kubelet
  25. tests := []struct {
  26. nodeIP string
  27. success bool
  28. testName string
  29. }{
  30. {
  31. nodeIP: "",
  32. success: true,
  33. testName: "IP not set",
  34. },
  35. {
  36. nodeIP: "127.0.0.1",
  37. success: false,
  38. testName: "loopback address",
  39. },
  40. {
  41. nodeIP: "FE80::0202:B3FF:FE1E:8329",
  42. success: false,
  43. testName: "IPv6 address",
  44. },
  45. {
  46. nodeIP: "1.2.3.4",
  47. success: false,
  48. testName: "IPv4 address that doesn't belong to host",
  49. },
  50. }
  51. for _, test := range tests {
  52. kubelet.nodeIP = net.ParseIP(test.nodeIP)
  53. err := kubelet.validateNodeIP()
  54. if err != nil && test.success {
  55. t.Errorf("Test: %s, expected no error but got: %v", test.testName, err)
  56. } else if err == nil && !test.success {
  57. t.Errorf("Test: %s, expected an error", test.testName)
  58. }
  59. }
  60. }
  61. type countingDNSScrubber struct {
  62. counter *int
  63. }
  64. func (cds countingDNSScrubber) ScrubDNS(nameservers, searches []string) (nsOut, srchOut []string) {
  65. (*cds.counter)++
  66. return nameservers, searches
  67. }
  68. func TestParseResolvConf(t *testing.T) {
  69. testCases := []struct {
  70. data string
  71. nameservers []string
  72. searches []string
  73. }{
  74. {"", []string{}, []string{}},
  75. {" ", []string{}, []string{}},
  76. {"\n", []string{}, []string{}},
  77. {"\t\n\t", []string{}, []string{}},
  78. {"#comment\n", []string{}, []string{}},
  79. {" #comment\n", []string{}, []string{}},
  80. {"#comment\n#comment", []string{}, []string{}},
  81. {"#comment\nnameserver", []string{}, []string{}},
  82. {"#comment\nnameserver\nsearch", []string{}, []string{}},
  83. {"nameserver 1.2.3.4", []string{"1.2.3.4"}, []string{}},
  84. {" nameserver 1.2.3.4", []string{"1.2.3.4"}, []string{}},
  85. {"\tnameserver 1.2.3.4", []string{"1.2.3.4"}, []string{}},
  86. {"nameserver\t1.2.3.4", []string{"1.2.3.4"}, []string{}},
  87. {"nameserver \t 1.2.3.4", []string{"1.2.3.4"}, []string{}},
  88. {"nameserver 1.2.3.4\nnameserver 5.6.7.8", []string{"1.2.3.4", "5.6.7.8"}, []string{}},
  89. {"search foo", []string{}, []string{"foo"}},
  90. {"search foo bar", []string{}, []string{"foo", "bar"}},
  91. {"search foo bar bat\n", []string{}, []string{"foo", "bar", "bat"}},
  92. {"search foo\nsearch bar", []string{}, []string{"bar"}},
  93. {"nameserver 1.2.3.4\nsearch foo bar", []string{"1.2.3.4"}, []string{"foo", "bar"}},
  94. {"nameserver 1.2.3.4\nsearch foo\nnameserver 5.6.7.8\nsearch bar", []string{"1.2.3.4", "5.6.7.8"}, []string{"bar"}},
  95. {"#comment\nnameserver 1.2.3.4\n#comment\nsearch foo\ncomment", []string{"1.2.3.4"}, []string{"foo"}},
  96. }
  97. for i, tc := range testCases {
  98. ns, srch, err := parseResolvConf(strings.NewReader(tc.data), nil)
  99. if err != nil {
  100. t.Errorf("expected success, got %v", err)
  101. continue
  102. }
  103. if !reflect.DeepEqual(ns, tc.nameservers) {
  104. t.Errorf("[%d] expected nameservers %#v, got %#v", i, tc.nameservers, ns)
  105. }
  106. if !reflect.DeepEqual(srch, tc.searches) {
  107. t.Errorf("[%d] expected searches %#v, got %#v", i, tc.searches, srch)
  108. }
  109. counter := 0
  110. cds := countingDNSScrubber{&counter}
  111. ns, srch, err = parseResolvConf(strings.NewReader(tc.data), cds)
  112. if err != nil {
  113. t.Errorf("expected success, got %v", err)
  114. continue
  115. }
  116. if !reflect.DeepEqual(ns, tc.nameservers) {
  117. t.Errorf("[%d] expected nameservers %#v, got %#v", i, tc.nameservers, ns)
  118. }
  119. if !reflect.DeepEqual(srch, tc.searches) {
  120. t.Errorf("[%d] expected searches %#v, got %#v", i, tc.searches, srch)
  121. }
  122. if counter != 1 {
  123. t.Errorf("[%d] expected dnsScrubber to have been called: got %d", i, counter)
  124. }
  125. }
  126. }
  127. func TestCleanupBandwidthLimits(t *testing.T) {
  128. testPod := func(name, ingress string) *api.Pod {
  129. pod := podWithUidNameNs("", name, "")
  130. if len(ingress) != 0 {
  131. pod.Annotations["kubernetes.io/ingress-bandwidth"] = ingress
  132. }
  133. return pod
  134. }
  135. // TODO(random-liu): We removed the test case for pod status not cached here. We should add a higher
  136. // layer status getter function and test that function instead.
  137. tests := []struct {
  138. status *api.PodStatus
  139. pods []*api.Pod
  140. inputCIDRs []string
  141. expectResetCIDRs []string
  142. name string
  143. }{
  144. {
  145. status: &api.PodStatus{
  146. PodIP: "1.2.3.4",
  147. Phase: api.PodRunning,
  148. },
  149. pods: []*api.Pod{
  150. testPod("foo", "10M"),
  151. testPod("bar", ""),
  152. },
  153. inputCIDRs: []string{"1.2.3.4/32", "2.3.4.5/32", "5.6.7.8/32"},
  154. expectResetCIDRs: []string{"2.3.4.5/32", "5.6.7.8/32"},
  155. name: "pod running",
  156. },
  157. {
  158. status: &api.PodStatus{
  159. PodIP: "1.2.3.4",
  160. Phase: api.PodFailed,
  161. },
  162. pods: []*api.Pod{
  163. testPod("foo", "10M"),
  164. testPod("bar", ""),
  165. },
  166. inputCIDRs: []string{"1.2.3.4/32", "2.3.4.5/32", "5.6.7.8/32"},
  167. expectResetCIDRs: []string{"1.2.3.4/32", "2.3.4.5/32", "5.6.7.8/32"},
  168. name: "pod not running",
  169. },
  170. {
  171. status: &api.PodStatus{
  172. PodIP: "1.2.3.4",
  173. Phase: api.PodFailed,
  174. },
  175. pods: []*api.Pod{
  176. testPod("foo", ""),
  177. testPod("bar", ""),
  178. },
  179. inputCIDRs: []string{"1.2.3.4/32", "2.3.4.5/32", "5.6.7.8/32"},
  180. expectResetCIDRs: []string{"1.2.3.4/32", "2.3.4.5/32", "5.6.7.8/32"},
  181. name: "no bandwidth limits",
  182. },
  183. }
  184. for _, test := range tests {
  185. shaper := &bandwidth.FakeShaper{
  186. CIDRs: test.inputCIDRs,
  187. }
  188. testKube := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
  189. testKube.kubelet.shaper = shaper
  190. for _, pod := range test.pods {
  191. testKube.kubelet.statusManager.SetPodStatus(pod, *test.status)
  192. }
  193. err := testKube.kubelet.cleanupBandwidthLimits(test.pods)
  194. if err != nil {
  195. t.Errorf("unexpected error: %v (%s)", test.name, err)
  196. }
  197. if !reflect.DeepEqual(shaper.ResetCIDRs, test.expectResetCIDRs) {
  198. t.Errorf("[%s]\nexpected: %v, saw: %v", test.name, test.expectResetCIDRs, shaper.ResetCIDRs)
  199. }
  200. }
  201. }
  202. func TestGetIPTablesMark(t *testing.T) {
  203. tests := []struct {
  204. bit int
  205. expect string
  206. }{
  207. {
  208. 14,
  209. "0x00004000/0x00004000",
  210. },
  211. {
  212. 15,
  213. "0x00008000/0x00008000",
  214. },
  215. }
  216. for _, tc := range tests {
  217. res := getIPTablesMark(tc.bit)
  218. if res != tc.expect {
  219. t.Errorf("getIPTablesMark output unexpected result: %v when input bit is %d. Expect result: %v", res, tc.bit, tc.expect)
  220. }
  221. }
  222. }