github.com/oam-dev/kubevela@v1.9.11/references/cli/provider_test.go (about)

     1  /*
     2  Copyright 2022 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 cli
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"testing"
    23  
    24  	terraformapi "github.com/oam-dev/terraform-controller/api/v1beta1"
    25  	"github.com/stretchr/testify/assert"
    26  	corev1 "k8s.io/api/core/v1"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/apimachinery/pkg/runtime"
    29  	"sigs.k8s.io/controller-runtime/pkg/client"
    30  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    31  
    32  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    33  	"github.com/oam-dev/kubevela/apis/types"
    34  	"github.com/oam-dev/kubevela/pkg/utils/util"
    35  )
    36  
    37  func TestListProviders(t *testing.T) {
    38  	ctx := context.Background()
    39  	type args struct {
    40  		k8sClient client.Client
    41  	}
    42  	type want struct {
    43  		errMsg string
    44  	}
    45  	s := runtime.NewScheme()
    46  	v1beta1.AddToScheme(s)
    47  	corev1.AddToScheme(s)
    48  	terraformapi.AddToScheme(s)
    49  	p1 := &terraformapi.Provider{
    50  		TypeMeta: metav1.TypeMeta{
    51  			Kind:       "Provider",
    52  			APIVersion: "terraform.core.oam.dev/v1beta1",
    53  		},
    54  		ObjectMeta: metav1.ObjectMeta{
    55  			Name:      "p1",
    56  			Namespace: "default",
    57  			Labels: map[string]string{
    58  				"config.oam.dev/type": types.TerraformProvider,
    59  			},
    60  		},
    61  	}
    62  	p2 := &terraformapi.Provider{
    63  		TypeMeta: metav1.TypeMeta{
    64  			Kind:       "Provider",
    65  			APIVersion: "terraform.core.oam.dev/v1beta1",
    66  		},
    67  		ObjectMeta: metav1.ObjectMeta{
    68  			Name:      "p2",
    69  			Namespace: "default",
    70  		},
    71  	}
    72  	k8sClient := fake.NewClientBuilder().WithScheme(s).WithObjects(p1, p2).Build()
    73  
    74  	ioStream := util.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
    75  
    76  	testcases := map[string]struct {
    77  		args args
    78  		want want
    79  	}{
    80  		"success": {
    81  			args: args{
    82  				k8sClient: k8sClient,
    83  			},
    84  		},
    85  	}
    86  
    87  	for name, tc := range testcases {
    88  		t.Run(name, func(t *testing.T) {
    89  			err := listProviders(ctx, tc.args.k8sClient, ioStream)
    90  			if err != nil || tc.want.errMsg != "" {
    91  				assert.Contains(t, err.Error(), tc.want.errMsg)
    92  			}
    93  		})
    94  	}
    95  }