Files
kubevela/pkg/policy/envbinding/placement_test.go
AshvinBambhaniya2003 d6ad578070 Feat(tests): Add unit test coverage for core packages (#6889)
* 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>
2025-09-11 07:29:07 +08:00

189 lines
5.3 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 envbinding
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime"
"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha1"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
)
func TestUpdateClusterConnections(t *testing.T) {
app := &v1beta1.Application{}
app.Status.LatestRevision = &common.Revision{Name: "v1"}
status := &v1alpha1.EnvBindingStatus{
ClusterConnections: []v1alpha1.ClusterConnection{{
ClusterName: "cluster-1",
LastActiveRevision: "v0",
}, {
ClusterName: "cluster-2",
LastActiveRevision: "v0",
}},
}
decisions := []v1alpha1.PlacementDecision{{
Cluster: "cluster-1",
}, {
Cluster: "cluster-3",
}}
updateClusterConnections(status, decisions, app)
r := require.New(t)
expectedConnections := []v1alpha1.ClusterConnection{{
ClusterName: "cluster-1",
LastActiveRevision: "v1",
}, {
ClusterName: "cluster-2",
LastActiveRevision: "v0",
}, {
ClusterName: "cluster-3",
LastActiveRevision: "v1",
}}
r.Equal(len(expectedConnections), len(status.ClusterConnections))
for idx, conn := range expectedConnections {
_conn := status.ClusterConnections[idx]
r.Equal(conn.ClusterName, _conn.ClusterName)
r.Equal(conn.LastActiveRevision, _conn.LastActiveRevision)
}
}
func TestWritePlacementDecisions(t *testing.T) {
policyName := "test-policy"
envName1 := "env-1"
envName2 := "env-2"
decisions1 := []v1alpha1.PlacementDecision{{Cluster: "cluster-1"}}
decisions2 := []v1alpha1.PlacementDecision{{Cluster: "cluster-2"}}
makeAppWithPolicy := func(t *testing.T) *v1beta1.Application {
app := &v1beta1.Application{}
err := WritePlacementDecisions(app, policyName, envName1, decisions1)
require.NoError(t, err)
return app
}
testCases := []struct {
name string
setupApp func(t *testing.T) *v1beta1.Application
envName string
decisions []v1alpha1.PlacementDecision
wantErr bool
verify func(t *testing.T, app *v1beta1.Application)
}{
{
name: "add to empty policy status",
setupApp: func(t *testing.T) *v1beta1.Application {
return &v1beta1.Application{}
},
envName: envName1,
decisions: decisions1,
wantErr: false,
verify: func(t *testing.T, app *v1beta1.Application) {
r := require.New(t)
r.Len(app.Status.PolicyStatus, 1)
r.Equal(policyName, app.Status.PolicyStatus[0].Name)
r.Equal(v1alpha1.EnvBindingPolicyType, app.Status.PolicyStatus[0].Type)
status := &v1alpha1.EnvBindingStatus{}
err := json.Unmarshal(app.Status.PolicyStatus[0].Status.Raw, status)
r.NoError(err)
r.Len(status.Envs, 1)
r.Equal(envName1, status.Envs[0].Env)
r.Equal(decisions1, status.Envs[0].Placements)
},
},
{
name: "update existing env in existing policy",
setupApp: makeAppWithPolicy,
envName: envName1,
decisions: decisions2,
wantErr: false,
verify: func(t *testing.T, app *v1beta1.Application) {
r := require.New(t)
r.Len(app.Status.PolicyStatus, 1)
status := &v1alpha1.EnvBindingStatus{}
err := json.Unmarshal(app.Status.PolicyStatus[0].Status.Raw, status)
r.NoError(err)
r.Len(status.Envs, 1)
r.Equal(envName1, status.Envs[0].Env)
r.Equal(decisions2, status.Envs[0].Placements)
},
},
{
name: "add new env to existing policy",
setupApp: makeAppWithPolicy,
envName: envName2,
decisions: decisions2,
wantErr: false,
verify: func(t *testing.T, app *v1beta1.Application) {
r := require.New(t)
r.Len(app.Status.PolicyStatus, 1)
status := &v1alpha1.EnvBindingStatus{}
err := json.Unmarshal(app.Status.PolicyStatus[0].Status.Raw, status)
r.NoError(err)
r.Len(status.Envs, 2)
envMap := make(map[string][]v1alpha1.PlacementDecision)
for _, envStatus := range status.Envs {
envMap[envStatus.Env] = envStatus.Placements
}
r.Equal(decisions1, envMap[envName1])
r.Equal(decisions2, envMap[envName2])
},
},
{
name: "handle malformed existing status",
setupApp: func(t *testing.T) *v1beta1.Application {
return &v1beta1.Application{
Status: common.AppStatus{
PolicyStatus: []common.PolicyStatus{
{
Name: policyName,
Type: v1alpha1.EnvBindingPolicyType,
Status: &runtime.RawExtension{Raw: []byte("this is not json")},
},
},
},
}
},
envName: envName1,
decisions: decisions1,
wantErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
r := require.New(t)
app := tc.setupApp(t)
err := WritePlacementDecisions(app, policyName, tc.envName, tc.decisions)
if tc.wantErr {
r.Error(err)
} else {
r.NoError(err)
}
if tc.verify != nil {
tc.verify(t, app)
}
})
}
}