github.com/oam-dev/kubevela@v1.9.11/references/cli/top/view/container_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 TestContainerView(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.CtxKeyPod, "pod1")
    54  	ctx = context.WithValue(ctx, &model.CtxKeyNamespace, "default")
    55  	ctx = context.WithValue(ctx, &model.CtxKeyCluster, "local")
    56  
    57  	containerView := new(ContainerView)
    58  
    59  	t.Run("init view", func(t *testing.T) {
    60  		assert.Empty(t, containerView.CommonResourceView)
    61  		containerView.InitView(ctx, app)
    62  		assert.NotEmpty(t, containerView.CommonResourceView)
    63  	})
    64  
    65  	t.Run("init", func(t *testing.T) {
    66  		containerView.Init()
    67  		assert.Equal(t, containerView.Table.GetTitle(), "[ Container ]")
    68  	})
    69  
    70  	t.Run("refresh", func(t *testing.T) {
    71  		keyEvent := containerView.Refresh(nil)
    72  		assert.Empty(t, keyEvent)
    73  	})
    74  
    75  	t.Run("start", func(t *testing.T) {
    76  		containerView.Start()
    77  		assert.Equal(t, containerView.GetCell(0, 0).Text, "Name")
    78  		assert.Equal(t, containerView.GetCell(0, 1).Text, "Image")
    79  		assert.Equal(t, containerView.GetCell(0, 2).Text, "Ready")
    80  		assert.Equal(t, containerView.GetCell(0, 11).Text, "RestartCount")
    81  	})
    82  
    83  	t.Run("stop", func(t *testing.T) {
    84  		containerView.Stop()
    85  		assert.Equal(t, containerView.GetCell(0, 0).Text, "")
    86  	})
    87  
    88  	t.Run("colorize text", func(t *testing.T) {
    89  		testData := [][]string{
    90  			{"test-container1", "test-image", "Yes", "Running", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "", "0"},
    91  			{"test-container2", "test-image", "No", "Waiting", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "", "0"},
    92  			{"test-container3", "test-image", "No", "Terminated", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "", "0"},
    93  		}
    94  		for i := 0; i < len(testData); i++ {
    95  			for j := 0; j < len(testData[i]); j++ {
    96  				containerView.Table.SetCell(1+i, j, tview.NewTableCell(testData[i][j]))
    97  			}
    98  		}
    99  		containerView.ColorizePhaseText(3)
   100  		assert.Equal(t, containerView.GetCell(1, 3).Text, fmt.Sprintf("[%s::]%s", containerView.app.config.Theme.Status.Healthy.String(), "Running"))
   101  		assert.Equal(t, containerView.GetCell(2, 3).Text, fmt.Sprintf("[%s::]%s", containerView.app.config.Theme.Status.Waiting.String(), "Waiting"))
   102  		assert.Equal(t, containerView.GetCell(3, 3).Text, fmt.Sprintf("[%s::]%s", containerView.app.config.Theme.Status.UnHealthy.String(), "Terminated"))
   103  	})
   104  
   105  	t.Run("hint", func(t *testing.T) {
   106  		assert.Equal(t, len(containerView.Hint()), 4)
   107  	})
   108  }