github.com/oam-dev/kubevela@v1.9.11/pkg/workflow/providers/terraform/terraform_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 terraform
    18  
    19  import (
    20  	"context"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/pkg/errors"
    25  	"github.com/stretchr/testify/require"
    26  
    27  	"github.com/kubevela/workflow/pkg/cue/model/value"
    28  	"github.com/kubevela/workflow/pkg/mock"
    29  
    30  	apicommon "github.com/oam-dev/kubevela/apis/core.oam.dev/common"
    31  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    32  	"github.com/oam-dev/kubevela/apis/types"
    33  	"github.com/oam-dev/kubevela/pkg/appfile"
    34  )
    35  
    36  func fakeWorkloadRenderer(_ context.Context, comp apicommon.ApplicationComponent) (*appfile.Component, error) {
    37  	if strings.HasPrefix(comp.Name, "error") {
    38  		return nil, errors.New(comp.Name)
    39  	}
    40  	if strings.HasPrefix(comp.Name, "terraform") {
    41  		return &appfile.Component{CapabilityCategory: types.TerraformCategory}, nil
    42  	}
    43  	return &appfile.Component{CapabilityCategory: types.CUECategory}, nil
    44  }
    45  
    46  func TestLoadTerraformComponents(t *testing.T) {
    47  	r := require.New(t)
    48  	testCases := []struct {
    49  		Inputs   []apicommon.ApplicationComponent
    50  		HasError bool
    51  		Outputs  []apicommon.ApplicationComponent
    52  	}{{
    53  		Inputs:   []apicommon.ApplicationComponent{{Name: "error"}},
    54  		HasError: true,
    55  	}, {
    56  		Inputs:  []apicommon.ApplicationComponent{{Name: "terraform-1"}, {Name: "cue"}, {Name: "terraform-2"}},
    57  		Outputs: []apicommon.ApplicationComponent{{Name: "terraform-1"}, {Name: "terraform-2"}},
    58  	}, {
    59  		Inputs:  []apicommon.ApplicationComponent{{Name: "cue"}},
    60  		Outputs: []apicommon.ApplicationComponent{},
    61  	}}
    62  	for _, testCase := range testCases {
    63  		app := &v1beta1.Application{}
    64  		app.Spec.Components = testCase.Inputs
    65  		p := &provider{
    66  			app:      app,
    67  			renderer: fakeWorkloadRenderer,
    68  		}
    69  		act := &mock.Action{}
    70  		v, err := value.NewValue("", nil, "")
    71  		r.NoError(err)
    72  		err = p.LoadTerraformComponents(nil, nil, v, act)
    73  		if testCase.HasError {
    74  			r.Error(err)
    75  			continue
    76  		}
    77  		r.NoError(err)
    78  		outputs, err := v.LookupValue("outputs", "components")
    79  		r.NoError(err)
    80  		var comps []apicommon.ApplicationComponent
    81  		r.NoError(outputs.UnmarshalTo(&comps))
    82  		r.Equal(testCase.Outputs, comps)
    83  	}
    84  }
    85  
    86  func TestGetConnectionStatus(t *testing.T) {
    87  	r := require.New(t)
    88  	testCases := []struct {
    89  		ComponentName string
    90  		Services      []apicommon.ApplicationComponentStatus
    91  		Healthy       bool
    92  		Error         string
    93  	}{{
    94  		ComponentName: "",
    95  		Error:         "failed to get component name",
    96  	}, {
    97  		ComponentName: "comp",
    98  		Services: []apicommon.ApplicationComponentStatus{{
    99  			Name:    "not-comp",
   100  			Healthy: true,
   101  		}},
   102  		Healthy: false,
   103  	}, {
   104  		ComponentName: "comp",
   105  		Services: []apicommon.ApplicationComponentStatus{{
   106  			Name:    "not-comp",
   107  			Healthy: true,
   108  		}, {
   109  			Name:    "comp",
   110  			Healthy: true,
   111  		}},
   112  		Healthy: true,
   113  	}, {
   114  		ComponentName: "comp",
   115  		Services: []apicommon.ApplicationComponentStatus{{
   116  			Name:    "not-comp",
   117  			Healthy: true,
   118  		}, {
   119  			Name:    "comp",
   120  			Healthy: false,
   121  		}},
   122  		Healthy: false,
   123  	}}
   124  	for _, testCase := range testCases {
   125  		app := &v1beta1.Application{}
   126  		app.Status.Services = testCase.Services
   127  		p := &provider{
   128  			app:      app,
   129  			renderer: fakeWorkloadRenderer,
   130  		}
   131  		act := &mock.Action{}
   132  		v, err := value.NewValue("", nil, "")
   133  		r.NoError(err)
   134  		if testCase.ComponentName != "" {
   135  			r.NoError(v.FillObject(map[string]string{"componentName": testCase.ComponentName}, "inputs"))
   136  		}
   137  		err = p.GetConnectionStatus(nil, nil, v, act)
   138  		if testCase.Error != "" {
   139  			r.Error(err)
   140  			r.Contains(err.Error(), testCase.Error)
   141  			continue
   142  		}
   143  		r.NoError(err)
   144  		healthy, err := v.GetBool("outputs", "healthy")
   145  		r.NoError(err)
   146  		r.Equal(testCase.Healthy, healthy)
   147  	}
   148  }