mirror of
https://github.com/kubevela/kubevela.git
synced 2026-02-14 10:00:06 +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>
92 lines
2.7 KiB
Go
92 lines
2.7 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 (
|
|
"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"])
|
|
})
|
|
}
|