|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2025 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/devfile/devworkspace-operator/apis/controller/v1alpha1" |
| 24 | + "github.com/devfile/devworkspace-operator/pkg/common" |
| 25 | + "github.com/devfile/devworkspace-operator/pkg/constants" |
| 26 | + "github.com/devfile/devworkspace-operator/pkg/infrastructure" |
| 27 | + "github.com/devfile/devworkspace-operator/pkg/provision/sync" |
| 28 | + "github.com/stretchr/testify/assert" |
| 29 | + corev1 "k8s.io/api/core/v1" |
| 30 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 33 | +) |
| 34 | + |
| 35 | +func TestGetSpecCommonPVCCleanupJobUsesConfigPodSecurityContext(t *testing.T) { |
| 36 | + infrastructure.InitializeForTesting(infrastructure.OpenShiftv4) |
| 37 | + |
| 38 | + fsGroupChangeOnRootMismatch := corev1.FSGroupChangeOnRootMismatch |
| 39 | + customPodSecurityContext := &corev1.PodSecurityContext{ |
| 40 | + FSGroupChangePolicy: &fsGroupChangeOnRootMismatch, |
| 41 | + SELinuxOptions: &corev1.SELinuxOptions{Type: "spc_t"}, |
| 42 | + } |
| 43 | + |
| 44 | + namespace := "test-ns" |
| 45 | + pvcName := "claim-devworkspace" |
| 46 | + fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects( |
| 47 | + &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}, |
| 48 | + ).Build() |
| 49 | + |
| 50 | + workspace := &common.DevWorkspaceWithConfig{ |
| 51 | + DevWorkspace: &dw.DevWorkspace{ |
| 52 | + ObjectMeta: metav1.ObjectMeta{ |
| 53 | + Name: "test-workspace", |
| 54 | + Namespace: namespace, |
| 55 | + Labels: map[string]string{ |
| 56 | + constants.DevWorkspaceCreatorLabel: "test-creator", |
| 57 | + }, |
| 58 | + }, |
| 59 | + Status: dw.DevWorkspaceStatus{ |
| 60 | + DevWorkspaceId: "test-workspace-id", |
| 61 | + }, |
| 62 | + }, |
| 63 | + Config: &v1alpha1.OperatorConfiguration{ |
| 64 | + Workspace: &v1alpha1.WorkspaceConfig{ |
| 65 | + PVCName: pvcName, |
| 66 | + PodSecurityContext: customPodSecurityContext, |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + clusterAPI := sync.ClusterAPI{ |
| 72 | + Client: fakeClient, |
| 73 | + Scheme: scheme, |
| 74 | + Logger: zap.New(zap.UseDevMode(true)), |
| 75 | + Ctx: context.Background(), |
| 76 | + } |
| 77 | + |
| 78 | + job, err := getSpecCommonPVCCleanupJob(workspace, clusterAPI) |
| 79 | + assert.NoError(t, err) |
| 80 | + assert.Equal(t, customPodSecurityContext, job.Spec.Template.Spec.SecurityContext) |
| 81 | +} |
0 commit comments