Skip to content

Commit 5af11e7

Browse files
committed
Add durable proxy acceptance fixtures
1 parent bfef988 commit 5af11e7

4 files changed

Lines changed: 204 additions & 0 deletions

File tree

internal/acctest/acctest.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,48 @@ func cleanupProfile(t testing.TB, client profileCleaner, projectID, id string) {
201201
})
202202
}
203203

204+
// CleanupProxy registers a cleanup that deletes the proxy from projectID;
205+
// empty means the env-configured default project.
206+
func CleanupProxy(t testing.TB, projectID, id string) {
207+
t.Helper()
208+
209+
cleanupProxy(t, ClientFromEnv(), projectID, id)
210+
}
211+
212+
type proxyCleaner interface {
213+
DefaultProjectID() string
214+
DeleteProxy(context.Context, string, string) error
215+
}
216+
217+
func cleanupProxy(t testing.TB, client proxyCleaner, projectID, id string) {
218+
t.Helper()
219+
220+
if id == "" {
221+
return
222+
}
223+
if !AcceptanceEnabled() {
224+
t.Fatalf("%s must be set to clean up Kernel acceptance test resources", EnvAcceptance)
225+
return
226+
}
227+
if os.Getenv(EnvAPIKey) == "" {
228+
t.Fatalf("%s must be set to clean up Kernel acceptance test resources", EnvAPIKey)
229+
return
230+
}
231+
232+
if projectID == "" {
233+
projectID = client.DefaultProjectID()
234+
}
235+
236+
t.Cleanup(func() {
237+
ctx, cancel := context.WithTimeout(context.Background(), cleanupTimeout)
238+
defer cancel()
239+
240+
if err := client.DeleteProxy(ctx, projectID, id); err != nil && !IsNotFound(err) {
241+
t.Errorf("cleanup Kernel proxy %s: %v", id, err)
242+
}
243+
})
244+
}
245+
204246
func ClientFromEnv() kernelclient.Clients {
205247
return kernelclient.New(kernelclient.Config{
206248
APIKey: os.Getenv(EnvAPIKey),

internal/acctest/proxy_test.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package acctest
2+
3+
import (
4+
"context"
5+
"errors"
6+
"testing"
7+
)
8+
9+
type fakeProxyCleaner struct {
10+
defaultProjectID string
11+
delete func(context.Context, string, string) error
12+
}
13+
14+
func (f fakeProxyCleaner) DefaultProjectID() string {
15+
return f.defaultProjectID
16+
}
17+
18+
func (f fakeProxyCleaner) DeleteProxy(ctx context.Context, projectID, id string) error {
19+
return f.delete(ctx, projectID, id)
20+
}
21+
22+
func TestCleanupProxy(t *testing.T) {
23+
tests := map[string]struct {
24+
acceptance string
25+
apiKey string
26+
projectID string
27+
id string
28+
deleteErr error
29+
wantProject string
30+
wantCleanups int
31+
wantDelete bool
32+
wantFailure bool
33+
}{
34+
"empty ID is ignored": {},
35+
"acceptance disabled": {
36+
apiKey: "test-key",
37+
id: "proxy_123",
38+
wantFailure: true,
39+
},
40+
"API key missing": {
41+
acceptance: "1",
42+
id: "proxy_123",
43+
wantFailure: true,
44+
},
45+
"default project is used": {
46+
acceptance: "1",
47+
apiKey: "test-key",
48+
id: "proxy_123",
49+
wantProject: "proj_env",
50+
wantCleanups: 1,
51+
wantDelete: true,
52+
},
53+
"explicit project is used": {
54+
acceptance: "1",
55+
apiKey: "test-key",
56+
projectID: "proj_explicit",
57+
id: "proxy_123",
58+
wantProject: "proj_explicit",
59+
wantCleanups: 1,
60+
wantDelete: true,
61+
},
62+
"not found is already clean": {
63+
acceptance: "1",
64+
apiKey: "test-key",
65+
id: "proxy_123",
66+
deleteErr: notFoundAPIError(),
67+
wantProject: "proj_env",
68+
wantCleanups: 1,
69+
wantDelete: true,
70+
},
71+
"delete error is reported": {
72+
acceptance: "1",
73+
apiKey: "test-key",
74+
id: "proxy_123",
75+
deleteErr: errors.New("connection reset"),
76+
wantProject: "proj_env",
77+
wantCleanups: 1,
78+
wantDelete: true,
79+
wantFailure: true,
80+
},
81+
}
82+
83+
for name, test := range tests {
84+
t.Run(name, func(t *testing.T) {
85+
t.Setenv(EnvAcceptance, test.acceptance)
86+
t.Setenv(EnvAPIKey, test.apiKey)
87+
88+
var gotID, gotProjectID string
89+
deleteCalled := false
90+
deadlineSet := false
91+
recorder := &testRecorder{TB: t}
92+
cleanupProxy(recorder, fakeProxyCleaner{
93+
defaultProjectID: "proj_env",
94+
delete: func(ctx context.Context, projectID, id string) error {
95+
deleteCalled = true
96+
_, deadlineSet = ctx.Deadline()
97+
gotID = id
98+
gotProjectID = projectID
99+
return test.deleteErr
100+
},
101+
}, test.projectID, test.id)
102+
103+
if got, want := len(recorder.cleanups), test.wantCleanups; got != want {
104+
t.Fatalf("cleanupProxy registered %d cleanups, want %d", got, want)
105+
}
106+
if test.wantCleanups == 1 {
107+
recorder.cleanups[0]()
108+
}
109+
if recorder.failed != test.wantFailure {
110+
t.Fatalf("cleanupProxy failure = %t, want %t", recorder.failed, test.wantFailure)
111+
}
112+
if deleteCalled != test.wantDelete {
113+
t.Fatalf("cleanupProxy called delete = %t, want %t", deleteCalled, test.wantDelete)
114+
}
115+
if deleteCalled && !deadlineSet {
116+
t.Fatal("cleanupProxy called delete without a context deadline")
117+
}
118+
if test.wantDelete && gotID != test.id {
119+
t.Fatalf("cleanup id = %q, want %q", gotID, test.id)
120+
}
121+
if test.wantDelete && gotProjectID != test.wantProject {
122+
t.Fatalf("cleanup project = %q, want %q", gotProjectID, test.wantProject)
123+
}
124+
})
125+
}
126+
}

internal/kernelclient/client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ func (c Clients) GetProxy(ctx context.Context, projectID, id string) (*kernel.Pr
103103
return c.proxies.Get(ctx, id, scope(projectID)...)
104104
}
105105

106+
func (c Clients) CreateProxy(ctx context.Context, projectID string, params kernel.ProxyNewParams) (*kernel.ProxyNewResponse, error) {
107+
return c.proxies.New(ctx, params, scope(projectID, noMutationRetries())...)
108+
}
109+
110+
func (c Clients) DeleteProxy(ctx context.Context, projectID, id string) error {
111+
return c.proxies.Delete(ctx, id, scope(projectID, noMutationRetries())...)
112+
}
113+
106114
func (c Clients) ListProxyPage(ctx context.Context, projectID string, offset int64) (ProxyPage, error) {
107115
var raw *http.Response
108116
params := kernel.ProxyListParams{

internal/kernelclient/client_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,26 @@ func TestMutationsDisableSDKRetriesAndUseExpectedScope(t *testing.T) {
350350
return clients.DeleteProfile(ctx, "project_123", "profile_123")
351351
},
352352
},
353+
"proxy create": {
354+
method: http.MethodPost,
355+
path: "/proxies",
356+
projectID: "project_123",
357+
call: func(ctx context.Context, clients Clients) error {
358+
_, err := clients.CreateProxy(ctx, "project_123", kernel.ProxyNewParams{
359+
Name: kernel.String("Proxy"),
360+
Type: kernel.ProxyNewParamsTypeDatacenter,
361+
})
362+
return err
363+
},
364+
},
365+
"proxy delete": {
366+
method: http.MethodDelete,
367+
path: "/proxies/proxy_123",
368+
projectID: "project_123",
369+
call: func(ctx context.Context, clients Clients) error {
370+
return clients.DeleteProxy(ctx, "project_123", "proxy_123")
371+
},
372+
},
353373
"browser pool create": {
354374
method: http.MethodPost,
355375
path: "/browser_pools",
@@ -468,6 +488,14 @@ func TestClientsDoNotExposeProfileArchiveMethods(t *testing.T) {
468488
}
469489
}
470490

491+
func TestClientsDoNotExposeProxyHealthCheck(t *testing.T) {
492+
t.Parallel()
493+
494+
if _, ok := reflect.TypeOf(Clients{}).MethodByName("CheckProxy"); ok {
495+
t.Fatal("Clients exposes runtime proxy health check")
496+
}
497+
}
498+
471499
type capturedRequest struct {
472500
Method string
473501
Path string

0 commit comments

Comments
 (0)