|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2026 Red Hat, Inc. |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +package storage |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "testing" |
| 21 | + |
| 22 | + dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 28 | + |
| 29 | + "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1" |
| 30 | + "github.com/devfile/devworkspace-operator/pkg/common" |
| 31 | + "github.com/devfile/devworkspace-operator/pkg/constants" |
| 32 | + "github.com/devfile/devworkspace-operator/pkg/infrastructure" |
| 33 | + "github.com/devfile/devworkspace-operator/pkg/provision/sync" |
| 34 | +) |
| 35 | + |
| 36 | +func TestGetSpecCommonPVCCleanupJobUsesConfigPodSecurityContext(t *testing.T) { |
| 37 | + infrastructure.InitializeForTesting(infrastructure.OpenShiftv4) |
| 38 | + |
| 39 | + fsGroupChangeOnRootMismatch := corev1.FSGroupChangeOnRootMismatch |
| 40 | + customPodSecurityContext := &corev1.PodSecurityContext{ |
| 41 | + FSGroupChangePolicy: &fsGroupChangeOnRootMismatch, |
| 42 | + SELinuxOptions: &corev1.SELinuxOptions{Type: "spc_t"}, |
| 43 | + } |
| 44 | + |
| 45 | + namespace := "test-ns" |
| 46 | + pvcName := "claim-devworkspace" |
| 47 | + fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects( |
| 48 | + &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}, |
| 49 | + ).Build() |
| 50 | + |
| 51 | + workspace := &common.DevWorkspaceWithConfig{ |
| 52 | + DevWorkspace: &dw.DevWorkspace{ |
| 53 | + ObjectMeta: metav1.ObjectMeta{ |
| 54 | + Name: "test-workspace", |
| 55 | + Namespace: namespace, |
| 56 | + Labels: map[string]string{ |
| 57 | + constants.DevWorkspaceCreatorLabel: "test-creator", |
| 58 | + }, |
| 59 | + }, |
| 60 | + Status: dw.DevWorkspaceStatus{ |
| 61 | + DevWorkspaceId: "test-workspace-id", |
| 62 | + }, |
| 63 | + }, |
| 64 | + Config: &v1alpha1.OperatorConfiguration{ |
| 65 | + Workspace: &v1alpha1.WorkspaceConfig{ |
| 66 | + PVCName: pvcName, |
| 67 | + PodSecurityContext: customPodSecurityContext, |
| 68 | + }, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + clusterAPI := sync.ClusterAPI{ |
| 73 | + Client: fakeClient, |
| 74 | + Scheme: scheme, |
| 75 | + Logger: zap.New(zap.UseDevMode(true)), |
| 76 | + Ctx: context.Background(), |
| 77 | + } |
| 78 | + |
| 79 | + job, err := getSpecCommonPVCCleanupJob(workspace, clusterAPI) |
| 80 | + assert.NoError(t, err) |
| 81 | + assert.Equal(t, customPodSecurityContext, job.Spec.Template.Spec.SecurityContext) |
| 82 | +} |
| 83 | + |
| 84 | +func TestGetSpecCommonPVCCleanupJobWithNilPodSecurityContext(t *testing.T) { |
| 85 | + infrastructure.InitializeForTesting(infrastructure.Kubernetes) |
| 86 | + |
| 87 | + namespace := "test-ns" |
| 88 | + pvcName := "claim-devworkspace" |
| 89 | + fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects( |
| 90 | + &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}, |
| 91 | + ).Build() |
| 92 | + |
| 93 | + workspace := &common.DevWorkspaceWithConfig{ |
| 94 | + DevWorkspace: &dw.DevWorkspace{ |
| 95 | + ObjectMeta: metav1.ObjectMeta{ |
| 96 | + Name: "test-workspace", |
| 97 | + Namespace: namespace, |
| 98 | + Labels: map[string]string{ |
| 99 | + constants.DevWorkspaceCreatorLabel: "test-creator", |
| 100 | + }, |
| 101 | + }, |
| 102 | + Status: dw.DevWorkspaceStatus{ |
| 103 | + DevWorkspaceId: "test-workspace-id", |
| 104 | + }, |
| 105 | + }, |
| 106 | + Config: &v1alpha1.OperatorConfiguration{ |
| 107 | + Workspace: &v1alpha1.WorkspaceConfig{ |
| 108 | + PVCName: pvcName, |
| 109 | + PodSecurityContext: nil, // No custom security context |
| 110 | + }, |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + clusterAPI := sync.ClusterAPI{ |
| 115 | + Client: fakeClient, |
| 116 | + Scheme: scheme, |
| 117 | + Logger: zap.New(zap.UseDevMode(true)), |
| 118 | + Ctx: context.Background(), |
| 119 | + } |
| 120 | + |
| 121 | + job, err := getSpecCommonPVCCleanupJob(workspace, clusterAPI) |
| 122 | + assert.NoError(t, err) |
| 123 | + assert.Nil(t, job.Spec.Template.Spec.SecurityContext) |
| 124 | +} |
0 commit comments