forked from kubeflow/kubeflow
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathnotebook_creation_test.go
302 lines (266 loc) · 10.2 KB
/
notebook_creation_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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package e2e
import (
"fmt"
"log"
"strings"
"testing"
"time"
nbv1 "github.com/kubeflow/kubeflow/components/notebook-controller/api/v1"
routev1 "github.com/openshift/api/route/v1"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
)
func creationTestSuite(t *testing.T) {
testCtx, err := NewTestContext()
require.NoError(t, err)
notebooksForSelectedDeploymentMode := notebooksForScenario(testCtx.testNotebooks, deploymentMode)
for _, nbContext := range notebooksForSelectedDeploymentMode {
// prepend Notebook name to every subtest
t.Run(nbContext.nbObjectMeta.Name, func(t *testing.T) {
t.Run("Creation of Notebook instance", func(t *testing.T) {
err = testCtx.testNotebookCreation(nbContext)
require.NoError(t, err, "error creating Notebook object ")
})
t.Run("Notebook Route Validation", func(t *testing.T) {
if deploymentMode == ServiceMesh {
t.Skipf("Skipping as it's not relevant for Service Mesh scenario")
}
err = testCtx.testNotebookRouteCreation(nbContext.nbObjectMeta)
require.NoError(t, err, "error testing Route for Notebook ")
})
t.Run("Notebook Network Policies Validation", func(t *testing.T) {
err = testCtx.testNetworkPolicyCreation(nbContext.nbObjectMeta)
require.NoError(t, err, "error testing Network Policies for Notebook ")
})
t.Run("Notebook Statefulset Validation", func(t *testing.T) {
err = testCtx.testNotebookValidation(nbContext.nbObjectMeta)
require.NoError(t, err, "error testing StatefulSet for Notebook ")
})
t.Run("Notebook OAuth sidecar Validation", func(t *testing.T) {
if deploymentMode == ServiceMesh {
t.Skipf("Skipping as it's not relevant for Service Mesh scenario")
}
err = testCtx.testNotebookOAuthSidecar(nbContext.nbObjectMeta)
require.NoError(t, err, "error testing sidecar for Notebook ")
})
t.Run("Verify Notebook Traffic", func(t *testing.T) {
err = testCtx.testNotebookTraffic(nbContext.nbObjectMeta)
require.NoError(t, err, "error testing Notebook traffic ")
})
t.Run("Verify Notebook Culling", func(t *testing.T) {
err = testCtx.testNotebookCulling(nbContext.nbObjectMeta)
require.NoError(t, err, "error testing Notebook culling ")
})
})
}
}
func (tc *testContext) testNotebookCreation(nbContext notebookContext) error {
testNotebook := &nbv1.Notebook{
ObjectMeta: *nbContext.nbObjectMeta,
Spec: *nbContext.nbSpec,
}
// Create test Notebook resource if not already created
notebookLookupKey := types.NamespacedName{Name: testNotebook.Name, Namespace: testNotebook.Namespace}
createdNotebook := nbv1.Notebook{}
err := tc.customClient.Get(tc.ctx, notebookLookupKey, &createdNotebook)
if err != nil {
if errors.IsNotFound(err) {
nberr := wait.Poll(tc.resourceRetryInterval, tc.resourceCreationTimeout, func() (done bool, err error) {
creationErr := tc.customClient.Create(tc.ctx, testNotebook)
if creationErr != nil {
log.Printf("Error creating Notebook resource %v: %v, trying again",
testNotebook.Name, creationErr)
return false, nil
} else {
return true, nil
}
})
if nberr != nil {
return fmt.Errorf("error creating test Notebook %s: %v", testNotebook.Name, nberr)
}
} else {
return fmt.Errorf("error getting test Notebook %s: %v", testNotebook.Name, err)
}
}
return nil
}
func (tc *testContext) testNotebookRouteCreation(nbMeta *metav1.ObjectMeta) error {
nbRoute, err := tc.getNotebookRoute(nbMeta)
if err != nil {
return fmt.Errorf("error getting Route for Notebook %v: %v", nbRoute.Name, err)
}
isReady := false
for _, ingress := range nbRoute.Status.Ingress {
if strings.Contains(ingress.Host, nbRoute.Name) {
for _, condition := range ingress.Conditions {
if condition.Type == routev1.RouteAdmitted && condition.Status == v1.ConditionTrue {
isReady = true
}
}
}
}
if !isReady {
return fmt.Errorf("Notebook Route %s is not Ready.", nbRoute.Name)
}
return err
}
func (tc *testContext) testNetworkPolicyCreation(nbMeta *metav1.ObjectMeta) error {
err := tc.ensureNetworkPolicyAllowingAccessToOnlyNotebookControllerExists(nbMeta)
if err != nil {
return err
}
if deploymentMode == OAuthProxy {
return tc.ensureOAuthNetworkPolicyExists(nbMeta, err)
}
return nil
}
func (tc *testContext) ensureOAuthNetworkPolicyExists(nbMeta *metav1.ObjectMeta, err error) error {
// Test Notebook Network policy that allows all requests on Notebook OAuth port
notebookOAuthNetworkPolicy, err := tc.getNotebookNetworkPolicy(nbMeta, nbMeta.Name+"-oauth-np")
if err != nil {
return fmt.Errorf("error getting network policy for Notebook OAuth port %v: %v", notebookOAuthNetworkPolicy.Name, err)
}
if len(notebookOAuthNetworkPolicy.Spec.PolicyTypes) == 0 || notebookOAuthNetworkPolicy.Spec.PolicyTypes[0] != netv1.PolicyTypeIngress {
return fmt.Errorf("invalid policy type. Expected value :%v", netv1.PolicyTypeIngress)
}
if len(notebookOAuthNetworkPolicy.Spec.Ingress) == 0 {
return fmt.Errorf("invalid network policy, should contain ingress rule")
} else if len(notebookOAuthNetworkPolicy.Spec.Ingress[0].Ports) != 0 {
isNotebookPort := false
for _, notebookport := range notebookOAuthNetworkPolicy.Spec.Ingress[0].Ports {
if notebookport.Port.IntVal == 8443 {
isNotebookPort = true
}
}
if !isNotebookPort {
return fmt.Errorf("invalid Network Policy comfiguration")
}
}
return err
}
func (tc *testContext) ensureNetworkPolicyAllowingAccessToOnlyNotebookControllerExists(nbMeta *metav1.ObjectMeta) error {
// Test Notebook Network Policy that allows access only to Notebook Controller
notebookNetworkPolicy, err := tc.getNotebookNetworkPolicy(nbMeta, nbMeta.Name+"-ctrl-np")
if err != nil {
return fmt.Errorf("error getting network policy for Notebook %v: %v", notebookNetworkPolicy.Name, err)
}
if len(notebookNetworkPolicy.Spec.PolicyTypes) == 0 || notebookNetworkPolicy.Spec.PolicyTypes[0] != netv1.PolicyTypeIngress {
return fmt.Errorf("invalid policy type. Expected value :%v", netv1.PolicyTypeIngress)
}
if len(notebookNetworkPolicy.Spec.Ingress) == 0 {
return fmt.Errorf("invalid network policy, should contain ingress rule")
} else if len(notebookNetworkPolicy.Spec.Ingress[0].Ports) != 0 {
isNotebookPort := false
for _, notebookport := range notebookNetworkPolicy.Spec.Ingress[0].Ports {
if notebookport.Port.IntVal == 8888 {
isNotebookPort = true
}
}
if !isNotebookPort {
return fmt.Errorf("invalid Network Policy comfiguration")
}
} else if len(notebookNetworkPolicy.Spec.Ingress[0].From) == 0 {
return fmt.Errorf("invalid Network Policy comfiguration")
}
return nil
}
func (tc *testContext) testNotebookValidation(nbMeta *metav1.ObjectMeta) error {
// Verify StatefulSet is running
err := wait.Poll(tc.resourceRetryInterval, tc.resourceCreationTimeout, func() (done bool, err error) {
notebookStatefulSet, err1 := tc.kubeClient.AppsV1().StatefulSets(tc.testNamespace).Get(tc.ctx,
nbMeta.Name, metav1.GetOptions{})
if err1 != nil {
if errors.IsNotFound(err1) {
return false, nil
} else {
log.Printf("Failed to get %s statefulset", nbMeta.Name)
return false, err1
}
}
if notebookStatefulSet.Status.AvailableReplicas == 1 &&
notebookStatefulSet.Status.ReadyReplicas == 1 {
return true, nil
}
return false, nil
})
if err != nil {
return fmt.Errorf("error validating notebook StatefulSet: %s", err)
}
return nil
}
func (tc *testContext) testNotebookOAuthSidecar(nbMeta *metav1.ObjectMeta) error {
nbPods, err := tc.kubeClient.CoreV1().Pods(tc.testNamespace).List(tc.ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("statefulset=%s", nbMeta.Name)})
if err != nil {
return fmt.Errorf("error retrieving Notebook pods :%v", err)
}
for _, nbpod := range nbPods.Items {
if nbpod.Status.Phase != v1.PodRunning {
return fmt.Errorf("notebook pod %v is not in Running phase", nbpod.Name)
}
for _, containerStatus := range nbpod.Status.ContainerStatuses {
if containerStatus.Name == "oauth-proxy" {
if !containerStatus.Ready {
return fmt.Errorf("oauth-proxy container is not in Ready state for pod %v", nbpod.Name)
}
}
}
}
return nil
}
func (tc *testContext) testNotebookTraffic(nbMeta *metav1.ObjectMeta) error {
resp, err := tc.curlNotebookEndpoint(*nbMeta)
if err != nil {
return fmt.Errorf("error accessing Notebook Endpoint: %v ", err)
}
if resp.StatusCode != 200 {
return fmt.Errorf("Unexpected response from Notebook Endpoint")
}
return nil
}
func (tc *testContext) testNotebookCulling(nbMeta *metav1.ObjectMeta) error {
// Create Configmap with culling configuration
cullingConfigMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "notebook-controller-culler-config",
Namespace: tc.testNamespace,
},
Data: map[string]string{
"ENABLE_CULLING": "true",
"CULL_IDLE_TIME": "2",
"IDLENESS_CHECK_PERIOD": "1",
},
}
_, err := tc.kubeClient.CoreV1().ConfigMaps(tc.testNamespace).Create(tc.ctx, cullingConfigMap,
metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("error creating configmapnotebook-controller-culler-config: %v", err)
}
// Restart the deployment to get changes from configmap
controllerDeployment, err := tc.kubeClient.AppsV1().Deployments(tc.testNamespace).Get(tc.ctx,
"notebook-controller-deployment", metav1.GetOptions{})
if err != nil {
return fmt.Errorf("error getting deployment %v: %v", controllerDeployment.Name, err)
}
defer tc.revertCullingConfiguration(cullingConfigMap.ObjectMeta, controllerDeployment.ObjectMeta)
err = tc.rolloutDeployment(controllerDeployment.ObjectMeta)
if err != nil {
return fmt.Errorf("error rolling out the deployment with culling configuration: %v", err)
}
// Wait for server to shut down after 'CULL_IDLE_TIME' minutes(around 2.5 minutes)
time.Sleep(150 * time.Second)
// Verify that the notebook kernel has shutdown, and the notebook endpoint returns 503
resp, err := tc.curlNotebookEndpoint(*nbMeta)
if err != nil {
return fmt.Errorf("error accessing Notebook Endpoint with 503: %v ", err)
}
if resp.StatusCode != 503 {
return fmt.Errorf("Unexpected response from Notebook Endpoint")
}
return nil
}