github.com/oam-dev/kubevela@v1.9.11/pkg/controller/utils/utils_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 utils
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
    26  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    27  )
    28  
    29  func TestConstructExtract(t *testing.T) {
    30  	tests := []string{"tam1", "test-comp", "xx", "tt-x-x-c"}
    31  	revisionNum := []int{1, 5, 10, 100000}
    32  	for idx, componentName := range tests {
    33  		t.Run(fmt.Sprintf("tests %d for component[%s]", idx, componentName), func(t *testing.T) {
    34  			revisionName := ConstructRevisionName(componentName, int64(revisionNum[idx]))
    35  			got := ExtractComponentName(revisionName)
    36  			if got != componentName {
    37  				t.Errorf("want to get %s from %s but got %s", componentName, revisionName, got)
    38  			}
    39  			revision, _ := ExtractRevision(revisionName)
    40  			if revision != revisionNum[idx] {
    41  				t.Errorf("want to get %d from %s but got %d", revisionNum[idx], revisionName, revision)
    42  			}
    43  		})
    44  	}
    45  	badRevision := []string{"xx", "yy-", "zz-0.1"}
    46  	t.Run(fmt.Sprintf("tests %s for extractRevision", badRevision), func(t *testing.T) {
    47  		for _, revisionName := range badRevision {
    48  			_, err := ExtractRevision(revisionName)
    49  			if err == nil {
    50  				t.Errorf("want to get err from %s but got nil", revisionName)
    51  			}
    52  		}
    53  	})
    54  }
    55  
    56  func TestGetAppRevison(t *testing.T) {
    57  	revisionName, latestRevision := GetAppNextRevision(nil)
    58  	assert.Equal(t, revisionName, "")
    59  	assert.Equal(t, latestRevision, int64(0))
    60  	// the first is always 1
    61  	app := &v1beta1.Application{}
    62  	app.Name = "myapp"
    63  	revisionName, latestRevision = GetAppNextRevision(app)
    64  	assert.Equal(t, revisionName, "myapp-v1")
    65  	assert.Equal(t, latestRevision, int64(1))
    66  	app.Status.LatestRevision = &common.Revision{
    67  		Name:     "myapp-v1",
    68  		Revision: 1,
    69  	}
    70  	// we always automatically advance the revision
    71  	revisionName, latestRevision = GetAppNextRevision(app)
    72  	assert.Equal(t, revisionName, "myapp-v2")
    73  	assert.Equal(t, latestRevision, int64(2))
    74  }