mirror of
https://github.com/kubevela/kubevela.git
synced 2026-05-06 01:17:09 +00:00
* 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>
172 lines
4.4 KiB
Go
172 lines
4.4 KiB
Go
/*
|
|
Copyright 2022 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 addon
|
|
|
|
import (
|
|
"os"
|
|
"path"
|
|
"testing"
|
|
|
|
"gotest.tools/assert"
|
|
)
|
|
|
|
func TestCheckAddonName(t *testing.T) {
|
|
var err error
|
|
|
|
err = CheckAddonName("")
|
|
assert.ErrorContains(t, err, "should not be empty")
|
|
|
|
invalidNames := []string{
|
|
"-addon",
|
|
"addon-",
|
|
"Caps",
|
|
"=",
|
|
".",
|
|
}
|
|
for _, name := range invalidNames {
|
|
err = CheckAddonName(name)
|
|
assert.ErrorContains(t, err, "should only")
|
|
}
|
|
|
|
validNames := []string{
|
|
"addon-name",
|
|
"3-addon-name",
|
|
"addon-name-3",
|
|
"addon",
|
|
}
|
|
for _, name := range validNames {
|
|
err = CheckAddonName(name)
|
|
assert.NilError(t, err)
|
|
}
|
|
}
|
|
|
|
func TestWriteHelmCUETemplate(t *testing.T) {
|
|
resourceTmpl := HelmCUETemplate{}
|
|
resourceTmpl.Output.Type = "helm"
|
|
resourceTmpl.Output.Properties.RepoType = "helm"
|
|
resourceTmpl.Output.Properties.URL = "https://charts.bitnami.com/bitnami"
|
|
resourceTmpl.Output.Properties.Chart = "bitnami/nginx"
|
|
resourceTmpl.Output.Properties.Version = "12.0.4"
|
|
err := writeHelmCUETemplate(resourceTmpl, "test.cue")
|
|
assert.NilError(t, err)
|
|
defer func() {
|
|
_ = os.Remove("test.cue")
|
|
}()
|
|
data, err := os.ReadFile("test.cue")
|
|
assert.NilError(t, err)
|
|
expected := `output: {
|
|
type: "helm"
|
|
properties: {
|
|
url: "https://charts.bitnami.com/bitnami"
|
|
repoType: "helm"
|
|
chart: "bitnami/nginx"
|
|
version: "12.0.4"
|
|
}
|
|
}`
|
|
assert.Equal(t, string(data), expected)
|
|
}
|
|
|
|
func TestCreateAddonFromHelmChart(t *testing.T) {
|
|
err := CreateAddonFromHelmChart("", "", "", "bitnami/nginx", "12.0.4")
|
|
assert.ErrorContains(t, err, "should not be empty")
|
|
|
|
checkFiles := func(base string) {
|
|
fileList := []string{
|
|
"definitions",
|
|
path.Join("resources", base+".cue"),
|
|
"schemas",
|
|
MetadataFileName,
|
|
ReadmeFileName,
|
|
TemplateFileName,
|
|
}
|
|
for _, file := range fileList {
|
|
_, err = os.Stat(path.Join(base, file))
|
|
assert.NilError(t, err)
|
|
}
|
|
}
|
|
|
|
// Empty dir already exists
|
|
_ = os.MkdirAll("test-addon", 0755)
|
|
err = CreateAddonFromHelmChart("test-addon", "./test-addon", "https://charts.bitnami.com/bitnami", "bitnami/nginx", "12.0.4")
|
|
checkFiles("test-addon")
|
|
defer func() {
|
|
_ = os.RemoveAll("test-addon")
|
|
}()
|
|
|
|
// Non-empty dir already exists
|
|
err = CreateAddonFromHelmChart("test-addon", "test-addon", "https://charts.bitnami.com/bitnami", "bitnami/nginx", "12.0.4")
|
|
assert.ErrorContains(t, err, "not empty")
|
|
|
|
// Name already taken
|
|
err = os.WriteFile("already-taken", []byte{}, 0644)
|
|
assert.NilError(t, err)
|
|
defer func() {
|
|
_ = os.Remove("already-taken")
|
|
}()
|
|
err = CreateAddonFromHelmChart("already-taken", "already-taken", "https://charts.bitnami.com/bitnami", "bitnami/nginx", "12.0.4")
|
|
assert.ErrorContains(t, err, "can't create")
|
|
|
|
// Invalid addon name
|
|
err = CreateAddonFromHelmChart("/", "./a", "https://charts.bitnami.com/bitnami", "bitnami/nginx", "12.0.4")
|
|
assert.ErrorContains(t, err, "should only")
|
|
|
|
// Invalid URL
|
|
err = CreateAddonFromHelmChart("invalid-url", "invalid-url", "invalid-url", "bitnami/nginx", "12.0.4")
|
|
assert.ErrorContains(t, err, "invalid helm repo url")
|
|
}
|
|
|
|
func TestCreateAddonSample(t *testing.T) {
|
|
checkFiles := func(base string) {
|
|
fileList := []string{
|
|
"definitions",
|
|
"resources",
|
|
"schemas",
|
|
MetadataFileName,
|
|
ReadmeFileName,
|
|
TemplateFileName,
|
|
}
|
|
for _, file := range fileList {
|
|
_, err := os.Stat(path.Join(base, file))
|
|
assert.NilError(t, err)
|
|
}
|
|
}
|
|
|
|
// Normal creation
|
|
err := CreateAddonSample("test-addon", "test-addon")
|
|
assert.NilError(t, err)
|
|
checkFiles("test-addon")
|
|
|
|
// Non-empty dir already exists
|
|
err = CreateAddonSample("test-addon", "test-addon")
|
|
assert.ErrorContains(t, err, "directory")
|
|
|
|
defer func() {
|
|
_ = os.RemoveAll("test-addon")
|
|
}()
|
|
|
|
err = CreateAddonSample("", "")
|
|
assert.ErrorContains(t, err, "empty")
|
|
}
|
|
|
|
func TestPreAddonCreation(t *testing.T) {
|
|
err := preAddonCreation("", "")
|
|
assert.ErrorContains(t, err, "empty")
|
|
|
|
err = preAddonCreation("=", "a")
|
|
assert.ErrorContains(t, err, "name")
|
|
}
|