Skip to content

Commit

Permalink
Handle create project reconciler test
Browse files Browse the repository at this point in the history
  • Loading branch information
rehanjaved237 committed Sep 1, 2020
1 parent 6ea489a commit 3101807
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 49 deletions.
43 changes: 43 additions & 0 deletions controllers/project_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package controllers

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
mockData "github.com/stakater/jira-service-desk-operator/mock"
)

var _ = Describe("ProjectController", func() {

Describe("Create New JiraServiceDeskProject Resource", func() {

var description string
var leadAccountId string
var projectTemplateKey string
var name string
var assigneeType string
var projectTypeKey string
var key string
var url string

BeforeEach(func() {
name = mockData.CreateProjectInput.Name
key = mockData.CreateProjectInput.Key
projectTypeKey = mockData.CreateProjectInput.ProjectTypeKey
projectTemplateKey = mockData.CreateProjectInput.ProjectTemplateKey
description = mockData.CreateProjectInput.Description
assigneeType = mockData.CreateProjectInput.AssigneeType
leadAccountId = mockData.CreateProjectInput.LeadAccountId
url = mockData.CreateProjectInput.URL
})

Context("With the required fields", func() {
It("should create a new project", func() {
_ = util.CreateProject(name, key, projectTypeKey, projectTemplateKey, description, assigneeType, leadAccountId, url, ns)
project := util.GetProject(name, ns)

Expect(project.Status.ID).To(Equal(mockData.CreateProjectResponseID))
})
})
})

})
50 changes: 50 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@ limitations under the License.
package controllers

import (
"context"
"path/filepath"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
mockData "github.com/stakater/jira-service-desk-operator/mock"
"gopkg.in/h2non/gock.v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
"sigs.k8s.io/controller-runtime/pkg/log"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

jiraservicedeskv1alpha1 "github.com/stakater/jira-service-desk-operator/api/v1alpha1"
controllerUtil "github.com/stakater/jira-service-desk-operator/controllers/util"
c "github.com/stakater/jira-service-desk-operator/pkg/jiraservicedesk/client"
// +kubebuilder:scaffold:imports
)

Expand All @@ -41,6 +47,11 @@ var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment

var ctx context.Context
var r *ProjectReconciler
var util *controllerUtil.TestUtil
var ns = "test"

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)

Expand Down Expand Up @@ -71,11 +82,50 @@ var _ = BeforeSuite(func(done Done) {
Expect(err).ToNot(HaveOccurred())
Expect(k8sClient).ToNot(BeNil())

ctx = context.Background()

// Turns on the Mock Gock Server with all the required testing hitpoints
// Real Networking Mode is enabled
TurnOnTheMockServer(mockData.BaseURL)

r = &ProjectReconciler{
Client: k8sClient,
Scheme: scheme.Scheme,
Log: log.Log.WithName("Reconciler"),
JiraServiceDeskClient: c.NewClient("", mockData.BaseURL, ""),
}
Expect(r).ToNot((BeNil()))

util = controllerUtil.New(ctx, k8sClient, r)
Expect(util).ToNot(BeNil())

util.CreateNamespace(ns)

close(done)
}, 60)

var _ = AfterSuite(func() {
defer TurnOffTheMockServer()

By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})

func TurnOnTheMockServer(baseURL string) {
// Enabling the Real Networking Mode
gock.EnableNetworking()

// HitPoint for the CreateProject Reconciler TestCase
gock.New(baseURL + "/rest/api/3/project").
Post("/").
MatchType("json").
JSON(mockData.CreateProjectInputJSON).
Reply(200).
JSON(mockData.CreateProjectResponseJSON)
}

func TurnOffTheMockServer() {
defer gock.DisableNetworking()
defer gock.Off()
}
100 changes: 100 additions & 0 deletions controllers/util/testUtil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package util

import (
"context"

"github.com/onsi/ginkgo"
ginko "github.com/onsi/ginkgo"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

jiraservicedeskv1alpha1 "github.com/stakater/jira-service-desk-operator/api/v1alpha1"
)

// TestUtil contains necessary objects required to perform operations during tests
type TestUtil struct {
ctx context.Context
k8sClient client.Client
r reconcile.Reconciler
}

// New creates new TestUtil
func New(ctx context.Context, k8sClient client.Client, r reconcile.Reconciler) *TestUtil {
return &TestUtil{
ctx: ctx,
k8sClient: k8sClient,
r: r,
}
}

