cloudflare/cloudflared

Public

mirrored fromhttps://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0c9014870a0624febb6f3fd63b0cd90101c94197

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

cfapi/hostname_test.go

99lines · modecode

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