UNCLASSIFIED

Commit ba09446a authored by Jason van Brackel's avatar Jason van Brackel
Browse files

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

parent ac608efa
...@@ -262,7 +262,7 @@ var _ = Describe("Reconcile", func() { ...@@ -262,7 +262,7 @@ var _ = Describe("Reconcile", func() {
kubernetesClient.createFunction = func(ctx context.Context, obj client.Object, opts ...client.CreateOption) error { kubernetesClient.createFunction = func(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
return &MockError{message: "processProjects Fails"} 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"} return &MockError{message: "processProjects Fails"}
} }
result, err := sut.Reconcile(context.TODO(), getGreenGroupControllerRequest()) result, err := sut.Reconcile(context.TODO(), getGreenGroupControllerRequest())
...@@ -468,7 +468,7 @@ var _ = Describe("updateProject", func() { ...@@ -468,7 +468,7 @@ var _ = Describe("updateProject", func() {
return nil 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 return mockError
}, },
} }
......
...@@ -32,7 +32,7 @@ type MockClient struct { ...@@ -32,7 +32,7 @@ type MockClient struct {
NotFoundError error NotFoundError error
statusWriter client.StatusWriter statusWriter client.StatusWriter
createFunction func(ctx context.Context, obj client.Object, opts ...client.CreateOption) error 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 updateFunctionCalled bool
schemeFunction func() *runtime.Scheme schemeFunction func() *runtime.Scheme
} }
...@@ -85,10 +85,10 @@ func (m *MockClient) Delete(ctx context.Context, obj client.Object, opts ...clie ...@@ -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 { func (m *MockClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
m.updateFunctionCalled = true m.updateFunctionCalled = true
if m.updateFunction == nil { if m.UpdateFunction == nil {
return 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 { func (m *MockClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
...@@ -426,6 +426,9 @@ type MockGitlabClient struct { ...@@ -426,6 +426,9 @@ type MockGitlabClient struct {
closeMRFunc func(projectID int, mrID int) error closeMRFunc func(projectID int, mrID int) error
getMRsFunc func(projectID int, sourceBranch string, targetBranch string) ([]*gitlab.MergeRequest, error) getMRsFunc func(projectID int, sourceBranch string, targetBranch string) ([]*gitlab.MergeRequest, error)
createMRFunc func(projectID int, mrOptions *gitlab.CreateMergeRequestOptions) (*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) { func (m *MockGitlabClient) GetUser(userID int) (*gitlab.User, int, error) {
...@@ -498,6 +501,11 @@ func (m *MockGitlabClient) GetProject(projectID int) (*gitlab.Project, int, erro ...@@ -498,6 +501,11 @@ func (m *MockGitlabClient) GetProject(projectID int) (*gitlab.Project, int, erro
var returnProject *gitlab.Project var returnProject *gitlab.Project
var statusCode int var statusCode int
var err error var err error
if m.getProjectFunction != nil {
return m.getProjectFunction(projectID)
}
if m.expectedProjects == nil { if m.expectedProjects == nil {
m.expectedProjects = make(map[int]*gitlab.Project) m.expectedProjects = make(map[int]*gitlab.Project)
} }
...@@ -521,6 +529,10 @@ func (m *MockGitlabClient) GetProjects(search *string) ([]*gitlab.Project, error ...@@ -521,6 +529,10 @@ func (m *MockGitlabClient) GetProjects(search *string) ([]*gitlab.Project, error
} }
func (m *MockGitlabClient) AddProject(createProjectOptions gitlab.CreateProjectOptions) (*gitlab.Project, int, error) { func (m *MockGitlabClient) AddProject(createProjectOptions gitlab.CreateProjectOptions) (*gitlab.Project, int, error) {
if m.addProjectFunction != nil {
return m.addProjectFunction(createProjectOptions)
}
if m.expectedProjects == nil { if m.expectedProjects == nil {
m.expectedProjects = make(map[int]*gitlab.Project) m.expectedProjects = make(map[int]*gitlab.Project)
} }
...@@ -534,7 +546,20 @@ func (m *MockGitlabClient) AddProject(createProjectOptions gitlab.CreateProjectO ...@@ -534,7 +546,20 @@ func (m *MockGitlabClient) AddProject(createProjectOptions gitlab.CreateProjectO
} }
func (m *MockGitlabClient) UpdateProject(projectID int, editProjectOptions gitlab.EditProjectOptions) (*gitlab.Project, int, error) { 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) { 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 ...@@ -99,7 +99,7 @@ func (r *ProjectReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
if gitlabProject, err = r.getGitlabProject(id); err != nil { if gitlabProject, err = r.getGitlabProject(id); err != nil {
r.Log.Error(err, errorGettingProjectFromGitlab) r.Log.Error(err, errorGettingProjectFromGitlab)
_ = r.updateStatus(ctx, project, errorGettingProjectFromGitlab) _ = r.updateStatus(ctx, project, errorGettingProjectFromGitlab)
return ctrl.Result{}, err return ctrl.Result{Requeue: true}, err
} }
if gitlabProject == nil { if gitlabProject == nil {
if gitlabProject, err = r.createGitlabProject(project); err != nil { if gitlabProject, err = r.createGitlabProject(project); err != nil {
...@@ -107,7 +107,6 @@ func (r *ProjectReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct ...@@ -107,7 +107,6 @@ func (r *ProjectReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
_ = r.updateStatus(ctx, project, errorCreatingGitlabProject) _ = r.updateStatus(ctx, project, errorCreatingGitlabProject)
return ctrl.Result{Requeue: true}, err return ctrl.Result{Requeue: true}, err
} }
return ctrl.Result{}, nil
} }
if gitlabProject, err = r.updateGitlabProject(id, project); err != 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 ...@@ -119,6 +118,7 @@ func (r *ProjectReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
if err = r.updateProjectIDAnnotation(ctx, project, gitlabProject); err != nil { if err = r.updateProjectIDAnnotation(ctx, project, gitlabProject); err != nil {
r.Log.Error(err, errorUpdatingProjectIDAnnotation) r.Log.Error(err, errorUpdatingProjectIDAnnotation)
_ = r.updateStatus(ctx, project, errorUpdatingProjectIDAnnotation) _ = r.updateStatus(ctx, project, errorUpdatingProjectIDAnnotation)
return ctrl.Result{Requeue: true}, err
} }
return ctrl.Result{}, nil return ctrl.Result{}, nil
......
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime" ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
gitlabv1alpha1 "valkyrie.dso.mil/valkyrie-api/apis/gitlab/v1alpha1" gitlabv1alpha1 "valkyrie.dso.mil/valkyrie-api/apis/gitlab/v1alpha1"
gitlabClient "valkyrie.dso.mil/valkyrie-api/clients/gitlab"
) )
const ( const (
...@@ -23,6 +24,7 @@ func getGreenProject() gitlabv1alpha1.Project { ...@@ -23,6 +24,7 @@ func getGreenProject() gitlabv1alpha1.Project {
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: GreenProjectName, Name: GreenProjectName,
Namespace: GreenNamespace, Namespace: GreenNamespace,
Annotations: make(map[string]string,0),
}, },
Spec: gitlabv1alpha1.ProjectSpec{ Spec: gitlabv1alpha1.ProjectSpec{
Name: GreenProjectName, Name: GreenProjectName,
...@@ -105,7 +107,7 @@ var _ = Describe("SetupWithManager", func() { ...@@ -105,7 +107,7 @@ var _ = Describe("SetupWithManager", func() {
}) })
var _ = Describe("reconcile", func() { var _ = Describe("reconcile", func() {
Context("green state", func() { Context("green state", func() {
sut, _, _, _ := getControllerWithMocksInGreenTestState() sut, _, _, _ := getControllerWithMocksInGreenTestState()
request := getRequestWithDefaultNamespacedTestProject() request := getRequestWithDefaultNamespacedTestProject()
result, err := sut.Reconcile(context.TODO(), request) result, err := sut.Reconcile(context.TODO(), request)
...@@ -116,6 +118,27 @@ var _ = Describe("reconcile", func() { ...@@ -116,6 +118,27 @@ var _ = Describe("reconcile", func() {
Expect(err).To(BeNil()) 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() { Context("project isn't found", func() {
sut, _, log, _ := getControllerWithMocksInGreenTestState() sut, _, log, _ := getControllerWithMocksInGreenTestState()
request := getRequestWithDefaultNamespacedTestProject() request := getRequestWithDefaultNamespacedTestProject()
...@@ -158,6 +181,78 @@ var _ = Describe("reconcile", func() { ...@@ -158,6 +181,78 @@ var _ = Describe("reconcile", func() {
Expect(log.loggedMessage).To(Equal(errorWhileLookingUpProject)) 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() { Context("getGitlabGroup returns an error", func() {
failingGitlabClient := MockGitlabClient{ failingGitlabClient := MockGitlabClient{
getGroupFunction: func(groupID int) (*gitlab.Group, int, error) { getGroupFunction: func(groupID int) (*gitlab.Group, int, error) {
...@@ -185,6 +280,28 @@ var _ = Describe("reconcile", func() { ...@@ -185,6 +280,28 @@ var _ = Describe("reconcile", func() {
Expect(status.updateFunctionCalled).To(BeTrue()) 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() { 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