// CreateNamespace creates a namespace in the kubernetes server
func (t *TestUtil) CreateNamespace(name string) {
namespaceObject := t.CreateNamespaceObject(name)
err := t.k8sClient.Create(t.ctx, namespaceObject)

if err != nil {
ginkgo.Fail(err.Error())
}
}

// CreateNamespaceObject creates a namespace object
func (t *TestUtil) CreateNamespaceObject(name string) *v1.Namespace {
return &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
}

// CreateProjectObject creates a jira project custom resource object
func (t *TestUtil) CreateProjectObject(name string, key string, projectTypeKey string, projectTemplateKey string, description string, assigneeType string, leadAccountId string, url string, namespace string) *jiraservicedeskv1alpha1.Project {
return &jiraservicedeskv1alpha1.Project{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: jiraservicedeskv1alpha1.ProjectSpec{
Name: name,
Key: key,
ProjectTypeKey: projectTypeKey,
ProjectTemplateKey: projectTemplateKey,
Description: description,
AssigneeType: assigneeType,
LeadAccountId: leadAccountId,
URL: url,
},
}
}

// CreateProject creates and submits a Project object to the kubernetes server
func (t *TestUtil) CreateProject(name string, key string, projectTypeKey string, projectTemplateKey string, description string, assigneeType string, leadAccountId string, url string, namespace string) *jiraservicedeskv1alpha1.Project {
projectObject := t.CreateProjectObject(name, key, projectTypeKey, projectTemplateKey, description, assigneeType, leadAccountId, url, namespace)
err := t.k8sClient.Create(t.ctx, projectObject)
if err != nil {
ginko.Fail(err.Error())
}

req := reconcile.Request{NamespacedName: types.NamespacedName{Name: name, Namespace: namespace}}

_, err = t.r.Reconcile(req)
if err != nil {
ginko.Fail(err.Error())
}

return projectObject
}

// GetProject fetches a project object from kubernetes
func (t *TestUtil) GetProject(name string, namespace string) *jiraservicedeskv1alpha1.Project {
projectObject := &jiraservicedeskv1alpha1.Project{}
err := t.k8sClient.Get(t.ctx, types.NamespacedName{Name: name, Namespace: namespace}, projectObject)

if err != nil {
ginko.Fail(err.Error())
}

return projectObject
}
70 changes: 70 additions & 0 deletions mock/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package mock

import "strconv"

const BaseURL = "https://sample.atlassian.net"

var CreateProjectResponseID = "10003"
var CreateProjectResponseIDInt, _ = strconv.Atoi(CreateProjectResponseID)

var CreateProjectInputJSON = map[string]string{
"name": "stakater",
"key": "STK",
"projectTypeKey": "service_desk",
"projectTemplateKey": "com.atlassian.servicedesk:itil-v2-service-desk-project",
"description": "Sample project for jira-service-desk-operator",
"assigneeType": "PROJECT_LEAD",
"leadAccountId": "5ebfbc3ead226b0ba46c359Z",
"url": "https://stakater.com",
}

var CreateProjectResponseJSON = map[string]interface{}{
"self": BaseURL + "/rest/api/3/project/10003",
"id": CreateProjectResponseIDInt,
"key": "KEY",
}

var CreateProjectInput = struct {
Name string
Key string
ProjectTypeKey string
ProjectTemplateKey string
Description string
AssigneeType string
LeadAccountId string
URL string
}{
Name: "stakater",
Key: "STK",
ProjectTypeKey: "service_desk",
ProjectTemplateKey: "com.atlassian.servicedesk:itil-v2-service-desk-project",
Description: "Sample project for jira-service-desk-operator",
AssigneeType: "PROJECT_LEAD",
LeadAccountId: "5ebfbc3ead226b0ba46c359Z",
URL: "https://stakater.com",
}

var GetProjectByIdResponseJSON = map[string]string{
"description": "Sample Project",
"name": "Sample",
"assigneeType": "UNASSIGNED",
"projectTypeKey": "business",
"key": "KEY",
"url": "https://www.sample.com",
}

var GetProjectByIdExpectedResponse = struct {
Description string
Name string
AssigneeType string
ProjectTypeKey string
Key string
URL string
}{
"Sample Project",
"Sample",
"UNASSIGNED",
"business",
"KEY",
"https://www.sample.com",
}
Loading

0 comments on commit 3101807

Please sign in to comment.