vmware_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package network
  15. import (
  16. "errors"
  17. "net"
  18. "reflect"
  19. "testing"
  20. )
  21. func mustParseMac(mac net.HardwareAddr, err error) net.HardwareAddr {
  22. if err != nil {
  23. panic(err)
  24. }
  25. return mac
  26. }
  27. func TestProcessVMwareNetconf(t *testing.T) {
  28. tests := []struct {
  29. config map[string]string
  30. interfaces []InterfaceGenerator
  31. err error
  32. }{
  33. {},
  34. {
  35. config: map[string]string{
  36. "interface.0.dhcp": "yes",
  37. },
  38. interfaces: []InterfaceGenerator{
  39. &physicalInterface{logicalInterface{
  40. config: configMethodDHCP{},
  41. }},
  42. },
  43. },
  44. {
  45. config: map[string]string{
  46. "interface.0.mac": "00:11:22:33:44:55",
  47. "interface.0.dhcp": "yes",
  48. },
  49. interfaces: []InterfaceGenerator{
  50. &physicalInterface{logicalInterface{
  51. hwaddr: mustParseMac(net.ParseMAC("00:11:22:33:44:55")),
  52. config: configMethodDHCP{hwaddress: mustParseMac(net.ParseMAC("00:11:22:33:44:55"))},
  53. }},
  54. },
  55. },
  56. {
  57. config: map[string]string{
  58. "interface.0.name": "eth0",
  59. "interface.0.dhcp": "yes",
  60. },
  61. interfaces: []InterfaceGenerator{
  62. &physicalInterface{logicalInterface{
  63. name: "eth0",
  64. config: configMethodDHCP{},
  65. }},
  66. },
  67. },
  68. {
  69. config: map[string]string{
  70. "interface.0.mac": "00:11:22:33:44:55",
  71. "interface.0.ip.0.address": "10.0.0.100/24",
  72. "interface.0.route.0.gateway": "10.0.0.1",
  73. "interface.0.route.0.destination": "0.0.0.0/0",
  74. },
  75. interfaces: []InterfaceGenerator{
  76. &physicalInterface{logicalInterface{
  77. hwaddr: mustParseMac(net.ParseMAC("00:11:22:33:44:55")),
  78. config: configMethodStatic{
  79. hwaddress: mustParseMac(net.ParseMAC("00:11:22:33:44:55")),
  80. addresses: []net.IPNet{{IP: net.ParseIP("10.0.0.100"), Mask: net.CIDRMask(24, net.IPv4len*8)}},
  81. // I realize how upset you must be that I am shoving an IPMask into an IP. This is because net.IPv4zero is
  82. // actually a magic IPv6 address which ruins our equality check. What's that? Just use IP::Equal()? I'd rather
  83. // DeepEqual just handle that for me, but until Go gets operator overloading, we are stuck with this.
  84. routes: []route{{
  85. destination: net.IPNet{IP: net.IP(net.CIDRMask(0, net.IPv4len*8)), Mask: net.CIDRMask(0, net.IPv4len*8)},
  86. gateway: net.ParseIP("10.0.0.1")},
  87. },
  88. },
  89. }},
  90. },
  91. },
  92. {
  93. config: map[string]string{
  94. "dns.server.0": "1.2.3.4",
  95. "dns.server.1": "5.6.7.8",
  96. "dns.domain.0": "coreos.com",
  97. "dns.domain.1": "example.com",
  98. "interface.0.mac": "00:11:22:33:44:55",
  99. "interface.0.ip.0.address": "10.0.0.100/24",
  100. "interface.0.ip.1.address": "10.0.0.101/24",
  101. "interface.0.route.0.gateway": "10.0.0.1",
  102. "interface.0.route.0.destination": "0.0.0.0/0",
  103. "interface.1.name": "eth0",
  104. "interface.1.ip.0.address": "10.0.1.100/24",
  105. "interface.1.route.0.gateway": "10.0.1.1",
  106. "interface.1.route.0.destination": "0.0.0.0/0",
  107. "interface.2.dhcp": "yes",
  108. "interface.2.mac": "00:11:22:33:44:77",
  109. },
  110. interfaces: []InterfaceGenerator{
  111. &physicalInterface{logicalInterface{
  112. hwaddr: mustParseMac(net.ParseMAC("00:11:22:33:44:55")),
  113. config: configMethodStatic{
  114. hwaddress: mustParseMac(net.ParseMAC("00:11:22:33:44:55")),
  115. addresses: []net.IPNet{
  116. {IP: net.ParseIP("10.0.0.100"), Mask: net.CIDRMask(24, net.IPv4len*8)},
  117. {IP: net.ParseIP("10.0.0.101"), Mask: net.CIDRMask(24, net.IPv4len*8)},
  118. },
  119. routes: []route{{
  120. destination: net.IPNet{IP: net.IP(net.CIDRMask(0, net.IPv4len*8)), Mask: net.CIDRMask(0, net.IPv4len*8)},
  121. gateway: net.ParseIP("10.0.0.1")},
  122. },
  123. nameservers: []net.IP{net.ParseIP("1.2.3.4"), net.ParseIP("5.6.7.8")},
  124. domains: []string{"coreos.com", "example.com"},
  125. },
  126. }},
  127. &physicalInterface{logicalInterface{
  128. name: "eth0",
  129. config: configMethodStatic{
  130. addresses: []net.IPNet{{IP: net.ParseIP("10.0.1.100"), Mask: net.CIDRMask(24, net.IPv4len*8)}},
  131. routes: []route{{
  132. destination: net.IPNet{IP: net.IP(net.CIDRMask(0, net.IPv4len*8)), Mask: net.CIDRMask(0, net.IPv4len*8)},
  133. gateway: net.ParseIP("10.0.1.1")},
  134. },
  135. nameservers: []net.IP{net.ParseIP("1.2.3.4"), net.ParseIP("5.6.7.8")},
  136. domains: []string{"coreos.com", "example.com"},
  137. },
  138. }},
  139. &physicalInterface{logicalInterface{
  140. hwaddr: mustParseMac(net.ParseMAC("00:11:22:33:44:77")),
  141. config: configMethodDHCP{hwaddress: mustParseMac(net.ParseMAC("00:11:22:33:44:77"))},
  142. }},
  143. },
  144. },
  145. {
  146. config: map[string]string{"dns.server.0": "test dns"},
  147. err: errors.New(`invalid nameserver: "test dns"`),
  148. },
  149. }
  150. for i, tt := range tests {
  151. interfaces, err := ProcessVMwareNetconf(tt.config)
  152. if !reflect.DeepEqual(tt.err, err) {
  153. t.Errorf("bad error (#%d): want %v, got %v", i, tt.err, err)
  154. }
  155. if !reflect.DeepEqual(tt.interfaces, interfaces) {
  156. t.Errorf("bad interfaces (#%d): want %#v, got %#v", i, tt.interfaces, interfaces)
  157. for _, iface := range tt.interfaces {
  158. t.Logf(" want: %#v", iface)
  159. }
  160. for _, iface := range interfaces {
  161. t.Logf(" got: %#v", iface)
  162. }
  163. }
  164. }
  165. }
  166. func TestProcessAddressConfig(t *testing.T) {
  167. tests := []struct {
  168. config map[string]string
  169. prefix string
  170. addresses []net.IPNet
  171. err error
  172. }{
  173. {},
  174. // static - ipv4
  175. {
  176. config: map[string]string{
  177. "ip.0.address": "10.0.0.100/24",
  178. },
  179. addresses: []net.IPNet{{IP: net.ParseIP("10.0.0.100"), Mask: net.CIDRMask(24, net.IPv4len*8)}},
  180. },
  181. {
  182. config: map[string]string{
  183. "this.is.a.prefix.ip.0.address": "10.0.0.100/24",
  184. },
  185. prefix: "this.is.a.prefix.",
  186. addresses: []net.IPNet{{IP: net.ParseIP("10.0.0.100"), Mask: net.CIDRMask(24, net.IPv4len*8)}},
  187. },
  188. {
  189. config: map[string]string{
  190. "ip.0.address": "10.0.0.100/24",
  191. "ip.1.address": "10.0.0.101/24",
  192. "ip.2.address": "10.0.0.102/24",
  193. },
  194. addresses: []net.IPNet{
  195. {IP: net.ParseIP("10.0.0.100"), Mask: net.CIDRMask(24, net.IPv4len*8)},
  196. {IP: net.ParseIP("10.0.0.101"), Mask: net.CIDRMask(24, net.IPv4len*8)},
  197. {IP: net.ParseIP("10.0.0.102"), Mask: net.CIDRMask(24, net.IPv4len*8)},
  198. },
  199. },
  200. // static - ipv6
  201. {
  202. config: map[string]string{
  203. "ip.0.address": "fe00::100/64",
  204. },
  205. addresses: []net.IPNet{{IP: net.ParseIP("fe00::100"), Mask: net.IPMask(net.CIDRMask(64, net.IPv6len*8))}},
  206. },
  207. {
  208. config: map[string]string{
  209. "ip.0.address": "fe00::100/64",
  210. "ip.1.address": "fe00::101/64",
  211. "ip.2.address": "fe00::102/64",
  212. },
  213. addresses: []net.IPNet{
  214. {IP: net.ParseIP("fe00::100"), Mask: net.CIDRMask(64, net.IPv6len*8)},
  215. {IP: net.ParseIP("fe00::101"), Mask: net.CIDRMask(64, net.IPv6len*8)},
  216. {IP: net.ParseIP("fe00::102"), Mask: net.CIDRMask(64, net.IPv6len*8)},
  217. },
  218. },
  219. // invalid
  220. {
  221. config: map[string]string{
  222. "ip.0.address": "test address",
  223. },
  224. err: errors.New(`invalid address: "test address"`),
  225. },
  226. }
  227. for i, tt := range tests {
  228. addresses, err := processAddressConfig(tt.config, tt.prefix)
  229. if !reflect.DeepEqual(tt.err, err) {
  230. t.Errorf("bad error (#%d): want %v, got %v", i, tt.err, err)
  231. }
  232. if err != nil {
  233. continue
  234. }
  235. if !reflect.DeepEqual(tt.addresses, addresses) {
  236. t.Errorf("bad addresses (#%d): want %#v, got %#v", i, tt.addresses, addresses)
  237. }
  238. }
  239. }
  240. func TestProcessRouteConfig(t *testing.T) {
  241. tests := []struct {
  242. config map[string]string
  243. prefix string
  244. routes []route
  245. err error
  246. }{
  247. {},
  248. {
  249. config: map[string]string{
  250. "route.0.gateway": "10.0.0.1",
  251. "route.0.destination": "0.0.0.0/0",
  252. },
  253. routes: []route{{destination: net.IPNet{IP: net.IP(net.CIDRMask(0, net.IPv4len*8)), Mask: net.CIDRMask(0, net.IPv4len*8)}, gateway: net.ParseIP("10.0.0.1")}},
  254. },
  255. {
  256. config: map[string]string{
  257. "this.is.a.prefix.route.0.gateway": "10.0.0.1",
  258. "this.is.a.prefix.route.0.destination": "0.0.0.0/0",
  259. },
  260. prefix: "this.is.a.prefix.",
  261. routes: []route{{destination: net.IPNet{IP: net.IP(net.CIDRMask(0, net.IPv4len*8)), Mask: net.CIDRMask(0, net.IPv4len*8)}, gateway: net.ParseIP("10.0.0.1")}},
  262. },
  263. {
  264. config: map[string]string{
  265. "route.0.gateway": "fe00::1",
  266. "route.0.destination": "::/0",
  267. },
  268. routes: []route{{destination: net.IPNet{IP: net.IPv6zero, Mask: net.IPMask(net.IPv6zero)}, gateway: net.ParseIP("fe00::1")}},
  269. },
  270. // invalid
  271. {
  272. config: map[string]string{
  273. "route.0.gateway": "test gateway",
  274. "route.0.destination": "0.0.0.0/0",
  275. },
  276. err: errors.New(`invalid gateway: "test gateway"`),
  277. },
  278. {
  279. config: map[string]string{
  280. "route.0.gateway": "10.0.0.1",
  281. "route.0.destination": "test destination",
  282. },
  283. err: &net.ParseError{Type: "CIDR address", Text: "test destination"},
  284. },
  285. }
  286. for i, tt := range tests {
  287. routes, err := processRouteConfig(tt.config, tt.prefix)
  288. if !reflect.DeepEqual(tt.err, err) {
  289. t.Errorf("bad error (#%d): want %v, got %v", i, tt.err, err)
  290. }
  291. if err != nil {
  292. continue
  293. }
  294. if !reflect.DeepEqual(tt.routes, routes) {
  295. t.Errorf("bad routes (#%d): want %#v, got %#v", i, tt.routes, routes)
  296. }
  297. }
  298. }
  299. func TestProcessDHCPConfig(t *testing.T) {
  300. tests := []struct {
  301. config map[string]string
  302. prefix string
  303. dhcp bool
  304. err error
  305. }{
  306. {},
  307. // prefix
  308. {config: map[string]string{"this.is.a.prefix.mac": ""}, prefix: "this.is.a.prefix.", dhcp: false},
  309. {config: map[string]string{"this.is.a.prefix.dhcp": "yes"}, prefix: "this.is.a.prefix.", dhcp: true},
  310. // dhcp
  311. {config: map[string]string{"dhcp": "yes"}, dhcp: true},
  312. {config: map[string]string{"dhcp": "no"}, dhcp: false},
  313. // invalid
  314. {config: map[string]string{"dhcp": "blah"}, err: errors.New(`invalid DHCP option: "blah"`)},
  315. }
  316. for i, tt := range tests {
  317. dhcp, err := processDHCPConfig(tt.config, tt.prefix)
  318. if !reflect.DeepEqual(tt.err, err) {
  319. t.Errorf("bad error (#%d): want %v, got %v", i, tt.err, err)
  320. }
  321. if err != nil {
  322. continue
  323. }
  324. if tt.dhcp != dhcp {
  325. t.Errorf("bad dhcp (#%d): want %v, got %v", i, tt.dhcp, dhcp)
  326. }
  327. }
  328. }