mirror of
https://github.com/kubevela/kubevela.git
synced 2026-05-19 07:46:51 +00:00
Some checks failed
Webhook Upgrade Validation / webhook-upgrade-check (push) Failing after 26s
* feat(common): Enhance unit test coverage for common utilities This commit significantly enhances the unit test coverage for the `references/common` package, covering a wide range of utilities related to application management, metrics, registry operations, traits, and workloads. Existing tests have also been refactored to improve readability and maintainability. Key additions and improvements include: - **Application Utilities**: New tests for `ExportFromAppFile`, `ApplyApp`, `IsAppfile`, `Info`, `SonLeafResource`, `LoadAppFile`, and `ApplyApplication` in `application_test.go`. - **Metrics Utilities**: Expanded tests for `ToPercentage`, `GetPodStorage`, and `GetPodOfManagedResource` in `metrics_test.go`, with existing tests refactored to use `testify/assert` and table-driven formats. - **Registry Operations**: New tests for `InstallComponentDefinition` and `InstallTraitDefinition` in `registry_test.go`. - **Trait Definitions**: New `trait_test.go` file with tests for `ListRawWorkloadDefinitions`. - **Workload Initialization**: New `workload_test.go` file with tests for `InitApplication` and `BaseComplete`. These changes collectively improve the robustness, reliability, and maintainability of the `references/common` package by providing a more comprehensive and standardized testing approach. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * test(common): Improve test assertions and error handling This commit improves the quality and reliability of unit tests in the `references/common` package by addressing several inconsistencies and potential issues. Key changes include: - Asserts the error returned by `v1beta1.AddToScheme` across multiple test files (`application_test.go`, `registry_test.go`, `workload_test.go`) to prevent masking scheme registration failures. - Replaces `strings.Contains` with the more idiomatic `assert.Contains` in `application_test.go`. - Adds an assertion to check the error returned by `tmpFile.Close()` in `application_test.go`. - Uses `assert.EqualError` instead of `assert.Equal` for comparing error messages in `registry_test.go` for more precise error checking. - Removes an unused `strings` import from `application_test.go`. These changes lead to more robust, readable, and consistent tests. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * fix(common): Fix flaky test in TestExportFromAppFile The `TestExportFromAppFile` test was passing locally but failing in CI with a "no matches for kind" error. This was caused by passing an uninitialized `common.Args` object to the `ExportFromAppFile` function. The function was using the client from this object, which was not the correctly configured fake client. This commit fixes the issue by explicitly setting the fake client on the `common.Args` object before it is used, making the test hermetic and reliable. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> --------- Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com>
91 lines
2.6 KiB
Go
91 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 common
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"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"
|
|
"github.com/oam-dev/kubevela/pkg/utils/common"
|
|
)
|
|
|
|
func TestListRawWorkloadDefinitions(t *testing.T) {
|
|
s := runtime.NewScheme()
|
|
v1beta1.AddToScheme(s)
|
|
|
|
sysWorkload := v1beta1.WorkloadDefinition{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "sys-worker",
|
|
Namespace: oam.SystemDefinitionNamespace,
|
|
},
|
|
}
|
|
userWorkload := v1beta1.WorkloadDefinition{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "user-worker",
|
|
Namespace: "my-ns",
|
|
},
|
|
}
|
|
|
|
t.Run("list from both user and system namespaces", func(t *testing.T) {
|
|
k8sClient := fake.NewClientBuilder().WithScheme(s).WithObjects(&sysWorkload, &userWorkload).Build()
|
|
c := common.Args{}
|
|
c.SetClient(k8sClient)
|
|
|
|
defs, err := ListRawWorkloadDefinitions("my-ns", c)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 2, len(defs))
|
|
})
|
|
|
|
t.Run("list from only system namespace", func(t *testing.T) {
|
|
k8sClient := fake.NewClientBuilder().WithScheme(s).WithObjects(&sysWorkload).Build()
|
|
c := common.Args{}
|
|
c.SetClient(k8sClient)
|
|
|
|
defs, err := ListRawWorkloadDefinitions("my-ns", c)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, len(defs))
|
|
assert.Equal(t, "sys-worker", defs[0].Name)
|
|
})
|
|
|
|
t.Run("list from only user namespace", func(t *testing.T) {
|
|
k8sClient := fake.NewClientBuilder().WithScheme(s).WithObjects(&userWorkload).Build()
|
|
c := common.Args{}
|
|
c.SetClient(k8sClient)
|
|
|
|
defs, err := ListRawWorkloadDefinitions("my-ns", c)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, len(defs))
|
|
assert.Equal(t, "user-worker", defs[0].Name)
|
|
})
|
|
|
|
t.Run("no definitions found", func(t *testing.T) {
|
|
k8sClient := fake.NewClientBuilder().WithScheme(s).Build()
|
|
c := common.Args{}
|
|
c.SetClient(k8sClient)
|
|
|
|
defs, err := ListRawWorkloadDefinitions("my-ns", c)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 0, len(defs))
|
|
})
|
|
}
|