network_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. Copyright 2019 The Rook Authors. All rights reserved.
  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 v1
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "testing"
  18. nadv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
  19. "github.com/stretchr/testify/assert"
  20. "k8s.io/apimachinery/pkg/util/yaml"
  21. )
  22. func TestNetworkCephSpecLegacy(t *testing.T) {
  23. netSpecYAML := []byte(`hostNetwork: true`)
  24. rawJSON, err := yaml.ToJSON(netSpecYAML)
  25. assert.Nil(t, err)
  26. var net NetworkSpec
  27. err = json.Unmarshal(rawJSON, &net)
  28. assert.Nil(t, err)
  29. expected := NetworkSpec{HostNetwork: true}
  30. assert.Equal(t, expected, net)
  31. }
  32. func TestNetworkCephIsHostLegacy(t *testing.T) {
  33. net := NetworkSpec{HostNetwork: true}
  34. assert.True(t, net.IsHost())
  35. }
  36. func TestNetworkSpec(t *testing.T) {
  37. netSpecYAML := []byte(`
  38. provider: host
  39. selectors:
  40. server: enp2s0f0
  41. broker: enp2s0f0`)
  42. rawJSON, err := yaml.ToJSON(netSpecYAML)
  43. assert.Nil(t, err)
  44. var net NetworkSpec
  45. err = json.Unmarshal(rawJSON, &net)
  46. assert.Nil(t, err)
  47. expected := NetworkSpec{
  48. Provider: "host",
  49. Selectors: map[CephNetworkType]string{
  50. "server": "enp2s0f0",
  51. "broker": "enp2s0f0",
  52. },
  53. }
  54. assert.Equal(t, expected, net)
  55. }
  56. func TestAddressRangesSpec_IsEmpty(t *testing.T) {
  57. var specNil *AddressRangesSpec
  58. assert.True(t, specNil.IsEmpty())
  59. empty := &AddressRangesSpec{}
  60. assert.True(t, empty.IsEmpty())
  61. someCIDR := CIDR("1.1.1.1/16")
  62. nonEmptyTests := []AddressRangesSpec{
  63. {Public: []CIDR{someCIDR}},
  64. {Public: []CIDR{someCIDR, someCIDR}},
  65. {Cluster: []CIDR{someCIDR}},
  66. {Cluster: []CIDR{someCIDR, someCIDR}},
  67. {Public: []CIDR{someCIDR}, Cluster: []CIDR{someCIDR}},
  68. {Public: []CIDR{someCIDR, someCIDR}, Cluster: []CIDR{someCIDR, someCIDR}},
  69. }
  70. for _, spec := range nonEmptyTests {
  71. assert.False(t, spec.IsEmpty())
  72. }
  73. }
  74. func TestAddressRangesSpec_Validate(t *testing.T) {
  75. // only test a small subset of CIDRs since Rook should definitely use the Go stdlib underneath
  76. v1 := CIDR("123.123.123.123/24")
  77. v2 := CIDR("1.0.0.1/24")
  78. v3 := CIDR("2000::/64")
  79. v4 := CIDR("2000:2000:2000:2000:2000:2000:2000:2000/64")
  80. v5 := CIDR("2000::128.128.128.128/96") // ipv4 expressed as subnet of ipv6 is valid
  81. // invalid CIDRs
  82. i1 := CIDR("123.123.123/24")
  83. i2 := CIDR("123.123.123.123/33")
  84. i4 := CIDR("2000/64")
  85. i3 := CIDR("2000:/64")
  86. i5 := CIDR("2000::128.128.128.128/129")
  87. tests := []struct {
  88. name string
  89. spec AddressRangesSpec
  90. numErrs int
  91. }{
  92. {"empty", AddressRangesSpec{}, 0},
  93. {"all valid", AddressRangesSpec{
  94. Public: []CIDR{v1},
  95. Cluster: []CIDR{v2, v3, v4, v5},
  96. }, 0},
  97. {"all invalid", AddressRangesSpec{
  98. Public: []CIDR{i1},
  99. Cluster: []CIDR{i2, i3, i4, i5},
  100. }, 5},
  101. {"public only, valid", AddressRangesSpec{Public: []CIDR{v1}}, 0},
  102. {"public only, invalid", AddressRangesSpec{Public: []CIDR{i1}}, 1},
  103. {"cluster only, valid", AddressRangesSpec{Cluster: []CIDR{v2}}, 0},
  104. {"cluster only, invalid", AddressRangesSpec{Cluster: []CIDR{i2}}, 1},
  105. {"public valid, cluster valid", AddressRangesSpec{
  106. Public: []CIDR{v1},
  107. Cluster: []CIDR{v2},
  108. }, 0},
  109. {"public valid, cluster invalid", AddressRangesSpec{
  110. Public: []CIDR{v2},
  111. Cluster: []CIDR{i2},
  112. }, 1},
  113. {"public invalid, cluster valid", AddressRangesSpec{
  114. Public: []CIDR{i3},
  115. Cluster: []CIDR{v2},
  116. }, 1},
  117. {"public invalid, cluster invalid", AddressRangesSpec{
  118. Public: []CIDR{i3},
  119. Cluster: []CIDR{i4},
  120. }, 2},
  121. {"both, valid and invalid", AddressRangesSpec{
  122. Public: []CIDR{v1, i2},
  123. Cluster: []CIDR{v3, i4},
  124. }, 2},
  125. }
  126. for _, tt := range tests {
  127. t.Run(tt.name, func(t *testing.T) {
  128. err := tt.spec.Validate()
  129. if tt.numErrs > 0 {
  130. assert.Error(t, err)
  131. t.Log(err)
  132. assert.ErrorContains(t, err, fmt.Sprintf("%d network ranges are invalid", tt.numErrs))
  133. } else {
  134. assert.NoError(t, err)
  135. }
  136. })
  137. }
  138. }
  139. // these two functions are should almost always used together and can be unit tested together more
  140. // easily than apart
  141. func TestNetworkSpec_GetNetworkSelection_NetworkSelectionsToAnnotationValue(t *testing.T) {
  142. // inputs are the same definition expressed in json format or non-json format
  143. input1 := func(json bool) string {
  144. if json {
  145. return `[{"name": "macvlan", "interface": "net1"}]`
  146. }
  147. return "macvlan@net1"
  148. }
  149. input2 := func(json bool) string {
  150. if json {
  151. return `[{"name": "macvlan", "interface": "net2"}]`
  152. }
  153. return "macvlan@net2"
  154. }
  155. // allow running the test suite with json-format or non-json-format inputs
  156. testGetNetworkAnnotationValue := func(t *testing.T, json bool) {
  157. t.Helper()
  158. tests := []struct {
  159. name string
  160. specSelectors map[CephNetworkType]string
  161. cephNets []CephNetworkType
  162. want string
  163. wantErr bool
  164. }{
  165. {
  166. name: "public want public",
  167. specSelectors: map[CephNetworkType]string{
  168. "public": input1(json),
  169. },
  170. cephNets: []CephNetworkType{CephNetworkPublic},
  171. want: `[{"name":"macvlan","namespace":"ns","interface":"net1"}]`,
  172. wantErr: false,
  173. },
  174. {
  175. name: "cluster want cluster",
  176. specSelectors: map[CephNetworkType]string{
  177. "cluster": input1(json),
  178. },
  179. cephNets: []CephNetworkType{CephNetworkCluster},
  180. want: `[{"name":"macvlan","namespace":"ns","interface":"net1"}]`,
  181. wantErr: false,
  182. },
  183. {
  184. name: "public want cluster",
  185. specSelectors: map[CephNetworkType]string{
  186. "public": input1(json),
  187. },
  188. cephNets: []CephNetworkType{CephNetworkCluster},
  189. want: ``,
  190. wantErr: false,
  191. },
  192. {
  193. name: "cluster want public",
  194. specSelectors: map[CephNetworkType]string{
  195. "cluster": input1(json),
  196. },
  197. cephNets: []CephNetworkType{CephNetworkPublic},
  198. want: ``,
  199. wantErr: false,
  200. },
  201. {
  202. name: "nothing want public",
  203. specSelectors: map[CephNetworkType]string{},
  204. cephNets: []CephNetworkType{CephNetworkPublic},
  205. want: ``,
  206. wantErr: false,
  207. },
  208. {
  209. name: "nothing want cluster",
  210. specSelectors: map[CephNetworkType]string{},
  211. cephNets: []CephNetworkType{CephNetworkCluster},
  212. want: ``,
  213. wantErr: false,
  214. },
  215. {
  216. name: "unknown want public",
  217. specSelectors: map[CephNetworkType]string{
  218. "uncleKnown": input1(json),
  219. },
  220. cephNets: []CephNetworkType{CephNetworkPublic},
  221. want: ``,
  222. wantErr: false,
  223. },
  224. {
  225. name: "unknown want cluster",
  226. specSelectors: map[CephNetworkType]string{
  227. "uncleKnown": input1(json),
  228. },
  229. cephNets: []CephNetworkType{CephNetworkCluster},
  230. want: ``,
  231. wantErr: false,
  232. },
  233. {
  234. name: "public want public and cluster",
  235. specSelectors: map[CephNetworkType]string{
  236. "public": input1(json),
  237. },
  238. cephNets: []CephNetworkType{CephNetworkPublic, CephNetworkCluster},
  239. want: `[{"name":"macvlan","namespace":"ns","interface":"net1"}]`,
  240. wantErr: false,
  241. },
  242. {
  243. name: "cluster want public and cluster",
  244. specSelectors: map[CephNetworkType]string{
  245. "cluster": input1(json),
  246. },
  247. cephNets: []CephNetworkType{CephNetworkPublic, CephNetworkCluster},
  248. want: `[{"name":"macvlan","namespace":"ns","interface":"net1"}]`,
  249. wantErr: false,
  250. },
  251. {
  252. name: "public and cluster want public and cluster",
  253. specSelectors: map[CephNetworkType]string{
  254. "public": input1(json),
  255. "cluster": input2(json),
  256. },
  257. cephNets: []CephNetworkType{CephNetworkPublic, CephNetworkCluster},
  258. want: `[{"name":"macvlan","namespace":"ns","interface":"net1"},{"name":"macvlan","namespace":"ns","interface":"net2"}]`,
  259. wantErr: false,
  260. },
  261. {
  262. name: "support mixed json-non-json spec",
  263. specSelectors: map[CephNetworkType]string{
  264. "public": input1(json),
  265. "cluster": input2(!json), // invert json-ness of this one
  266. },
  267. cephNets: []CephNetworkType{CephNetworkPublic, CephNetworkCluster},
  268. want: `[{"name":"macvlan","namespace":"ns","interface":"net1"},{"name":"macvlan","namespace":"ns","interface":"net2"}]`,
  269. wantErr: false,
  270. },
  271. {
  272. name: "public and cluster want nothing",
  273. specSelectors: map[CephNetworkType]string{
  274. "public": input1(json),
  275. "cluster": input2(json),
  276. },
  277. cephNets: []CephNetworkType{},
  278. want: ``,
  279. wantErr: false,
  280. },
  281. {
  282. name: "legacy single json object support",
  283. specSelectors: map[CephNetworkType]string{
  284. "public": `{"name": "legacyJsonObject"}`,
  285. },
  286. cephNets: []CephNetworkType{CephNetworkPublic, CephNetworkCluster},
  287. want: `[{"name":"legacyJsonObject","namespace":"ns"}]`,
  288. wantErr: false,
  289. },
  290. {
  291. name: "invalid network selections",
  292. specSelectors: map[CephNetworkType]string{
  293. "public": `[{"name": "jsonWithNoClosingBracket"}`,
  294. "cluster": "multus%net",
  295. },
  296. cephNets: []CephNetworkType{CephNetworkPublic, CephNetworkCluster},
  297. want: ``,
  298. wantErr: true,
  299. },
  300. }
  301. for _, tt := range tests {
  302. t.Run(tt.name, func(t *testing.T) {
  303. n := &NetworkSpec{
  304. Selectors: tt.specSelectors,
  305. }
  306. selections := []*nadv1.NetworkSelectionElement{}
  307. errs := []error{}
  308. for _, net := range tt.cephNets {
  309. s, err := n.GetNetworkSelection("ns", net)
  310. if err != nil {
  311. errs = append(errs, err)
  312. }
  313. selections = append(selections, s)
  314. }
  315. got, err := NetworkSelectionsToAnnotationValue(selections...)
  316. if err != nil {
  317. errs = append(errs, err)
  318. }
  319. assert.Equal(t, tt.wantErr, len(errs) > 0, "wantErr %v but got errs %v", tt.wantErr, errs)
  320. assert.Equal(t, tt.want, got)
  321. })
  322. }
  323. }
  324. // Actual subtests
  325. t.Run("non-JSON input", func(t *testing.T) {
  326. testGetNetworkAnnotationValue(t, false)
  327. })
  328. t.Run("JSON input", func(t *testing.T) {
  329. testGetNetworkAnnotationValue(t, true)
  330. })
  331. }