This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
integrationtest_test.go
112 lines (84 loc) · 3.51 KB
/
integrationtest_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//go:build integrationtest
// +build integrationtest
package bunny_test
import (
"context"
"os"
"testing"
"github.com/google/uuid"
bunny "github.com/simplesurance/bunny-go"
"github.com/stretchr/testify/require"
)
const envVarApiKeyName = "BUNNY_API_KEY"
func newClient(t *testing.T) *bunny.Client {
t.Helper()
apiKey := os.Getenv(envVarApiKeyName)
if apiKey == "" {
t.Fatalf("the environment variable %q is unset or empty, it must be set to a valid API key that is used for running integration tests",
envVarApiKeyName)
}
return bunny.NewClient(apiKey, bunny.WithHTTPRequestLogger(t.Logf))
}
func randomResourceName(resource string) string {
return "bunny-go-test-" + resource + "-" + uuid.New().String()
}
// createPullZone creates a Pull Zone via the bunny client and registers a
// testing cleanup function to remove it when the test terminates.
// If creating the Pull Zone fails, t.Fatal is called.
func createPullZone(t *testing.T, clt *bunny.Client, opts *bunny.PullZoneAddOptions) *bunny.PullZone {
t.Helper()
pz, err := clt.PullZone.Add(context.Background(), opts)
require.NoError(t, err, "creating pull zone failed")
require.NotNil(t, pz.ID, "add returned pull zone with nil id")
require.NotNil(t, pz.Name, "add returned pull zone with nil name")
t.Logf("created pull zone: %q, id: %d", *pz.Name, *pz.ID)
t.Cleanup(func() {
err := clt.PullZone.Delete(context.Background(), *pz.ID)
if err != nil {
t.Errorf("could not delete pull zone (id: %d, name: %q) on test cleanup: %s", *pz.ID, *pz.Name, err)
return
}
t.Logf("cleanup: deleted pull zone: %q, id: %d", *pz.Name, *pz.ID)
})
return pz
}
// createStorageZone creates a Storage Zone via the bunny client and registers a
// testing cleanup function to remove it when the test terminates.
// If creating the Storage Zone fails, t.Fatal is called.
func createStorageZone(t *testing.T, clt *bunny.Client, opts *bunny.StorageZoneAddOptions) *bunny.StorageZone {
t.Helper()
sz, err := clt.StorageZone.Add(context.Background(), opts)
require.NoError(t, err, "creating storage zone failed")
require.NotNil(t, sz.ID, "add returned storage zone with nil id")
require.NotNil(t, sz.Name, "add returned storage zone with nil name")
t.Logf("created storage zone: %q, id: %d", *sz.Name, *sz.ID)
t.Cleanup(func() {
err := clt.StorageZone.Delete(context.Background(), *sz.ID)
if err != nil {
t.Errorf("could not delete storage zone (id: %d, name: %q) on test cleanup: %s", *sz.ID, *sz.Name, err)
return
}
t.Logf("cleanup: deleted storage zone: %q, id: %d", *sz.Name, *sz.ID)
})
return sz
}
// createVideoLibrary creates a Video Library via the bunny client and registers a
// testing cleanup function to remove it when the test terminates.
// If creating the Video Library fails, t.Fatal is called.
func createVideoLibrary(t *testing.T, clt *bunny.Client, opts *bunny.VideoLibraryAddOptions) *bunny.VideoLibrary {
t.Helper()
vl, err := clt.VideoLibrary.Add(context.Background(), opts)
require.NoError(t, err, "creating video library failed")
require.NotNil(t, vl.ID, "add returned video library with nil id")
require.NotNil(t, vl.Name, "add returned video library with nil name")
t.Logf("created video library: %q, id: %d", *vl.Name, *vl.ID)
t.Cleanup(func() {
err := clt.VideoLibrary.Delete(context.Background(), *vl.ID)
if err != nil {
t.Errorf("could not delete video library (id: %d, name: %q) on test cleanup: %s", *vl.ID, *vl.Name, err)
return
}
t.Logf("cleanup: deleted video library: %q, id: %d", *vl.Name, *vl.ID)
})
return vl
}