Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zreigz committed Oct 22, 2024
1 parent e618ab8 commit 2e0a7b0
Show file tree
Hide file tree
Showing 5 changed files with 336 additions and 0 deletions.
69 changes: 69 additions & 0 deletions pkg/controller/pipelinegates/reconciler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package pipelinegates_test

import (
"context"
"os"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
console "github.com/pluralsh/console/go/client"
"github.com/pluralsh/deployment-operator/api/v1alpha1"
"github.com/pluralsh/deployment-operator/cmd/agent/args"
"github.com/pluralsh/deployment-operator/pkg/cache"
"github.com/pluralsh/deployment-operator/pkg/controller/pipelinegates"
"github.com/pluralsh/deployment-operator/pkg/test/mocks"
"github.com/stretchr/testify/mock"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

var _ = Describe("Reconciler", Ordered, func() {
Context("When reconciling a resource", func() {
const (
namespace = "default"
gateId = "1"
pipelinegateName = "test"
)
gateFragment := &console.PipelineGateFragment{
ID: gateId,
Type: console.GateTypeJob,
}
pipelinegate := &v1alpha1.PipelineGate{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: pipelinegateName,
},
Spec: v1alpha1.PipelineGateSpec{
Name: pipelinegateName,
Type: v1alpha1.GateType(console.GateTypeApproval),
},
}

ctx := context.Background()

BeforeEach(func() {
os.Setenv("OPERATOR_NAMESPACE", "default")
})
AfterEach(func() {
os.Unsetenv("OPERATOR_NAMESPACE")
})

It("should create Gate object", func() {
fakeConsoleClient := mocks.NewClientMock(mocks.TestingT)
fakeConsoleClient.On("GetClusterGate", mock.Anything).Return(gateFragment, nil)
fakeConsoleClient.On("ParsePipelineGateCR", mock.Anything, mock.Anything).Return(pipelinegate, nil)

cache.InitGateCache(args.ControllerCacheTTL(), fakeConsoleClient)

reconciler, err := pipelinegates.NewGateReconciler(fakeConsoleClient, kClient, cfg, time.Minute)
Expect(err).NotTo(HaveOccurred())
_, err = reconciler.Reconcile(ctx, gateId)
Expect(err).NotTo(HaveOccurred())

Expect(kClient.Get(ctx, types.NamespacedName{Name: pipelinegateName, Namespace: namespace}, &v1alpha1.PipelineGate{})).NotTo(HaveOccurred())

})

})
})
74 changes: 74 additions & 0 deletions pkg/controller/pipelinegates/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2024.
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 pipelinegates_test

import (
"fmt"
"path/filepath"
"runtime"
"testing"

"k8s.io/client-go/rest"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/pluralsh/deployment-operator/api/v1alpha1"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
var testEnv *envtest.Environment
var kClient client.Client
var cfg *rest.Config

func TestStacks(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Stack Suite")
}

var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s", fmt.Sprintf("1.28.3-%s-%s", runtime.GOOS, runtime.GOARCH)),
}
var err error
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

err = v1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

kClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(kClient).NotTo(BeNil())
})

var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})
118 changes: 118 additions & 0 deletions pkg/controller/service/reconciler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package service_test

import (
"context"
"log"
"net/http"
"os"
"path/filepath"
"time"

"github.com/gin-gonic/gin"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
console "github.com/pluralsh/console/go/client"
"github.com/pluralsh/deployment-operator/pkg/controller/service"
"github.com/pluralsh/deployment-operator/pkg/test/mocks"
"github.com/samber/lo"
"github.com/stretchr/testify/mock"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

var _ = Describe("Reconciler", Ordered, func() {
Context("When reconciling a resource", func() {
const (
namespace = "default"
serviceId = "1"
serviceName = "test"
clusterId = "1"
clusterName = "cluster-test"
operatorNamespace = "plrl-deploy-operator"
)
consoleService := &console.GetServiceDeploymentForAgent_ServiceDeployment{
ID: serviceId,
Name: serviceName,
Namespace: namespace,
Tarball: lo.ToPtr("http://localhost:8080/ext/v1/digests"),
Configuration: []*console.GetServiceDeploymentForAgent_ServiceDeployment_Configuration{
{
Name: "name",
Value: serviceName,
},
},
Cluster: &console.GetServiceDeploymentForAgent_ServiceDeployment_Cluster{
ID: clusterId,
Name: clusterName,
},
Revision: &console.GetServiceDeploymentForAgent_ServiceDeployment_Revision{
ID: serviceId,
},
}
ctx := context.Background()
tarPath := filepath.Join("..", "..", "..", "test", "tarball", "test.tar.gz")

r := gin.Default()
r.GET("/ext/v1/digests", func(c *gin.Context) {
res, err := os.ReadFile(tarPath)
Expect(err).NotTo(HaveOccurred())
c.String(http.StatusOK, string(res))
})

srv := &http.Server{
Addr: ":8080",
Handler: r,
}
dir := ""
BeforeEach(func() {
var err error
dir, err = os.MkdirTemp("", "test")
Expect(err).NotTo(HaveOccurred())
Expect(kClient.Create(ctx, &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: operatorNamespace,
},
})).NotTo(HaveOccurred())
// Initializing the server in a goroutine so that
// it won't block the graceful shutdown handling below
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
Expect(err).NotTo(HaveOccurred())
}
}()
})
AfterEach(func() {
os.RemoveAll(dir)
Expect(kClient.Delete(ctx, &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: operatorNamespace,
},
})).NotTo(HaveOccurred())
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server forced to shutdown: ", err)
}

log.Println("Server exiting")
})

It("should create NewServiceReconciler and apply service", func() {
fakeConsoleClient := mocks.NewClientMock(mocks.TestingT)
fakeConsoleClient.On("GetCredentials").Return("", "")
fakeConsoleClient.On("GetService", mock.Anything).Return(consoleService, nil)
fakeConsoleClient.On("UpdateComponents", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)

reconciler, err := service.NewServiceReconciler(fakeConsoleClient, cfg, time.Minute, time.Minute, namespace, "http://localhost:8080")
Expect(err).NotTo(HaveOccurred())
_, err = reconciler.Reconcile(ctx, serviceId)
Expect(err).NotTo(HaveOccurred())

Expect(kClient.Get(ctx, types.NamespacedName{Name: serviceName, Namespace: namespace}, &v1.Pod{})).NotTo(HaveOccurred())
})

})
})
75 changes: 75 additions & 0 deletions pkg/controller/service/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2024.
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 service_test

import (
"fmt"
"path/filepath"
"runtime"
"testing"

"k8s.io/client-go/rest"

velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
var testEnv *envtest.Environment
var kClient client.Client
var cfg *rest.Config

func TestStacks(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Stack Suite")
}

var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "test", "crd")},
ErrorIfCRDPathMissing: true,
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s", fmt.Sprintf("1.28.3-%s-%s", runtime.GOOS, runtime.GOARCH)),
}
var err error
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

err = velerov1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

kClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(kClient).NotTo(BeNil())
})

var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})
Binary file added test/tarball/test.tar.gz
Binary file not shown.

0 comments on commit 2e0a7b0

Please sign in to comment.