hostname_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package cfapi
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestDNSRouteUnmarshalResult(t *testing.T) {
  8. route := &DNSRoute{
  9. userHostname: "example.com",
  10. }
  11. result, err := route.UnmarshalResult(strings.NewReader(`{"success": true, "result": {"cname": "new"}}`))
  12. assert.NoError(t, err)
  13. assert.Equal(t, &DNSRouteResult{
  14. route: route,
  15. CName: ChangeNew,
  16. }, result)
  17. badJSON := []string{
  18. `abc`,
  19. `{"success": false, "result": {"cname": "new"}}`,
  20. `{"errors": [{"code": 1003, "message":"An A, AAAA or CNAME record already exists with that host"}], "result": {"cname": "new"}}`,
  21. `{"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"}}`,
  22. `{"result": {"cname": "new"}}`,
  23. `{"result": {"cname": "new"}}`,
  24. }
  25. for _, j := range badJSON {
  26. _, err = route.UnmarshalResult(strings.NewReader(j))
  27. assert.NotNil(t, err)
  28. }
  29. }
  30. func TestLBRouteUnmarshalResult(t *testing.T) {
  31. route := &LBRoute{
  32. lbName: "lb.example.com",
  33. lbPool: "pool",
  34. }
  35. result, err := route.UnmarshalResult(strings.NewReader(`{"success": true, "result": {"pool": "unchanged", "load_balancer": "updated"}}`))
  36. assert.NoError(t, err)
  37. assert.Equal(t, &LBRouteResult{
  38. route: route,
  39. LoadBalancer: ChangeUpdated,
  40. Pool: ChangeUnchanged,
  41. }, result)
  42. badJSON := []string{
  43. `abc`,
  44. `{"success": false, "result": {"pool": "unchanged", "load_balancer": "updated"}}`,
  45. `{"errors": [{"code": 1003, "message":"An A, AAAA or CNAME record already exists with that host"}], "result": {"pool": "unchanged", "load_balancer": "updated"}}`,
  46. `{"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"}}`,
  47. `{"result": {"pool": "unchanged", "load_balancer": "updated"}}`,
  48. }
  49. for _, j := range badJSON {
  50. _, err = route.UnmarshalResult(strings.NewReader(j))
  51. assert.NotNil(t, err)
  52. }
  53. }
  54. func TestLBRouteResultSuccessSummary(t *testing.T) {
  55. route := &LBRoute{
  56. lbName: "lb.example.com",
  57. lbPool: "POOL",
  58. }
  59. tests := []struct {
  60. lb Change
  61. pool Change
  62. expected string
  63. }{
  64. {ChangeNew, ChangeNew, "Created load balancer lb.example.com and added a new pool POOL with this tunnel as an origin"},
  65. {ChangeNew, ChangeUpdated, "Created load balancer lb.example.com with an existing pool POOL which was updated to use this tunnel as an origin"},
  66. {ChangeNew, ChangeUnchanged, "Created load balancer lb.example.com with an existing pool POOL which already has this tunnel as an origin"},
  67. {ChangeUpdated, ChangeNew, "Added new pool POOL with this tunnel as an origin to load balancer lb.example.com"},
  68. {ChangeUpdated, ChangeUpdated, "Updated pool POOL to use this tunnel as an origin and added it to load balancer lb.example.com"},
  69. {ChangeUpdated, ChangeUnchanged, "Added pool POOL, which already has this tunnel as an origin, to load balancer lb.example.com"},
  70. {ChangeUnchanged, ChangeNew, "Something went wrong: failed to modify load balancer lb.example.com with pool POOL; please check traffic manager configuration in the dashboard"},
  71. {ChangeUnchanged, ChangeUpdated, "Added this tunnel as an origin in pool POOL which is already used by load balancer lb.example.com"},
  72. {ChangeUnchanged, ChangeUnchanged, "Load balancer lb.example.com already uses pool POOL which has this tunnel as an origin"},
  73. {"", "", "Something went wrong: failed to modify load balancer lb.example.com with pool POOL; please check traffic manager configuration in the dashboard"},
  74. {"a", "b", "Something went wrong: failed to modify load balancer lb.example.com with pool POOL; please check traffic manager configuration in the dashboard"},
  75. }
  76. for i, tt := range tests {
  77. res := &LBRouteResult{
  78. route: route,
  79. LoadBalancer: tt.lb,
  80. Pool: tt.pool,
  81. }
  82. actual := res.SuccessSummary()
  83. assert.Equal(t, tt.expected, actual, "case %d", i+1)
  84. }
  85. }