forked from digitalocean/godo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry_test.go
119 lines (99 loc) · 2.46 KB
/
registry_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
113
114
115
116
117
118
119
package godo
import (
"encoding/json"
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
func TestRegistry_Create(t *testing.T) {
setup()
defer teardown()
want := &Registry{
Name: "foo",
}
createRequest := &RegistryCreateRequest{
Name: want.Name,
}
createResponseJSON := `
{
"registry": {
"name": "foo"
}
}`
mux.HandleFunc("/v2/registry", func(w http.ResponseWriter, r *http.Request) {
v := new(RegistryCreateRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
require.Equal(t, v, createRequest)
fmt.Fprint(w, createResponseJSON)
})
got, _, err := client.Registry.Create(ctx, createRequest)
require.NoError(t, err)
require.Equal(t, want, got)
}
func TestRegistry_Get(t *testing.T) {
setup()
defer teardown()
want := &Registry{
Name: "foo",
}
getResponseJSON := `
{
"registry": {
"name": "foo"
}
}`
mux.HandleFunc("/v2/registry", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, getResponseJSON)
})
got, _, err := client.Registry.Get(ctx)
require.NoError(t, err)
require.Equal(t, want, got)
}
func TestRegistry_Delete(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/registry", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.Registry.Delete(ctx)
require.NoError(t, err)
}
func TestRegistry_DockerCredentials(t *testing.T) {
returnedConfig := "this could be a docker config"
tests := []struct {
name string
params *RegistryDockerCredentialsRequest
expectedReadWrite string
}{
{
name: "read-only (default)",
params: &RegistryDockerCredentialsRequest{},
expectedReadWrite: "false",
},
{
name: "read/write",
params: &RegistryDockerCredentialsRequest{ReadWrite: true},
expectedReadWrite: "true",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/registry/docker-credentials", func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, test.expectedReadWrite, r.URL.Query().Get("read_write"))
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, returnedConfig)
})
got, _, err := client.Registry.DockerCredentials(ctx, test.params)
require.NoError(t, err)
require.Equal(t, []byte(returnedConfig), got.DockerConfigJSON)
})
}
}