-
Notifications
You must be signed in to change notification settings - Fork 885
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/karmadactl/cmdinit: unit test bootstraptoken
In this commit, we unit test bootstraptoken agent on tlsbootstrap functionality by testing `AllowBootstrapTokensToPostCSRs`, `AutoApproveKarmadaAgentBootstrapTokens`, and `AutoApproveAgentCertificateRotation` making sure they are working as expected. Signed-off-by: Mohamed Awnallah <mohamedmohey2352@gmail.com>
- Loading branch information
1 parent
9c0bd72
commit 0a56741
Showing
2 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
pkg/karmadactl/cmdinit/bootstraptoken/agent/tlsbootstrap_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/* | ||
Copyright 2024 The Karmada 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 agent | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
rbacv1 "k8s.io/api/rbac/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
clientset "k8s.io/client-go/kubernetes" | ||
fakeclientset "k8s.io/client-go/kubernetes/fake" | ||
|
||
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/utils" | ||
) | ||
|
||
func TestAllowBootstrapTokensToPostCSRs(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
client clientset.Interface | ||
prep func(clientset.Interface) error | ||
verify func(clientset.Interface) error | ||
}{ | ||
{ | ||
name: "AllowBootstrapTokensToPostCSRs_CreateClusterRoleBinding_Created", | ||
client: fakeclientset.NewSimpleClientset(), | ||
prep: func(clientset.Interface) error { return nil }, | ||
verify: func(client clientset.Interface) error { | ||
return verifyClusterRoleBinding(client, KarmadaAgentBootstrap, KarmadaAgentBootstrapperClusterRoleName) | ||
}, | ||
}, | ||
{ | ||
name: "AllowBootstrapTokensToPostCSRs_ClusterRoleBindingAlreadyExists_Updated", | ||
client: fakeclientset.NewSimpleClientset(), | ||
prep: func(client clientset.Interface) error { | ||
return createClusterRoleBinding(client, KarmadaAgentBootstrap, KarmadaAgentBootstrapperClusterRoleName, KarmadaAgentBootstrapTokenAuthGroup) | ||
}, | ||
verify: func(client clientset.Interface) error { | ||
return verifyClusterRoleBinding(client, KarmadaAgentBootstrap, KarmadaAgentBootstrapperClusterRoleName) | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
if err := test.prep(test.client); err != nil { | ||
t.Fatalf("failed to prep before allowing bootstrap tokens to post CSRs, got: %v", err) | ||
} | ||
if err := AllowBootstrapTokensToPostCSRs(test.client); err != nil { | ||
t.Errorf("unexpected error while allowing bootstrap tokens to post CSRs, got: %v", err) | ||
} | ||
if err := test.verify(test.client); err != nil { | ||
t.Errorf("failed to verify the creation of cluster role bindings, got: %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAutoApproveKarmadaAgentBootstrapTokens(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
client clientset.Interface | ||
prep func(clientset.Interface) error | ||
verify func(clientset.Interface) error | ||
}{ | ||
{ | ||
name: "AutoApproveKarmadaAgentBootstrapTokens_CreateClusterRoleBindings_Created", | ||
client: fakeclientset.NewSimpleClientset(), | ||
prep: func(clientset.Interface) error { return nil }, | ||
verify: func(client clientset.Interface) error { | ||
return verifyClusterRoleBinding(client, KarmadaAgentAutoApproveBootstrapClusterRoleBinding, CSRAutoApprovalClusterRoleName) | ||
}, | ||
}, | ||
{ | ||
name: "AutoApproveKarmadaAgentBootstrapTokens_ClusterRoleBindingAlreadyExists_Updated", | ||
client: fakeclientset.NewSimpleClientset(), | ||
prep: func(client clientset.Interface) error { | ||
return createClusterRoleBinding(client, KarmadaAgentAutoApproveBootstrapClusterRoleBinding, CSRAutoApprovalClusterRoleName, KarmadaAgentBootstrapTokenAuthGroup) | ||
}, | ||
verify: func(client clientset.Interface) error { | ||
return verifyClusterRoleBinding(client, KarmadaAgentAutoApproveBootstrapClusterRoleBinding, CSRAutoApprovalClusterRoleName) | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
if err := test.prep(test.client); err != nil { | ||
t.Fatalf("failed to prep before auto-approve karmada agent bootstrap tokens, got: %v", err) | ||
} | ||
if err := AutoApproveKarmadaAgentBootstrapTokens(test.client); err != nil { | ||
t.Errorf("unexpected error, got: %v", err) | ||
} | ||
if err := test.verify(test.client); err != nil { | ||
t.Errorf("failed to verify the creation of cluster role bindings, got error: %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAutoApproveAgentCertificateRotation(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
client clientset.Interface | ||
prep func(clientset.Interface) error | ||
verify func(clientset.Interface) error | ||
}{ | ||
{ | ||
name: "AutoApproveAgentCertificateRotation_CreateClusterRoleBindings_Created", | ||
client: fakeclientset.NewSimpleClientset(), | ||
prep: func(clientset.Interface) error { return nil }, | ||
verify: func(client clientset.Interface) error { | ||
return verifyClusterRoleBinding(client, KarmadaAgentAutoApproveCertificateRotationClusterRoleBinding, KarmadaAgentSelfCSRAutoApprovalClusterRoleName) | ||
}, | ||
}, | ||
{ | ||
name: "AutoApproveAgentCertificateRotation_ClusterRoleBindingAlreadyExists_Updated", | ||
client: fakeclientset.NewSimpleClientset(), | ||
prep: func(client clientset.Interface) error { | ||
return createClusterRoleBinding(client, KarmadaAgentAutoApproveCertificateRotationClusterRoleBinding, KarmadaAgentSelfCSRAutoApprovalClusterRoleName, KarmadaAgentGroup) | ||
}, | ||
verify: func(client clientset.Interface) error { | ||
return verifyClusterRoleBinding(client, KarmadaAgentAutoApproveCertificateRotationClusterRoleBinding, KarmadaAgentSelfCSRAutoApprovalClusterRoleName) | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
if err := test.prep(test.client); err != nil { | ||
t.Fatalf("failed to prep before auto-approve agent certificate rotation, got: %v", err) | ||
} | ||
if err := AutoApproveAgentCertificateRotation(test.client); err != nil { | ||
t.Errorf("unexpected error, got: %v", err) | ||
} | ||
if err := test.verify(test.client); err != nil { | ||
t.Errorf("failed to verify the creation of cluster role bindings, got error: %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func createClusterRoleBinding(client clientset.Interface, crbName, crName, subjectName string) error { | ||
clusterRoleBinding := utils.ClusterRoleBindingFromSubjects(crbName, crName, | ||
[]rbacv1.Subject{ | ||
{ | ||
Kind: rbacv1.GroupKind, | ||
Name: subjectName, | ||
}, | ||
}, nil) | ||
if _, err := client.RbacV1().ClusterRoleBindings().Create(context.TODO(), clusterRoleBinding, metav1.CreateOptions{}); err != nil { | ||
return fmt.Errorf("failed to create cluster role binding, got: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
func verifyClusterRoleBinding(client clientset.Interface, crbName, crName string) error { | ||
clusterRoleBinding, err := client.RbacV1().ClusterRoleBindings().Get(context.TODO(), crbName, metav1.GetOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("failed to get cluster role binding, got: %v", err) | ||
} | ||
if clusterRoleBinding.RoleRef.Name != crName { | ||
return fmt.Errorf("expected cluster role ref name to be %s, but got %s", crName, clusterRoleBinding.RoleRef.Name) | ||
} | ||
return nil | ||
} |