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
b6567e56
Commit
b6567e56
authored
Jun 29, 2021
by
abrichards
Browse files
groups integration test
parent
599d0558
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
245 additions
and
24 deletions
+245
-24
custom/p1/groups.go
custom/p1/groups.go
+137
-0
custom/p1/project.go
custom/p1/project.go
+20
-19
custom/p1/types.go
custom/p1/types.go
+2
-2
integration-tests/custom/p1/p1_groups_test.go
integration-tests/custom/p1/p1_groups_test.go
+83
-0
integration-tests/custom/p1/p1_projects_test.go
integration-tests/custom/p1/p1_projects_test.go
+3
-3
No files found.
custom/p1/groups.go
0 → 100644
View file @
b6567e56
package
p1
import
(
"fmt"
"net/http"
"github.com/romana/rlog"
gogitlab
"github.com/xanzy/go-gitlab"
apigitlab
"valkyrie.dso.mil/valkyrie-api/apis/gitlab/v1alpha1"
gitlab
"valkyrie.dso.mil/valkyrie-api/clients/gitlab"
)
// processError - helper
func
processGroupError
(
logPrefix
string
,
err
error
)
{
rlog
.
Warnf
(
"%s error: %v"
,
logPrefix
,
err
)
}
func
logGroupStart
(
logPrefix
string
,
groupSpec
apigitlab
.
GroupSpec
)
{
rlog
.
Debugf
(
"%s start. groupConfig %s %s"
,
logPrefix
,
groupSpec
.
Name
,
groupSpec
.
FullPath
,
)
}
// CreateGroup -
func
CreateGroup
(
gitlabCredentials
GitlabCredentials
,
groupSpec
apigitlab
.
GroupSpec
,
httpClient
*
http
.
Client
)
error
{
logPrefix
:=
"CreateGroup"
logGroupStart
(
logPrefix
,
groupSpec
)
client
,
err
:=
gitlab
.
NewClient
(
gitlabCredentials
.
ServerURL
,
gitlabCredentials
.
ServerToken
,
httpClient
)
if
err
!=
nil
{
processGroupError
(
logPrefix
,
err
)
return
err
}
// Identify parent Group for group
groupFullPath
:=
groupSpec
.
FullPath
groupParentFullPath
,
groupPath
:=
ParseGitlabPath
(
groupFullPath
)
// strip any trailing /
groupParentFullPath
=
StripLastChar
(
groupParentFullPath
,
"/"
)
parentGroup
,
statusCode
,
err
:=
client
.
GetGroupByFullPath
(
&
groupParentFullPath
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to find group with fullPath %s. error: %v"
,
groupParentFullPath
,
err
)
}
if
statusCode
!=
http
.
StatusFound
{
return
fmt
.
Errorf
(
"failed to find group with fullPath %s. status code: %d"
,
groupParentFullPath
,
statusCode
)
}
groupOptions
:=
gogitlab
.
CreateGroupOptions
{
Name
:
&
groupSpec
.
Name
,
Path
:
&
groupPath
,
ParentID
:
&
parentGroup
.
ID
,
}
group
,
statusCode
,
err
:=
client
.
AddGroup
(
groupOptions
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to add group with name %s to group path %s. error: %v"
,
*
groupOptions
.
Name
,
groupParentFullPath
,
err
)
}
if
statusCode
!=
http
.
StatusCreated
{
return
fmt
.
Errorf
(
"failed to add group with name %s to group path %s. statusCode: %d"
,
*
groupOptions
.
Name
,
groupParentFullPath
,
statusCode
)
}
rlog
.
Debugf
(
"Added group %s to path %s with status code %d"
,
group
.
Name
,
group
.
FullPath
,
statusCode
)
return
nil
}
// DeleteGroup -
func
DeleteGroup
(
gitlabCredentials
GitlabCredentials
,
groupSpec
apigitlab
.
GroupSpec
,
httpClient
*
http
.
Client
)
error
{
logPrefix
:=
"DeleteGroup"
logGroupStart
(
logPrefix
,
groupSpec
)
client
,
err
:=
gitlab
.
NewClient
(
gitlabCredentials
.
ServerURL
,
gitlabCredentials
.
ServerToken
,
httpClient
)
if
err
!=
nil
{
processGroupError
(
logPrefix
,
err
)
return
err
}
groupFullPath
:=
groupSpec
.
FullPath
group
,
statusCode
,
err
:=
client
.
GetGroupByFullPath
(
&
groupFullPath
)
if
err
!=
nil
{
processGroupError
(
logPrefix
,
err
)
return
err
}
if
statusCode
!=
http
.
StatusFound
{
return
fmt
.
Errorf
(
"failed to find group with fullPath %s"
,
groupFullPath
)
}
// delete and wait for delete to complete up to 500ms * 240 tries
_
,
err
=
client
.
DeleteGroup
(
group
.
ID
,
500
,
240
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to delete group with fullPath: %s"
,
groupFullPath
)
}
return
nil
}
// UpdateGroup -
func
UpdateGroup
(
gitlabCredentials
GitlabCredentials
,
groupSpec
apigitlab
.
GroupSpec
,
httpClient
*
http
.
Client
)
error
{
logPrefix
:=
"UpdateGroup"
logGroupStart
(
logPrefix
,
groupSpec
)
client
,
err
:=
gitlab
.
NewClient
(
gitlabCredentials
.
ServerURL
,
gitlabCredentials
.
ServerToken
,
httpClient
)
if
err
!=
nil
{
processGroupError
(
logPrefix
,
err
)
return
err
}
// Identify parent Group for group
groupFullPath
:=
groupSpec
.
FullPath
group
,
statusCode
,
err
:=
client
.
GetGroupByFullPath
(
&
groupFullPath
)
if
err
!=
nil
{
processGroupError
(
logPrefix
,
err
)
return
err
}
if
statusCode
!=
http
.
StatusFound
{
return
fmt
.
Errorf
(
"failed to find group with fullPath %s"
,
groupFullPath
)
}
groupOptions
:=
gogitlab
.
UpdateGroupOptions
{
Name
:
&
groupSpec
.
Name
,
Description
:
&
groupSpec
.
Description
,
}
group
,
statusCode
,
err
=
client
.
UpdateGroup
(
group
.
ID
,
&
groupOptions
)
if
err
!=
nil
{
processGroupError
(
logPrefix
,
err
)
return
fmt
.
Errorf
(
fmt
.
Sprintf
(
"Faild to update group with ID %d error: %v"
,
int
(
group
.
ID
),
err
))
}
rlog
.
Debugf
(
"Updated group %s with status code %d"
,
group
.
Name
,
statusCode
)
return
nil
}
custom/p1/project.go
View file @
b6567e56
...
...
@@ -10,27 +10,28 @@ import (
gitlab
"valkyrie.dso.mil/valkyrie-api/clients/gitlab"
)
// processError - helper
func
processError
(
logPrefix
string
,
err
error
)
{
// process
Project
Error - helper
func
process
Project
Error
(
logPrefix
string
,
err
error
)
{
rlog
.
Warnf
(
"%s error: %v"
,
logPrefix
,
err
)
}
func
logStart
(
logPrefix
string
,
projectSpec
apigitlab
.
ProjectSpec
)
{
func
log
Project
Start
(
logPrefix
string
,
projectSpec
apigitlab
.
ProjectSpec
)
{
rlog
.
Debugf
(
"%s start. projectConfig %s %s %s"
,
logPrefix
,
projectSpec
.
Name
,
projectSpec
.
GroupID
,
projectSpec
.
Language
)
projectSpec
.
FullPath
,
projectSpec
.
Language
,
)
}
// CreateProject -
func
CreateProject
(
projectCredentials
Project
Credentials
,
projectSpec
apigitlab
.
ProjectSpec
,
httpClient
*
http
.
Client
)
error
{
func
CreateProject
(
projectCredentials
Gitlab
Credentials
,
projectSpec
apigitlab
.
ProjectSpec
,
httpClient
*
http
.
Client
)
error
{
logPrefix
:=
"CreateProject"
logStart
(
logPrefix
,
projectSpec
)
log
Project
Start
(
logPrefix
,
projectSpec
)
client
,
err
:=
gitlab
.
NewClient
(
projectCredentials
.
ServerURL
,
projectCredentials
.
ServerToken
,
httpClient
)
if
err
!=
nil
{
processError
(
logPrefix
,
err
)
process
Project
Error
(
logPrefix
,
err
)
return
err
}
...
...
@@ -78,20 +79,20 @@ func CreateProject(projectCredentials ProjectCredentials, projectSpec apigitlab.
}
// DeleteProject -
func
DeleteProject
(
projectCredentials
Project
Credentials
,
projectSpec
apigitlab
.
ProjectSpec
,
httpClient
*
http
.
Client
)
error
{
func
DeleteProject
(
projectCredentials
Gitlab
Credentials
,
projectSpec
apigitlab
.
ProjectSpec
,
httpClient
*
http
.
Client
)
error
{
logPrefix
:=
"DeleteProject"
logStart
(
logPrefix
,
projectSpec
)
log
Project
Start
(
logPrefix
,
projectSpec
)
client
,
err
:=
gitlab
.
NewClient
(
projectCredentials
.
ServerURL
,
projectCredentials
.
ServerToken
,
httpClient
)
if
err
!=
nil
{
processError
(
logPrefix
,
err
)
process
Project
Error
(
logPrefix
,
err
)
return
err
}
projectFullPath
:=
projectSpec
.
FullPath
project
,
statusCode
,
err
:=
client
.
GetProjectByFullPath
(
&
projectFullPath
)
if
err
!=
nil
{
processError
(
logPrefix
,
err
)
process
Project
Error
(
logPrefix
,
err
)
return
err
}
...
...
@@ -109,13 +110,13 @@ func DeleteProject(projectCredentials ProjectCredentials, projectSpec apigitlab.
}
// UpdateProject -
func
UpdateProject
(
projectCredentials
Project
Credentials
,
projectSpec
apigitlab
.
ProjectSpec
,
httpClient
*
http
.
Client
)
error
{
func
UpdateProject
(
projectCredentials
Gitlab
Credentials
,
projectSpec
apigitlab
.
ProjectSpec
,
httpClient
*
http
.
Client
)
error
{
logPrefix
:=
"UpdateProject"
logStart
(
logPrefix
,
projectSpec
)
log
Project
Start
(
logPrefix
,
projectSpec
)
client
,
err
:=
gitlab
.
NewClient
(
projectCredentials
.
ServerURL
,
projectCredentials
.
ServerToken
,
httpClient
)
if
err
!=
nil
{
processError
(
logPrefix
,
err
)
process
Project
Error
(
logPrefix
,
err
)
return
err
}
...
...
@@ -126,7 +127,7 @@ func UpdateProject(projectCredentials ProjectCredentials, projectSpec apigitlab.
groupFullPath
=
StripLastChar
(
groupFullPath
,
"/"
)
group
,
_
,
err
:=
client
.
GetGroupByFullPath
(
&
groupFullPath
)
if
err
!=
nil
{
processError
(
logPrefix
,
err
)
process
Project
Error
(
logPrefix
,
err
)
return
fmt
.
Errorf
(
"failed to find group with fullPath %s"
,
groupFullPath
)
}
...
...
@@ -134,7 +135,7 @@ func UpdateProject(projectCredentials ProjectCredentials, projectSpec apigitlab.
project
,
statusCode
,
err
:=
client
.
GetProjectByFullPath
(
&
projectFullPath
)
if
err
!=
nil
{
processError
(
logPrefix
,
err
)
process
Project
Error
(
logPrefix
,
err
)
return
err
}
if
statusCode
!=
http
.
StatusFound
{
...
...
@@ -143,7 +144,7 @@ func UpdateProject(projectCredentials ProjectCredentials, projectSpec apigitlab.
ciConfigPath
,
err
:=
generateCIConfigPath
(
projectPath
,
parentGroupFullPath
)
if
err
!=
nil
{
processError
(
logPrefix
,
err
)
process
Project
Error
(
logPrefix
,
err
)
return
err
}
...
...
@@ -155,7 +156,7 @@ func UpdateProject(projectCredentials ProjectCredentials, projectSpec apigitlab.
project
,
statusCode
,
err
=
client
.
UpdateProject
(
project
.
ID
,
projectOptions
)
if
err
!=
nil
{
processError
(
logPrefix
,
err
)
process
Project
Error
(
logPrefix
,
err
)
return
fmt
.
Errorf
(
fmt
.
Sprintf
(
"Faild to update project with ID %d error: %v"
,
int
(
project
.
ID
),
err
))
}
rlog
.
Debugf
(
"Updated project %s with status code %d"
,
project
.
Name
,
statusCode
)
...
...
custom/p1/types.go
View file @
b6567e56
...
...
@@ -4,8 +4,8 @@ import (
"fmt"
)
//
Project
Credentials -
type
Project
Credentials
struct
{
//
Gitlab
Credentials -
type
Gitlab
Credentials
struct
{
ServerURL
string
ServerToken
string
}
...
...
integration-tests/custom/p1/p1_groups_test.go
0 → 100644
View file @
b6567e56
// +build integration
package
integration
import
(
"testing"
"valkyrie.dso.mil/valkyrie-api/apis/gitlab/v1alpha1"
custom_p1
"valkyrie.dso.mil/valkyrie-api/custom/p1"
)
/*
P1Config is initialized in the TestMain wrapper method
*/
const
integrationTestGroupPath
=
"int-test-group"
func
Test_P1_AddGroup
(
t
*
testing
.
T
)
{
logPrefix
:=
"Test_P1_AddGroup"
t
.
Run
(
"test"
,
func
(
t
*
testing
.
T
)
{
groupSpec
:=
v1alpha1
.
GroupSpec
{
Name
:
"My Test Group"
,
FullPath
:
p1IntegrationRootGroupPath
+
"/"
+
integrationTestGroupPath
,
}
creds
:=
custom_p1
.
GitlabCredentials
{
ServerURL
:
P1Config
.
gitlabAPIURL
,
ServerToken
:
P1Config
.
gitlabAPIToken
,
}
err
:=
custom_p1
.
CreateGroup
(
creds
,
groupSpec
,
nil
)
if
err
!=
nil
{
t
.
Errorf
(
"CreateGroup() error = %v"
,
err
)
return
}
t
.
Logf
(
"%s created group %v"
,
logPrefix
,
groupSpec
)
})
}
func
Test_P1_UpdateGroup
(
t
*
testing
.
T
)
{
logPrefix
:=
"Test_P1_UpdateGroup"
t
.
Run
(
"test"
,
func
(
t
*
testing
.
T
)
{
groupSpec
:=
v1alpha1
.
GroupSpec
{
Name
:
"My Test Updated Group"
,
FullPath
:
p1IntegrationRootGroupPath
+
"/"
+
integrationTestGroupPath
,
}
creds
:=
custom_p1
.
GitlabCredentials
{
ServerURL
:
P1Config
.
gitlabAPIURL
,
ServerToken
:
P1Config
.
gitlabAPIToken
,
}
err
:=
custom_p1
.
UpdateGroup
(
creds
,
groupSpec
,
nil
)
if
err
!=
nil
{
t
.
Errorf
(
"UpdateGroup() error = %v"
,
err
)
return
}
t
.
Logf
(
"%s update group %v"
,
logPrefix
,
groupSpec
)
})
}
func
Test_P1_DeleteGroup
(
t
*
testing
.
T
)
{
logPrefix
:=
"Test_P1_DeleteGroup"
t
.
Run
(
"test"
,
func
(
t
*
testing
.
T
)
{
groupSpec
:=
v1alpha1
.
GroupSpec
{
FullPath
:
p1IntegrationRootGroupPath
+
"/"
+
integrationTestGroupPath
,
}
creds
:=
custom_p1
.
GitlabCredentials
{
ServerURL
:
P1Config
.
gitlabAPIURL
,
ServerToken
:
P1Config
.
gitlabAPIToken
,
}
err
:=
custom_p1
.
DeleteGroup
(
creds
,
groupSpec
,
nil
)
if
err
!=
nil
{
t
.
Errorf
(
"DeleteGroup() error = %v"
,
err
)
return
}
t
.
Logf
(
"%s deleted group %v"
,
logPrefix
,
groupSpec
)
})
}
integration-tests/custom/p1/p1_projects_test.go
View file @
b6567e56
...
...
@@ -25,7 +25,7 @@ func Test_P1_AddProject(t *testing.T) {
Language
:
custom_p1
.
LangTypeAngular
,
}
creds
:=
custom_p1
.
Project
Credentials
{
creds
:=
custom_p1
.
Gitlab
Credentials
{
ServerURL
:
P1Config
.
gitlabAPIURL
,
ServerToken
:
P1Config
.
gitlabAPIToken
,
}
...
...
@@ -49,7 +49,7 @@ func Test_P1_UpdateProject(t *testing.T) {
Language
:
custom_p1
.
LangTypeJavaMaven
,
}
creds
:=
custom_p1
.
Project
Credentials
{
creds
:=
custom_p1
.
Gitlab
Credentials
{
ServerURL
:
P1Config
.
gitlabAPIURL
,
ServerToken
:
P1Config
.
gitlabAPIToken
,
}
...
...
@@ -70,7 +70,7 @@ func Test_P1_DeleteProject(t *testing.T) {
FullPath
:
p1IntegrationRootGroupPath
+
"/"
+
integrationTestProjectPath
,
}
creds
:=
custom_p1
.
Project
Credentials
{
creds
:=
custom_p1
.
Gitlab
Credentials
{
ServerURL
:
P1Config
.
gitlabAPIURL
,
ServerToken
:
P1Config
.
gitlabAPIToken
,
}
...
...
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