github.com/oam-dev/kubevela@v1.9.11/references/cli/top/view/managed_resource_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  	"github.com/oam-dev/kubevela/pkg/utils/common"
    32  	"github.com/oam-dev/kubevela/references/cli/top/model"
    33  )
    34  
    35  func TestManagedResourceView(t *testing.T) {
    36  	testEnv := &envtest.Environment{
    37  		ControlPlaneStartTimeout: time.Minute * 3,
    38  		ControlPlaneStopTimeout:  time.Minute,
    39  		UseExistingCluster:       pointer.Bool(false),
    40  	}
    41  	cfg, err := testEnv.Start()
    42  	assert.NoError(t, err)
    43  	defer func() {
    44  		assert.NoError(t, testEnv.Stop())
    45  	}()
    46  
    47  	testClient, err := client.New(cfg, client.Options{Scheme: common.Scheme})
    48  	assert.NoError(t, err)
    49  	app := NewApp(testClient, cfg, "")
    50  	assert.Equal(t, len(app.Components()), 4)
    51  
    52  	ctx := context.Background()
    53  	ctx = context.WithValue(ctx, &model.CtxKeyAppName, "")
    54  	ctx = context.WithValue(ctx, &model.CtxKeyNamespace, "")
    55  	ctx = context.WithValue(ctx, &model.CtxKeyCluster, "")
    56  
    57  	resourceView := new(ManagedResourceView)
    58  
    59  	t.Run("init view", func(t *testing.T) {
    60  		assert.Empty(t, resourceView.CommonResourceView)
    61  		resourceView.InitView(ctx, app)
    62  		assert.NotEmpty(t, resourceView.CommonResourceView)
    63  	})
    64  
    65  	t.Run("init", func(t *testing.T) {
    66  		resourceView.Init()
    67  		assert.Equal(t, resourceView.Table.GetTitle(), "[ Managed Resource (all/all) ]")
    68  	})
    69  
    70  	t.Run("refresh", func(t *testing.T) {
    71  		keyEvent := resourceView.Refresh(nil)
    72  		assert.Empty(t, keyEvent)
    73  	})
    74  
    75  	t.Run("start", func(t *testing.T) {
    76  		resourceView.Start()
    77  		assert.Equal(t, resourceView.GetCell(0, 0).Text, "Name")
    78  	})
    79  
    80  	t.Run("stop", func(t *testing.T) {
    81  		resourceView.Stop()
    82  		assert.Equal(t, resourceView.GetCell(0, 0).Text, "")
    83  	})
    84  
    85  	t.Run("colorize text", func(t *testing.T) {
    86  		testData := [][]string{
    87  			{"app", "ns", "", "", "", "", "Healthy"},
    88  			{"app", "ns", "", "", "", "", "UnHealthy"},
    89  			{"app", "ns", "", "", "", "", "Progressing"},
    90  			{"app", "ns", "", "", "", "", "UnKnown"}}
    91  		for i := 0; i < len(testData); i++ {
    92  			for j := 0; j < len(testData[i]); j++ {
    93  				resourceView.Table.SetCell(1+i, j, tview.NewTableCell(testData[i][j]))
    94  			}
    95  		}
    96  		resourceView.ColorizeStatusText(4)
    97  		assert.Equal(t, resourceView.GetCell(1, 6).Text, fmt.Sprintf("[%s::]%s", resourceView.app.config.Theme.Status.Healthy.String(), "Healthy"))
    98  		assert.Equal(t, resourceView.GetCell(2, 6).Text, fmt.Sprintf("[%s::]%s", resourceView.app.config.Theme.Status.UnHealthy.String(), "UnHealthy"))
    99  		assert.Equal(t, resourceView.GetCell(3, 6).Text, fmt.Sprintf("[%s::]%s", resourceView.app.config.Theme.Status.Waiting.String(), "Progressing"))
   100  		assert.Equal(t, resourceView.GetCell(4, 6).Text, fmt.Sprintf("[%s::]%s", resourceView.app.config.Theme.Status.Unknown.String(), "UnKnown"))
   101  	})
   102  
   103  	t.Run("hint", func(t *testing.T) {
   104  		assert.Equal(t, len(resourceView.Hint()), 8)
   105  	})
   106  
   107  	t.Run("select cluster", func(t *testing.T) {
   108  		assert.Empty(t, resourceView.clusterView(nil))
   109  	})
   110  
   111  	t.Run("select cluster namespace", func(t *testing.T) {
   112  		assert.Empty(t, resourceView.clusterNamespaceView(nil))
   113  	})
   114  
   115  	t.Run("pod view", func(t *testing.T) {
   116  		resourceView.Table.Table = resourceView.Table.Select(1, 1)
   117  		assert.Empty(t, resourceView.podView(nil))
   118  	})
   119  }