github.com/oam-dev/kubevela@v1.9.11/references/cli/top/view/cluster_namespace_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  
    27  	"github.com/stretchr/testify/assert"
    28  	"k8s.io/utils/pointer"
    29  	"sigs.k8s.io/controller-runtime/pkg/client"
    30  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    31  
    32  	"github.com/oam-dev/kubevela/pkg/utils/common"
    33  	"github.com/oam-dev/kubevela/references/cli/top/model"
    34  )
    35  
    36  func TestClusterNamespaceView(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.CtxKeyAppName, "")
    55  	ctx = context.WithValue(ctx, &model.CtxKeyNamespace, "")
    56  
    57  	cnsView := new(ClusterNamespaceView)
    58  
    59  	t.Run("init view", func(t *testing.T) {
    60  		assert.Empty(t, cnsView.CommonResourceView)
    61  		cnsView.InitView(ctx, app)
    62  		assert.NotEmpty(t, cnsView.CommonResourceView)
    63  	})
    64  
    65  	t.Run("init", func(t *testing.T) {
    66  		cnsView.Init()
    67  		assert.Equal(t, cnsView.GetTitle(), "[ ClusterNamespace ]")
    68  	})
    69  
    70  	t.Run("refresh", func(t *testing.T) {
    71  		keyEvent := cnsView.Refresh(nil)
    72  		assert.Empty(t, keyEvent)
    73  	})
    74  
    75  	t.Run("start", func(t *testing.T) {
    76  		cnsView.Start()
    77  		assert.Equal(t, cnsView.GetCell(0, 0).Text, "Name")
    78  	})
    79  
    80  	t.Run("stop", func(t *testing.T) {
    81  		cnsView.Stop()
    82  		assert.Equal(t, cnsView.GetCell(0, 0).Text, "")
    83  	})
    84  
    85  	t.Run("colorize text", func(t *testing.T) {
    86  		testData := [][]string{
    87  			{"test1", "Active", ""},
    88  			{"test2", "Terminating", ""},
    89  		}
    90  		for i := 0; i < len(testData); i++ {
    91  			for j := 0; j < 3; j++ {
    92  				cnsView.Table.SetCell(1+i, j, tview.NewTableCell(testData[i][j]))
    93  			}
    94  		}
    95  		cnsView.ColorizeStatusText(2)
    96  		assert.Equal(t, cnsView.GetCell(1, 1).Text, fmt.Sprintf("[%s::]%s", cnsView.app.config.Theme.Status.Healthy.String(), "Active"))
    97  		assert.Equal(t, cnsView.GetCell(2, 1).Text, fmt.Sprintf("[%s::]%s", cnsView.app.config.Theme.Status.UnHealthy.String(), "Terminating"))
    98  	})
    99  
   100  	t.Run("hint", func(t *testing.T) {
   101  		assert.Equal(t, len(cnsView.Hint()), 5)
   102  	})
   103  
   104  	t.Run("managed resource view", func(t *testing.T) {
   105  		cnsView.Table.Table = cnsView.Table.Select(1, 1)
   106  		assert.Empty(t, cnsView.managedResourceView(nil))
   107  	})
   108  
   109  }