UNCLASSIFIED

Commit ea5daf0f authored by abrichards's avatar abrichards
Browse files

Merge branch 'dev-jvb-712' into 'master'

fix: Update tests to hit coverage metric, fix bugs found in the process

See merge request !48
parents ac608efa ba09446a
......@@ -262,7 +262,7 @@ var _ = Describe("Reconcile", func() {
kubernetesClient.createFunction = func(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
return &MockError{message: "processProjects Fails"}
}
kubernetesClient.updateFunction = func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
kubernetesClient.UpdateFunction = func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
return &MockError{message: "processProjects Fails"}
}
result, err := sut.Reconcile(context.TODO(), getGreenGroupControllerRequest())
......@@ -468,7 +468,7 @@ var _ = Describe("updateProject", func() {
return nil
},
},
updateFunction: func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
UpdateFunction: func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
return mockError
},
}
......
......@@ -32,7 +32,7 @@ type MockClient struct {
NotFoundError error
statusWriter client.StatusWriter
createFunction func(ctx context.Context, obj client.Object, opts ...client.CreateOption) error
updateFunction func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error
UpdateFunction func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error
updateFunctionCalled bool
schemeFunction func() *runtime.Scheme
}
......@@ -85,10 +85,10 @@ func (m *MockClient) Delete(ctx context.Context, obj client.Object, opts ...clie
func (m *MockClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
m.updateFunctionCalled = true
if m.updateFunction == nil {
if m.UpdateFunction == nil {
return nil
}
return m.updateFunction(ctx, obj, opts...)
return m.UpdateFunction(ctx, obj, opts...)
}
func (m *MockClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
......@@ -426,6 +426,9 @@ type MockGitlabClient struct {
closeMRFunc func(projectID int, mrID int) error
getMRsFunc func(projectID int, sourceBranch string, targetBranch string) ([]*gitlab.MergeRequest, error)
createMRFunc func(projectID int, mrOptions *gitlab.CreateMergeRequestOptions) (*gitlab.MergeRequest, error)
getProjectFunction func(projectID int) (*gitlab.Project, int, error)
addProjectFunction func(options gitlab.CreateProjectOptions) (*gitlab.Project, int, error)
updateProjectFunction func(projectID int, editProjectOptions gitlab.EditProjectOptions) (*gitlab.Project, int, error)
}
func (m *MockGitlabClient) GetUser(userID int) (*gitlab.User, int, error) {
......@@ -498,6 +501,11 @@ func (m *MockGitlabClient) GetProject(projectID int) (*gitlab.Project, int, erro
var returnProject *gitlab.Project
var statusCode int
var err error
if m.getProjectFunction != nil {
return m.getProjectFunction(projectID)
}
if m.expectedProjects == nil {
m.expectedProjects = make(map[int]*gitlab.Project)
}
......@@ -521,6 +529,10 @@ func (m *MockGitlabClient) GetProjects(search *string) ([]*gitlab.Project, error
}
func (m *MockGitlabClient) AddProject(createProjectOptions gitlab.CreateProjectOptions) (*gitlab.Project, int, error) {
if m.addProjectFunction != nil {
return m.addProjectFunction(createProjectOptions)
}
if m.expectedProjects == nil {
m.expectedProjects = make(map[int]*gitlab.Project)
}
......@@ -534,7 +546,20 @@ func (m *MockGitlabClient) AddProject(createProjectOptions gitlab.CreateProjectO
}
func (m *MockGitlabClient) UpdateProject(projectID int, editProjectOptions gitlab.EditProjectOptions) (*gitlab.Project, int, error) {
panic("implement me")
if m.updateProjectFunction != nil {
return m.updateProjectFunction(projectID, editProjectOptions)
}
if m.expectedProjects == nil {
m.expectedProjects = make(map[int]*gitlab.Project)
}
m.expectedProjects[projectID] = &gitlab.Project{
ID: projectID,
Name: *editProjectOptions.Name,
Path: *editProjectOptions.Path,
}
return m.expectedProjects[projectID], 200, nil
}
func (m *MockGitlabClient) DeleteProject(projectID int, waitInterval int, waitCount int) (int, error) {
......
......@@ -99,7 +99,7 @@ func (r *ProjectReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
if gitlabProject, err = r.getGitlabProject(id); err != nil {
r.Log.Error(err, errorGettingProjectFromGitlab)
_ = r.updateStatus(ctx, project, errorGettingProjectFromGitlab)
return ctrl.Result{}, err
return ctrl.Result{Requeue: true}, err
}
if gitlabProject == nil {
if gitlabProject, err = r.createGitlabProject(project); err != nil {
......@@ -107,7 +107,6 @@ func (r *ProjectReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
_ = r.updateStatus(ctx, project, errorCreatingGitlabProject)
return ctrl.Result{Requeue: true}, err
}
return ctrl.Result{}, nil
}
if gitlabProject, err = r.updateGitlabProject(id, project); err != nil {
......@@ -119,6 +118,7 @@ func (r *ProjectReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
if err = r.updateProjectIDAnnotation(ctx, project, gitlabProject); err != nil {
r.Log.Error(err, errorUpdatingProjectIDAnnotation)
_ = r.updateStatus(ctx, project, errorUpdatingProjectIDAnnotation)
return ctrl.Result{Requeue: true}, err
}
return ctrl.Result{}, nil
......
......@@ -10,6 +10,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
gitlabv1alpha1 "valkyrie.dso.mil/valkyrie-api/apis/gitlab/v1alpha1"
gitlabClient "valkyrie.dso.mil/valkyrie-api/clients/gitlab"
)
const (
......@@ -23,6 +24,7 @@ func getGreenProject() gitlabv1alpha1.Project {
ObjectMeta: metav1.ObjectMeta{
Name: GreenProjectName,
Namespace: GreenNamespace,
Annotations: make(map[string]string,0),
},
Spec: gitlabv1alpha1.ProjectSpec{
Name: GreenProjectName,
......@@ -105,7 +107,7 @@ var _ = Describe("SetupWithManager", func() {
})
var _ = Describe("reconcile", func() {
Context("green state", func() {
Context("green state", func() {
sut, _, _, _ := getControllerWithMocksInGreenTestState()
request := getRequestWithDefaultNamespacedTestProject()
result, err := sut.Reconcile(context.TODO(), request)
......@@ -116,6 +118,27 @@ var _ = Describe("reconcile", func() {
Expect(err).To(BeNil())
})
})
Context("SetupClient fails", func() {
sut, _, log, _ := getControllerWithMocksInGreenTestState()
request := getRequestWithDefaultNamespacedTestProject()
sut.gitlabClient = nil
sut.gitlabClientConfiguration = &MockClientConfiguration{
SetupClientFunction: func(client client.Client, credentialsName string) (gitlabClient.Client, error) {
return nil, &MockError{message: "SetupClientFunction fails."}
},
}
result, err := sut.Reconcile(context.TODO(), request)
It("should log the error", func() {
Expect(log.logLevelCalled).To(Equal("Error"))
Expect(log.loggedMessage).To(Equal(errorUnableToSetupGitlabClient))
})
It("should requeue the object", func() {
Expect(result).To(Equal(ctrl.Result{Requeue: true}))
})
It("should return an error", func() {
Expect(err).To(Not(BeNil()))
})
})
Context("project isn't found", func() {
sut, _, log, _ := getControllerWithMocksInGreenTestState()
request := getRequestWithDefaultNamespacedTestProject()
......@@ -158,6 +181,78 @@ var _ = Describe("reconcile", func() {
Expect(log.loggedMessage).To(Equal(errorWhileLookingUpProject))
})
})
Context("getGitlabProject returns an error", func() {
failingGitlabClient := MockGitlabClient{
getProjectFunction: func(groupID int) (*gitlab.Project, int, error) {
return nil, 500, &MockError{
message: "failure in getGitlabProject",
}
},
}
sut, _, log, _ := getControllerWithMocksInGreenTestState()
sut.gitlabClient = &failingGitlabClient
request := getRequestWithDefaultNamespacedTestProject()
result, err := sut.Reconcile(context.TODO(), request)
It("should return an error", func() {
Expect(err).ToNot(BeNil())
})
It("should log the error", func() {
Expect(log.loggedMessage).To(Equal(errorGettingProjectFromGitlab))
})
It("should requeue the object", func() {
Expect(result).To(Equal(ctrl.Result{Requeue: true}))
})
})
Context("createGitlabProject returns an error", func() {
failingGitlabClient := MockGitlabClient{
addProjectFunction: func(options gitlab.CreateProjectOptions) (*gitlab.Project, int, error) {
return nil, 500, &MockError{
message: "failure in addProject",
}
},
}
sut, _, log, _ := getControllerWithMocksInGreenTestState()
sut.gitlabClient = &failingGitlabClient
request := getRequestWithDefaultNamespacedTestProject()
result, err := sut.Reconcile(context.TODO(), request)
It("should return an error", func() {
Expect(err).ToNot(BeNil())
})
It("should log the error", func() {
Expect(log.loggedMessage).To(Equal(errorCreatingGitlabProject))
})
It("should requeue the object", func() {
Expect(result).To(Equal(ctrl.Result{Requeue: true}))
})
})
Context("updateGitlabProject returns an error", func() {
failingGitlabClient := MockGitlabClient{
getProjectFunction: func(projectID int) (*gitlab.Project, int, error) {
return &gitlab.Project{}, 1, nil
},
updateProjectFunction: func(int, gitlab.EditProjectOptions) (*gitlab.Project, int, error) {
return nil, 500, &MockError{
message: "failure in updateGitlabProject",
}
},
}
sut, _, log, _ := getControllerWithMocksInGreenTestState()
sut.gitlabClient = &failingGitlabClient
request := getRequestWithDefaultNamespacedTestProject()
result, err := sut.Reconcile(context.TODO(), request)
It("should return an error", func() {
Expect(err).ToNot(BeNil())
})
It("should log the error", func() {
Expect(log.loggedMessage).To(Equal(errorUpdatingGitlabProject))
})
It("should requeue the object", func() {
Expect(result).To(Equal(ctrl.Result{Requeue: true}))
})
})
Context("getGitlabGroup returns an error", func() {
failingGitlabClient := MockGitlabClient{
getGroupFunction: func(groupID int) (*gitlab.Group, int, error) {
......@@ -185,6 +280,28 @@ var _ = Describe("reconcile", func() {
Expect(status.updateFunctionCalled).To(BeTrue())
})
})
Context("updateProjectIDAnnotation returns an error", func() {
sut, _, log, _ := getControllerWithMocksInGreenTestState()
sut.Client.(*MockClient).UpdateFunction = func(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
return &MockError{message: errorUpdatingProjectIDAnnotation}
}
request := getGreenRequest()
result, err := sut.Reconcile(context.TODO(), request)
It("should return an error", func() {
Expect(err).ToNot(BeNil())
})
It("should requeue the object", func() {
Expect(result).To(Equal(ctrl.Result{Requeue: true}))
})
It("should log the error", func() {
Expect(log.logLevelCalled).To(Equal("Error"))
Expect(log.loggedMessage).To(Equal(errorUpdatingProjectIDAnnotation))
})
})
})
var _ = Describe("getProject", func() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment