mirror of
https://github.com/kubevela/kubevela.git
synced 2026-02-14 18:10:21 +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>
121 lines
2.6 KiB
Go
121 lines
2.6 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 api
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestGetType(t *testing.T) {
|
|
svc1 := Service{}
|
|
got := svc1.GetType()
|
|
assert.Equal(t, DefaultWorkloadType, got)
|
|
|
|
var workload2 = "W2"
|
|
map2 := map[string]interface{}{
|
|
"type": workload2,
|
|
"cpu": "0.5",
|
|
}
|
|
svc2 := Service(map2)
|
|
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)
|
|
})
|
|
}
|
|
}
|