github.com/oam-dev/kubevela@v1.9.11/pkg/utils/env/env_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 env
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"path/filepath"
    24  
    25  	"github.com/google/go-cmp/cmp"
    26  	"github.com/kubevela/pkg/util/singleton"
    27  	"github.com/stretchr/testify/assert"
    28  	"k8s.io/apimachinery/pkg/runtime"
    29  	clientgoscheme "k8s.io/client-go/kubernetes/scheme"
    30  	"k8s.io/client-go/rest"
    31  	"sigs.k8s.io/controller-runtime/pkg/client"
    32  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    33  
    34  	"github.com/oam-dev/kubevela/apis/types"
    35  )
    36  
    37  var testEnv *envtest.Environment
    38  var cfg *rest.Config
    39  var rawClient client.Client
    40  var testScheme = runtime.NewScheme()
    41  
    42  func TestCreateEnv(t *testing.T) {
    43  
    44  	testEnv = &envtest.Environment{
    45  		ControlPlaneStartTimeout: time.Minute,
    46  		ControlPlaneStopTimeout:  time.Minute,
    47  		CRDDirectoryPaths: []string{
    48  			filepath.Join("../../..", "charts/vela-core/crds"), // this has all the required CRDs,
    49  		},
    50  	}
    51  	var err error
    52  	cfg, err = testEnv.Start()
    53  	assert.NoError(t, err)
    54  	defer func() {
    55  		assert.NoError(t, testEnv.Stop())
    56  	}()
    57  	assert.NoError(t, clientgoscheme.AddToScheme(testScheme))
    58  
    59  	rawClient, err = client.New(cfg, client.Options{Scheme: testScheme})
    60  	assert.NoError(t, err)
    61  
    62  	type want struct {
    63  		data string
    64  	}
    65  	testcases := []struct {
    66  		name    string
    67  		envMeta *types.EnvMeta
    68  		want    want
    69  	}{
    70  		{
    71  			name: "env-application",
    72  			envMeta: &types.EnvMeta{
    73  				Name:      "env-application",
    74  				Namespace: "default",
    75  			},
    76  			want: want{
    77  				data: "",
    78  			},
    79  		},
    80  		{
    81  			name: "default",
    82  			envMeta: &types.EnvMeta{
    83  				Name:      "default",
    84  				Namespace: "default",
    85  			},
    86  			want: want{
    87  				data: "the namespace default was already assigned to env env-application",
    88  			},
    89  		},
    90  	}
    91  	for _, tc := range testcases {
    92  		t.Run(tc.name, func(t *testing.T) {
    93  			singleton.KubeClient.Set(rawClient)
    94  			err = CreateEnv(tc.envMeta)
    95  			if err != nil && cmp.Diff(tc.want.data, err.Error()) != "" {
    96  				t.Errorf("CreateEnv(...): \n -want: \n%s,\n +got:\n%s", tc.want.data, err.Error())
    97  			}
    98  		})
    99  	}
   100  
   101  }