UNCLASSIFIED
Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
Platform One
P
Party Bus
Valkyrie
valkyrie-api
Commits
ba09446a
Commit
ba09446a
authored
Jul 12, 2021
by
Jason van Brackel
Browse files
fix: Update tests to hit coverage metric, fix bugs found in the process
parent
ac608efa
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
151 additions
and
9 deletions
+151
-9
controllers/gitlab/group_controller_test.go
controllers/gitlab/group_controller_test.go
+2
-2
controllers/gitlab/mocks_test.go
controllers/gitlab/mocks_test.go
+29
-4
controllers/gitlab/project_controller.go
controllers/gitlab/project_controller.go
+2
-2
controllers/gitlab/project_controller_test.go
controllers/gitlab/project_controller_test.go
+118
-1
No files found.
controllers/gitlab/group_controller_test.go
View file @
ba09446a
...
@@ -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
.
u
pdateFunction
=
func
(
ctx
context
.
Context
,
obj
client
.
Object
,
opts
...
client
.
UpdateOption
)
error
{
kubernetesClient
.
U
pdateFunction
=
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
},
},
},
},
u
pdateFunction
:
func
(
ctx
context
.
Context
,
obj
client
.
Object
,
opts
...
client
.
UpdateOption
)
error
{
U
pdateFunction
:
func
(
ctx
context
.
Context
,
obj
client
.
Object
,
opts
...
client
.
UpdateOption
)
error
{
return
mockError
return
mockError
},
},
}
}
...
...
controllers/gitlab/mocks_test.go
View file @
ba09446a
...
@@ -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
u
pdateFunction
func
(
ctx
context
.
Context
,
obj
client
.
Object
,
opts
...
client
.
UpdateOption
)
error
U
pdateFunction
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
.
u
pdateFunction
==
nil
{
if
m
.
U
pdateFunction
==
nil
{
return
nil
return
nil
}
}
return
m
.
u
pdateFunction
(
ctx
,
obj
,
opts
...
)
return
m
.
U
pdateFunction
(
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
)
{
...
...
controllers/gitlab/project_controller.go
View file @
ba09446a
...
@@ -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
...
...
controllers/gitlab/project_controller_test.go
View file @
ba09446a
...
@@ -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
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment