address_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (c) 2016 Arista Networks, Inc.
  2. // Use of this source code is governed by the Apache License 2.0
  3. // that can be found in the COPYING file.
  4. package netns
  5. import (
  6. "testing"
  7. )
  8. func TestParseAddress(t *testing.T) {
  9. tests := []struct {
  10. desc string
  11. arg string
  12. vrf string
  13. addr string
  14. err bool
  15. }{{
  16. "Parse address with VRF",
  17. "vrf1/1.2.3.4:50",
  18. "ns-vrf1",
  19. "1.2.3.4:50",
  20. false,
  21. }, {
  22. "Parse address without VRF",
  23. "1.2.3.4:50",
  24. "",
  25. "1.2.3.4:50",
  26. false,
  27. }, {
  28. "Parse malformed input",
  29. "vrf1/1.2.3.4/24",
  30. "",
  31. "",
  32. true,
  33. }}
  34. for _, tt := range tests {
  35. vrf, addr, err := ParseAddress(tt.arg)
  36. if tt.err {
  37. if err == nil {
  38. t.Fatalf("%s: expected error, but got success", tt.desc)
  39. }
  40. } else {
  41. if err != nil {
  42. t.Fatalf("%s: expected success, but got error %s", tt.desc, err)
  43. }
  44. if addr != tt.addr {
  45. t.Fatalf("%s: expected addr %s, but got %s", tt.desc, tt.addr, addr)
  46. }
  47. if vrf != tt.vrf {
  48. t.Fatalf("%s: expected VRF %s, but got %s", tt.desc, tt.vrf, vrf)
  49. }
  50. }
  51. }
  52. }
  53. func TestVrfToNetNSTests(t *testing.T) {
  54. tests := []struct {
  55. desc string
  56. vrf string
  57. netNS string
  58. }{{
  59. "Empty VRF name",
  60. "",
  61. "",
  62. }, {
  63. "Default VRF",
  64. "default",
  65. "default",
  66. }, {
  67. "Regular VRF name",
  68. "cust1",
  69. "ns-cust1",
  70. }}
  71. for _, tt := range tests {
  72. netNS := VRFToNetNS(tt.vrf)
  73. if netNS != tt.netNS {
  74. t.Fatalf("%s: expected netNS %s, but got %s", tt.desc, tt.netNS,
  75. netNS)
  76. }
  77. }
  78. }