github.com/oam-dev/kubevela@v1.9.11/references/cli/up_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 cli
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"fmt"
    23  	"os"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  	corev1 "k8s.io/api/core/v1"
    29  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    30  	"sigs.k8s.io/controller-runtime/pkg/client"
    31  
    32  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    33  	"github.com/oam-dev/kubevela/apis/types"
    34  	velacmd "github.com/oam-dev/kubevela/pkg/cmd"
    35  	"github.com/oam-dev/kubevela/pkg/utils/util"
    36  	"github.com/oam-dev/kubevela/references/common"
    37  )
    38  
    39  func TestUp(t *testing.T) {
    40  
    41  	app := &v1beta1.Application{}
    42  	app.Name = "app-up"
    43  	msg := common.Info(app)
    44  	assert.Contains(t, msg, "App has been deployed")
    45  	// This test case can not run in the TERM with the color.
    46  	assert.Contains(t, msg, fmt.Sprintf("App status: vela status %s", app.Name))
    47  }
    48  
    49  func TestUpOverrideNamespace(t *testing.T) {
    50  	cases := map[string]struct {
    51  		application       string
    52  		applicationName   string
    53  		namespace         string
    54  		expectedNamespace string
    55  	}{
    56  		"use default namespace if not set": {
    57  			application: `
    58  apiVersion: core.oam.dev/v1beta1
    59  kind: Application
    60  metadata:
    61    name: first-vela-app
    62  spec:
    63    components: []
    64  `,
    65  			applicationName:   "first-vela-app",
    66  			namespace:         "",
    67  			expectedNamespace: types.DefaultAppNamespace,
    68  		},
    69  		"override namespace": {
    70  			application: `
    71  apiVersion: core.oam.dev/v1beta1
    72  kind: Application
    73  metadata:
    74    name: first-vela-app
    75  spec:
    76    components: []
    77  `,
    78  			applicationName:   "first-vela-app",
    79  			namespace:         "overridden-namespace",
    80  			expectedNamespace: "overridden-namespace",
    81  		},
    82  		"use application namespace": {
    83  			application: `
    84  apiVersion: core.oam.dev/v1beta1
    85  kind: Application
    86  metadata:
    87    name: first-vela-app
    88    namespace: vela-apps
    89  spec:
    90    components: []
    91  `,
    92  			applicationName:   "first-vela-app",
    93  			namespace:         "",
    94  			expectedNamespace: "vela-apps",
    95  		},
    96  		"override application namespace": {
    97  			application: `
    98  apiVersion: core.oam.dev/v1beta1
    99  kind: Application
   100  metadata:
   101    name: first-vela-app
   102    namespace: vela-apps
   103  spec:
   104    components: []
   105  `,
   106  			applicationName:   "first-vela-app",
   107  			namespace:         "overridden-namespace",
   108  			expectedNamespace: "overridden-namespace",
   109  		},
   110  	}
   111  
   112  	for name, c := range cases {
   113  		t.Run(name, func(t *testing.T) {
   114  			args := initArgs()
   115  			kc, err := args.GetClient()
   116  			require.NoError(t, err)
   117  
   118  			af, err := os.CreateTemp(os.TempDir(), "up-override-namespace-*.yaml")
   119  			require.NoError(t, err)
   120  			defer func() {
   121  				_ = af.Close()
   122  				_ = os.Remove(af.Name())
   123  			}()
   124  			_, err = af.WriteString(c.application)
   125  			require.NoError(t, err)
   126  
   127  			// Ensure namespace
   128  			require.NoError(t, kc.Create(context.TODO(), &corev1.Namespace{
   129  				ObjectMeta: metav1.ObjectMeta{Name: c.expectedNamespace},
   130  			}))
   131  
   132  			var buf bytes.Buffer
   133  			cmd := NewUpCommand(velacmd.NewDelegateFactory(args.GetClient, args.GetConfig), "", args, util.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr})
   134  			cmd.SetArgs([]string{})
   135  			cmd.SetOut(&buf)
   136  			cmd.SetErr(&buf)
   137  			if c.namespace != "" {
   138  				require.NoError(t, cmd.Flags().Set(FlagNamespace, c.namespace))
   139  			}
   140  			require.NoError(t, cmd.Flags().Set("file", af.Name()))
   141  			require.NoError(t, cmd.Execute())
   142  
   143  			var app v1beta1.Application
   144  			require.NoError(t, kc.Get(context.TODO(), client.ObjectKey{
   145  				Name:      c.applicationName,
   146  				Namespace: c.expectedNamespace,
   147  			}, &app))
   148  			require.Equal(t, c.expectedNamespace, app.Namespace)
   149  			require.Equal(t, c.applicationName, app.Name)
   150  		})
   151  	}
   152  }