Skip to content

Commit

Permalink
Add unit tests for kopia repository server controller(PR #2) (#1997)
Browse files Browse the repository at this point in the history
* add unit tests for repository server controller

* test

* add prerequisites for tests

* add server ready test

* remove unwanted changes

* fix lint issues

* automate creation of repo server CRD

* address review comments

* add symbolic link

* address comments

* remove custom resource

* renaming cli to kubecli

* add more tests

* fix lint issue

* Add tests for Immutability

Signed-off-by: Rajat Gupta <rajat.gupta@veeam.com>

* Fix naming conventions

Signed-off-by: Rajat Gupta <rajat.gupta@veeam.com>

* Update variables

Signed-off-by: Rajat Gupta <rajat.gupta@veeam.com>

* add tests related to secrets

* add secrets configuration for testutils

* add base suite for repository server controller

* use constants for location secret keys

* add unit tests

* make the constants local to the package

* add licence headers

* change package name

* fix build errors

* fix licence headers

* fix lint issues

* remove secret manager changes

* remove unused functions and move to next PR

* add utility functions

* add tests for secrets and cache settings

* fix build issues

* move secret creation utils under test suite

* resolve conflicts

* add base suite for repository server controller

* use constants for location secret keys

* make the constants local to the package

* add licence headers

* fix licence headers

* fix lint issues

* remove unused functions and move to next PR

* fix build issues

* move secret creation utils under test suite

* address review comments

* add tests which got deleted during rebase

* address review comments

* updating tests to use the latest changed values for repository server progress field in status

* move test utilities to pkg/testutil/testutil.go

* rename kopia repository path constant name

* improve tests and comments for better readability

* refactor repository_test.go and secrets_manager_test.go for better readability

* fix tests

* test

* fix tests

* remove unnecessary whitespaces

* rename conflicting env variables with s3compliant prefix

* uncomment tests

* move the env variable related comment at the right place

* check error message for immuatability test

* address comments related to better readability

* change waitForRepoServerInfoUpdateInCR function to return error
if podName and serviceName are not set even after the polling time
exceeds

---------

Signed-off-by: Rajat Gupta <rajat.gupta@veeam.com>
Co-authored-by: Rajat Gupta <37516416+r4rajat@users.noreply.github.com>
Co-authored-by: Rajat Gupta <rajat.gupta@veeam.com>
  • Loading branch information
3 people authored Jul 13, 2023
1 parent 1ed0c60 commit 6b1ceaa
Show file tree
Hide file tree
Showing 7 changed files with 563 additions and 28 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
# before running `make test`, to create some CRDs on the cluster.
- run: |
make install-csi-hostpath-driver
make install-minio
if: matrix.testSuite == 'test'
- run: make ${{ matrix.testSuite }}
build:
Expand Down
15 changes: 15 additions & 0 deletions build/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ if [ -n "${ERRS}" ]; then
fi
echo

check_dependencies() {
# Check if minio is already deployed
if helm status minio -n minio > /dev/null 2>&1 ; then
# Setting env vars to access MinIO
export S3_COMPLIANT_AWS_ACCESS_KEY_ID="AKIAIOSFODNN7EXAMPLE"
export S3_COMPLIANT_AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
export S3_COMPLIANT_AWS_REGION="us-west-2"
export S3_COMPLIANT_LOCATION_ENDPOINT="http://minio.minio.svc.cluster.local:9000"
else
echo "Please install MinIO using 'make install-minio' and try again."
exit 1
fi
}

check_dependencies
echo "Running tests:"
go test -v -installsuffix "static" -i ${TARGETS}
go test -v ${TARGETS} -list .
Expand Down
9 changes: 6 additions & 3 deletions pkg/controllers/repositoryserver/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ type RepoServerHandler struct {
}

func (h *RepoServerHandler) CreateOrUpdateOwnedResources(ctx context.Context) error {
if err := h.getSecretsFromCR(ctx); err != nil {
return errors.Wrap(err, "Failed to get Kopia API server secrets")
}

svc, err := h.reconcileService(ctx)
if err != nil {
return errors.Wrap(err, "Failed to reconcile service")
}
if err = h.getSecretsFromCR(ctx); err != nil {
return errors.Wrap(err, "Failed to get Kopia API server secrets")
}

envVars, pod, err := h.reconcilePod(ctx, svc)
if err != nil {
return errors.Wrap(err, "Failed to reconcile Kopia API server pod")
Expand Down Expand Up @@ -222,6 +224,7 @@ func (h *RepoServerHandler) updateServiceNameInPodLabels(pod *corev1.Pod, svc *c

func (h *RepoServerHandler) createPod(ctx context.Context, repoServerNamespace string, svc *corev1.Service) (*corev1.Pod, []corev1.EnvVar, error) {
podOverride, err := h.preparePodOverride(ctx)

if err != nil {
return nil, nil, err
}
Expand Down
73 changes: 73 additions & 0 deletions pkg/controllers/repositoryserver/repository_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2023 The Kanister 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 repositoryserver

import (
"github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/kopia/command"
"github.com/kanisterio/kanister/pkg/testutil"
. "gopkg.in/check.v1"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

func (s *RepoServerControllerSuite) TestCacheSizeConfiguration(c *C) {
repositoryServer := testutil.GetTestKopiaRepositoryServerCR(s.repoServerControllerNamespace)
setRepositoryServerSecretsInCR(&s.repoServerSecrets, repositoryServer)

defaultcontentCacheMB, defaultmetadataCacheMB := command.GetGeneralCacheSizeSettings()

repoServerHandler := RepoServerHandler{
Req: reconcile.Request{},
Reconciler: s.DefaultRepoServerReconciler,
KubeCli: s.kubeCli,
RepositoryServer: repositoryServer,
}

// Test if Default cache size settings are set
contentCacheMB, metadataCacheMB, err := repoServerHandler.getRepositoryCacheSettings()
c.Assert(err, IsNil)
c.Assert(contentCacheMB, Equals, defaultcontentCacheMB)
c.Assert(metadataCacheMB, Equals, defaultmetadataCacheMB)

// Test if configfured cache size settings are set
repositoryServer.Spec.Repository.CacheSizeSettings = v1alpha1.CacheSizeSettings{
Metadata: "1000",
Content: "1100",
}
contentCacheMB, metadataCacheMB, err = repoServerHandler.getRepositoryCacheSettings()
c.Assert(err, IsNil)
c.Assert(contentCacheMB, Equals, 1100)
c.Assert(metadataCacheMB, Equals, 1000)

// Check if default Content Cache size is set
repositoryServer.Spec.Repository.CacheSizeSettings = v1alpha1.CacheSizeSettings{
Metadata: "1000",
Content: "",
}
contentCacheMB, metadataCacheMB, err = repoServerHandler.getRepositoryCacheSettings()
c.Assert(err, IsNil)
c.Assert(contentCacheMB, Equals, defaultcontentCacheMB)
c.Assert(metadataCacheMB, Equals, 1000)

// Check if default Metadata Cache size is set
repositoryServer.Spec.Repository.CacheSizeSettings = v1alpha1.CacheSizeSettings{
Metadata: "",
Content: "1100",
}
contentCacheMB, metadataCacheMB, err = repoServerHandler.getRepositoryCacheSettings()
c.Assert(err, IsNil)
c.Assert(contentCacheMB, Equals, 1100)
c.Assert(metadataCacheMB, Equals, defaultmetadataCacheMB)
}
Loading

0 comments on commit 6b1ceaa

Please sign in to comment.