Skip to content

Commit bf4c53c

Browse files
authored
feat: remove dependency on go-arty and improve testing suite (#156)
1 parent 8630ae7 commit bf4c53c

29 files changed

+846
-635
lines changed

Diff for: .github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424

2525
- name: test
2626
run: |
27-
go test -race -covermode=atomic -coverprofile=coverage.out ./...
27+
go test -covermode=atomic -coverprofile=coverage.out ./...
2828
2929
- name: coverage
3030
uses: codecov/codecov-action@v3

Diff for: Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ARG JFROG_VERSION=1.33.2
1111
ADD https://releases.jfrog.io/artifactory/jfrog-cli/v1/${JFROG_VERSION}/jfrog-cli-linux-amd64/jfrog /bin/jfrog
1212

1313
RUN chmod a+x /bin/jfrog
14+
RUN chmod -R 777 /tmp
1415

1516
##############################################################################
1617
## docker build --no-cache --target certs -t vela-artifactory:certs . ##
@@ -27,6 +28,7 @@ RUN apk add --update --no-cache ca-certificates
2728
FROM scratch
2829

2930
COPY --from=binary /bin/jfrog /bin/jfrog
31+
COPY --from=binary /tmp /tmp
3032

3133
COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
3234

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fix:
8686
test:
8787
@echo
8888
@echo "### Testing Go Code"
89-
@go test -race ./...
89+
@go test ./...
9090

9191
# The `test-cover` target is intended to run
9292
# the tests for the Go source code and then

Diff for: cmd/vela-artifactory/artifactoryservice.go

-15
This file was deleted.

Diff for: cmd/vela-artifactory/config.go

+21-11
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,28 @@ import (
99

1010
"github.com/jfrog/jfrog-client-go/artifactory"
1111
"github.com/jfrog/jfrog-client-go/artifactory/auth"
12+
"github.com/jfrog/jfrog-client-go/config"
13+
"github.com/jfrog/jfrog-client-go/http/httpclient"
1214
"github.com/jfrog/jfrog-client-go/utils/log"
1315

1416
"github.com/sirupsen/logrus"
1517
)
1618

1719
// Config represents the plugin configuration for Artifactory config information.
1820
type Config struct {
19-
// action to perform against the Artifactory instance
21+
// Action to perform against the Artifactory instance
2022
Action string
23+
// Token for communication with the Artifactory instance
24+
Token string
2125
// API key for communication with the Artifactory instance
2226
APIKey string
23-
// enables pretending to perform the action against the Artifactory instance
27+
// DryRun enables pretending to perform the action against the Artifactory instance
2428
DryRun bool
25-
// password for communication with the Artifactory instance
29+
// Password for communication with the Artifactory instance
2630
Password string
27-
// full url to Artifactory instance
31+
// URL points to the Artifactory instance
2832
URL string
29-
// user name for communication with the Artifactory instance
33+
// Username for communication with the Artifactory instance
3034
Username string
3135
}
3236

@@ -51,10 +55,16 @@ func (c *Config) New() (*artifactory.ArtifactoryServicesManager, error) {
5155
details.SetUser(c.Username)
5256
}
5357

54-
// check if API key is provided
55-
if len(c.APIKey) > 0 {
58+
// check if Access/Identity token is provided
59+
if len(c.Token) > 0 && !httpclient.IsApiKey(c.Token) {
60+
// set Access/Identity token for Artifactory details
61+
details.SetAccessToken(c.APIKey)
62+
} else if len(c.APIKey) > 0 && httpclient.IsApiKey(c.APIKey) { // check if API key is provided
5663
// set API key for Artifactory details
5764
details.SetApiKey(c.APIKey)
65+
} else if len(c.APIKey) > 0 && !httpclient.IsApiKey(c.APIKey) {
66+
// set Access/Identity token for Artifactory details
67+
details.SetAccessToken(c.APIKey)
5868
}
5969

6070
// check if password is provided
@@ -70,21 +80,21 @@ func (c *Config) New() (*artifactory.ArtifactoryServicesManager, error) {
7080
)
7181

7282
// create new Artifactory config from details
73-
config, err := artifactory.NewConfigBuilder().
74-
SetArtDetails(details).
83+
config, err := config.NewConfigBuilder().
84+
SetServiceDetails(details).
7585
SetDryRun(c.DryRun).
7686
Build()
7787
if err != nil {
7888
return nil, err
7989
}
8090

8191
// create new Artifactory client from config and details
82-
client, err := artifactory.New(&details, config)
92+
client, err := artifactory.New(config)
8393
if err != nil {
8494
return nil, err
8595
}
8696

87-
return client, nil
97+
return &client, nil
8898
}
8999

90100
// Validate verifies the Config is properly configured.

Diff for: cmd/vela-artifactory/config_test.go

+28-26
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ package main
44

55
import (
66
"testing"
7+
8+
"github.com/go-vela/vela-artifactory/cmd/vela-artifactory/mock"
79
)
810

911
func TestArtifactory_Config_New(t *testing.T) {
1012
// setup types
1113
c := &Config{
1214
Action: "copy",
13-
APIKey: "superSecretAPIKey",
15+
APIKey: mock.APIKey,
1416
DryRun: false,
15-
Password: "superSecretPassword",
16-
URL: "https://myarti.com/artifactory",
17-
Username: "octocat",
17+
URL: mock.InvalidArtifactoryServerURL,
18+
Username: mock.Username,
19+
Password: mock.Password,
1820
}
1921

2022
got, err := c.New()
@@ -31,11 +33,11 @@ func TestArtifactory_Config_Validate(t *testing.T) {
3133
// setup types
3234
c := &Config{
3335
Action: "copy",
34-
APIKey: "superSecretAPIKey",
36+
APIKey: mock.APIKey,
3537
DryRun: false,
36-
Password: "superSecretPassword",
37-
URL: "https://myarti.com/artifactory",
38-
Username: "octocat",
38+
URL: mock.InvalidArtifactoryServerURL,
39+
Username: mock.Username,
40+
Password: mock.Password,
3941
}
4042

4143
err := c.Validate()
@@ -47,11 +49,11 @@ func TestArtifactory_Config_Validate(t *testing.T) {
4749
func TestArtifactory_Config_Validate_NoAction(t *testing.T) {
4850
// setup types
4951
c := &Config{
50-
APIKey: "superSecretAPIKey",
52+
APIKey: mock.APIKey,
5153
DryRun: false,
52-
Password: "superSecretPassword",
53-
URL: "https://myarti.com/artifactory",
54-
Username: "octocat",
54+
URL: mock.InvalidArtifactoryServerURL,
55+
Username: mock.Username,
56+
Password: mock.Password,
5557
}
5658

5759
err := c.Validate()
@@ -65,8 +67,8 @@ func TestArtifactory_Config_Validate_NoAPIKeyOrPassword(t *testing.T) {
6567
c := &Config{
6668
Action: "copy",
6769
DryRun: false,
68-
URL: "https://myarti.com/artifactory",
69-
Username: "octocat",
70+
URL: mock.InvalidArtifactoryServerURL,
71+
Username: mock.Username,
7072
}
7173

7274
err := c.Validate()
@@ -80,9 +82,9 @@ func TestArtifactory_Config_Validate_NoAPIKey(t *testing.T) {
8082
c := &Config{
8183
Action: "copy",
8284
DryRun: false,
83-
Password: "superSecretPassword",
84-
URL: "https://myarti.com/artifactory",
85-
Username: "octocat",
85+
Password: mock.Password,
86+
URL: mock.InvalidArtifactoryServerURL,
87+
Username: mock.Username,
8688
}
8789

8890
err := c.Validate()
@@ -96,9 +98,9 @@ func TestArtifactory_Config_Validate_NoPassword(t *testing.T) {
9698
c := &Config{
9799
Action: "copy",
98100
DryRun: false,
99-
APIKey: "superSecretAPIKey",
100-
URL: "https://myarti.com/artifactory",
101-
Username: "octocat",
101+
APIKey: mock.APIKey,
102+
URL: mock.InvalidArtifactoryServerURL,
103+
Username: mock.Username,
102104
}
103105

104106
err := c.Validate()
@@ -112,9 +114,9 @@ func TestArtifactory_Config_Validate_NoUrl(t *testing.T) {
112114
c := &Config{
113115
Action: "copy",
114116
DryRun: false,
115-
APIKey: "superSecretAPIKey",
116-
Password: "superSecretPassword",
117-
Username: "octocat",
117+
APIKey: mock.APIKey,
118+
Username: mock.Username,
119+
Password: mock.Password,
118120
}
119121

120122
err := c.Validate()
@@ -128,8 +130,8 @@ func TestArtifactory_Config_Validate_NoUsername(t *testing.T) {
128130
c := &Config{
129131
Action: "copy",
130132
DryRun: false,
131-
Password: "superSecretPassword",
132-
URL: "https://myarti.com/artifactory",
133+
Password: mock.Password,
134+
URL: mock.InvalidArtifactoryServerURL,
133135
}
134136

135137
err := c.Validate()
@@ -143,7 +145,7 @@ func TestArtifactory_Config_Validate_NoAuth(t *testing.T) {
143145
c := &Config{
144146
Action: "copy",
145147
DryRun: false,
146-
URL: "https://myarti.com/artifactory",
148+
URL: mock.InvalidArtifactoryServerURL,
147149
}
148150

149151
err := c.Validate()

Diff for: cmd/vela-artifactory/copy.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package main
55
import (
66
"fmt"
77

8+
"github.com/jfrog/jfrog-client-go/artifactory"
89
"github.com/jfrog/jfrog-client-go/artifactory/services"
910
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
1011

@@ -15,25 +16,25 @@ const copyAction = "copy"
1516

1617
// Copy represents the plugin configuration for copy information.
1718
type Copy struct {
18-
// enables removing source file directory hierarchy
19+
// Flat is a flag that enables removing source file directory hierarchy
1920
Flat bool
20-
// enables copying sub-directories from source
21+
// Recursive is a flag that enables copying sub-directories from source
2122
Recursive bool
22-
// source path to artifact(s) to copy
23+
// Path is the source path to artifact(s) to copy
2324
Path string
24-
// target path to copy artifact(s) to
25+
// Target is the path to copy artifact(s) to
2526
Target string
2627
}
2728

2829
// Exec formats and runs the commands for copying artifacts in Artifactory.
29-
func (c *Copy) Exec(cli ArtifactoryServicesManager) error {
30+
func (c *Copy) Exec(cli artifactory.ArtifactoryServicesManager) error {
3031
logrus.Trace("running copy with provided configuration")
3132

3233
// create new copy parameters
3334
p := services.NewMoveCopyParams()
3435

3536
// add copy configuration to copy parameters
36-
p.ArtifactoryCommonParams = &utils.ArtifactoryCommonParams{
37+
p.CommonParams = &utils.CommonParams{
3738
Pattern: c.Path,
3839
Recursive: c.Recursive,
3940
Target: c.Target,

Diff for: cmd/vela-artifactory/copy_test.go

+42-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,53 @@
22

33
package main
44

5-
import "testing"
5+
import (
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/go-vela/vela-artifactory/cmd/vela-artifactory/mock"
10+
)
11+
12+
func TestArtifactory_Copy_Exec(t *testing.T) {
13+
// setup types
14+
s := httptest.NewServer(mock.Handlers())
15+
16+
p := &Plugin{
17+
Config: &Config{
18+
Action: "copy",
19+
Token: mock.Token,
20+
APIKey: mock.APIKey,
21+
DryRun: false,
22+
URL: s.URL,
23+
Username: mock.Username,
24+
Password: mock.Password,
25+
},
26+
Copy: &Copy{
27+
Flat: false,
28+
Recursive: false,
29+
Path: "foo/bar",
30+
Target: "bar/foo",
31+
},
32+
Delete: &Delete{},
33+
SetProp: &SetProp{},
34+
Upload: &Upload{},
35+
}
36+
37+
err := p.Exec()
38+
if err != nil {
39+
t.Errorf("Exec returned err %v", err)
40+
}
41+
}
642

743
func TestArtifactory_Copy_Exec_Error(t *testing.T) {
844
// setup types
945
config := &Config{
1046
Action: "copy",
11-
APIKey: "superSecretAPIKey",
47+
APIKey: mock.APIKey,
1248
DryRun: false,
13-
Password: "superSecretPassword",
14-
URL: "http://localhost:8081/artifactory",
15-
Username: "octocat",
49+
URL: mock.InvalidArtifactoryServerURL,
50+
Username: mock.Username,
51+
Password: mock.Password,
1652
}
1753

1854
cli, err := config.New()
@@ -27,7 +63,7 @@ func TestArtifactory_Copy_Exec_Error(t *testing.T) {
2763
Target: "bar/foo",
2864
}
2965

30-
err = c.Exec(cli)
66+
err = c.Exec(*cli)
3167
if err == nil {
3268
t.Errorf("Exec should have returned err")
3369
}

Diff for: cmd/vela-artifactory/delete.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/sirupsen/logrus"
99

10+
"github.com/jfrog/jfrog-client-go/artifactory"
1011
"github.com/jfrog/jfrog-client-go/artifactory/services"
1112
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
1213
)
@@ -15,21 +16,21 @@ const deleteAction = "delete"
1516

1617
// Delete represents the plugin configuration for delete information.
1718
type Delete struct {
18-
// enables removing sub-directories for the artifact(s) in the path
19+
// Recursive is a flag that enables removing sub-directories for the artifact(s) in the path
1920
Recursive bool
20-
// target path to artifact(s) to remove
21+
// Path is the target path to artifact(s) to remove
2122
Path string
2223
}
2324

2425
// Exec formats and runs the commands for removing artifacts in Artifactory.
25-
func (d *Delete) Exec(cli ArtifactoryServicesManager) error {
26+
func (d *Delete) Exec(cli artifactory.ArtifactoryServicesManager) error {
2627
logrus.Trace("running delete with provided configuration")
2728

2829
// create new delete parameters
2930
p := services.NewDeleteParams()
3031

3132
// add delete configuration to delete parameters
32-
p.ArtifactoryCommonParams = &utils.ArtifactoryCommonParams{
33+
p.CommonParams = &utils.CommonParams{
3334
Pattern: d.Path,
3435
Recursive: d.Recursive,
3536
}

0 commit comments

Comments
 (0)