ip_route_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package cfapi
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "strings"
  7. "testing"
  8. "github.com/google/uuid"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestUnmarshalRoute(t *testing.T) {
  12. testCases := []struct {
  13. Json string
  14. HasVnet bool
  15. }{
  16. {
  17. `{
  18. "network":"10.1.2.40/29",
  19. "tunnel_id":"fba6ffea-807f-4e7a-a740-4184ee1b82c8",
  20. "comment":"test",
  21. "created_at":"2020-12-22T02:00:15.587008Z",
  22. "deleted_at":null
  23. }`,
  24. false,
  25. },
  26. {
  27. `{
  28. "network":"10.1.2.40/29",
  29. "tunnel_id":"fba6ffea-807f-4e7a-a740-4184ee1b82c8",
  30. "comment":"test",
  31. "created_at":"2020-12-22T02:00:15.587008Z",
  32. "deleted_at":null,
  33. "virtual_network_id":"38c95083-8191-4110-8339-3f438d44fdb9"
  34. }`,
  35. true,
  36. },
  37. }
  38. for _, testCase := range testCases {
  39. data := testCase.Json
  40. var r Route
  41. err := json.Unmarshal([]byte(data), &r)
  42. // Check everything worked
  43. require.NoError(t, err)
  44. require.Equal(t, uuid.MustParse("fba6ffea-807f-4e7a-a740-4184ee1b82c8"), r.TunnelID)
  45. require.Equal(t, "test", r.Comment)
  46. _, cidr, err := net.ParseCIDR("10.1.2.40/29")
  47. require.NoError(t, err)
  48. require.Equal(t, CIDR(*cidr), r.Network)
  49. require.Equal(t, "test", r.Comment)
  50. if testCase.HasVnet {
  51. require.Equal(t, uuid.MustParse("38c95083-8191-4110-8339-3f438d44fdb9"), *r.VNetID)
  52. } else {
  53. require.Nil(t, r.VNetID)
  54. }
  55. }
  56. }
  57. func TestDetailedRouteJsonRoundtrip(t *testing.T) {
  58. testCases := []struct {
  59. Json string
  60. HasVnet bool
  61. }{
  62. {
  63. `{
  64. "id":"91ebc578-cc99-4641-9937-0fb630505fa0",
  65. "network":"10.1.2.40/29",
  66. "tunnel_id":"fba6ffea-807f-4e7a-a740-4184ee1b82c8",
  67. "comment":"test",
  68. "created_at":"2020-12-22T02:00:15.587008Z",
  69. "deleted_at":"2021-01-14T05:01:42.183002Z",
  70. "tunnel_name":"Mr. Tun"
  71. }`,
  72. false,
  73. },
  74. {
  75. `{
  76. "id":"91ebc578-cc99-4641-9937-0fb630505fa0",
  77. "network":"10.1.2.40/29",
  78. "tunnel_id":"fba6ffea-807f-4e7a-a740-4184ee1b82c8",
  79. "virtual_network_id":"38c95083-8191-4110-8339-3f438d44fdb9",
  80. "comment":"test",
  81. "created_at":"2020-12-22T02:00:15.587008Z",
  82. "deleted_at":"2021-01-14T05:01:42.183002Z",
  83. "tunnel_name":"Mr. Tun"
  84. }`,
  85. true,
  86. },
  87. }
  88. for _, testCase := range testCases {
  89. data := testCase.Json
  90. var r DetailedRoute
  91. err := json.Unmarshal([]byte(data), &r)
  92. // Check everything worked
  93. require.NoError(t, err)
  94. require.Equal(t, uuid.MustParse("fba6ffea-807f-4e7a-a740-4184ee1b82c8"), r.TunnelID)
  95. require.Equal(t, "test", r.Comment)
  96. _, cidr, err := net.ParseCIDR("10.1.2.40/29")
  97. require.NoError(t, err)
  98. require.Equal(t, CIDR(*cidr), r.Network)
  99. require.Equal(t, "test", r.Comment)
  100. require.Equal(t, "Mr. Tun", r.TunnelName)
  101. if testCase.HasVnet {
  102. require.Equal(t, uuid.MustParse("38c95083-8191-4110-8339-3f438d44fdb9"), *r.VNetID)
  103. } else {
  104. require.Nil(t, r.VNetID)
  105. }
  106. bytes, err := json.Marshal(r)
  107. require.NoError(t, err)
  108. obtainedJson := string(bytes)
  109. data = strings.Replace(data, "\t", "", -1)
  110. data = strings.Replace(data, "\n", "", -1)
  111. require.Equal(t, data, obtainedJson)
  112. }
  113. }
  114. func TestMarshalNewRoute(t *testing.T) {
  115. _, network, err := net.ParseCIDR("1.2.3.4/32")
  116. require.NoError(t, err)
  117. require.NotNil(t, network)
  118. vnetId := uuid.New()
  119. newRoutes := []NewRoute{
  120. {
  121. Network: *network,
  122. TunnelID: uuid.New(),
  123. Comment: "hi",
  124. },
  125. {
  126. Network: *network,
  127. TunnelID: uuid.New(),
  128. Comment: "hi",
  129. VNetID: &vnetId,
  130. },
  131. }
  132. for _, newRoute := range newRoutes {
  133. // Test where receiver is struct
  134. serialized, err := json.Marshal(newRoute)
  135. require.NoError(t, err)
  136. require.True(t, strings.Contains(string(serialized), "tunnel_id"))
  137. // Test where receiver is pointer to struct
  138. serialized, err = json.Marshal(&newRoute)
  139. require.NoError(t, err)
  140. require.True(t, strings.Contains(string(serialized), "tunnel_id"))
  141. if newRoute.VNetID == nil {
  142. require.False(t, strings.Contains(string(serialized), "virtual_network_id"))
  143. } else {
  144. require.True(t, strings.Contains(string(serialized), "virtual_network_id"))
  145. }
  146. }
  147. }
  148. func TestRouteTableString(t *testing.T) {
  149. _, network, err := net.ParseCIDR("1.2.3.4/32")
  150. require.NoError(t, err)
  151. require.NotNil(t, network)
  152. r := DetailedRoute{
  153. ID: uuid.Nil,
  154. Network: CIDR(*network),
  155. }
  156. row := r.TableString()
  157. fmt.Println(row)
  158. require.True(t, strings.HasPrefix(row, "00000000-0000-0000-0000-000000000000\t1.2.3.4/32"))
  159. }