Skip to content

Commit

Permalink
Improve repository server controller tests (#2271)
Browse files Browse the repository at this point in the history
* improve repository server controller tests

* remove unnecessary trailing spaces

Signed-off-by: Amruta Kale <amruta.kale@veeam.com>

---------

Signed-off-by: Amruta Kale <amruta.kale@veeam.com>
  • Loading branch information
kale-amruta committed Sep 6, 2023
1 parent 7af0dba commit 535baf7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"

crkanisteriov1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
)

// maximum concurrent reconcilations that can be triggered by the controller
const maxConcurrentReconciles = 3

// RepositoryServerReconciler reconciles a RepositoryServer object
type RepositoryServerReconciler struct {
client.Client
Expand Down Expand Up @@ -139,10 +143,13 @@ func newRepositoryServerHandler(

// SetupWithManager sets up the controller with the Manager.
func (r *RepositoryServerReconciler) SetupWithManager(mgr ctrl.Manager) error {
opts := controller.Options{
MaxConcurrentReconciles: maxConcurrentReconciles,
}
// The 'Owns' function allows the controller to set owner refs on
// child resources and run the same reconcile loop for all events on child resources
r.Recorder = mgr.GetEventRecorderFor("RepositoryServer")
return ctrl.NewControllerManagedBy(mgr).
return ctrl.NewControllerManagedBy(mgr).WithOptions(opts).
For(&crkanisteriov1alpha1.RepositoryServer{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Owns(&corev1.Service{}).
Owns(&networkingv1.NetworkPolicy{}).
Expand Down
112 changes: 65 additions & 47 deletions pkg/controllers/repositoryserver/repositoryserver_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,59 +316,74 @@ func (s *RepoServerControllerSuite) TestCreationOfOwnedResources(c *C) {

func (s *RepoServerControllerSuite) TestInvalidRepositoryPassword(c *C) {
ctx := context.Background()
originalrepoServerCR := testutil.GetTestKopiaRepositoryServerCR(s.repoServerControllerNamespace)
setRepositoryServerSecretsInCR(&s.repoServerSecrets, &originalrepoServerCR)
for _, tc := range []struct {
description string
testFunction func(rs *v1alpha1.RepositoryServer)
}{
{
description: "Invalid Repository Password",
testFunction: func(rs *v1alpha1.RepositoryServer) {
InvalidRepositoryPassword, err := s.CreateRepositoryPasswordSecret(testutil.GetRepoPasswordSecretData("invalidPassword"))
c.Assert(err, IsNil)

rs.Spec.Repository.PasswordSecretRef.Name = InvalidRepositoryPassword.Name
rs.Spec.Repository.PasswordSecretRef.Namespace = InvalidRepositoryPassword.Namespace
},
},
{
description: "Invalid Storage Location",
testFunction: func(rs *v1alpha1.RepositoryServer) {
storageLocationData := testutil.GetDefaultS3CompliantStorageLocation()
storageLocationData[repositoryserver.BucketKey] = []byte("invalidbucket")
repoServerCR := testutil.GetTestKopiaRepositoryServerCR(s.repoServerControllerNamespace)
setRepositoryServerSecretsInCR(&s.repoServerSecrets, &repoServerCR)

InvalidStorageLocationSecret, err := s.CreateStorageLocationSecret(storageLocationData)
c.Assert(err, IsNil)
InvalidRepositoryPassword, err := s.CreateRepositoryPasswordSecret(testutil.GetRepoPasswordSecretData("invalidPassword"))
c.Assert(err, IsNil)

rs.Spec.Storage.SecretRef.Name = InvalidStorageLocationSecret.Name
rs.Spec.Storage.SecretRef.Namespace = InvalidStorageLocationSecret.Namespace
},
},
{
description: "Invalid Storage location credentials",
testFunction: func(rs *v1alpha1.RepositoryServer) {
storageLocationCredsData := testutil.GetDefaultS3StorageCreds(c)
storageLocationCredsData[secrets.AWSAccessKeyID] = []byte("testaccesskey")
repoServerCR.Spec.Repository.PasswordSecretRef.Name = InvalidRepositoryPassword.Name
repoServerCR.Spec.Repository.PasswordSecretRef.Namespace = InvalidRepositoryPassword.Namespace

InvalidStorageLocationCrdesSecret, err := s.CreateStorageLocationSecret(storageLocationCredsData)
c.Assert(err, IsNil)
repoServerCRCreated, err := s.crCli.RepositoryServers(s.repoServerControllerNamespace).Create(ctx, &repoServerCR, metav1.CreateOptions{})
c.Assert(err, IsNil)

rs.Spec.Storage.CredentialSecretRef.Name = InvalidStorageLocationCrdesSecret.Name
rs.Spec.Storage.CredentialSecretRef.Namespace = InvalidStorageLocationCrdesSecret.Namespace
},
},
} {
invalidCR := originalrepoServerCR
tc.testFunction(&invalidCR)
state, err := s.waitOnRepositoryServerState(c, repoServerCRCreated.Name)
c.Assert(err, NotNil)
c.Assert(state, Equals, v1alpha1.Failed)

repoServerCRCreated, err := s.crCli.RepositoryServers(s.repoServerControllerNamespace).Create(ctx, &invalidCR, metav1.CreateOptions{})
c.Assert(err, IsNil)
err = s.crCli.RepositoryServers(s.repoServerControllerNamespace).Delete(context.Background(), repoServerCRCreated.Name, metav1.DeleteOptions{})
c.Assert(err, IsNil)
}

state, err := s.waitOnRepositoryServerState(c, repoServerCRCreated.Name)
c.Assert(err, NotNil)
c.Assert(state, Equals, v1alpha1.Failed)
}
func (s *RepoServerControllerSuite) TestInvalidStorageLocation(c *C) {
ctx := context.Background()
repoServerCR := testutil.GetTestKopiaRepositoryServerCR(s.repoServerControllerNamespace)
setRepositoryServerSecretsInCR(&s.repoServerSecrets, &repoServerCR)

storageLocationData := testutil.GetDefaultS3CompliantStorageLocation()
storageLocationData[repositoryserver.BucketKey] = []byte("invalidbucket")

InvalidStorageLocationSecret, err := s.CreateStorageLocationSecret(storageLocationData)
c.Assert(err, IsNil)

repoServerCR.Spec.Storage.SecretRef.Name = InvalidStorageLocationSecret.Name
repoServerCR.Spec.Storage.SecretRef.Namespace = InvalidStorageLocationSecret.Namespace

repoServerCRCreated, err := s.crCli.RepositoryServers(s.repoServerControllerNamespace).Create(ctx, &repoServerCR, metav1.CreateOptions{})
c.Assert(err, IsNil)

state, err := s.waitOnRepositoryServerState(c, repoServerCRCreated.Name)
c.Assert(err, NotNil)
c.Assert(state, Equals, v1alpha1.Failed)

err = s.crCli.RepositoryServers(s.repoServerControllerNamespace).Delete(context.Background(), repoServerCRCreated.Name, metav1.DeleteOptions{})
c.Assert(err, IsNil)
}

func (s *RepoServerControllerSuite) TestInvalidStorageLocationCredentials(c *C) {
ctx := context.Background()
repoServerCR := testutil.GetTestKopiaRepositoryServerCR(s.repoServerControllerNamespace)
setRepositoryServerSecretsInCR(&s.repoServerSecrets, &repoServerCR)

storageLocationCredsData := testutil.GetDefaultS3StorageCreds(c)
storageLocationCredsData[secrets.AWSAccessKeyID] = []byte("testaccesskey")

InvalidStorageLocationCrdesSecret, err := s.CreateAWSStorageCredentialsSecret(storageLocationCredsData)
c.Assert(err, IsNil)

repoServerCR.Spec.Storage.CredentialSecretRef.Name = InvalidStorageLocationCrdesSecret.Name
repoServerCR.Spec.Storage.CredentialSecretRef.Namespace = InvalidStorageLocationCrdesSecret.Namespace

repoServerCRCreated, err := s.crCli.RepositoryServers(s.repoServerControllerNamespace).Create(ctx, &repoServerCR, metav1.CreateOptions{})
c.Assert(err, IsNil)

state, err := s.waitOnRepositoryServerState(c, repoServerCRCreated.Name)
c.Assert(err, NotNil)
c.Assert(state, Equals, v1alpha1.Failed)

err = s.crCli.RepositoryServers(s.repoServerControllerNamespace).Delete(context.Background(), repoServerCRCreated.Name, metav1.DeleteOptions{})
c.Assert(err, IsNil)
}

func (s *RepoServerControllerSuite) TestFilestoreLocationVolumeMountOnRepoServerPod(c *C) {
Expand Down Expand Up @@ -419,6 +434,9 @@ func (s *RepoServerControllerSuite) TestFilestoreLocationVolumeMountOnRepoServer
}
}
c.Assert(volumeattached, Equals, true)

err = s.crCli.RepositoryServers(s.repoServerControllerNamespace).Delete(context.Background(), repoServerCRCreated.Name, metav1.DeleteOptions{})
c.Assert(err, IsNil)
}

func (s *RepoServerControllerSuite) waitForRepoServerInfoUpdateInCR(repoServerName string) error {
Expand Down

0 comments on commit 535baf7

Please sign in to comment.