client_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package tunnelstore
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "reflect"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/google/uuid"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestDNSRouteUnmarshalResult(t *testing.T) {
  15. route := &DNSRoute{
  16. userHostname: "example.com",
  17. }
  18. result, err := route.UnmarshalResult(strings.NewReader(`{"success": true, "result": {"cname": "new"}}`))
  19. assert.NoError(t, err)
  20. assert.Equal(t, &DNSRouteResult{
  21. route: route,
  22. CName: ChangeNew,
  23. }, result)
  24. badJSON := []string{
  25. `abc`,
  26. `{"success": false, "result": {"cname": "new"}}`,
  27. `{"errors": [{"code": 1003, "message":"An A, AAAA or CNAME record already exists with that host"}], "result": {"cname": "new"}}`,
  28. `{"errors": [{"code": 1003, "message":"An A, AAAA or CNAME record already exists with that host"}, {"code": 1004, "message":"Cannot use tunnel as origin for non-proxied load balancer"}], "result": {"cname": "new"}}`,
  29. `{"result": {"cname": "new"}}`,
  30. `{"result": {"cname": "new"}}`,
  31. }
  32. for _, j := range badJSON {
  33. _, err = route.UnmarshalResult(strings.NewReader(j))
  34. assert.NotNil(t, err)
  35. }
  36. }
  37. func TestLBRouteUnmarshalResult(t *testing.T) {
  38. route := &LBRoute{
  39. lbName: "lb.example.com",
  40. lbPool: "pool",
  41. }
  42. result, err := route.UnmarshalResult(strings.NewReader(`{"success": true, "result": {"pool": "unchanged", "load_balancer": "updated"}}`))
  43. assert.NoError(t, err)
  44. assert.Equal(t, &LBRouteResult{
  45. route: route,
  46. LoadBalancer: ChangeUpdated,
  47. Pool: ChangeUnchanged,
  48. }, result)
  49. badJSON := []string{
  50. `abc`,
  51. `{"success": false, "result": {"pool": "unchanged", "load_balancer": "updated"}}`,
  52. `{"errors": [{"code": 1003, "message":"An A, AAAA or CNAME record already exists with that host"}], "result": {"pool": "unchanged", "load_balancer": "updated"}}`,
  53. `{"errors": [{"code": 1003, "message":"An A, AAAA or CNAME record already exists with that host"}, {"code": 1004, "message":"Cannot use tunnel as origin for non-proxied load balancer"}], "result": {"pool": "unchanged", "load_balancer": "updated"}}`,
  54. `{"result": {"pool": "unchanged", "load_balancer": "updated"}}`,
  55. }
  56. for _, j := range badJSON {
  57. _, err = route.UnmarshalResult(strings.NewReader(j))
  58. assert.NotNil(t, err)
  59. }
  60. }
  61. func TestLBRouteResultSuccessSummary(t *testing.T) {
  62. route := &LBRoute{
  63. lbName: "lb.example.com",
  64. lbPool: "POOL",
  65. }
  66. tests := []struct {
  67. lb Change
  68. pool Change
  69. expected string
  70. }{
  71. {ChangeNew, ChangeNew, "Created load balancer lb.example.com and added a new pool POOL with this tunnel as an origin"},
  72. {ChangeNew, ChangeUpdated, "Created load balancer lb.example.com with an existing pool POOL which was updated to use this tunnel as an origin"},
  73. {ChangeNew, ChangeUnchanged, "Created load balancer lb.example.com with an existing pool POOL which already has this tunnel as an origin"},
  74. {ChangeUpdated, ChangeNew, "Added new pool POOL with this tunnel as an origin to load balancer lb.example.com"},
  75. {ChangeUpdated, ChangeUpdated, "Updated pool POOL to use this tunnel as an origin and added it to load balancer lb.example.com"},
  76. {ChangeUpdated, ChangeUnchanged, "Added pool POOL, which already has this tunnel as an origin, to load balancer lb.example.com"},
  77. {ChangeUnchanged, ChangeNew, "Something went wrong: failed to modify load balancer lb.example.com with pool POOL; please check traffic manager configuration in the dashboard"},
  78. {ChangeUnchanged, ChangeUpdated, "Added this tunnel as an origin in pool POOL which is already used by load balancer lb.example.com"},
  79. {ChangeUnchanged, ChangeUnchanged, "Load balancer lb.example.com already uses pool POOL which has this tunnel as an origin"},
  80. {"", "", "Something went wrong: failed to modify load balancer lb.example.com with pool POOL; please check traffic manager configuration in the dashboard"},
  81. {"a", "b", "Something went wrong: failed to modify load balancer lb.example.com with pool POOL; please check traffic manager configuration in the dashboard"},
  82. }
  83. for i, tt := range tests {
  84. res := &LBRouteResult{
  85. route: route,
  86. LoadBalancer: tt.lb,
  87. Pool: tt.pool,
  88. }
  89. actual := res.SuccessSummary()
  90. assert.Equal(t, tt.expected, actual, "case %d", i+1)
  91. }
  92. }
  93. func Test_parseListTunnels(t *testing.T) {
  94. type args struct {
  95. body string
  96. }
  97. tests := []struct {
  98. name string
  99. args args
  100. want []*Tunnel
  101. wantErr bool
  102. }{
  103. {
  104. name: "empty list",
  105. args: args{body: `{"success": true, "result": []}`},
  106. want: []*Tunnel{},
  107. },
  108. {
  109. name: "success is false",
  110. args: args{body: `{"success": false, "result": []}`},
  111. wantErr: true,
  112. },
  113. {
  114. name: "errors are present",
  115. args: args{body: `{"errors": [{"code": 1003, "message":"An A, AAAA or CNAME record already exists with that host"}], "result": []}`},
  116. wantErr: true,
  117. },
  118. {
  119. name: "invalid response",
  120. args: args{body: `abc`},
  121. wantErr: true,
  122. },
  123. }
  124. for _, tt := range tests {
  125. t.Run(tt.name, func(t *testing.T) {
  126. body := ioutil.NopCloser(bytes.NewReader([]byte(tt.args.body)))
  127. got, err := parseListTunnels(body)
  128. if (err != nil) != tt.wantErr {
  129. t.Errorf("parseListTunnels() error = %v, wantErr %v", err, tt.wantErr)
  130. return
  131. }
  132. if !reflect.DeepEqual(got, tt.want) {
  133. t.Errorf("parseListTunnels() = %v, want %v", got, tt.want)
  134. }
  135. })
  136. }
  137. }
  138. func Test_unmarshalTunnel(t *testing.T) {
  139. type args struct {
  140. reader io.Reader
  141. }
  142. tests := []struct {
  143. name string
  144. args args
  145. want *Tunnel
  146. wantErr bool
  147. }{
  148. // TODO: Add test cases.
  149. }
  150. for _, tt := range tests {
  151. t.Run(tt.name, func(t *testing.T) {
  152. got, err := unmarshalTunnel(tt.args.reader)
  153. if (err != nil) != tt.wantErr {
  154. t.Errorf("unmarshalTunnel() error = %v, wantErr %v", err, tt.wantErr)
  155. return
  156. }
  157. if !reflect.DeepEqual(got, tt.want) {
  158. t.Errorf("unmarshalTunnel() = %v, want %v", got, tt.want)
  159. }
  160. })
  161. }
  162. }
  163. func TestUnmarshalTunnelOk(t *testing.T) {
  164. jsonBody := `{"success": true, "result": {"id": "00000000-0000-0000-0000-000000000000","name":"test","created_at":"0001-01-01T00:00:00Z","connections":[]}}`
  165. expected := Tunnel{
  166. ID: uuid.Nil,
  167. Name: "test",
  168. CreatedAt: time.Time{},
  169. Connections: []Connection{},
  170. }
  171. actual, err := unmarshalTunnel(bytes.NewReader([]byte(jsonBody)))
  172. assert.NoError(t, err)
  173. assert.Equal(t, &expected, actual)
  174. }
  175. func TestUnmarshalTunnelErr(t *testing.T) {
  176. tests := []string{
  177. `abc`,
  178. `{"success": true, "result": abc}`,
  179. `{"success": false, "result": {"id": "00000000-0000-0000-0000-000000000000","name":"test","created_at":"0001-01-01T00:00:00Z","connections":[]}}}`,
  180. `{"errors": [{"code": 1003, "message":"An A, AAAA or CNAME record already exists with that host"}], "result": {"id": "00000000-0000-0000-0000-000000000000","name":"test","created_at":"0001-01-01T00:00:00Z","connections":[]}}}`,
  181. }
  182. for i, test := range tests {
  183. _, err := unmarshalTunnel(bytes.NewReader([]byte(test)))
  184. assert.Error(t, err, fmt.Sprintf("Test #%v failed", i))
  185. }
  186. }