cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2026.1.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

cfapi/hostname_test.go

99lines · modeblame

6822e4f8Nuno Diegues4 years ago1package cfapi
85d0afd3Igor Postelnik5 years ago2
3import (
4"strings"
5"testing"
6
7"github.com/stretchr/testify/assert"
8)
9
10func TestDNSRouteUnmarshalResult(t *testing.T) {
11route := &DNSRoute{
12userHostname: "example.com",
13}
14
61f3fab7Adam Chalmers5 years ago15result, err := route.UnmarshalResult(strings.NewReader(`{"success": true, "result": {"cname": "new"}}`))
85d0afd3Igor Postelnik5 years ago16
17assert.NoError(t, err)
18assert.Equal(t, &DNSRouteResult{
19route: route,
20CName: ChangeNew,
21}, result)
22
61f3fab7Adam Chalmers5 years ago23badJSON := []string{
24`abc`,
25`{"success": false, "result": {"cname": "new"}}`,
812244d7cthuang5 years ago26`{"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"}}`,
61f3fab7Adam Chalmers5 years ago28`{"result": {"cname": "new"}}`,
29`{"result": {"cname": "new"}}`,
30}
31
32for _, j := range badJSON {
33_, err = route.UnmarshalResult(strings.NewReader(j))
34assert.NotNil(t, err)
35}
85d0afd3Igor Postelnik5 years ago36}
37
38func TestLBRouteUnmarshalResult(t *testing.T) {
39route := &LBRoute{
40lbName: "lb.example.com",
41lbPool: "pool",
42}
43
61f3fab7Adam Chalmers5 years ago44result, err := route.UnmarshalResult(strings.NewReader(`{"success": true, "result": {"pool": "unchanged", "load_balancer": "updated"}}`))
85d0afd3Igor Postelnik5 years ago45
46assert.NoError(t, err)
47assert.Equal(t, &LBRouteResult{
48route: route,
49LoadBalancer: ChangeUpdated,
50Pool: ChangeUnchanged,
51}, result)
52
61f3fab7Adam Chalmers5 years ago53badJSON := []string{
54`abc`,
55`{"success": false, "result": {"pool": "unchanged", "load_balancer": "updated"}}`,
812244d7cthuang5 years ago56`{"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"}}`,
61f3fab7Adam Chalmers5 years ago58`{"result": {"pool": "unchanged", "load_balancer": "updated"}}`,
59}
60
61for _, j := range badJSON {
62_, err = route.UnmarshalResult(strings.NewReader(j))
63assert.NotNil(t, err)
64}
85d0afd3Igor Postelnik5 years ago65}
66
67func TestLBRouteResultSuccessSummary(t *testing.T) {
68route := &LBRoute{
69lbName: "lb.example.com",
70lbPool: "POOL",
71}
72
73tests := []struct {
74lb Change
75pool Change
76expected string
77}{
61f3fab7Adam Chalmers5 years ago78{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"},
85d0afd3Igor Postelnik5 years ago89}
90for i, tt := range tests {
91res := &LBRouteResult{
92route: route,
93LoadBalancer: tt.lb,
94Pool: tt.pool,
61f3fab7Adam Chalmers5 years ago95}
85d0afd3Igor Postelnik5 years ago96actual := res.SuccessSummary()
97assert.Equal(t, tt.expected, actual, "case %d", i+1)
98}
99}