Skip to content

Commit

Permalink
feat: remove dependency on go-arty and improve testing suite (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 authored Nov 28, 2023
1 parent 8630ae7 commit bf4c53c
Show file tree
Hide file tree
Showing 29 changed files with 846 additions and 635 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:

- name: test
run: |
go test -race -covermode=atomic -coverprofile=coverage.out ./...
go test -covermode=atomic -coverprofile=coverage.out ./...
- name: coverage
uses: codecov/codecov-action@v3
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ARG JFROG_VERSION=1.33.2
ADD https://releases.jfrog.io/artifactory/jfrog-cli/v1/${JFROG_VERSION}/jfrog-cli-linux-amd64/jfrog /bin/jfrog

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

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

COPY --from=binary /bin/jfrog /bin/jfrog
COPY --from=binary /tmp /tmp

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

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fix:
test:
@echo
@echo "### Testing Go Code"
@go test -race ./...
@go test ./...

# The `test-cover` target is intended to run
# the tests for the Go source code and then
Expand Down
15 changes: 0 additions & 15 deletions cmd/vela-artifactory/artifactoryservice.go

This file was deleted.

32 changes: 21 additions & 11 deletions cmd/vela-artifactory/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,28 @@ import (

"github.com/jfrog/jfrog-client-go/artifactory"
"github.com/jfrog/jfrog-client-go/artifactory/auth"
"github.com/jfrog/jfrog-client-go/config"
"github.com/jfrog/jfrog-client-go/http/httpclient"
"github.com/jfrog/jfrog-client-go/utils/log"

"github.com/sirupsen/logrus"
)

// Config represents the plugin configuration for Artifactory config information.
type Config struct {
// action to perform against the Artifactory instance
// Action to perform against the Artifactory instance
Action string
// Token for communication with the Artifactory instance
Token string
// API key for communication with the Artifactory instance
APIKey string
// enables pretending to perform the action against the Artifactory instance
// DryRun enables pretending to perform the action against the Artifactory instance
DryRun bool
// password for communication with the Artifactory instance
// Password for communication with the Artifactory instance
Password string
// full url to Artifactory instance
// URL points to the Artifactory instance
URL string
// user name for communication with the Artifactory instance
// Username for communication with the Artifactory instance
Username string
}

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

// check if API key is provided
if len(c.APIKey) > 0 {
// check if Access/Identity token is provided
if len(c.Token) > 0 && !httpclient.IsApiKey(c.Token) {
// set Access/Identity token for Artifactory details
details.SetAccessToken(c.APIKey)
} else if len(c.APIKey) > 0 && httpclient.IsApiKey(c.APIKey) { // check if API key is provided
// set API key for Artifactory details
details.SetApiKey(c.APIKey)
} else if len(c.APIKey) > 0 && !httpclient.IsApiKey(c.APIKey) {
// set Access/Identity token for Artifactory details
details.SetAccessToken(c.APIKey)
}

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

// create new Artifactory config from details
config, err := artifactory.NewConfigBuilder().
SetArtDetails(details).
config, err := config.NewConfigBuilder().
SetServiceDetails(details).
SetDryRun(c.DryRun).
Build()
if err != nil {
return nil, err
}

// create new Artifactory client from config and details
client, err := artifactory.New(&details, config)
client, err := artifactory.New(config)
if err != nil {
return nil, err
}

return client, nil
return &client, nil
}

// Validate verifies the Config is properly configured.
Expand Down
54 changes: 28 additions & 26 deletions cmd/vela-artifactory/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ package main

import (
"testing"

"github.com/go-vela/vela-artifactory/cmd/vela-artifactory/mock"
)

func TestArtifactory_Config_New(t *testing.T) {
// setup types
c := &Config{
Action: "copy",
APIKey: "superSecretAPIKey",
APIKey: mock.APIKey,
DryRun: false,
Password: "superSecretPassword",
URL: "https://myarti.com/artifactory",
Username: "octocat",
URL: mock.InvalidArtifactoryServerURL,
Username: mock.Username,
Password: mock.Password,
}

got, err := c.New()
Expand All @@ -31,11 +33,11 @@ func TestArtifactory_Config_Validate(t *testing.T) {
// setup types
c := &Config{
Action: "copy",
APIKey: "superSecretAPIKey",
APIKey: mock.APIKey,
DryRun: false,
Password: "superSecretPassword",
URL: "https://myarti.com/artifactory",
Username: "octocat",
URL: mock.InvalidArtifactoryServerURL,
Username: mock.Username,
Password: mock.Password,
}

err := c.Validate()
Expand All @@ -47,11 +49,11 @@ func TestArtifactory_Config_Validate(t *testing.T) {
func TestArtifactory_Config_Validate_NoAction(t *testing.T) {
// setup types
c := &Config{
APIKey: "superSecretAPIKey",
APIKey: mock.APIKey,
DryRun: false,
Password: "superSecretPassword",
URL: "https://myarti.com/artifactory",
Username: "octocat",
URL: mock.InvalidArtifactoryServerURL,
Username: mock.Username,
Password: mock.Password,
}

err := c.Validate()
Expand All @@ -65,8 +67,8 @@ func TestArtifactory_Config_Validate_NoAPIKeyOrPassword(t *testing.T) {
c := &Config{
Action: "copy",
DryRun: false,
URL: "https://myarti.com/artifactory",
Username: "octocat",
URL: mock.InvalidArtifactoryServerURL,
Username: mock.Username,
}

err := c.Validate()
Expand All @@ -80,9 +82,9 @@ func TestArtifactory_Config_Validate_NoAPIKey(t *testing.T) {
c := &Config{
Action: "copy",
DryRun: false,
Password: "superSecretPassword",
URL: "https://myarti.com/artifactory",
Username: "octocat",
Password: mock.Password,
URL: mock.InvalidArtifactoryServerURL,
Username: mock.Username,
}

err := c.Validate()
Expand All @@ -96,9 +98,9 @@ func TestArtifactory_Config_Validate_NoPassword(t *testing.T) {
c := &Config{
Action: "copy",
DryRun: false,
APIKey: "superSecretAPIKey",
URL: "https://myarti.com/artifactory",
Username: "octocat",
APIKey: mock.APIKey,
URL: mock.InvalidArtifactoryServerURL,
Username: mock.Username,
}

err := c.Validate()
Expand All @@ -112,9 +114,9 @@ func TestArtifactory_Config_Validate_NoUrl(t *testing.T) {
c := &Config{
Action: "copy",
DryRun: false,
APIKey: "superSecretAPIKey",
Password: "superSecretPassword",
Username: "octocat",
APIKey: mock.APIKey,
Username: mock.Username,
Password: mock.Password,
}

err := c.Validate()
Expand All @@ -128,8 +130,8 @@ func TestArtifactory_Config_Validate_NoUsername(t *testing.T) {
c := &Config{
Action: "copy",
DryRun: false,
Password: "superSecretPassword",
URL: "https://myarti.com/artifactory",
Password: mock.Password,
URL: mock.InvalidArtifactoryServerURL,
}

err := c.Validate()
Expand All @@ -143,7 +145,7 @@ func TestArtifactory_Config_Validate_NoAuth(t *testing.T) {
c := &Config{
Action: "copy",
DryRun: false,
URL: "https://myarti.com/artifactory",
URL: mock.InvalidArtifactoryServerURL,
}

err := c.Validate()
Expand Down
13 changes: 7 additions & 6 deletions cmd/vela-artifactory/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main
import (
"fmt"

"github.com/jfrog/jfrog-client-go/artifactory"
"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"

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

// Copy represents the plugin configuration for copy information.
type Copy struct {
// enables removing source file directory hierarchy
// Flat is a flag that enables removing source file directory hierarchy
Flat bool
// enables copying sub-directories from source
// Recursive is a flag that enables copying sub-directories from source
Recursive bool
// source path to artifact(s) to copy
// Path is the source path to artifact(s) to copy
Path string
// target path to copy artifact(s) to
// Target is the path to copy artifact(s) to
Target string
}

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

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

// add copy configuration to copy parameters
p.ArtifactoryCommonParams = &utils.ArtifactoryCommonParams{
p.CommonParams = &utils.CommonParams{
Pattern: c.Path,
Recursive: c.Recursive,
Target: c.Target,
Expand Down
48 changes: 42 additions & 6 deletions cmd/vela-artifactory/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,53 @@

package main

import "testing"
import (
"net/http/httptest"
"testing"

"github.com/go-vela/vela-artifactory/cmd/vela-artifactory/mock"
)

func TestArtifactory_Copy_Exec(t *testing.T) {
// setup types
s := httptest.NewServer(mock.Handlers())

p := &Plugin{
Config: &Config{
Action: "copy",
Token: mock.Token,
APIKey: mock.APIKey,
DryRun: false,
URL: s.URL,
Username: mock.Username,
Password: mock.Password,
},
Copy: &Copy{
Flat: false,
Recursive: false,
Path: "foo/bar",
Target: "bar/foo",
},
Delete: &Delete{},
SetProp: &SetProp{},
Upload: &Upload{},
}

err := p.Exec()
if err != nil {
t.Errorf("Exec returned err %v", err)
}
}

func TestArtifactory_Copy_Exec_Error(t *testing.T) {
// setup types
config := &Config{
Action: "copy",
APIKey: "superSecretAPIKey",
APIKey: mock.APIKey,
DryRun: false,
Password: "superSecretPassword",
URL: "http://localhost:8081/artifactory",
Username: "octocat",
URL: mock.InvalidArtifactoryServerURL,
Username: mock.Username,
Password: mock.Password,
}

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

err = c.Exec(cli)
err = c.Exec(*cli)
if err == nil {
t.Errorf("Exec should have returned err")
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/vela-artifactory/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/sirupsen/logrus"

"github.com/jfrog/jfrog-client-go/artifactory"
"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
)
Expand All @@ -15,21 +16,21 @@ const deleteAction = "delete"

// Delete represents the plugin configuration for delete information.
type Delete struct {
// enables removing sub-directories for the artifact(s) in the path
// Recursive is a flag that enables removing sub-directories for the artifact(s) in the path
Recursive bool
// target path to artifact(s) to remove
// Path is the target path to artifact(s) to remove
Path string
}

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

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

// add delete configuration to delete parameters
p.ArtifactoryCommonParams = &utils.ArtifactoryCommonParams{
p.CommonParams = &utils.CommonParams{
Pattern: d.Path,
Recursive: d.Recursive,
}
Expand Down
Loading

0 comments on commit bf4c53c

Please sign in to comment.