Feat: Enhance unit test coverage for references/appfile package (#6913)

* feat(appfile): Enhance unit test coverage and migrate to standard Go testing

This commit significantly enhances the unit test coverage for the `references/appfile` package by introducing a comprehensive suite of new test cases and migrating existing tests to the standard Go `testing` framework with `testify/assert`.

Key additions and improvements include:
- **New Test Cases for `references/appfile/api/appfile.go`**: Added tests for `NewAppFile`, `JSONToYaml`, and `LoadFromBytes` to ensure correct application file initialization, parsing, and loading.
- **New Test Cases for `references/appfile/api/service.go`**: Introduced tests for `GetUserConfigName`, `GetApplicationConfig`, and `ToStringSlice` to validate service configuration extraction and type conversions.
- **Expanded Test Coverage for `references/appfile/app.go`**: Added new tests for `NewApplication`, `Validate`, `GetComponents`, `GetServiceConfig`, `GetApplicationSettings`, `GetWorkload`, and `GetTraits`, ensuring the robustness of application-level operations.
- **Dedicated Test Files for `modify.go` and `run.go`**: Created `modify_test.go` and `run_test.go` to provide specific unit tests for `SetWorkload`, `CreateOrUpdateApplication`, `CreateOrUpdateObjects`, and `Run` functions.
- **Test Framework Migration**: Refactored `addon_suit_test.go` to `main_test.go` and `addon_test.go` to use standard Go `testing` and `testify/assert`, improving consistency and maintainability.

These changes collectively improve the robustness, reliability, and maintainability of the `appfile` package by providing a more comprehensive and standardized testing approach.

Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com>

* chore(references/appfile): improve test suite robustness and style

This commit introduces two improvements to the test suite in the `references/appfile` package.

First, the `TestMain` function in `main_test.go` is refactored to ensure the `envtest` control-plane is always stopped, even if test setup fails. This is achieved by creating a single exit path that handles cleanup, preventing resource leaks.

Second, a minor linting issue (S1005) in `modify_test.go` is fixed by removing an unnecessary assignment to the blank identifier.

Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com>

* Chore: remove comment to trigger ci

Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com>

---------

Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com>
This commit is contained in:
AshvinBambhaniya2003
2025-10-31 19:21:09 +05:30
committed by GitHub
parent 24f6718619
commit 260fc1a294
8 changed files with 777 additions and 203 deletions

View File

@@ -1,114 +0,0 @@
/*
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 appfile
import (
"context"
"os"
"path/filepath"
"testing"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/yaml"
coreoam "github.com/oam-dev/kubevela/apis/core.oam.dev"
corev1beta1 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/pkg/oam/util"
"github.com/oam-dev/kubevela/pkg/utils/system"
// +kubebuilder:scaffold:imports
)
var cfg *rest.Config
var scheme *runtime.Scheme
var k8sClient client.Client
var testEnv *envtest.Environment
var definitionDir string
var wd corev1beta1.WorkloadDefinition
var addonNamespace = "test-addon"
func TestAppFile(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Cli Suite")
}
var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.UseDevMode(true), zap.WriteTo(GinkgoWriter)))
ctx := context.Background()
By("bootstrapping test environment")
useExistCluster := false
testEnv = &envtest.Environment{
ControlPlaneStartTimeout: time.Minute,
ControlPlaneStopTimeout: time.Minute,
CRDDirectoryPaths: []string{filepath.Join("..", "..", "charts", "vela-core", "crds")},
UseExistingCluster: &useExistCluster,
}
var err error
cfg, err = testEnv.Start()
Expect(err).ToNot(HaveOccurred())
Expect(cfg).ToNot(BeNil())
scheme = runtime.NewScheme()
Expect(coreoam.AddToScheme(scheme)).NotTo(HaveOccurred())
Expect(clientgoscheme.AddToScheme(scheme)).NotTo(HaveOccurred())
Expect(crdv1.AddToScheme(scheme)).NotTo(HaveOccurred())
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
Expect(err).ToNot(HaveOccurred())
Expect(k8sClient).ToNot(BeNil())
definitionDir, err = system.GetCapabilityDir()
Expect(err).Should(BeNil())
Expect(os.MkdirAll(definitionDir, 0755)).Should(BeNil())
Expect(k8sClient.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: addonNamespace}})).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
workloadData, err := os.ReadFile("testdata/workloadDef.yaml")
Expect(err).Should(BeNil())
Expect(yaml.Unmarshal(workloadData, &wd)).Should(BeNil())
wd.Namespace = addonNamespace
logf.Log.Info("Creating workload definition", "data", wd)
Expect(k8sClient.Create(ctx, &wd)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
def, err := os.ReadFile("testdata/terraform-aliyun-oss-workloadDefinition.yaml")
Expect(err).Should(BeNil())
var terraformDefinition corev1beta1.WorkloadDefinition
Expect(yaml.Unmarshal(def, &terraformDefinition)).Should(BeNil())
terraformDefinition.Namespace = addonNamespace
logf.Log.Info("Creating workload definition", "data", terraformDefinition)
Expect(k8sClient.Create(ctx, &terraformDefinition)).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
})
var _ = AfterSuite(func() {
By("tearing down the test environment")
_ = k8sClient.Delete(context.Background(), &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: addonNamespace}})
_ = k8sClient.Delete(context.Background(), &wd)
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})

View File

@@ -19,9 +19,9 @@ package appfile
import (
"fmt"
"os"
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
@@ -31,7 +31,7 @@ import (
"github.com/oam-dev/kubevela/pkg/utils/util"
)
var _ = It("Test ApplyTerraform", func() {
func TestApplyTerraform(t *testing.T) {
app := &v1beta1.Application{
ObjectMeta: v1.ObjectMeta{Name: "test-terraform-app"},
Spec: v1beta1.ApplicationSpec{Components: []commontype.ApplicationComponent{{
@@ -46,27 +46,29 @@ var _ = It("Test ApplyTerraform", func() {
Schema: scheme,
}
err := arg.SetConfig(cfg)
Expect(err).Should(BeNil())
assert.NoError(t, err)
_, err = ApplyTerraform(app, k8sClient, ioStream, addonNamespace, arg)
Expect(err).Should(BeNil())
})
assert.NoError(t, err)
}
var _ = Describe("Test generateSecretFromTerraformOutput", func() {
func TestGenerateSecretFromTerraformOutput(t *testing.T) {
var name = "test-addon-secret"
It("namespace doesn't exist", func() {
t.Run("namespace doesn't exist", func(t *testing.T) {
badNamespace := "a-not-existed-namespace"
err := generateSecretFromTerraformOutput(k8sClient, "", name, badNamespace)
Expect(err).Should(Equal(fmt.Errorf("namespace %s doesn't exist", badNamespace)))
})
It("valid output list", func() {
rawOutput := "name=aaa\nage=1"
err := generateSecretFromTerraformOutput(k8sClient, rawOutput, name, addonNamespace)
Expect(err).Should(BeNil())
assert.EqualError(t, err, fmt.Sprintf("namespace %s doesn't exist", badNamespace))
})
It("invalid output list", func() {
t.Run("valid output list", func(t *testing.T) {
rawOutput := "name=aaa\nage=1"
err := generateSecretFromTerraformOutput(k8sClient, rawOutput, name, addonNamespace)
assert.NoError(t, err)
})
t.Run("invalid output list", func(t *testing.T) {
rawOutput := "name"
err := generateSecretFromTerraformOutput(k8sClient, rawOutput, name, addonNamespace)
Expect(err).Should(Equal(fmt.Errorf("terraform output isn't in the right format: %q", rawOutput)))
assert.EqualError(t, err, fmt.Sprintf("terraform output isn't in the right format: %q", rawOutput))
})
})
}

View File

@@ -34,6 +34,58 @@ import (
"github.com/oam-dev/kubevela/references/appfile/template"
)
func TestNewAppFile(t *testing.T) {
appFile := NewAppFile()
assert.NotNil(t, appFile)
assert.NotNil(t, appFile.Services)
assert.NotNil(t, appFile.Secrets)
assert.Empty(t, appFile.Name)
}
func TestJSONToYaml(t *testing.T) {
t.Run("valid json", func(t *testing.T) {
jsonData := `{"name":"test-app","services":{"my-svc":{"image":"nginx"}}}`
appFile := NewAppFile()
_, err := JSONToYaml([]byte(jsonData), appFile)
assert.NoError(t, err)
assert.Equal(t, "test-app", appFile.Name)
assert.Contains(t, appFile.Services, "my-svc")
assert.Equal(t, "nginx", appFile.Services["my-svc"]["image"])
})
t.Run("invalid json", func(t *testing.T) {
jsonData := `{"name":"test-app"`
appFile := NewAppFile()
_, err := JSONToYaml([]byte(jsonData), appFile)
assert.Error(t, err)
})
}
func TestLoadFromBytes(t *testing.T) {
t.Run("valid yaml", func(t *testing.T) {
yamlData := `
name: test-yaml-app
services:
main:
image: httpd
`
appFile, err := LoadFromBytes([]byte(yamlData))
assert.NoError(t, err)
assert.Equal(t, "test-yaml-app", appFile.Name)
assert.Len(t, appFile.Services, 1)
assert.Equal(t, "httpd", appFile.Services["main"]["image"])
})
t.Run("valid json", func(t *testing.T) {
jsonData := `{"name":"test-json-app","services":{"sidecar":{"image":"redis"}}}`
appFile, err := LoadFromBytes([]byte(jsonData))
assert.NoError(t, err)
assert.Equal(t, "test-json-app", appFile.Name)
assert.Len(t, appFile.Services, 1)
assert.Equal(t, "redis", appFile.Services["sidecar"]["image"])
})
}
func TestBuildOAMApplication2(t *testing.T) {
expectNs := "test-ns"

View File

@@ -36,3 +36,85 @@ func TestGetType(t *testing.T) {
got = svc2.GetType()
assert.Equal(t, workload2, got)
}
func TestGetUserConfigName(t *testing.T) {
t.Run("config name exists", func(t *testing.T) {
svc := Service{"config": "my-config"}
assert.Equal(t, "my-config", svc.GetUserConfigName())
})
t.Run("config name does not exist", func(t *testing.T) {
svc := Service{"image": "nginx"}
assert.Equal(t, "", svc.GetUserConfigName())
})
}
func TestGetApplicationConfig(t *testing.T) {
svc := Service{
"image": "nginx",
"port": 80,
"type": "webservice",
"build": "./",
"config": "my-config",
}
config := svc.GetApplicationConfig()
assert.Contains(t, config, "image")
assert.Contains(t, config, "port")
assert.NotContains(t, config, "type")
assert.NotContains(t, config, "build")
assert.NotContains(t, config, "config")
assert.Len(t, config, 2)
}
func TestToStringSlice(t *testing.T) {
testCases := []struct {
name string
input interface{}
expected []string
}{
{
name: "string",
input: "one",
expected: []string{"one"},
},
{
name: "[]string",
input: []string{"one", "two"},
expected: []string{"one", "two"},
},
{
name: "[]interface{} of strings",
input: []interface{}{"one", "two"},
expected: []string{"one", "two"},
},
{
name: "[]interface{} of mixed types",
input: []interface{}{"one", 2, "three"},
expected: []string{"one", "three"},
},
{
name: "nil input",
input: nil,
expected: nil,
},
{
name: "empty []string",
input: []string{},
expected: []string{},
},
{
name: "other type (int)",
input: 123,
expected: nil,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := toStringSlice(tc.input)
assert.Equal(t, tc.expected, result)
})
}
}

View File

@@ -22,14 +22,18 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/yaml"
"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/apis/types"
"github.com/oam-dev/kubevela/references/appfile/api"
"github.com/oam-dev/kubevela/references/appfile/template"
)
func TestApplication(t *testing.T) {
yamlNormal := `name: myapp
const (
yamlNormal = `name: myapp
services:
frontend:
image: inanimate/echo-server
@@ -45,55 +49,207 @@ services:
type: cloneset
image: "back:v1"
`
yamlNoService := `name: myapp`
yamlNoName := `services:
yamlNoService = `name: myapp`
yamlNoName = `services:
frontend:
image: inanimate/echo-server
env:
PORT: 8080`
yamlTraitNotMap := `name: myapp
yamlTraitNotMap = `name: myapp
services:
frontend:
image: inanimate/echo-server
env:
PORT: 8080
autoscaling: 10`
)
cases := map[string]struct {
raw string
InValid bool
InvalidReason error
ExpName string
ExpComponents []string
WantWorkload string
ExpWorkload map[string]interface{}
ExpWorkloadType string
ExpTraits map[string]map[string]interface{}
func TestNewApplication(t *testing.T) {
tm := template.NewFakeTemplateManager()
app := NewApplication(nil, tm)
assert.NotNil(t, app)
assert.Equal(t, tm, app.Tm)
assert.NotNil(t, app.AppFile)
appfile := api.NewAppFile()
appfile.Name = "test-app"
app = NewApplication(appfile, tm)
assert.NotNil(t, app)
assert.Equal(t, "test-app", app.Name)
}
func TestValidate(t *testing.T) {
testCases := map[string]struct {
raw string
expErr error
addFake bool
}{
"normal case backend": {
raw: yamlNormal,
ExpName: "myapp",
ExpComponents: []string{"backend", "frontend"},
WantWorkload: "backend",
ExpWorkload: map[string]interface{}{
"image": "back:v1",
},
ExpWorkloadType: "cloneset",
ExpTraits: map[string]map[string]interface{}{},
"normal": {
raw: yamlNormal,
expErr: nil,
},
"normal case frontend": {
raw: yamlNormal,
ExpName: "myapp",
ExpComponents: []string{"backend", "frontend"},
WantWorkload: "frontend",
ExpWorkload: map[string]interface{}{
"no service": {
raw: yamlNoService,
expErr: errors.New("at least one service is required"),
},
"no name": {
raw: yamlNoName,
expErr: errors.New("name is required"),
},
"trait not map": {
raw: yamlTraitNotMap,
expErr: fmt.Errorf("trait autoscaling in 'frontend' must be map"),
addFake: true,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
tm := template.NewFakeTemplateManager()
if tc.addFake {
tm.Templates["autoscaling"] = &template.Template{
Captype: types.TypeTrait,
}
}
app := NewApplication(nil, tm)
err := yaml.Unmarshal([]byte(tc.raw), &app)
assert.NoError(t, err)
err = Validate(app)
assert.Equal(t, tc.expErr, err)
})
}
}
func TestGetComponents(t *testing.T) {
app := &v1beta1.Application{
Spec: v1beta1.ApplicationSpec{
Components: []common.ApplicationComponent{
{Name: "c"},
{Name: "a"},
{Name: "b"},
},
},
}
comps := GetComponents(app)
assert.Equal(t, []string{"a", "b", "c"}, comps)
}
func TestGetServiceConfig(t *testing.T) {
tm := template.NewFakeTemplateManager()
app := NewApplication(nil, tm)
err := yaml.Unmarshal([]byte(yamlNormal), &app)
assert.NoError(t, err)
tp, cfg := GetServiceConfig(app, "frontend")
assert.Equal(t, "webservice", tp)
assert.NotEmpty(t, cfg)
assert.Contains(t, cfg, "image")
tp, cfg = GetServiceConfig(app, "backend")
assert.Equal(t, "cloneset", tp)
assert.NotEmpty(t, cfg)
assert.Contains(t, cfg, "image")
tp, cfg = GetServiceConfig(app, "non-existent")
assert.Equal(t, "", tp)
assert.Empty(t, cfg)
}
func TestGetApplicationSettings(t *testing.T) {
app := &v1beta1.Application{
Spec: v1beta1.ApplicationSpec{
Components: []common.ApplicationComponent{
{
Name: "comp-1",
Type: "worker",
Properties: &runtime.RawExtension{
Raw: []byte(`{"image":"my-image"}`),
},
},
},
},
}
tp, settings := GetApplicationSettings(app, "comp-1")
assert.Equal(t, "worker", tp)
assert.Equal(t, map[string]interface{}{"image": "my-image"}, settings)
tp, settings = GetApplicationSettings(app, "non-existent")
assert.Equal(t, "", tp)
assert.Empty(t, settings)
}
func TestGetWorkload(t *testing.T) {
tm := template.NewFakeTemplateManager()
tm.Templates["autoscaling"] = &template.Template{Captype: types.TypeTrait}
tm.Templates["rollout"] = &template.Template{Captype: types.TypeTrait}
app := NewApplication(nil, tm)
err := yaml.Unmarshal([]byte(yamlNormal), &app)
assert.NoError(t, err)
testCases := map[string]struct {
componentName string
expWorkloadType string
expWorkload map[string]interface{}
}{
"frontend": {
componentName: "frontend",
expWorkloadType: "webservice",
expWorkload: map[string]interface{}{
"image": "inanimate/echo-server",
"env": map[string]interface{}{
"PORT": float64(8080),
},
},
ExpWorkloadType: "webservice",
ExpTraits: map[string]map[string]interface{}{
},
"backend": {
componentName: "backend",
expWorkloadType: "cloneset",
expWorkload: map[string]interface{}{
"image": "back:v1",
},
},
"non-existent": {
componentName: "non-existent",
expWorkloadType: "",
expWorkload: map[string]interface{}{},
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
workloadType, workload := GetWorkload(app, tc.componentName)
assert.Equal(t, tc.expWorkloadType, workloadType)
assert.Equal(t, tc.expWorkload, workload)
})
}
}
func TestGetTraits(t *testing.T) {
tm := template.NewFakeTemplateManager()
tm.Templates["autoscaling"] = &template.Template{Captype: types.TypeTrait}
tm.Templates["rollout"] = &template.Template{Captype: types.TypeTrait}
app := NewApplication(nil, tm)
err := yaml.Unmarshal([]byte(yamlNormal), &app)
assert.NoError(t, err)
// Test case with invalid trait format
invalidTraitApp := NewApplication(nil, tm)
err = yaml.Unmarshal([]byte(yamlTraitNotMap), &invalidTraitApp)
assert.NoError(t, err)
testCases := map[string]struct {
app *api.Application
compName string
exp map[string]map[string]interface{}
expErr string
}{
"frontend traits": {
app: app,
compName: "frontend",
exp: map[string]map[string]interface{}{
"autoscaling": {
"max": float64(10),
"min": float64(1),
@@ -104,50 +260,33 @@ services:
},
},
},
"no component": {
raw: yamlNoService,
ExpName: "myapp",
InValid: true,
InvalidReason: errors.New("at least one service is required"),
"backend traits (none)": {
app: app,
compName: "backend",
exp: map[string]map[string]interface{}{},
},
"no name": {
raw: yamlNoName,
ExpName: "",
InValid: true,
InvalidReason: errors.New("name is required"),
"non-existent component": {
app: app,
compName: "non-existent",
exp: map[string]map[string]interface{}{},
},
"trait must be map": {
raw: yamlTraitNotMap,
ExpTraits: map[string]map[string]interface{}{
"autoscaling": {},
},
ExpName: "myapp",
InValid: true,
InvalidReason: fmt.Errorf("trait autoscaling in 'frontend' must be map"),
"invalid trait format": {
app: invalidTraitApp,
compName: "frontend",
exp: nil,
expErr: "autoscaling is trait, but with invalid format float64, should be map[string]interface{}",
},
}
for caseName, c := range cases {
tm := template.NewFakeTemplateManager()
for k := range c.ExpTraits {
tm.Templates[k] = &template.Template{
Captype: types.TypeTrait,
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
traits, err := GetTraits(tc.app, tc.compName)
if tc.expErr != "" {
assert.EqualError(t, err, tc.expErr)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.exp, traits)
}
}
app := NewApplication(nil, tm)
err := yaml.Unmarshal([]byte(c.raw), &app)
assert.NoError(t, err, caseName)
err = Validate(app)
if c.InValid {
assert.Equal(t, c.InvalidReason, err)
continue
}
assert.Equal(t, c.ExpName, app.Name, caseName)
workloadType, workload := GetWorkload(app, c.WantWorkload)
assert.Equal(t, c.ExpWorkload, workload, caseName)
assert.Equal(t, c.ExpWorkloadType, workloadType, caseName)
traits, err := GetTraits(app, c.WantWorkload)
assert.NoError(t, err, caseName)
assert.Equal(t, c.ExpTraits, traits, caseName)
})
}
}

View File

@@ -0,0 +1,161 @@
/*
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 appfile
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"time"
corev1 "k8s.io/api/core/v1"
crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/yaml"
coreoam "github.com/oam-dev/kubevela/apis/core.oam.dev"
corev1beta1 "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/pkg/utils/system"
)
var cfg *rest.Config
var scheme *runtime.Scheme
var k8sClient client.Client
var testEnv *envtest.Environment
var definitionDir string
var wd corev1beta1.WorkloadDefinition
var addonNamespace = "test-addon"
func TestMain(m *testing.M) {
logf.SetLogger(zap.New(zap.UseDevMode(true)))
ctx := context.Background()
useExistCluster := false
testEnv = &envtest.Environment{
ControlPlaneStartTimeout: time.Minute,
ControlPlaneStopTimeout: time.Minute,
CRDDirectoryPaths: []string{filepath.Join("..", "..", "charts", "vela-core", "crds")},
UseExistingCluster: &useExistCluster,
}
var err error
cfg, err = testEnv.Start()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to start test environment: %v\n", err)
os.Exit(1)
}
// cleanupAndExit stops the test environment and exits with the given code.
cleanupAndExit := func(code int) {
// Clean up other resources before stopping the environment
if k8sClient != nil {
_ = k8sClient.Delete(context.Background(), &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: addonNamespace}})
_ = k8sClient.Delete(context.Background(), &wd)
}
if err := testEnv.Stop(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to stop test environment: %v\n", err)
}
os.Exit(code)
}
scheme = runtime.NewScheme()
if err := coreoam.AddToScheme(scheme); err != nil {
fmt.Fprintf(os.Stderr, "Failed to add coreoam to scheme: %v\n", err)
cleanupAndExit(1)
}
if err := clientgoscheme.AddToScheme(scheme); err != nil {
fmt.Fprintf(os.Stderr, "Failed to add clientgoscheme to scheme: %v\n", err)
cleanupAndExit(1)
}
if err := crdv1.AddToScheme(scheme); err != nil {
fmt.Fprintf(os.Stderr, "Failed to add crdv1 to scheme: %v\n", err)
cleanupAndExit(1)
}
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create k8sClient: %v\n", err)
cleanupAndExit(1)
}
definitionDir, err = system.GetCapabilityDir()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get capability dir: %v\n", err)
cleanupAndExit(1)
}
if err := os.MkdirAll(definitionDir, 0755); err != nil {
fmt.Fprintf(os.Stderr, "Failed to create capability dir: %v\n", err)
cleanupAndExit(1)
}
if err := k8sClient.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: addonNamespace}}); err != nil {
if !errors.IsAlreadyExists(err) {
fmt.Fprintf(os.Stderr, "Failed to create test namespace: %v\n", err)
cleanupAndExit(1)
}
}
workloadData, err := os.ReadFile("testdata/workloadDef.yaml")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read workloadDef.yaml: %v\n", err)
cleanupAndExit(1)
}
if err := yaml.Unmarshal(workloadData, &wd); err != nil {
fmt.Fprintf(os.Stderr, "Failed to unmarshal workloadDef.yaml: %v\n", err)
cleanupAndExit(1)
}
wd.Namespace = addonNamespace
if err := k8sClient.Create(ctx, &wd); err != nil {
if !errors.IsAlreadyExists(err) {
fmt.Fprintf(os.Stderr, "Failed to create workload definition: %v\n", err)
cleanupAndExit(1)
}
}
def, err := os.ReadFile("testdata/terraform-aliyun-oss-workloadDefinition.yaml")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read terraform-aliyun-oss-workloadDefinition.yaml: %v\n", err)
cleanupAndExit(1)
}
var terraformDefinition corev1beta1.WorkloadDefinition
if err := yaml.Unmarshal(def, &terraformDefinition); err != nil {
fmt.Fprintf(os.Stderr, "Failed to unmarshal terraformDefinition: %v\n", err)
cleanupAndExit(1)
}
terraformDefinition.Namespace = addonNamespace
if err := k8sClient.Create(ctx, &terraformDefinition); err != nil {
if !errors.IsAlreadyExists(err) {
fmt.Fprintf(os.Stderr, "Failed to create terraform workload definition: %v\n", err)
cleanupAndExit(1)
}
}
code := m.Run()
cleanupAndExit(code)
}

View File

@@ -0,0 +1,91 @@
/*
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 appfile
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/oam-dev/kubevela/references/appfile/api"
"github.com/oam-dev/kubevela/references/appfile/template"
)
func TestSetWorkload(t *testing.T) {
tm := template.NewFakeTemplateManager()
t.Run("app is nil", func(t *testing.T) {
err := SetWorkload(nil, "comp", "worker", nil)
assert.EqualError(t, err, errorAppNilPointer.Error())
})
t.Run("add new component", func(t *testing.T) {
app := NewApplication(nil, tm)
app.Name = "test-app"
workloadData := map[string]interface{}{"image": "test-image", "cmd": []string{"sleep", "1000"}}
err := SetWorkload(app, "my-comp", "worker", workloadData)
assert.NoError(t, err)
assert.Len(t, app.Services, 1)
svc, ok := app.Services["my-comp"]
assert.True(t, ok)
assert.NotNil(t, svc)
assert.Equal(t, "worker", svc["type"])
assert.Equal(t, "test-image", svc["image"])
assert.Equal(t, []string{"sleep", "1000"}, svc["cmd"])
})
t.Run("update existing component", func(t *testing.T) {
app := NewApplication(nil, tm)
app.Name = "test-app"
app.Services["my-comp"] = api.Service{
"type": "worker",
"image": "initial-image",
}
updatedWorkloadData := map[string]interface{}{"image": "updated-image", "port": 8080}
err := SetWorkload(app, "my-comp", "webservice", updatedWorkloadData)
assert.NoError(t, err)
assert.Len(t, app.Services, 1)
svc, ok := app.Services["my-comp"]
assert.True(t, ok)
assert.NotNil(t, svc)
assert.Equal(t, "webservice", svc["type"])
assert.Equal(t, "updated-image", svc["image"])
assert.Equal(t, 8080, svc["port"])
})
t.Run("add to existing services", func(t *testing.T) {
app := NewApplication(nil, tm)
app.Name = "test-app"
app.Services["comp-1"] = api.Service{
"type": "worker",
}
workloadData := map[string]interface{}{"image": "test-image"}
err := SetWorkload(app, "comp-2", "task", workloadData)
assert.NoError(t, err)
assert.Len(t, app.Services, 2)
assert.Contains(t, app.Services, "comp-1")
assert.Contains(t, app.Services, "comp-2")
svc2 := app.Services["comp-2"]
assert.Equal(t, "task", svc2["type"])
assert.Equal(t, "test-image", svc2["image"])
})
}

View File

@@ -0,0 +1,161 @@
/*
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 appfile
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
"github.com/oam-dev/kubevela/pkg/oam"
)
func TestCreateOrUpdateApplication(t *testing.T) {
scheme := runtime.NewScheme()
assert.NoError(t, v1beta1.AddToScheme(scheme))
app := &v1beta1.Application{
ObjectMeta: metav1.ObjectMeta{
Name: "test-app",
Namespace: "default",
},
}
t.Run("create application", func(t *testing.T) {
builder := fake.NewClientBuilder().WithScheme(scheme)
fakeClient := builder.Build()
err := CreateOrUpdateApplication(context.Background(), fakeClient, app)
assert.NoError(t, err)
var created v1beta1.Application
err = fakeClient.Get(context.Background(), client.ObjectKeyFromObject(app), &created)
assert.NoError(t, err)
assert.Equal(t, "test-app", created.Name)
})
t.Run("update application", func(t *testing.T) {
appToUpdate := app.DeepCopy()
appToUpdate.SetAnnotations(map[string]string{"key": "val"})
builder := fake.NewClientBuilder().WithScheme(scheme).WithObjects(app)
fakeClient := builder.Build()
err := CreateOrUpdateApplication(context.Background(), fakeClient, appToUpdate)
assert.NoError(t, err)
var updated v1beta1.Application
err = fakeClient.Get(context.Background(), client.ObjectKeyFromObject(app), &updated)
assert.NoError(t, err)
assert.Equal(t, "val", updated.Annotations["key"])
})
}
func TestCreateOrUpdateObjects(t *testing.T) {
scheme := runtime.NewScheme()
assert.NoError(t, corev1.AddToScheme(scheme))
cm := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-cm",
Namespace: "default",
},
Data: map[string]string{
"initial": "true",
},
}
t.Run("create object", func(t *testing.T) {
builder := fake.NewClientBuilder().WithScheme(scheme)
fakeClient := builder.Build()
objects := []oam.Object{cm}
err := CreateOrUpdateObjects(context.Background(), fakeClient, objects)
assert.NoError(t, err)
var created corev1.ConfigMap
err = fakeClient.Get(context.Background(), client.ObjectKeyFromObject(cm), &created)
assert.NoError(t, err)
assert.Equal(t, "true", created.Data["initial"])
})
t.Run("update object", func(t *testing.T) {
cmToUpdate := cm.DeepCopy()
cmToUpdate.Data["initial"] = "false"
builder := fake.NewClientBuilder().WithScheme(scheme).WithObjects(cm)
fakeClient := builder.Build()
objects := []oam.Object{cmToUpdate}
err := CreateOrUpdateObjects(context.Background(), fakeClient, objects)
assert.NoError(t, err)
var updated corev1.ConfigMap
err = fakeClient.Get(context.Background(), client.ObjectKeyFromObject(cm), &updated)
assert.NoError(t, err)
assert.Equal(t, "false", updated.Data["initial"])
})
}
func TestRun(t *testing.T) {
scheme := runtime.NewScheme()
assert.NoError(t, v1beta1.AddToScheme(scheme))
assert.NoError(t, corev1.AddToScheme(scheme))
app := &v1beta1.Application{
ObjectMeta: metav1.ObjectMeta{
Name: "test-app-run",
Namespace: "default",
},
}
cm := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-cm-run",
Namespace: "default",
},
}
builder := fake.NewClientBuilder().WithScheme(scheme)
fakeClient := builder.Build()
err := Run(context.Background(), fakeClient, app, []oam.Object{cm})
assert.NoError(t, err)
var createdApp v1beta1.Application
err = fakeClient.Get(context.Background(), client.ObjectKeyFromObject(app), &createdApp)
assert.NoError(t, err)
assert.Equal(t, "test-app-run", createdApp.Name)
var createdCM corev1.ConfigMap
err = fakeClient.Get(context.Background(), client.ObjectKeyFromObject(cm), &createdCM)
assert.NoError(t, err)
assert.Equal(t, "test-cm-run", createdCM.Name)
}