-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle create project reconciler test
- Loading branch information
1 parent
6ea489a
commit 3101807
Showing
5 changed files
with
295 additions
and
49 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
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)) | ||
}) | ||
}) | ||
}) | ||
|
||
}) |
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
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,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 | ||
} |
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,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", | ||
} |
Oops, something went wrong.