mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-18 20:17:04 +00:00
* test(resourcekeeper): add unit tests for resource management This commit introduces new unit tests to improve the test coverage of the `resourcekeeper` package. - A new test file `containsresources_test.go` is added, which includes a comprehensive table-driven test for the `ContainsResources` function. - A new table-driven test, `TestUpdateSharedManagedResourceOwner`, is added to `gc_test.go` to verify the logic for updating ownership of shared resources. These tests follow Go best practices and enhance the robustness of the resourcekeeper functionality. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * feat(resourcetracker): add unit tests for tree display logic This commit enhances the test coverage for the resource tree display logic in the `pkg/resourcetracker` package. - Refactors `TestResourceTreePrintOption_getWidthForDetails` to cover more cases and improve test clarity. - Adds a comprehensive test for `TestPrintResourceTree` to verify the output of the resource tree printing. - Introduces a new test for the `tableRoundTripper` to ensure the HTTP `Accept` header is correctly mutated. - Adds tests for helper functions like `TestLoadResourceRows`, `TestSortRows`, and `TestFillResourceRows` to ensure each part of the tree building logic is working as expected. These changes improve the overall quality and reliability of the resource tracker's tree view functionality. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * feat(envbinding): add unit tests for placement logic This commit enhances the test coverage for the `envbinding` policy package. - Adds a new test for `WritePlacementDecisions` to verify the logic of writing placement decisions to the application status. This includes scenarios for adding new policies, updating existing ones, and handling malformed data. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * feat(schema): add unit tests for schema parsing and conversion This commit enhances the test coverage for the `pkg/schema` package by adding unit tests for CUE parsing and OpenAPI schema conversion. - Adds a new test for `ParsePropertiesToSchema` to verify that CUE parameter definitions are correctly parsed into OpenAPI schemas. - Introduces a new test for `ConvertOpenAPISchema2SwaggerObject` to ensure the conversion from a raw OpenAPI v3 schema to a Swagger object is handled correctly, including error cases. These tests improve the reliability of the schema generation and conversion logic, which is critical for capability definitions. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> --------- Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com>
157 lines
4.3 KiB
Go
157 lines
4.3 KiB
Go
/*
|
|
Copyright 2022 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 resourcekeeper
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
|
|
|
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
|
|
"github.com/oam-dev/kubevela/pkg/utils/common"
|
|
)
|
|
|
|
// newUnstructured is a helper to create a simple unstructured object for testing.
|
|
func newUnstructured(name string) *unstructured.Unstructured {
|
|
return &unstructured.Unstructured{
|
|
Object: map[string]interface{}{
|
|
"apiVersion": "v1",
|
|
"kind": "ConfigMap",
|
|
"metadata": map[string]interface{}{
|
|
"name": name,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestContainsResources(t *testing.T) {
|
|
res1 := newUnstructured("res-1")
|
|
res2 := newUnstructured("res-2")
|
|
res3 := newUnstructured("res-3")
|
|
res4Missing := newUnstructured("res-4-missing")
|
|
|
|
baseKeeperSetup := func() *resourceKeeper {
|
|
return &resourceKeeper{
|
|
Client: fake.NewClientBuilder().WithScheme(common.Scheme).Build(),
|
|
_currentRT: &v1beta1.ResourceTracker{},
|
|
_rootRT: &v1beta1.ResourceTracker{},
|
|
}
|
|
}
|
|
|
|
testCases := []struct {
|
|
name string
|
|
keeper *resourceKeeper
|
|
input []*unstructured.Unstructured
|
|
expected bool
|
|
}{
|
|
{
|
|
name: "all resources exist across both trackers",
|
|
keeper: func() *resourceKeeper {
|
|
k := baseKeeperSetup()
|
|
k._currentRT.AddManagedResource(res1, true, false, "")
|
|
k._rootRT.AddManagedResource(res2, true, false, "")
|
|
k._rootRT.AddManagedResource(res3, true, false, "")
|
|
return k
|
|
}(),
|
|
input: []*unstructured.Unstructured{res1, res2, res3},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "one resource exists in currentRT",
|
|
keeper: func() *resourceKeeper {
|
|
k := baseKeeperSetup()
|
|
k._currentRT.AddManagedResource(res1, true, false, "")
|
|
return k
|
|
}(),
|
|
input: []*unstructured.Unstructured{res1},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "one resource exists in rootRT",
|
|
keeper: func() *resourceKeeper {
|
|
k := baseKeeperSetup()
|
|
k._rootRT.AddManagedResource(res2, true, false, "")
|
|
return k
|
|
}(),
|
|
input: []*unstructured.Unstructured{res2},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "one resource is missing",
|
|
keeper: func() *resourceKeeper {
|
|
k := baseKeeperSetup()
|
|
k._currentRT.AddManagedResource(res1, true, false, "")
|
|
return k
|
|
}(),
|
|
input: []*unstructured.Unstructured{res1, res4Missing},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "empty input slice should return true",
|
|
keeper: &resourceKeeper{
|
|
Client: fake.NewClientBuilder().WithScheme(common.Scheme).Build(),
|
|
},
|
|
input: []*unstructured.Unstructured{},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "trackers are nil",
|
|
keeper: &resourceKeeper{
|
|
Client: fake.NewClientBuilder().WithScheme(common.Scheme).Build(),
|
|
},
|
|
input: []*unstructured.Unstructured{res1},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "only rootRT is nil, resource in currentRT",
|
|
keeper: func() *resourceKeeper {
|
|
k := &resourceKeeper{
|
|
Client: fake.NewClientBuilder().WithScheme(common.Scheme).Build(),
|
|
_currentRT: &v1beta1.ResourceTracker{},
|
|
}
|
|
k._currentRT.AddManagedResource(res1, true, false, "")
|
|
return k
|
|
}(),
|
|
input: []*unstructured.Unstructured{res1},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "only rootRT is nil, resource not in currentRT",
|
|
keeper: func() *resourceKeeper {
|
|
k := &resourceKeeper{
|
|
Client: fake.NewClientBuilder().WithScheme(common.Scheme).Build(),
|
|
_currentRT: &v1beta1.ResourceTracker{},
|
|
}
|
|
k._currentRT.AddManagedResource(res1, true, false, "")
|
|
return k
|
|
}(),
|
|
input: []*unstructured.Unstructured{res2},
|
|
expected: false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
r := require.New(t)
|
|
result := tc.keeper.ContainsResources(tc.input)
|
|
r.Equal(tc.expected, result)
|
|
})
|
|
}
|
|
}
|