Files
kubevela/references/cli/addon_test.go
Charlie Chiang f8833e34bc Feat: implement addon init command (#4162)
* Feat: implement addon create command

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Refactor: make global vars local

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Style: fix typos

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Test: create test for file utils

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Feat: use -p flag to manually specify paths

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Refactor: make changes according to comments

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Feat: create empty scaffold if Chart-related paramaters are not provided

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Refactor: fix golangci-lint warnings

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Test: add cli tests

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* Feat: show URL in errors if an invalid URL is detected

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>
2022-06-24 14:25:36 +08:00

441 lines
9.9 KiB
Go

/*
Copyright 2021 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cli
import (
"fmt"
"os"
"strings"
"testing"
"github.com/fatih/color"
pkgaddon "github.com/oam-dev/kubevela/pkg/addon"
"github.com/getkin/kin-openapi/openapi3"
"github.com/oam-dev/kubevela/pkg/utils/common"
"github.com/oam-dev/kubevela/pkg/utils/util"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"gotest.tools/assert"
)
func TestParseMap(t *testing.T) {
testcase := []struct {
args []string
res map[string]interface{}
nilError bool
}{
{
args: []string{"key1=value1"},
res: map[string]interface{}{
"key1": "value1",
},
nilError: true,
},
{
args: []string{"dbUrl=mongodb=mgset-58800212"},
res: map[string]interface{}{
"dbUrl": "mongodb=mgset-58800212",
},
nilError: true,
},
{
args: []string{"imagePullSecrets={a,b,c}"},
res: map[string]interface{}{
"imagePullSecrets": []interface{}{
"a", "b", "c",
},
},
nilError: true,
},
{
args: []string{"image.repo=www.test.com", "image.tag=1.1"},
res: map[string]interface{}{
"image": map[string]interface{}{
"repo": "www.test.com",
"tag": "1.1",
},
},
nilError: true,
},
{
args: []string{"local=true"},
res: map[string]interface{}{
"local": true,
},
nilError: true,
},
{
args: []string{"replicas=3"},
res: map[string]interface{}{
"replicas": int64(3),
},
nilError: true,
},
}
for _, s := range testcase {
r, err := parseAddonArgsToMap(s.args)
if s.nilError {
assert.NilError(t, err)
assert.DeepEqual(t, s.res, r)
} else {
assert.Error(t, err, fmt.Sprintf("%v should be error case", s.args))
}
}
}
func TestAddonEnableCmdWithErrLocalPath(t *testing.T) {
testcase := []struct {
args []string
errMsg string
}{
{
args: []string{"./a_local_path"},
errMsg: "addon directory ./a_local_path not found in local",
},
{
args: []string{"a_local_path/"},
errMsg: "addon directory a_local_path/ not found in local",
},
}
ioStream := util.IOStreams{}
commandArgs := common.Args{}
cmd := NewAddonEnableCommand(commandArgs, ioStream)
for _, s := range testcase {
cmd.SetArgs(s.args)
err := cmd.Execute()
assert.Error(t, err, s.errMsg)
}
}
var _ = Describe("Test AddonRegistry Cmd", func() {
It("Test AddonRegistryAddCmd", func() {
testAddonRegistryAddCmd()
})
})
func testAddonRegistryAddCmd() {
testcase := []struct {
args []string
errMsg string
}{
{
args: []string{"noAuthRegistry", "--type=helm", "--endpoint=http://127.0.0.1/chartrepo/oam"},
errMsg: "fail to add no auth addon registry",
},
{
args: []string{"basicAuthRegistry", "--type=helm", "--endpoint=http://127.0.0.1/chartrepo/oam", "--username=hello", "--password=word"},
errMsg: "fail to add basis auth addon registry",
},
}
ioStream := util.IOStreams{}
commandArgs := common.Args{}
commandArgs.SetClient(k8sClient)
cmd := NewAddAddonRegistryCommand(commandArgs, ioStream)
for _, s := range testcase {
cmd.SetArgs(s.args)
err := cmd.Execute()
Expect(err).Should(BeNil(), s.errMsg)
}
}
func TestAddonUpgradeCmdWithErrLocalPath(t *testing.T) {
testcase := []struct {
args []string
errMsg string
}{
{
args: []string{"./a_local_path"},
errMsg: "addon directory ./a_local_path not found in local",
},
{
args: []string{"a_local_path/"},
errMsg: "addon directory a_local_path/ not found in local",
},
}
ioStream := util.IOStreams{}
commandArgs := common.Args{}
cmd := NewAddonUpgradeCommand(commandArgs, ioStream)
for _, s := range testcase {
cmd.SetArgs(s.args)
err := cmd.Execute()
assert.Error(t, err, s.errMsg)
}
}
func TestTransCluster(t *testing.T) {
testcase := []struct {
str string
res []string
}{
{
str: "{cluster1, cluster2}",
res: []string{"cluster1", "cluster2"},
},
{
str: "{cluster1,cluster2}",
res: []string{"cluster1", "cluster2"},
},
{
str: "{cluster1, cluster2 }",
res: []string{"cluster1", "cluster2"},
},
}
for _, s := range testcase {
assert.DeepEqual(t, transClusters(s.str), s.res)
}
}
func TestGenerateAvailableVersions(t *testing.T) {
type testcase struct {
inVersion string
versions []string
}
testcases := []struct {
c testcase
res string
}{
{
c: testcase{
inVersion: "1.2.1",
versions: []string{"1.2.1"},
},
res: fmt.Sprintf("[%s]", color.New(color.Bold, color.FgGreen).Sprintf("1.2.1")),
},
{
c: testcase{
inVersion: "1.2.1",
versions: []string{"1.2.3", "1.2.2", "1.2.1"},
},
res: fmt.Sprintf("[%s, 1.2.3, 1.2.2]", color.New(color.Bold, color.FgGreen).Sprintf("1.2.1")),
},
{
c: testcase{
inVersion: "1.2.1",
versions: []string{"1.2.3", "1.2.2", "1.2.1", "1.2.0"},
},
res: fmt.Sprintf("[%s, 1.2.3, 1.2.2, ...]", color.New(color.Bold, color.FgGreen).Sprintf("1.2.1")),
},
}
for _, s := range testcases {
re := genAvailableVersionInfo(s.c.versions, s.c.inVersion, 3)
assert.Equal(t, re, s.res)
}
}
func TestLimitStringLength(t *testing.T) {
type testcase struct {
testString string
lengthLimit int
}
testcases := []struct {
c testcase
res string
}{
// len = limit
{
c: testcase{
testString: "4444",
lengthLimit: 4,
},
res: "4444",
},
// len > limit
{
c: testcase{
testString: "3333",
lengthLimit: 3,
},
res: "333...",
},
// len < limit
{
c: testcase{
testString: "22",
lengthLimit: 3,
},
res: "22",
},
// limit = 0
{
c: testcase{
testString: "000",
lengthLimit: 0,
},
res: "000",
},
// limit < 0
{
c: testcase{
testString: "000",
lengthLimit: -1,
},
res: "000",
},
}
for _, s := range testcases {
re := limitStringLength(s.c.testString, s.c.lengthLimit)
assert.Equal(t, re, s.res)
}
}
func TestAddonPackageCmdWithInvalidArgs(t *testing.T) {
testcase := []struct {
args []string
msg string
}{
{
args: []string{},
msg: "must specify addon directory path",
},
{
args: []string{"./a_local_path"},
msg: "fail to package",
},
{
args: []string{"a_local_path/"},
msg: "fail to package",
},
}
commandArgs := common.Args{}
cmd := NewAddonPackageCommand(commandArgs)
for _, s := range testcase {
cmd.SetArgs(s.args)
err := cmd.Execute()
assert.ErrorContains(t, err, s.msg)
}
}
func TestPackageValidAddon(t *testing.T) {
commandArgs := common.Args{}
cmd := NewAddonPackageCommand(commandArgs)
cmd.SetArgs([]string{"./test-data/addon/sample"})
err := cmd.Execute()
assert.NilError(t, err)
}
func TestGenerateParameterString(t *testing.T) {
testcase := []struct {
status pkgaddon.Status
addonPackage *pkgaddon.WholeAddonPackage
outputs []string
}{
{
status: pkgaddon.Status{},
addonPackage: &pkgaddon.WholeAddonPackage{
APISchema: nil,
},
outputs: []string{""},
},
{
status: pkgaddon.Status{
Parameters: map[string]interface{}{
"database": "kubevela",
"dbType": "kubeapi",
},
},
addonPackage: &pkgaddon.WholeAddonPackage{
APISchema: &openapi3.Schema{
Required: []string{"dbType", "serviceAccountName", "serviceType", "dex"},
Properties: openapi3.Schemas{
"database": &openapi3.SchemaRef{
Value: &openapi3.Schema{
Description: "Specify the database name, for the kubeapi db type, it represents namespace.",
Default: nil,
},
},
"dbURL": &openapi3.SchemaRef{
Value: &openapi3.Schema{
Description: "Specify the MongoDB URL. it only enabled where DB type is MongoDB.",
Default: nil,
},
},
"dbType": &openapi3.SchemaRef{
Value: &openapi3.Schema{
Description: "Specify the database type, current support KubeAPI(default) and MongoDB.",
Default: "kubeapi",
},
},
},
},
},
outputs: []string{
// dbType
color.New(color.FgCyan).Sprintf("-> ") +
color.New(color.Bold).Sprint("dbType") + ": " +
"Specify the database type, current support KubeAPI(default) and MongoDB.\n" +
"\tcurrent: " + color.New(color.FgGreen).Sprint("\"kubeapi\"\n") +
"\tdefault: " + "\"kubeapi\"\n" +
"\trequired: " + color.GreenString("✔\n"),
// dbURL
color.New(color.FgCyan).Sprintf("-> ") +
color.New(color.Bold).Sprint("dbURL") + ": " +
"Specify the MongoDB URL. it only enabled where DB type is MongoDB.",
// database
color.New(color.FgCyan).Sprintf("-> ") +
color.New(color.Bold).Sprint("database") + ": " +
"Specify the database name, for the kubeapi db type, it represents namespace.\n" +
"\tcurrent: " + color.New(color.FgGreen).Sprint("\"kubevela\""),
},
},
}
for _, s := range testcase {
res := generateParameterString(s.status, s.addonPackage)
for _, o := range s.outputs {
assert.Check(t, strings.Contains(res, o))
}
}
}
func TestNewAddonCreateCommand(t *testing.T) {
cmd := NewAddonCreateCommand()
cmd.SetArgs([]string{})
err := cmd.Execute()
assert.ErrorContains(t, err, "required")
cmd.SetArgs([]string{"--chart", "a", "--helm-repo-url", "https://some.com", "--version", "c"})
err = cmd.Execute()
assert.ErrorContains(t, err, "required")
cmd.SetArgs([]string{"test-addon", "--chart", "a", "--helm-repo-url", "https://some.com", "--version", "c"})
err = cmd.Execute()
assert.NilError(t, err)
_ = os.RemoveAll("test-addon")
cmd.SetArgs([]string{"test-addon"})
err = cmd.Execute()
assert.NilError(t, err)
_ = os.RemoveAll("test-addon")
}