mirror of
https://github.com/kubevela/kubevela.git
synced 2026-05-19 15:56:54 +00:00
* 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>
75 lines
2.4 KiB
Go
75 lines
2.4 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 appfile
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
commontype "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/pkg/utils/common"
|
|
"github.com/oam-dev/kubevela/pkg/utils/util"
|
|
)
|
|
|
|
func TestApplyTerraform(t *testing.T) {
|
|
app := &v1beta1.Application{
|
|
ObjectMeta: v1.ObjectMeta{Name: "test-terraform-app"},
|
|
Spec: v1beta1.ApplicationSpec{Components: []commontype.ApplicationComponent{{
|
|
Name: "test-terraform-svc",
|
|
Type: "aliyun-oss",
|
|
Properties: &runtime.RawExtension{Raw: []byte("{\"bucket\": \"oam-website\"}")},
|
|
},
|
|
}},
|
|
}
|
|
ioStream := util.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
|
|
arg := common.Args{
|
|
Schema: scheme,
|
|
}
|
|
err := arg.SetConfig(cfg)
|
|
assert.NoError(t, err)
|
|
_, err = ApplyTerraform(app, k8sClient, ioStream, addonNamespace, arg)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestGenerateSecretFromTerraformOutput(t *testing.T) {
|
|
var name = "test-addon-secret"
|
|
|
|
t.Run("namespace doesn't exist", func(t *testing.T) {
|
|
badNamespace := "a-not-existed-namespace"
|
|
err := generateSecretFromTerraformOutput(k8sClient, "", name, badNamespace)
|
|
assert.EqualError(t, err, fmt.Sprintf("namespace %s doesn't exist", badNamespace))
|
|
})
|
|
|
|
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)
|
|
assert.EqualError(t, err, fmt.Sprintf("terraform output isn't in the right format: %q", rawOutput))
|
|
})
|
|
}
|