|
| 1 | +// Copyright 2018 The Kubeflow Authors |
| 2 | +// |
| 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 | +// https://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 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/kubeflow/pipelines/backend/src/apiserver/common" |
| 21 | + commonutil "github.com/kubeflow/pipelines/backend/src/common/util" |
| 22 | + util "github.com/kubeflow/pipelines/backend/src/crd/controller/scheduledworkflow/util" |
| 23 | + swfapi "github.com/kubeflow/pipelines/backend/src/crd/pkg/apis/scheduledworkflow/v1beta1" |
| 24 | + "github.com/spf13/viper" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | +) |
| 28 | + |
| 29 | +func TestIsV1PipelineBlocked(t *testing.T) { |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + blockV1 string |
| 33 | + allowedNamespaces string |
| 34 | + namespace string |
| 35 | + expected bool |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "Blocking disabled - not blocked", |
| 39 | + blockV1: "false", |
| 40 | + namespace: "ns1", |
| 41 | + expected: false, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "Blocking not set - not blocked", |
| 45 | + blockV1: "", |
| 46 | + namespace: "ns1", |
| 47 | + expected: false, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "Blocking enabled, no allowed namespaces - blocked", |
| 51 | + blockV1: "true", |
| 52 | + namespace: "ns1", |
| 53 | + expected: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "Blocking enabled, namespace allowed - not blocked", |
| 57 | + blockV1: "true", |
| 58 | + allowedNamespaces: "ns1", |
| 59 | + namespace: "ns1", |
| 60 | + expected: false, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "Blocking enabled, namespace not in allowed list - blocked", |
| 64 | + blockV1: "true", |
| 65 | + allowedNamespaces: "ns2,ns3", |
| 66 | + namespace: "ns1", |
| 67 | + expected: true, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "Blocking enabled, namespace in allowed list - not blocked", |
| 71 | + blockV1: "true", |
| 72 | + allowedNamespaces: "ns1,ns2,ns3", |
| 73 | + namespace: "ns2", |
| 74 | + expected: false, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "Blocking enabled, case insensitive namespace match - not blocked", |
| 78 | + blockV1: "true", |
| 79 | + allowedNamespaces: "NS1", |
| 80 | + namespace: "ns1", |
| 81 | + expected: false, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + for _, tt := range tests { |
| 86 | + t.Run(tt.name, func(t *testing.T) { |
| 87 | + viper.Set(common.BlockV1Pipelines, tt.blockV1) |
| 88 | + viper.Set(common.V1NamespaceWhitelist, tt.allowedNamespaces) |
| 89 | + defer func() { |
| 90 | + viper.Set(common.BlockV1Pipelines, "") |
| 91 | + viper.Set(common.V1NamespaceWhitelist, "") |
| 92 | + }() |
| 93 | + |
| 94 | + result := isV1PipelineBlocked(tt.namespace) |
| 95 | + assert.Equal(t, tt.expected, result) |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestShouldEnforceV1Block(t *testing.T) { |
| 101 | + tests := []struct { |
| 102 | + name string |
| 103 | + apiVersion string |
| 104 | + blockV1 string |
| 105 | + namespace string |
| 106 | + expected bool |
| 107 | + }{ |
| 108 | + { |
| 109 | + name: "V1 SWF, blocking enabled - blocked", |
| 110 | + apiVersion: commonutil.ApiVersionV1, |
| 111 | + blockV1: "true", |
| 112 | + namespace: "ns1", |
| 113 | + expected: true, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "V1 SWF, blocking disabled - not blocked", |
| 117 | + apiVersion: commonutil.ApiVersionV1, |
| 118 | + blockV1: "false", |
| 119 | + namespace: "ns1", |
| 120 | + expected: false, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "V2 SWF, blocking enabled - not blocked", |
| 124 | + apiVersion: commonutil.ApiVersionV2, |
| 125 | + blockV1: "true", |
| 126 | + namespace: "ns1", |
| 127 | + expected: false, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "V2 SWF, blocking disabled - not blocked", |
| 131 | + apiVersion: commonutil.ApiVersionV2, |
| 132 | + blockV1: "false", |
| 133 | + namespace: "ns1", |
| 134 | + expected: false, |
| 135 | + }, |
| 136 | + } |
| 137 | + |
| 138 | + for _, tt := range tests { |
| 139 | + t.Run(tt.name, func(t *testing.T) { |
| 140 | + viper.Set(common.BlockV1Pipelines, tt.blockV1) |
| 141 | + defer viper.Set(common.BlockV1Pipelines, "") |
| 142 | + |
| 143 | + swf := util.NewScheduledWorkflow(&swfapi.ScheduledWorkflow{ |
| 144 | + TypeMeta: metav1.TypeMeta{APIVersion: tt.apiVersion}, |
| 145 | + ObjectMeta: metav1.ObjectMeta{Namespace: tt.namespace}, |
| 146 | + }) |
| 147 | + assert.Equal(t, tt.expected, shouldEnforceV1Block(swf)) |
| 148 | + }) |
| 149 | + } |
| 150 | +} |
0 commit comments