github.com/oam-dev/kubevela@v1.9.11/references/cli/top/view/application_view_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 view
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/rivo/tview"
    26  	"github.com/stretchr/testify/assert"
    27  	"k8s.io/utils/pointer"
    28  	"sigs.k8s.io/controller-runtime/pkg/client"
    29  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    30  
    31  	types "github.com/oam-dev/kubevela/apis/core.oam.dev/common"
    32  	"github.com/oam-dev/kubevela/pkg/utils/common"
    33  	"github.com/oam-dev/kubevela/references/cli/top/model"
    34  )
    35  
    36  func TestApplicationView(t *testing.T) {
    37  	testEnv := &envtest.Environment{
    38  		ControlPlaneStartTimeout: time.Minute * 3,
    39  		ControlPlaneStopTimeout:  time.Minute,
    40  		UseExistingCluster:       pointer.Bool(false),
    41  	}
    42  	cfg, err := testEnv.Start()
    43  	assert.NoError(t, err)
    44  	defer func() {
    45  		assert.NoError(t, testEnv.Stop())
    46  	}()
    47  
    48  	testClient, err := client.New(cfg, client.Options{Scheme: common.Scheme})
    49  	assert.NoError(t, err)
    50  	app := NewApp(testClient, cfg, "")
    51  	assert.Equal(t, len(app.Components()), 4)
    52  
    53  	ctx := context.Background()
    54  	ctx = context.WithValue(ctx, &model.CtxKeyNamespace, "")
    55  
    56  	appView := new(ApplicationView)
    57  
    58  	t.Run("init view", func(t *testing.T) {
    59  		assert.Empty(t, appView.CommonResourceView)
    60  		appView.InitView(ctx, app)
    61  		assert.NotEmpty(t, appView.CommonResourceView)
    62  	})
    63  
    64  	t.Run("init", func(t *testing.T) {
    65  		appView.Init()
    66  		assert.Equal(t, appView.Table.GetTitle(), "[ Application (all) ]")
    67  	})
    68  
    69  	t.Run("refresh", func(t *testing.T) {
    70  		keyEvent := appView.Refresh(nil)
    71  		assert.Empty(t, keyEvent)
    72  	})
    73  
    74  	t.Run("start", func(t *testing.T) {
    75  		appView.Start()
    76  		assert.Equal(t, appView.GetCell(0, 0).Text, "Name")
    77  	})
    78  
    79  	t.Run("stop", func(t *testing.T) {
    80  		appView.Stop()
    81  		assert.Equal(t, appView.GetCell(0, 0).Text, "")
    82  	})
    83  
    84  	t.Run("colorize text", func(t *testing.T) {
    85  		testData := [][]string{
    86  			{"app", "ns", "starting", ""},
    87  			{"app", "ns", "rendering", ""},
    88  			{"app", "ns", "generatingPolicy", ""},
    89  			{"app", "ns", "runningWorkflow", ""},
    90  			{"app", "ns", "workflowSuspending", ""},
    91  			{"app", "ns", "workflowTerminated", ""},
    92  			{"app", "ns", "workflowFailed", ""},
    93  			{"app", "ns", "unhealthy", ""},
    94  			{"app", "ns", "deleting", ""},
    95  			{"app", "ns", "running", ""},
    96  		}
    97  		for i := 0; i < len(testData); i++ {
    98  			for j := 0; j < 4; j++ {
    99  				appView.Table.SetCell(1+i, j, tview.NewTableCell(testData[i][j]))
   100  			}
   101  		}
   102  		appView.ColorizeStatusText(10)
   103  		assert.Equal(t, appView.GetCell(1, 2).Text, fmt.Sprintf("[%s::]%s", appView.app.config.Theme.Status.Starting.String(), types.ApplicationStarting))
   104  		assert.Equal(t, appView.GetCell(2, 2).Text, fmt.Sprintf("[%s::]%s", appView.app.config.Theme.Status.Waiting.String(), types.ApplicationRendering))
   105  		assert.Equal(t, appView.GetCell(3, 2).Text, fmt.Sprintf("[%s::]%s", appView.app.config.Theme.Status.Waiting.String(), types.ApplicationPolicyGenerating))
   106  		assert.Equal(t, appView.GetCell(4, 2).Text, fmt.Sprintf("[%s::]%s", appView.app.config.Theme.Status.Waiting.String(), types.ApplicationRunningWorkflow))
   107  		assert.Equal(t, appView.GetCell(5, 2).Text, fmt.Sprintf("[%s::]%s", appView.app.config.Theme.Status.Waiting.String(), types.ApplicationWorkflowSuspending))
   108  		assert.Equal(t, appView.GetCell(6, 2).Text, fmt.Sprintf("[%s::]%s", appView.app.config.Theme.Status.Failed.String(), types.ApplicationWorkflowTerminated))
   109  		assert.Equal(t, appView.GetCell(7, 2).Text, fmt.Sprintf("[%s::]%s", appView.app.config.Theme.Status.Failed.String(), types.ApplicationWorkflowFailed))
   110  		assert.Equal(t, appView.GetCell(8, 2).Text, fmt.Sprintf("[%s::]%s", appView.app.config.Theme.Status.Failed.String(), types.ApplicationUnhealthy))
   111  		assert.Equal(t, appView.GetCell(9, 2).Text, fmt.Sprintf("[%s::]%s", appView.app.config.Theme.Status.Failed.String(), types.ApplicationDeleting))
   112  		assert.Equal(t, appView.GetCell(10, 2).Text, fmt.Sprintf("[%s::]%s", appView.app.config.Theme.Status.Healthy.String(), types.ApplicationRunning))
   113  	})
   114  
   115  	t.Run("hint", func(t *testing.T) {
   116  		assert.Equal(t, len(appView.Hint()), 9)
   117  	})
   118  
   119  	t.Run("managed resource view", func(t *testing.T) {
   120  		appView.Table.Table = appView.Table.Select(1, 1)
   121  		assert.Empty(t, appView.managedResourceView(nil))
   122  	})
   123  
   124  	t.Run("namespace view", func(t *testing.T) {
   125  		appView.Table.Table = appView.Table.Select(1, 1)
   126  		assert.Empty(t, appView.namespaceView(nil))
   127  	})
   128  
   129  	t.Run("yaml view", func(t *testing.T) {
   130  		appView.Table.Table = appView.Table.Select(1, 1)
   131  		assert.Empty(t, appView.managedResourceView(nil))
   132  	})
   133  
   134  }