Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. func AllocateAndReleaseAddressesTest(ipVersion string, ipRange string, ipGateway string, expectedAddress string) {
  2. const ifname string = "eth0"
  3. const nspath string = "/some/where"
  4.  
  5. conf := fmt.Sprintf(`{
  6. "cniVersion": "0.3.1",
  7. "name": "mynet",
  8. "type": "ipvlan",
  9. "master": "foo0",
  10. "ipam": {
  11. "type": "whereabouts",
  12. "log_file" : "/tmp/whereabouts.log",
  13. "log_level" : "debug",
  14. "etcd_host": "127.0.0.1:2379",
  15. "range": "%s",
  16. "gateway": "%s",
  17. "routes": [
  18. { "dst": "0.0.0.0/0" }
  19. ]
  20. }
  21. }`, ipRange, ipGateway)
  22.  
  23. args := &skel.CmdArgs{
  24. ContainerID: "dummy",
  25. Netns: nspath,
  26. IfName: ifname,
  27. StdinData: []byte(conf),
  28. }
  29.  
  30. // Allocate the IP
  31. r, raw, err := testutils.CmdAddWithArgs(args, func() error {
  32. return cmdAdd(args)
  33. })
  34. Expect(err).NotTo(HaveOccurred())
  35. // fmt.Printf("!bang raw: %s\n", raw)
  36. Expect(strings.Index(string(raw), "\"version\":")).Should(BeNumerically(">", 0))
  37.  
  38. result, err := current.GetResult(r)
  39. Expect(err).NotTo(HaveOccurred())
  40.  
  41. // Gomega is cranky about slices with different caps
  42. Expect(*result.IPs[0]).To(Equal(
  43. current.IPConfig{
  44. Version: ipVersion,
  45. Address: mustCIDR(expectedAddress),
  46. Gateway: net.ParseIP(ipGateway),
  47. }))
  48.  
  49. // Release the IP
  50. err = testutils.CmdDelWithArgs(args, func() error {
  51. return cmdDel(args)
  52. })
  53. Expect(err).NotTo(HaveOccurred())
  54.  
  55. // Now, create the same thing again, and expect the same IP
  56. // That way we know it dealloced the IP and assigned it again.
  57. r, _, err = testutils.CmdAddWithArgs(args, func() error {
  58. return cmdAdd(args)
  59. })
  60. Expect(err).NotTo(HaveOccurred())
  61.  
  62. result, err = current.GetResult(r)
  63. Expect(err).NotTo(HaveOccurred())
  64.  
  65. Expect(*result.IPs[0]).To(Equal(
  66. current.IPConfig{
  67. Version: ipVersion,
  68. Address: mustCIDR(expectedAddress),
  69. Gateway: net.ParseIP(ipGateway),
  70. }))
  71.  
  72. // And we'll release the IP again.
  73. err = testutils.CmdDelWithArgs(args, func() error {
  74. return cmdDel(args)
  75. })
  76. Expect(err).NotTo(HaveOccurred())
  77. }
  78.  
  79. var _ = Describe("Whereabouts operations", func() {
  80. It("allocates and releases addresses on ADD/DEL", func() {
  81. ipVersion := "4"
  82. ipRange := "192.168.1.0/24"
  83. ipGateway := "192.168.10.1"
  84. expectedAddress := "192.168.1.1/24"
  85.  
  86. AllocateAndReleaseAddressesTest(ipVersion, ipRange, ipGateway, expectedAddress)
  87.  
  88. ipVersion = "6"
  89. ipRange = "2001::1/116"
  90. ipGateway = "2001::f:1"
  91. expectedAddress = "2001::1/116"
  92.  
  93. AllocateAndReleaseAddressesTest(ipVersion, ipRange, ipGateway, expectedAddress)
  94. })
  95.  
  96. ....***blah other tests***
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement