forked from redhat-developer/web-terminal-exec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations_test.go
More file actions
281 lines (263 loc) · 8.45 KB
/
Copy pathoperations_test.go
File metadata and controls
281 lines (263 loc) · 8.45 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright (c) 2019-2025 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
package operations
import (
"context"
"fmt"
"os"
"path"
"reflect"
"strings"
"testing"
"github.com/redhat-developer/web-terminal-exec/pkg/config"
"github.com/stretchr/testify/assert"
authenticationv1 "k8s.io/api/authentication/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
fakedynamic "k8s.io/client-go/dynamic/fake"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/rest"
k8stesting "k8s.io/client-go/testing"
"sigs.k8s.io/yaml"
)
func TestGetCurrentUserUID(t *testing.T) {
const expectedUID = "test-user-uid"
tests := []struct {
name string
provider ClientProvider
errRegexp string
expectedUID string
}{
{
name: "Should return UID from OpenShift User API",
provider: testUserIDClientProvider{
userAPIUID: expectedUID,
},
expectedUID: expectedUID,
},
{
name: "Should fall back to SelfSubjectReview when OpenShift User API is unavailable",
provider: testUserIDClientProvider{
returnUserAPIError: apierrors.NewNotFound(schema.GroupResource{Group: "user.openshift.io", Resource: "users"}, "~"),
userUID: expectedUID,
},
expectedUID: expectedUID,
},
{
name: "Should return error when client creation fails",
provider: testUserIDClientProvider{
returnClientError: true,
},
errRegexp: "failed to create client to check user info",
},
{
name: "Should return error when both user lookups fail",
provider: testUserIDClientProvider{
returnUserAPIError: apierrors.NewNotFound(schema.GroupResource{Group: "user.openshift.io", Resource: "users"}, "~"),
returnReviewError: apierrors.NewNotFound(schema.GroupResource{Group: "authentication.k8s.io", Resource: "selfsubjectreviews"}, "self"),
},
errRegexp: "failed to get current user information",
},
{
name: "Should allow empty UID from OpenShift User API for kube:admin",
provider: testUserIDClientProvider{
userAPIUID: "",
emptyUserAPIUID: true,
},
expectedUID: "",
},
{
name: "Should return error when SelfSubjectReview returns empty UID on fallback",
provider: testUserIDClientProvider{
returnUserAPIError: apierrors.NewNotFound(schema.GroupResource{Group: "user.openshift.io", Resource: "users"}, "~"),
userUID: "",
},
errRegexp: "SelfSubjectReview returned empty UID",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
uid, err := GetCurrentUserUID("test-token", tt.provider)
if tt.errRegexp != "" {
assert.Error(t, err)
assert.Regexp(t, tt.errRegexp, err.Error())
if tt.name == "Should return error when both user lookups fail" {
assert.Contains(t, err.Error(), "OpenShift User API error:")
assert.Contains(t, err.Error(), "SelfSubjectReview error:")
}
return
}
assert.NoError(t, err)
assert.Equal(t, tt.expectedUID, uid)
})
}
}
type testUserIDClientProvider struct {
userUID string
userAPIUID string
returnClientError bool
returnReviewError error
returnUserAPIError error
emptyUserAPIUID bool
}
func (p testUserIDClientProvider) NewDevWorkspaceClient() (dynamic.Interface, *rest.Config, error) {
return nil, nil, nil
}
func (p testUserIDClientProvider) NewClientWithToken(string) (kubernetes.Interface, *rest.Config, error) {
if p.returnClientError {
return nil, nil, fmt.Errorf("(TEST) expected error")
}
client := fake.NewSimpleClientset()
client.PrependReactor("create", "selfsubjectreviews", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
if p.returnReviewError != nil {
return true, nil, p.returnReviewError
}
return true, &authenticationv1.SelfSubjectReview{
Status: authenticationv1.SelfSubjectReviewStatus{
UserInfo: authenticationv1.UserInfo{
UID: p.userUID,
},
},
}, nil
})
return client, &rest.Config{}, nil
}
func (p testUserIDClientProvider) NewOpenShiftUserClient(string) (dynamic.Interface, *rest.Config, error) {
if p.returnUserAPIError != nil {
return fakedynamic.NewSimpleDynamicClient(&runtime.Scheme{}), &rest.Config{}, nil
}
if p.userAPIUID == "" && !p.emptyUserAPIUID {
return nil, nil, fmt.Errorf("(TEST) OpenShift User API not configured")
}
fakeUser := &unstructured.Unstructured{
Object: map[string]interface{}{
"metadata": map[string]interface{}{
"name": "~",
"uid": p.userAPIUID,
},
},
}
fakeUser.SetGroupVersionKind(schema.GroupVersionKind{
Group: "user.openshift.io",
Version: "v1",
Kind: "User",
})
client := fakedynamic.NewSimpleDynamicClient(&runtime.Scheme{}, fakeUser)
return client, &rest.Config{}, nil
}
func setConfigForTest() {
config.DevWorkspaceName = "test-workspace"
config.DevWorkspaceNamespace = "test-namespace"
config.DevWorkspaceID = "test-id"
}
func loadDevWorkspaceFromFile(t *testing.T) unstructured.Unstructured {
bytes, err := os.ReadFile("testdata/devworkspace.yaml")
if err != nil {
t.Fatal(err)
}
var workspace unstructured.Unstructured
if err := yaml.Unmarshal(bytes, &workspace); err != nil {
t.Fatal(err)
}
return workspace
}
func TestStopDevWorkspace(t *testing.T) {
setConfigForTest()
defer config.ResetConfigForTest()
workspace := loadDevWorkspaceFromFile(t)
fakeDynamic := fakedynamic.NewSimpleDynamicClient(&runtime.Scheme{}, &workspace)
err := StopDevWorkspace(fakeDynamic)
assert.NoError(t, err, "Should not return error when stopping workspace")
result, err := fakeDynamic.Resource(devworkspaceGVR).Namespace(workspace.GetNamespace()).Get(context.TODO(), workspace.GetName(), metav1.GetOptions{})
assert.NoError(t, err, "Unexpected error getting devworkspace")
assert.False(t, workspaceIsStarted(t, result), "Workspace should be stopped")
}
func TestGetCurrentWorkspacePod(t *testing.T) {
const expectedPodName = "test-terminal-pod"
t.Setenv("HOSTNAME", expectedPodName)
tests := []struct {
name string
podFilenames []string
errRegexp string
}{
{
name: "Simple test with one pod",
podFilenames: []string{"pod.yaml"},
},
{
name: "No pods in namespace",
podFilenames: []string{},
errRegexp: "no workspace pods found",
},
{
name: "Multiple pods in namespace",
podFilenames: []string{"pod.yaml", "alternate-pod.yaml"},
},
{
name: "Multiple pods in namespace but no terminal",
podFilenames: []string{"alternate-pod.yaml", "alternate-pod-2.yaml"},
errRegexp: "failed to get current workspace pod",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
setConfigForTest()
defer config.ResetConfigForTest()
var pods []runtime.Object
for _, filename := range tt.podFilenames {
pods = append(pods, loadPodFromFile(t, filename))
}
client := fake.NewSimpleClientset(pods...)
actualPod, err := GetCurrentWorkspacePod(client)
if tt.errRegexp != "" {
assert.Error(t, err)
assert.Regexp(t, tt.errRegexp, err.Error())
} else {
assert.NoError(t, err)
assert.Equal(t, expectedPodName, actualPod.Name)
}
})
}
}
func workspaceIsStarted(t *testing.T, workspace *unstructured.Unstructured) bool {
return readUnstructuredPath(t, workspace, reflect.TypeOf(false), "spec", "started").(bool)
}
func readUnstructuredPath(t *testing.T, obj *unstructured.Unstructured, resultType reflect.Type, fields ...string) interface{} {
var innerField map[string]interface{}
for i := 0; i < len(fields)-1; i++ {
temp, ok := obj.Object[fields[i]].(map[string]interface{})
if !ok {
t.Fatalf("Failed to read field '%s' in object", strings.Join(fields, ", "))
}
innerField = temp
}
result := innerField[fields[len(fields)-1]]
if reflect.TypeOf(result) != resultType {
t.Fatalf("Failed to read into parameter, types don't match")
}
return result
}
func loadPodFromFile(t *testing.T, filepath string) runtime.Object {
podbytes, err := os.ReadFile(path.Join("testdata", filepath))
if err != nil {
t.Fatal(err)
}
pod := &corev1.Pod{}
if err := yaml.Unmarshal(podbytes, pod); err != nil {
t.Fatal(err)
}
return pod
}