github.com/oam-dev/kubevela@v1.9.11/pkg/resourcetracker/tree_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 resourcetracker
    18  
    19  import (
    20  	"math"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/require"
    24  	"k8s.io/utils/pointer"
    25  
    26  	"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
    27  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    28  	"github.com/oam-dev/kubevela/pkg/multicluster"
    29  )
    30  
    31  func TestResourceTreePrintOption_getWidthForDetails(t *testing.T) {
    32  	r := require.New(t)
    33  	options := &ResourceTreePrintOptions{}
    34  	r.Equal(math.MaxInt, options._getWidthForDetails(nil))
    35  	options.MaxWidth = pointer.Int(50 + applyTimeWidth)
    36  	r.Equal(30, options._getWidthForDetails([]int{10, 10}))
    37  	r.Equal(math.MaxInt, options._getWidthForDetails([]int{20, 20}))
    38  }
    39  
    40  func TestResourceTreePrintOptions_wrapDetails(t *testing.T) {
    41  	r := require.New(t)
    42  	options := &ResourceTreePrintOptions{}
    43  	detail := "test-key: test-val\ttest-data: test-val\ntest-next-line: text-next-value  test-long-key: test long long long long value  test-append: test-append-val"
    44  	r.Equal(
    45  		[]string{
    46  			"test-key: test-val test-data: test-val",
    47  			"test-next-line: text-next-value",
    48  			"test-long-key: test long long long long ",
    49  			"value  test-append: test-append-val",
    50  		},
    51  		options._wrapDetails(detail, 40))
    52  }
    53  
    54  func TestBuildResourceRow(t *testing.T) {
    55  	r := require.New(t)
    56  
    57  	cases := map[string]struct {
    58  		Cluster                   string
    59  		ResourceRowStatus         string
    60  		ExpectedCluster           string
    61  		ExpectedResourceRowStatus string
    62  	}{
    63  		"localCluster": {
    64  			Cluster:                   "",
    65  			ResourceRowStatus:         resourceRowStatusUpdated,
    66  			ExpectedCluster:           multicluster.ClusterLocalName,
    67  			ExpectedResourceRowStatus: resourceRowStatusUpdated,
    68  		},
    69  		"remoteCluster": {
    70  			Cluster:                   "remoteCluster",
    71  			ResourceRowStatus:         resourceRowStatusUpdated,
    72  			ExpectedCluster:           "remoteCluster",
    73  			ExpectedResourceRowStatus: resourceRowStatusUpdated,
    74  		},
    75  	}
    76  
    77  	for name, c := range cases {
    78  		mr := v1beta1.ManagedResource{
    79  			ClusterObjectReference: common.ClusterObjectReference{
    80  				Cluster: c.Cluster,
    81  			},
    82  		}
    83  		rr := buildResourceRow(mr, c.ResourceRowStatus)
    84  		r.Equal(c.ExpectedCluster, rr.mr.Cluster, name)
    85  		r.Equal(c.ExpectedResourceRowStatus, rr.status, name)
    86  	}
    87  
    88  }