cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
cfapi/tunnel_test.go
166lines · modecode
| 1 | package cfapi |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "net" |
| 6 | "reflect" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/google/uuid" |
| 12 | "github.com/stretchr/testify/assert" |
| 13 | "github.com/stretchr/testify/require" |
| 14 | ) |
| 15 | |
| 16 | var loc, _ = time.LoadLocation("UTC") |
| 17 | |
| 18 | func Test_unmarshalTunnel(t *testing.T) { |
| 19 | type args struct { |
| 20 | body string |
| 21 | } |
| 22 | tests := []struct { |
| 23 | name string |
| 24 | args args |
| 25 | want *Tunnel |
| 26 | wantErr bool |
| 27 | }{ |
| 28 | { |
| 29 | name: "empty list", |
| 30 | args: args{body: `{"success": true, "result": {"id":"b34cc7ce-925b-46ee-bc23-4cb5c18d8292","created_at":"2021-07-29T13:46:14.090955Z","deleted_at":"2021-07-29T14:07:27.559047Z","name":"qt-bIWWN7D662ogh61pCPfu5s2XgqFY1OyV","account_id":6946212,"account_tag":"5ab4e9dfbd435d24068829fda0077963","conns_active_at":null,"conns_inactive_at":"2021-07-29T13:47:22.548482Z","tun_type":"cfd_tunnel","metadata":{"qtid":"a6fJROgkXutNruBGaJjD"}}}`}, |
| 31 | want: &Tunnel{ |
| 32 | ID: uuid.MustParse("b34cc7ce-925b-46ee-bc23-4cb5c18d8292"), |
| 33 | Name: "qt-bIWWN7D662ogh61pCPfu5s2XgqFY1OyV", |
| 34 | CreatedAt: time.Date(2021, 07, 29, 13, 46, 14, 90955000, loc), |
| 35 | DeletedAt: time.Date(2021, 07, 29, 14, 7, 27, 559047000, loc), |
| 36 | Connections: nil, |
| 37 | }, |
| 38 | }, |
| 39 | } |
| 40 | for _, tt := range tests { |
| 41 | t.Run(tt.name, func(t *testing.T) { |
| 42 | got, err := unmarshalTunnel(strings.NewReader(tt.args.body)) |
| 43 | if (err != nil) != tt.wantErr { |
| 44 | t.Errorf("unmarshalTunnel() error = %v, wantErr %v", err, tt.wantErr) |
| 45 | return |
| 46 | } |
| 47 | if !reflect.DeepEqual(got, tt.want) { |
| 48 | t.Errorf("unmarshalTunnel() = %v, want %v", got, tt.want) |
| 49 | } |
| 50 | }) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestUnmarshalTunnelOk(t *testing.T) { |
| 55 | jsonBody := `{"success": true, "result": {"id": "00000000-0000-0000-0000-000000000000","name":"test","created_at":"0001-01-01T00:00:00Z","connections":[]}}` |
| 56 | expected := Tunnel{ |
| 57 | ID: uuid.Nil, |
| 58 | Name: "test", |
| 59 | CreatedAt: time.Time{}, |
| 60 | Connections: []Connection{}, |
| 61 | } |
| 62 | actual, err := unmarshalTunnel(bytes.NewReader([]byte(jsonBody))) |
| 63 | require.NoError(t, err) |
| 64 | require.Equal(t, &expected, actual) |
| 65 | } |
| 66 | |
| 67 | func TestUnmarshalTunnelErr(t *testing.T) { |
| 68 | tests := []string{ |
| 69 | `abc`, |
| 70 | `{"success": true, "result": abc}`, |
| 71 | `{"success": false, "result": {"id": "00000000-0000-0000-0000-000000000000","name":"test","created_at":"0001-01-01T00:00:00Z","connections":[]}}}`, |
| 72 | `{"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":[]}}}`, |
| 73 | } |
| 74 | |
| 75 | for i, test := range tests { |
| 76 | _, err := unmarshalTunnel(bytes.NewReader([]byte(test))) |
| 77 | assert.Error(t, err, "Test #%v failed", i) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestManagementResource_String(t *testing.T) { |
| 82 | tests := []struct { |
| 83 | name string |
| 84 | resource ManagementResource |
| 85 | want string |
| 86 | }{ |
| 87 | { |
| 88 | name: "Logs", |
| 89 | resource: Logs, |
| 90 | want: "logs", |
| 91 | }, |
| 92 | { |
| 93 | name: "Admin", |
| 94 | resource: Admin, |
| 95 | want: "admin", |
| 96 | }, |
| 97 | { |
| 98 | name: "HostDetails", |
| 99 | resource: HostDetails, |
| 100 | want: "host_details", |
| 101 | }, |
| 102 | } |
| 103 | for _, tt := range tests { |
| 104 | t.Run(tt.name, func(t *testing.T) { |
| 105 | assert.Equal(t, tt.want, tt.resource.String()) |
| 106 | }) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestManagementResource_String_Unknown(t *testing.T) { |
| 111 | unknown := ManagementResource(999) |
| 112 | assert.Equal(t, "", unknown.String()) |
| 113 | } |
| 114 | |
| 115 | func TestManagementEndpointPath(t *testing.T) { |
| 116 | tunnelID := uuid.MustParse("b34cc7ce-925b-46ee-bc23-4cb5c18d8292") |
| 117 | |
| 118 | tests := []struct { |
| 119 | name string |
| 120 | resource ManagementResource |
| 121 | want string |
| 122 | }{ |
| 123 | { |
| 124 | name: "Logs resource", |
| 125 | resource: Logs, |
| 126 | want: "b34cc7ce-925b-46ee-bc23-4cb5c18d8292/management/logs", |
| 127 | }, |
| 128 | { |
| 129 | name: "Admin resource", |
| 130 | resource: Admin, |
| 131 | want: "b34cc7ce-925b-46ee-bc23-4cb5c18d8292/management/admin", |
| 132 | }, |
| 133 | { |
| 134 | name: "HostDetails resource", |
| 135 | resource: HostDetails, |
| 136 | want: "b34cc7ce-925b-46ee-bc23-4cb5c18d8292/management/host_details", |
| 137 | }, |
| 138 | } |
| 139 | for _, tt := range tests { |
| 140 | t.Run(tt.name, func(t *testing.T) { |
| 141 | got := managementEndpointPath(tunnelID, tt.resource) |
| 142 | assert.Equal(t, tt.want, got) |
| 143 | }) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func TestUnmarshalConnections(t *testing.T) { |
| 148 | jsonBody := `{"success":true,"messages":[],"errors":[],"result":[{"id":"d4041254-91e3-4deb-bd94-b46e11680b1e","features":["ha-origin"],"version":"2021.2.5","arch":"darwin_amd64","conns":[{"colo_name":"LIS","id":"ac2286e5-c708-4588-a6a0-ba6b51940019","is_pending_reconnect":false,"origin_ip":"148.38.28.2","opened_at":"0001-01-01T00:00:00Z"}],"run_at":"0001-01-01T00:00:00Z"}]}` |
| 149 | expected := ActiveClient{ |
| 150 | ID: uuid.MustParse("d4041254-91e3-4deb-bd94-b46e11680b1e"), |
| 151 | Features: []string{"ha-origin"}, |
| 152 | Version: "2021.2.5", |
| 153 | Arch: "darwin_amd64", |
| 154 | RunAt: time.Time{}, |
| 155 | Connections: []Connection{{ |
| 156 | ID: uuid.MustParse("ac2286e5-c708-4588-a6a0-ba6b51940019"), |
| 157 | ColoName: "LIS", |
| 158 | IsPendingReconnect: false, |
| 159 | OriginIP: net.ParseIP("148.38.28.2"), |
| 160 | OpenedAt: time.Time{}, |
| 161 | }}, |
| 162 | } |
| 163 | actual, err := parseConnectionsDetails(bytes.NewReader([]byte(jsonBody))) |
| 164 | require.NoError(t, err) |
| 165 | assert.Equal(t, []*ActiveClient{&expected}, actual) |
| 166 | } |
| 167 | |