mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-19 04:26:39 +00:00
refactor appRevison extract func to utils (#1481)
* refactor appRevison extract func to utils * add some corener case check * add more checks
This commit is contained in:
@@ -25,6 +25,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/oam-dev/kubevela/pkg/dsl/model"
|
||||
"github.com/oam-dev/kubevela/pkg/oam/util"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -161,7 +162,9 @@ func (ctx *templateContext) BaseContextFile() string {
|
||||
buff += fmt.Sprintf(ContextName+": \"%s\"\n", ctx.name)
|
||||
buff += fmt.Sprintf(ContextAppName+": \"%s\"\n", ctx.appName)
|
||||
buff += fmt.Sprintf(ContextAppRevision+": \"%s\"\n", ctx.appRevision)
|
||||
buff += fmt.Sprintf(ContextAppRevisionNum+": %s\n", extractRevisionNum(ctx.appRevision))
|
||||
// the appRevision is generated by vela, the error always is nil, so ignore it
|
||||
revNum, _ := util.ExtractRevisionNum(ctx.appRevision)
|
||||
buff += fmt.Sprintf(ContextAppRevisionNum+": %d\n", revNum)
|
||||
buff += fmt.Sprintf(ContextNamespace+": \"%s\"\n", ctx.namespace)
|
||||
|
||||
if ctx.base != nil {
|
||||
@@ -257,9 +260,3 @@ func structMarshal(v string) string {
|
||||
}
|
||||
return fmt.Sprintf("{%s}", v)
|
||||
}
|
||||
|
||||
func extractRevisionNum(appRevision string) string {
|
||||
app := strings.Split(appRevision, "-")
|
||||
vision := app[len(app)-1]
|
||||
return strings.Replace(vision, "v", "", 1)
|
||||
}
|
||||
|
||||
@@ -111,29 +111,3 @@ image: "myserver"
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, "{\"password\":\"123\"}", string(requiredSecrets))
|
||||
}
|
||||
|
||||
func TestExtractRevisionNum(t *testing.T) {
|
||||
testcases := []struct {
|
||||
appRevision string
|
||||
wantRevisionNum string
|
||||
}{{
|
||||
appRevision: "myapp-v1",
|
||||
wantRevisionNum: "1",
|
||||
}, {
|
||||
appRevision: "new-app-v2",
|
||||
wantRevisionNum: "2",
|
||||
}, {
|
||||
appRevision: "v1-v10",
|
||||
wantRevisionNum: "10",
|
||||
}, {
|
||||
appRevision: "v10-v1-v1",
|
||||
wantRevisionNum: "1",
|
||||
}, {
|
||||
appRevision: "myapp-v1-v2",
|
||||
wantRevisionNum: "2",
|
||||
}}
|
||||
|
||||
for _, tt := range testcases {
|
||||
assert.Equal(t, tt.wantRevisionNum, extractRevisionNum(tt.appRevision))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"hash/fnv"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -721,6 +722,20 @@ func ConvertComponentDef2WorkloadDef(dm discoverymapper.DiscoveryMapper, compone
|
||||
return nil
|
||||
}
|
||||
|
||||
// ExtractRevisionNum extract revision number from appRevision name
|
||||
func ExtractRevisionNum(appRevision string) (int, error) {
|
||||
splits := strings.Split(appRevision, "-")
|
||||
// check some bad appRevision name, eg:v1, appv2
|
||||
if len(splits) == 1 {
|
||||
return 0, fmt.Errorf("bad revison name")
|
||||
}
|
||||
// check some bad appRevision name, eg:myapp-a1
|
||||
if !strings.HasPrefix(splits[len(splits)-1], "v") {
|
||||
return 0, fmt.Errorf("bad revison name")
|
||||
}
|
||||
return strconv.Atoi(strings.TrimPrefix(splits[len(splits)-1], "v"))
|
||||
}
|
||||
|
||||
// Min for int
|
||||
func Min(a, b int) int {
|
||||
if a < b {
|
||||
|
||||
@@ -2054,3 +2054,50 @@ spec:
|
||||
assert.Equal(t, expectWd.Spec.Status, wd.Spec.Status)
|
||||
assert.Equal(t, expectWd.Spec.Schematic, wd.Spec.Schematic)
|
||||
}
|
||||
|
||||
func TestExtractRevisionNum(t *testing.T) {
|
||||
testcases := []struct {
|
||||
appRevision string
|
||||
wantRevisionNum int
|
||||
hasError bool
|
||||
}{{
|
||||
appRevision: "myapp-v1",
|
||||
wantRevisionNum: 1,
|
||||
hasError: false,
|
||||
}, {
|
||||
appRevision: "new-app-v2",
|
||||
wantRevisionNum: 2,
|
||||
hasError: false,
|
||||
}, {
|
||||
appRevision: "v1-v10",
|
||||
wantRevisionNum: 10,
|
||||
hasError: false,
|
||||
}, {
|
||||
appRevision: "v10-v1-v1",
|
||||
wantRevisionNum: 1,
|
||||
hasError: false,
|
||||
}, {
|
||||
appRevision: "myapp-v1-v2",
|
||||
wantRevisionNum: 2,
|
||||
hasError: false,
|
||||
}, {
|
||||
appRevision: "myapp-v1-vv",
|
||||
wantRevisionNum: 0,
|
||||
hasError: true,
|
||||
}, {
|
||||
appRevision: "v1",
|
||||
wantRevisionNum: 0,
|
||||
hasError: true,
|
||||
}, {
|
||||
appRevision: "myapp-a1",
|
||||
wantRevisionNum: 0,
|
||||
hasError: true,
|
||||
}}
|
||||
|
||||
for _, tt := range testcases {
|
||||
revision, err := util.ExtractRevisionNum(tt.appRevision)
|
||||
hasError := err != nil
|
||||
assert.Equal(t, tt.wantRevisionNum, revision)
|
||||
assert.Equal(t, tt.hasError, hasError)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user