forked from ericchiang/k8s
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_test.go
163 lines (146 loc) · 4.07 KB
/
resource_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package k8s
import (
"testing"
"time"
metav1 "github.com/ericchiang/k8s/apis/meta/v1"
)
// Redefine types since all API groups import "github.com/ericchiang/k8s"
// We can't use them here because it'll create a circular import cycle.
type Pod struct {
Metadata *metav1.ObjectMeta
}
type PodList struct {
Metadata *metav1.ListMeta
}
func (p *Pod) GetMetadata() *metav1.ObjectMeta { return p.Metadata }
func (p *PodList) GetMetadata() *metav1.ListMeta { return p.Metadata }
type Deployment struct {
Metadata *metav1.ObjectMeta
}
type DeploymentList struct {
Metadata *metav1.ListMeta
}
func (p *Deployment) GetMetadata() *metav1.ObjectMeta { return p.Metadata }
func (p *DeploymentList) GetMetadata() *metav1.ListMeta { return p.Metadata }
type ClusterRole struct {
Metadata *metav1.ObjectMeta
}
type ClusterRoleList struct {
Metadata *metav1.ListMeta
}
func (p *ClusterRole) GetMetadata() *metav1.ObjectMeta { return p.Metadata }
func (p *ClusterRoleList) GetMetadata() *metav1.ListMeta { return p.Metadata }
func init() {
Register("", "v1", "pods", true, &Pod{})
RegisterList("", "v1", "pods", true, &PodList{})
Register("apps", "v1beta2", "deployments", true, &Deployment{})
RegisterList("apps", "v1beta2", "deployments", true, &DeploymentList{})
Register("rbac.authorization.k8s.io", "v1", "clusterroles", false, &ClusterRole{})
RegisterList("rbac.authorization.k8s.io", "v1", "clusterroles", false, &ClusterRoleList{})
}
func TestResourceURL(t *testing.T) {
tests := []struct {
name string
endpoint string
resource Resource
withName bool
options []Option
want string
wantErr bool
}{
{
name: "pod",
endpoint: "https://example.com",
resource: &Pod{
Metadata: &metav1.ObjectMeta{
Namespace: String("my-namespace"),
Name: String("my-pod"),
},
},
want: "https://example.com/api/v1/namespaces/my-namespace/pods",
},
{
name: "deployment",
endpoint: "https://example.com",
resource: &Deployment{
Metadata: &metav1.ObjectMeta{
Namespace: String("my-namespace"),
Name: String("my-deployment"),
},
},
want: "https://example.com/apis/apps/v1beta2/namespaces/my-namespace/deployments",
},
{
name: "deployment-with-name",
endpoint: "https://example.com",
resource: &Deployment{
Metadata: &metav1.ObjectMeta{
Namespace: String("my-namespace"),
Name: String("my-deployment"),
},
},
withName: true,
want: "https://example.com/apis/apps/v1beta2/namespaces/my-namespace/deployments/my-deployment",
},
{
name: "deployment-with-subresource",
endpoint: "https://example.com",
resource: &Deployment{
Metadata: &metav1.ObjectMeta{
Namespace: String("my-namespace"),
Name: String("my-deployment"),
},
},
withName: true,
options: []Option{
Subresource("status"),
},
want: "https://example.com/apis/apps/v1beta2/namespaces/my-namespace/deployments/my-deployment/status",
},
{
name: "pod-with-timeout",
endpoint: "https://example.com",
resource: &Pod{
Metadata: &metav1.ObjectMeta{
Namespace: String("my-namespace"),
Name: String("my-pod"),
},
},
options: []Option{
Timeout(time.Minute),
},
want: "https://example.com/api/v1/namespaces/my-namespace/pods?timeoutSeconds=60",
},
{
name: "pod-with-resource-version",
endpoint: "https://example.com",
resource: &Pod{
Metadata: &metav1.ObjectMeta{
Namespace: String("my-namespace"),
Name: String("my-pod"),
},
},
options: []Option{
ResourceVersion("foo"),
},
want: "https://example.com/api/v1/namespaces/my-namespace/pods?resourceVersion=foo",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := resourceURL(test.endpoint, test.resource, test.withName, test.options...)
if err != nil {
if test.wantErr {
return
}
t.Fatalf("constructing resource URL: %v", err)
}
if test.wantErr {
t.Fatal("expected error")
}
if test.want != got {
t.Errorf("wanted=%q, got=%q", test.want, got)
}
})
}
}