github.com/oam-dev/kubevela@v1.9.11/references/common/registry_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 common
    18  
    19  import (
    20  	"encoding/json"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  
    27  	"github.com/oam-dev/kubevela/apis/types"
    28  )
    29  
    30  func TestAddSourceIntoDefinition(t *testing.T) {
    31  	caseJson := []byte(`{"template":""}`)
    32  	wantJson := []byte(`{"source":{"repoName":"foo"},"template":""}`)
    33  	source := types.Source{RepoName: "foo"}
    34  	testcase := runtime.RawExtension{Raw: caseJson}
    35  	err := addSourceIntoExtension(&testcase, &source)
    36  	if err != nil {
    37  		t.Error("meet an error ", err)
    38  		return
    39  	}
    40  	var result, want map[string]interface{}
    41  	err = json.Unmarshal(testcase.Raw, &result)
    42  	if err != nil {
    43  		t.Error("marshaling object meet an error ", err)
    44  		return
    45  	}
    46  	err = json.Unmarshal(wantJson, &want)
    47  	if err != nil {
    48  		t.Error("marshaling object meet an error ", err)
    49  		return
    50  	}
    51  	if !reflect.DeepEqual(result, want) {
    52  		t.Errorf("error result want %s, got %s", result, testcase)
    53  	}
    54  }
    55  
    56  func TestCheckLabelExistence(t *testing.T) {
    57  	cases := map[string]struct {
    58  		labels  map[string]string
    59  		label   string
    60  		existed bool
    61  	}{
    62  		"label exists": {
    63  			labels: map[string]string{
    64  				"env": "prod",
    65  			},
    66  			label:   "env=prod",
    67  			existed: true,
    68  		},
    69  
    70  		"label's key matches": {
    71  			labels: map[string]string{
    72  				"env": "prod",
    73  			},
    74  			label:   "env=dev",
    75  			existed: false,
    76  		},
    77  		"label's key doesn't match": {
    78  			labels: map[string]string{
    79  				"env": "prod",
    80  			},
    81  			label:   "type=terraform",
    82  			existed: false,
    83  		},
    84  	}
    85  
    86  	for name, tc := range cases {
    87  		t.Run(name, func(t *testing.T) {
    88  			result := CheckLabelExistence(tc.labels, tc.label)
    89  			assert.Equal(t, result, tc.existed)
    90  		})
    91  	}
    92  }