mirror of
https://github.com/kubevela/kubevela.git
synced 2026-02-14 18:10:21 +00:00
* feat(config): add unit test for config pkg - add unit test cases for writer.go file - add unit test cases for factory.go file Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * feat(auth): add unit tests for auth package This commit introduces a comprehensive suite of unit tests for the `pkg/auth` package, significantly improving test coverage and ensuring the correctness of the authentication and authorization logic. The following key areas are now covered: - **Identity:** Tests for identity creation, validation, matching, and subject generation. - **Kubeconfig:** Tests for kubeconfig generation options, certificate creation, and identity reading from kubeconfig. - **Privileges:** Tests for privilege description implementations, listing privileges, and pretty-printing. By adding these tests, we increase the robustness of the auth package and make future refactoring safer. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * feat(oam): add unit tests for auxiliary functions This commit adds comprehensive unit tests for the auxiliary functions in the pkg/oam package. The new tests cover the following functions: - GetPublishVersion / SetPublishVersion - GetDeployVersion - GetLastAppliedTime - GetControllerRequirement / SetControllerRequirement - GetCluster / SetCluster / SetClusterIfEmpty These tests address the lack of coverage for these functions and ensure their behavior is correct, including edge cases like handling missing annotations or removing annotations when set to an empty string. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * feat(cue): add unit tests for cue packages This commit enhances the test coverage of the `pkg/cue` directory by adding unit tests for the `definition`, `script`, and `task` packages. The new tests cover the following areas: - `pkg/cue/definition`: - `TestWorkloadGetTemplateContext`: Verifies the template context retrieval for workloads. - `TestTraitGetTemplateContext`: Ensures correct template context retrieval for traits. - `TestGetCommonLabels`: Checks the conversion of context labels to OAM labels. - `TestGetBaseContextLabels`: Validates the creation of base context labels. - `pkg/cue/script`: - Adds tests for `ParseToValue`, `ParseToValueWithCueX`, `ParseToTemplateValue`, and `ParseToTemplateValueWithCueX` to ensure CUE scripts are parsed correctly. - `pkg/cue/task`: - Refactors the existing test for `Process` to use a table-driven approach, improving readability and covering more cases. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> * fix(tests): address PR comments and improve test reliability This commit addresses several comments from a pull request review, improving the reliability and correctness of tests in various packages. The following changes are included: - **`pkg/config/writer`**: - Renamed `writter_test.go` to `writer_test.go` to fix a typo. - **`pkg/cue/task`**: - Replaced the use of an external invalid URL with a local unreachable endpoint (`http://127.0.0.1:3000`) in `process_test.go` to prevent network flakiness. - Switched to using `assert.ErrorContains` for safer error message assertions, avoiding potential panics. - Corrected an assertion to compare a byte slice with a string by converting the byte slice to a string first. - **`pkg/oam`**: - Updated `auxliary_test.go` to use `time.Time.Equal` for time comparisons, making the test robust against timezone differences. Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com> --------- Signed-off-by: Ashvin Bambhaniya <ashvin.bambhaniya@improwised.com>
255 lines
7.0 KiB
Go
255 lines
7.0 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 auth
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
rbacv1 "k8s.io/api/rbac/v1"
|
|
)
|
|
|
|
func TestIdentity(t *testing.T) {
|
|
t.Run("String", func(t *testing.T) {
|
|
testCases := map[string]struct {
|
|
identity Identity
|
|
expected string
|
|
}{
|
|
"user only": {
|
|
identity: Identity{User: "test-user"},
|
|
expected: "User=test-user",
|
|
},
|
|
"user and groups": {
|
|
identity: Identity{User: "test-user", Groups: []string{"group1", "group2"}},
|
|
expected: "User=test-user Groups=group1,group2",
|
|
},
|
|
"service account only": {
|
|
identity: Identity{ServiceAccount: "sa-name", ServiceAccountNamespace: "sa-ns"},
|
|
expected: "SA=system:serviceaccount:sa-ns:sa-name",
|
|
},
|
|
"all fields": {
|
|
identity: Identity{User: "test-user", Groups: []string{"group1"}, ServiceAccount: "sa-name", ServiceAccountNamespace: "sa-ns"},
|
|
expected: "User=test-user Groups=group1 SA=system:serviceaccount:sa-ns:sa-name",
|
|
},
|
|
"empty": {
|
|
identity: Identity{},
|
|
expected: "",
|
|
},
|
|
}
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
r := require.New(t)
|
|
r.Equal(tc.expected, tc.identity.String())
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("Match", func(t *testing.T) {
|
|
identity := &Identity{
|
|
User: "test-user",
|
|
Groups: []string{"group1", "group2"},
|
|
ServiceAccount: "sa-name",
|
|
ServiceAccountNamespace: "sa-ns",
|
|
}
|
|
testCases := map[string]struct {
|
|
subject rbacv1.Subject
|
|
expected bool
|
|
}{
|
|
"match user": {
|
|
subject: rbacv1.Subject{Kind: rbacv1.UserKind, Name: "test-user"},
|
|
expected: true,
|
|
},
|
|
"not match user": {
|
|
subject: rbacv1.Subject{Kind: rbacv1.UserKind, Name: "another-user"},
|
|
expected: false,
|
|
},
|
|
"match group": {
|
|
subject: rbacv1.Subject{Kind: rbacv1.GroupKind, Name: "group1"},
|
|
expected: true,
|
|
},
|
|
"not match group": {
|
|
subject: rbacv1.Subject{Kind: rbacv1.GroupKind, Name: "group3"},
|
|
expected: false,
|
|
},
|
|
"match service account": {
|
|
subject: rbacv1.Subject{Kind: rbacv1.ServiceAccountKind, Name: "sa-name", Namespace: "sa-ns"},
|
|
expected: true,
|
|
},
|
|
"not match service account name": {
|
|
subject: rbacv1.Subject{Kind: rbacv1.ServiceAccountKind, Name: "another-sa", Namespace: "sa-ns"},
|
|
expected: false,
|
|
},
|
|
"not match service account namespace": {
|
|
subject: rbacv1.Subject{Kind: rbacv1.ServiceAccountKind, Name: "sa-name", Namespace: "another-ns"},
|
|
expected: false,
|
|
},
|
|
"unknown kind": {
|
|
subject: rbacv1.Subject{Kind: "Unknown", Name: "test-user"},
|
|
expected: false,
|
|
},
|
|
}
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
r := require.New(t)
|
|
r.Equal(tc.expected, identity.Match(tc.subject))
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("MatchAny", func(t *testing.T) {
|
|
identity := &Identity{
|
|
User: "test-user",
|
|
Groups: []string{"group1"},
|
|
}
|
|
testCases := map[string]struct {
|
|
subjects []rbacv1.Subject
|
|
expected bool
|
|
}{
|
|
"match one": {
|
|
subjects: []rbacv1.Subject{
|
|
{Kind: rbacv1.GroupKind, Name: "group-other"},
|
|
{Kind: rbacv1.UserKind, Name: "test-user"},
|
|
},
|
|
expected: true,
|
|
},
|
|
"match none": {
|
|
subjects: []rbacv1.Subject{
|
|
{Kind: rbacv1.GroupKind, Name: "group-other"},
|
|
{Kind: rbacv1.UserKind, Name: "user-other"},
|
|
},
|
|
expected: false,
|
|
},
|
|
"empty subjects": {
|
|
subjects: []rbacv1.Subject{},
|
|
expected: false,
|
|
},
|
|
}
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
r := require.New(t)
|
|
r.Equal(tc.expected, identity.MatchAny(tc.subjects))
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("Regularize", func(t *testing.T) {
|
|
testCases := map[string]struct {
|
|
identity *Identity
|
|
expected *Identity
|
|
}{
|
|
"trim spaces": {
|
|
identity: &Identity{User: " user ", ServiceAccount: " sa "},
|
|
expected: &Identity{User: "user", ServiceAccount: "sa", ServiceAccountNamespace: "default"},
|
|
},
|
|
"remove duplicate groups": {
|
|
identity: &Identity{User: "user", Groups: []string{" g1 ", "g2", " g1", ""}},
|
|
expected: &Identity{User: "user", Groups: []string{"g1", "g2", ""}},
|
|
},
|
|
"default sa namespace": {
|
|
identity: &Identity{ServiceAccount: "sa"},
|
|
expected: &Identity{ServiceAccount: "sa", ServiceAccountNamespace: "default"},
|
|
},
|
|
"no change": {
|
|
identity: &Identity{User: "user", Groups: []string{"g1"}},
|
|
expected: &Identity{User: "user", Groups: []string{"g1"}},
|
|
},
|
|
}
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
r := require.New(t)
|
|
tc.identity.Regularize()
|
|
r.Equal(tc.expected, tc.identity)
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("Validate", func(t *testing.T) {
|
|
testCases := map[string]struct {
|
|
identity Identity
|
|
expectErr bool
|
|
}{
|
|
"valid user": {
|
|
identity: Identity{User: "user"},
|
|
expectErr: false,
|
|
},
|
|
"valid service account": {
|
|
identity: Identity{ServiceAccount: "sa", ServiceAccountNamespace: "ns"},
|
|
expectErr: false,
|
|
},
|
|
"invalid empty": {
|
|
identity: Identity{},
|
|
expectErr: true,
|
|
},
|
|
"invalid user and sa": {
|
|
identity: Identity{User: "user", ServiceAccount: "sa"},
|
|
expectErr: true,
|
|
},
|
|
"invalid group and sa": {
|
|
identity: Identity{Groups: []string{"g1"}, ServiceAccount: "sa"},
|
|
expectErr: true,
|
|
},
|
|
"invalid sa namespace without sa": {
|
|
identity: Identity{User: "user", ServiceAccountNamespace: "ns"},
|
|
expectErr: true,
|
|
},
|
|
}
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
r := require.New(t)
|
|
err := tc.identity.Validate()
|
|
if tc.expectErr {
|
|
r.Error(err)
|
|
} else {
|
|
r.NoError(err)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("Subjects", func(t *testing.T) {
|
|
testCases := map[string]struct {
|
|
identity Identity
|
|
expected []rbacv1.Subject
|
|
}{
|
|
"user and groups": {
|
|
identity: Identity{User: "user", Groups: []string{"g1", "g2"}},
|
|
expected: []rbacv1.Subject{
|
|
{Kind: rbacv1.UserKind, APIGroup: rbacv1.GroupName, Name: "user"},
|
|
{Kind: rbacv1.GroupKind, APIGroup: rbacv1.GroupName, Name: "g1"},
|
|
{Kind: rbacv1.GroupKind, APIGroup: rbacv1.GroupName, Name: "g2"},
|
|
},
|
|
},
|
|
"service account": {
|
|
identity: Identity{ServiceAccount: "sa", ServiceAccountNamespace: "ns"},
|
|
expected: []rbacv1.Subject{
|
|
{Kind: rbacv1.ServiceAccountKind, Name: "sa", Namespace: "ns"},
|
|
},
|
|
},
|
|
"empty": {
|
|
identity: Identity{},
|
|
expected: nil,
|
|
},
|
|
}
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
r := require.New(t)
|
|
r.ElementsMatch(tc.expected, tc.identity.Subjects())
|
|
})
|
|
}
|
|
})
|
|
}
|