github.com/oam-dev/kubevela@v1.9.11/pkg/resourcekeeper/admission_test.go (about)

     1  /*
     2  Copyright 2021 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package resourcekeeper
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/require"
    24  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    26  
    27  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    28  )
    29  
    30  func TestNamespaceAdmissionHandler_Validate(t *testing.T) {
    31  	AllowCrossNamespaceResource = false
    32  	defer func() {
    33  		AllowCrossNamespaceResource = true
    34  	}()
    35  	handler := &NamespaceAdmissionHandler{
    36  		app: &v1beta1.Application{ObjectMeta: v1.ObjectMeta{Namespace: "test"}},
    37  	}
    38  	objs := []*unstructured.Unstructured{{
    39  		Object: map[string]interface{}{
    40  			"metadata": map[string]interface{}{
    41  				"name":      "demo",
    42  				"namespace": "demo",
    43  			},
    44  		},
    45  	}}
    46  	err := handler.Validate(context.Background(), objs)
    47  	r := require.New(t)
    48  	r.NotNil(err)
    49  	r.Contains(err.Error(), "forbidden resource")
    50  	AllowCrossNamespaceResource = true
    51  	r.NoError(handler.Validate(context.Background(), objs))
    52  }
    53  
    54  func TestResourceTypeAdmissionHandler_Validate(t *testing.T) {
    55  	defer func() {
    56  		AllowResourceTypes = ""
    57  	}()
    58  	r := require.New(t)
    59  	objs := []*unstructured.Unstructured{{
    60  		Object: map[string]interface{}{
    61  			"apiVersion": "v1",
    62  			"kind":       "Secret",
    63  			"metadata": map[string]interface{}{
    64  				"name":      "demo",
    65  				"namespace": "demo",
    66  			},
    67  		},
    68  	}}
    69  	AllowResourceTypes = "blacklist:Service.v1,Secret.v1"
    70  	err := (&ResourceTypeAdmissionHandler{}).Validate(context.Background(), objs)
    71  	r.NotNil(err)
    72  	r.Contains(err.Error(), "forbidden resource")
    73  	AllowResourceTypes = "blacklist:ConfigMap.v1,Deployment.v1.apps"
    74  	r.NoError((&ResourceTypeAdmissionHandler{}).Validate(context.Background(), objs))
    75  	AllowResourceTypes = "whitelist:ConfigMap.v1,Deployment.v1.apps"
    76  	err = (&ResourceTypeAdmissionHandler{}).Validate(context.Background(), objs)
    77  	r.NotNil(err)
    78  	r.Contains(err.Error(), "forbidden resource")
    79  	AllowResourceTypes = "whitelist:Service.v1,Secret.v1"
    80  	r.NoError((&ResourceTypeAdmissionHandler{}).Validate(context.Background(), objs))
    81  }