2017-12-02 19:41:05 +01:00
package artifactory
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
2018-02-25 20:17:45 -03:00
"sync"
2017-12-02 19:41:05 +01:00
"testing"
2017-12-17 19:19:44 -02:00
"github.com/goreleaser/goreleaser/internal/artifact"
2021-09-18 10:21:29 -03:00
"github.com/goreleaser/goreleaser/internal/testlib"
2018-08-14 23:50:20 -03:00
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
2020-10-06 09:48:04 -03:00
"github.com/stretchr/testify/require"
2017-12-02 19:41:05 +01:00
)
var (
// mux is the HTTP request multiplexer used with the test server.
mux * http . ServeMux
// server is a test HTTP server used to provide mock API responses.
server * httptest . Server
)
func setup ( ) {
// test server
mux = http . NewServeMux ( )
server = httptest . NewServer ( mux )
}
// teardown closes the test HTTP server.
func teardown ( ) {
server . Close ( )
}
2021-04-25 14:20:49 -03:00
func requireMethodPut ( t * testing . T , r * http . Request ) {
2020-12-29 15:40:21 +00:00
t . Helper ( )
2021-04-25 14:20:49 -03:00
require . Equal ( t , http . MethodPut , r . Method )
2017-12-02 19:41:05 +01:00
}
2021-04-25 14:20:49 -03:00
func requireHeader ( t * testing . T , r * http . Request , header , want string ) {
2020-12-29 15:40:21 +00:00
t . Helper ( )
2021-04-25 14:20:49 -03:00
require . Equal ( t , want , r . Header . Get ( header ) )
2017-12-02 19:41:05 +01:00
}
2021-09-16 14:31:57 -03:00
// TODO: improve all tests below by checking wether the mocked handlers
2018-02-14 11:13:30 -02:00
// were called or not.
2017-12-09 22:25:03 +01:00
func TestRunPipe_ModeBinary ( t * testing . T ) {
2017-12-02 19:41:05 +01:00
setup ( )
defer teardown ( )
2021-04-01 09:03:11 -03:00
folder := t . TempDir ( )
dist := filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0 o755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "mybin" ) , 0 o755 ) )
binPath := filepath . Join ( dist , "mybin" , "mybin" )
2017-12-02 19:41:05 +01:00
d1 := [ ] byte ( "hello\ngo\n" )
2021-04-25 13:00:51 -03:00
require . NoError ( t , os . WriteFile ( binPath , d1 , 0 o666 ) )
2017-12-02 19:41:05 +01:00
// Dummy artifactories
mux . HandleFunc ( "/example-repo-local/mybin/darwin/amd64/mybin" , func ( w http . ResponseWriter , r * http . Request ) {
2021-04-25 14:20:49 -03:00
requireMethodPut ( t , r )
requireHeader ( t , r , "Content-Length" , "9" )
2017-12-02 19:41:05 +01:00
// Basic auth of user "deployuser" with secret "deployuser-secret"
2021-04-25 14:20:49 -03:00
requireHeader ( t , r , "Authorization" , "Basic ZGVwbG95dXNlcjpkZXBsb3l1c2VyLXNlY3JldA==" )
2017-12-02 19:41:05 +01:00
w . WriteHeader ( http . StatusCreated )
fmt . Fprint ( w , ` {
"repo" : "example-repo-local" ,
"path" : "/mybin/darwin/amd64/mybin" ,
"created" : "2017-12-02T19:30:45.436Z" ,
"createdBy" : "deployuser" ,
"downloadUri" : "http://127.0.0.1:56563/example-repo-local/mybin/darwin/amd64/mybin" ,
"mimeType" : "application/octet-stream" ,
"size" : "9" ,
"checksums" : {
"sha1" : "65d01857a69f14ade727fe1ceee0f52a264b6e57" ,
"md5" : "a55e303e7327dc871a8e2a84f30b9983" ,
"sha256" : "ead9b172aec5c24ca6c12e85a1e6fc48dd341d8fac38c5ba00a78881eabccf0e"
} ,
"originalChecksums" : {
"sha256" : "ead9b172aec5c24ca6c12e85a1e6fc48dd341d8fac38c5ba00a78881eabccf0e"
} ,
"uri" : "http://127.0.0.1:56563/example-repo-local/mybin/darwin/amd64/mybin"
} ` )
} )
mux . HandleFunc ( "/example-repo-local/mybin/linux/amd64/mybin" , func ( w http . ResponseWriter , r * http . Request ) {
2021-04-25 14:20:49 -03:00
requireMethodPut ( t , r )
requireHeader ( t , r , "Content-Length" , "9" )
2017-12-02 19:41:05 +01:00
// Basic auth of user "deployuser" with secret "deployuser-secret"
2021-04-25 14:20:49 -03:00
requireHeader ( t , r , "Authorization" , "Basic ZGVwbG95dXNlcjpkZXBsb3l1c2VyLXNlY3JldA==" )
2017-12-02 19:41:05 +01:00
w . WriteHeader ( http . StatusCreated )
fmt . Fprint ( w , ` {
"repo" : "example-repo-local" ,
"path" : "mybin/linux/amd64/mybin" ,
"created" : "2017-12-02T19:30:46.436Z" ,
"createdBy" : "deployuser" ,
"downloadUri" : "http://127.0.0.1:56563/example-repo-local/mybin/linux/amd64/mybin" ,
"mimeType" : "application/octet-stream" ,
"size" : "9" ,
"checksums" : {
"sha1" : "65d01857a69f14ade727fe1ceee0f52a264b6e57" ,
"md5" : "a55e303e7327dc871a8e2a84f30b9983" ,
"sha256" : "ead9b172aec5c24ca6c12e85a1e6fc48dd341d8fac38c5ba00a78881eabccf0e"
} ,
"originalChecksums" : {
"sha256" : "ead9b172aec5c24ca6c12e85a1e6fc48dd341d8fac38c5ba00a78881eabccf0e"
} ,
"uri" : "http://127.0.0.1:56563/example-repo-local/mybin/linux/amd64/mybin"
} ` )
} )
mux . HandleFunc ( "/production-repo-remote/mybin/darwin/amd64/mybin" , func ( w http . ResponseWriter , r * http . Request ) {
2021-04-25 14:20:49 -03:00
requireMethodPut ( t , r )
requireHeader ( t , r , "Content-Length" , "9" )
2017-12-02 19:41:05 +01:00
// Basic auth of user "productionuser" with secret "productionuser-apikey"
2021-04-25 14:20:49 -03:00
requireHeader ( t , r , "Authorization" , "Basic cHJvZHVjdGlvbnVzZXI6cHJvZHVjdGlvbnVzZXItYXBpa2V5" )
2017-12-02 19:41:05 +01:00
w . WriteHeader ( http . StatusCreated )
fmt . Fprint ( w , ` {
"repo" : "production-repo-remote" ,
"path" : "mybin/darwin/amd64/mybin" ,
"created" : "2017-12-02T19:30:46.436Z" ,
"createdBy" : "productionuser" ,
"downloadUri" : "http://127.0.0.1:56563/production-repo-remote/mybin/darwin/amd64/mybin" ,
"mimeType" : "application/octet-stream" ,
"size" : "9" ,
"checksums" : {
"sha1" : "65d01857a69f14ade727fe1ceee0f52a264b6e57" ,
"md5" : "a55e303e7327dc871a8e2a84f30b9983" ,
"sha256" : "ead9b172aec5c24ca6c12e85a1e6fc48dd341d8fac38c5ba00a78881eabccf0e"
} ,
"originalChecksums" : {
"sha256" : "ead9b172aec5c24ca6c12e85a1e6fc48dd341d8fac38c5ba00a78881eabccf0e"
} ,
"uri" : "http://127.0.0.1:56563/production-repo-remote/mybin/darwin/amd64/mybin"
} ` )
} )
mux . HandleFunc ( "/production-repo-remote/mybin/linux/amd64/mybin" , func ( w http . ResponseWriter , r * http . Request ) {
2021-04-25 14:20:49 -03:00
requireMethodPut ( t , r )
requireHeader ( t , r , "Content-Length" , "9" )
2017-12-02 19:41:05 +01:00
// Basic auth of user "productionuser" with secret "productionuser-apikey"
2021-04-25 14:20:49 -03:00
requireHeader ( t , r , "Authorization" , "Basic cHJvZHVjdGlvbnVzZXI6cHJvZHVjdGlvbnVzZXItYXBpa2V5" )
2017-12-02 19:41:05 +01:00
w . WriteHeader ( http . StatusCreated )
fmt . Fprint ( w , ` {
"repo" : "production-repo-remote" ,
"path" : "mybin/linux/amd64/mybin" ,
"created" : "2017-12-02T19:30:46.436Z" ,
"createdBy" : "productionuser" ,
"downloadUri" : "http://127.0.0.1:56563/production-repo-remote/mybin/linux/amd64/mybin" ,
"mimeType" : "application/octet-stream" ,
"size" : "9" ,
"checksums" : {
"sha1" : "65d01857a69f14ade727fe1ceee0f52a264b6e57" ,
"md5" : "a55e303e7327dc871a8e2a84f30b9983" ,
"sha256" : "ead9b172aec5c24ca6c12e85a1e6fc48dd341d8fac38c5ba00a78881eabccf0e"
} ,
"originalChecksums" : {
"sha256" : "ead9b172aec5c24ca6c12e85a1e6fc48dd341d8fac38c5ba00a78881eabccf0e"
} ,
"uri" : "http://127.0.0.1:56563/production-repo-remote/mybin/linux/amd64/mybin"
} ` )
} )
2021-04-01 09:03:11 -03:00
ctx := context . New ( config . Project {
2017-12-17 22:46:49 -02:00
ProjectName : "mybin" ,
Dist : dist ,
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload {
2017-12-17 22:46:49 -02:00
{
Name : "production-us" ,
Mode : "binary" ,
Target : fmt . Sprintf ( "%s/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" , server . URL ) ,
Username : "deployuser" ,
} ,
{
Name : "production-eu" ,
Mode : "binary" ,
Target : fmt . Sprintf ( "%s/production-repo-remote/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" , server . URL ) ,
Username : "productionuser" ,
2017-12-02 19:41:05 +01:00
} ,
} ,
2019-12-29 15:02:15 -03:00
Archives : [ ] config . Archive {
{ } ,
} ,
2017-12-17 22:46:49 -02:00
} )
ctx . Env = map [ string ] string {
"ARTIFACTORY_PRODUCTION-US_SECRET" : "deployuser-secret" ,
"ARTIFACTORY_PRODUCTION-EU_SECRET" : "productionuser-apikey" ,
2017-12-02 19:41:05 +01:00
}
2017-12-17 21:57:24 -02:00
for _ , goos := range [ ] string { "linux" , "darwin" } {
2019-08-12 17:44:48 -03:00
ctx . Artifacts . Add ( & artifact . Artifact {
2017-12-17 21:57:24 -02:00
Name : "mybin" ,
Path : binPath ,
Goarch : "amd64" ,
Goos : goos ,
Type : artifact . UploadableBinary ,
} )
2017-12-02 19:41:05 +01:00
}
2017-12-09 22:25:03 +01:00
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
require . NoError ( t , Pipe { } . Publish ( ctx ) )
2017-12-09 22:25:03 +01:00
}
func TestRunPipe_ModeArchive ( t * testing . T ) {
setup ( )
defer teardown ( )
2021-04-01 09:03:11 -03:00
folder := t . TempDir ( )
2017-12-09 22:25:03 +01:00
tarfile , err := os . Create ( filepath . Join ( folder , "bin.tar.gz" ) )
2020-10-06 09:48:04 -03:00
require . NoError ( t , err )
2021-04-25 11:34:40 -03:00
require . NoError ( t , tarfile . Close ( ) )
2017-12-09 22:25:03 +01:00
debfile , err := os . Create ( filepath . Join ( folder , "bin.deb" ) )
2020-10-06 09:48:04 -03:00
require . NoError ( t , err )
2021-04-25 11:34:40 -03:00
require . NoError ( t , debfile . Close ( ) )
2017-12-09 22:25:03 +01:00
2021-04-01 09:03:11 -03:00
ctx := context . New ( config . Project {
2017-12-17 22:46:49 -02:00
ProjectName : "goreleaser" ,
Dist : folder ,
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload {
2017-12-17 22:46:49 -02:00
{
Name : "production" ,
Mode : "archive" ,
Target : fmt . Sprintf ( "%s/example-repo-local/{{ .ProjectName }}/{{ .Version }}/" , server . URL ) ,
Username : "deployuser" ,
2017-12-09 22:25:03 +01:00
} ,
} ,
2019-12-29 15:02:15 -03:00
Archives : [ ] config . Archive {
{ } ,
} ,
2017-12-17 22:46:49 -02:00
} )
ctx . Env = map [ string ] string {
"ARTIFACTORY_PRODUCTION_SECRET" : "deployuser-secret" ,
2017-12-09 22:25:03 +01:00
}
2017-12-17 22:46:49 -02:00
ctx . Version = "1.0.0"
2019-08-12 17:44:48 -03:00
ctx . Artifacts . Add ( & artifact . Artifact {
2017-12-17 19:19:44 -02:00
Type : artifact . UploadableArchive ,
Name : "bin.tar.gz" ,
Path : tarfile . Name ( ) ,
} )
2019-08-12 17:44:48 -03:00
ctx . Artifacts . Add ( & artifact . Artifact {
2017-12-17 19:19:44 -02:00
Type : artifact . LinuxPackage ,
Name : "bin.deb" ,
Path : debfile . Name ( ) ,
} )
2017-12-09 22:25:03 +01:00
2018-02-25 20:17:45 -03:00
var uploads sync . Map
2018-02-14 11:06:15 -02:00
2017-12-09 22:25:03 +01:00
// Dummy artifactories
mux . HandleFunc ( "/example-repo-local/goreleaser/1.0.0/bin.tar.gz" , func ( w http . ResponseWriter , r * http . Request ) {
2021-04-25 14:20:49 -03:00
requireMethodPut ( t , r )
2017-12-09 22:25:03 +01:00
// Basic auth of user "deployuser" with secret "deployuser-secret"
2021-04-25 14:20:49 -03:00
requireHeader ( t , r , "Authorization" , "Basic ZGVwbG95dXNlcjpkZXBsb3l1c2VyLXNlY3JldA==" )
2017-12-09 22:25:03 +01:00
w . WriteHeader ( http . StatusCreated )
fmt . Fprint ( w , ` {
"repo" : "example-repo-local" ,
"path" : "/goreleaser/bin.tar.gz" ,
"created" : "2017-12-02T19:30:45.436Z" ,
"createdBy" : "deployuser" ,
"downloadUri" : "http://127.0.0.1:56563/example-repo-local/goreleaser/bin.tar.gz" ,
"mimeType" : "application/octet-stream" ,
"size" : "9" ,
"checksums" : {
"sha1" : "65d01857a69f14ade727fe1ceee0f52a264b6e57" ,
"md5" : "a55e303e7327dc871a8e2a84f30b9983" ,
"sha256" : "ead9b172aec5c24ca6c12e85a1e6fc48dd341d8fac38c5ba00a78881eabccf0e"
} ,
"originalChecksums" : {
"sha256" : "ead9b172aec5c24ca6c12e85a1e6fc48dd341d8fac38c5ba00a78881eabccf0e"
} ,
"uri" : "http://127.0.0.1:56563/example-repo-local/goreleaser/bin.tar.gz"
} ` )
2018-02-25 20:17:45 -03:00
uploads . Store ( "targz" , true )
2017-12-09 22:25:03 +01:00
} )
mux . HandleFunc ( "/example-repo-local/goreleaser/1.0.0/bin.deb" , func ( w http . ResponseWriter , r * http . Request ) {
2021-04-25 14:20:49 -03:00
requireMethodPut ( t , r )
2017-12-09 22:25:03 +01:00
// Basic auth of user "deployuser" with secret "deployuser-secret"
2021-04-25 14:20:49 -03:00
requireHeader ( t , r , "Authorization" , "Basic ZGVwbG95dXNlcjpkZXBsb3l1c2VyLXNlY3JldA==" )
2017-12-09 22:25:03 +01:00
w . WriteHeader ( http . StatusCreated )
fmt . Fprint ( w , ` {
"repo" : "example-repo-local" ,
"path" : "goreleaser/bin.deb" ,
"created" : "2017-12-02T19:30:46.436Z" ,
"createdBy" : "deployuser" ,
"downloadUri" : "http://127.0.0.1:56563/example-repo-local/goreleaser/bin.deb" ,
"mimeType" : "application/octet-stream" ,
"size" : "9" ,
"checksums" : {
"sha1" : "65d01857a69f14ade727fe1ceee0f52a264b6e57" ,
"md5" : "a55e303e7327dc871a8e2a84f30b9983" ,
"sha256" : "ead9b172aec5c24ca6c12e85a1e6fc48dd341d8fac38c5ba00a78881eabccf0e"
} ,
"originalChecksums" : {
"sha256" : "ead9b172aec5c24ca6c12e85a1e6fc48dd341d8fac38c5ba00a78881eabccf0e"
} ,
"uri" : "http://127.0.0.1:56563/example-repo-local/goreleaser/bin.deb"
} ` )
2018-02-25 20:17:45 -03:00
uploads . Store ( "deb" , true )
2017-12-09 22:25:03 +01:00
} )
2017-12-02 19:41:05 +01:00
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
require . NoError ( t , Pipe { } . Publish ( ctx ) )
2018-02-25 20:17:45 -03:00
_ , ok := uploads . Load ( "targz" )
2020-10-06 09:48:04 -03:00
require . True ( t , ok , "tar.gz file was not uploaded" )
2018-02-25 20:17:45 -03:00
_ , ok = uploads . Load ( "deb" )
2020-10-06 09:48:04 -03:00
require . True ( t , ok , "deb file was not uploaded" )
2017-12-02 19:41:05 +01:00
}
2017-12-17 22:54:24 -02:00
func TestRunPipe_ArtifactoryDown ( t * testing . T ) {
2021-04-01 09:03:11 -03:00
folder := t . TempDir ( )
2017-12-17 22:54:24 -02:00
tarfile , err := os . Create ( filepath . Join ( folder , "bin.tar.gz" ) )
2020-10-06 09:48:04 -03:00
require . NoError ( t , err )
2021-04-25 11:34:40 -03:00
require . NoError ( t , tarfile . Close ( ) )
2017-12-17 22:54:24 -02:00
2021-04-01 09:03:11 -03:00
ctx := context . New ( config . Project {
2017-12-17 22:54:24 -02:00
ProjectName : "goreleaser" ,
Dist : folder ,
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload {
2017-12-17 22:54:24 -02:00
{
Name : "production" ,
Mode : "archive" ,
Target : "http://localhost:1234/example-repo-local/{{ .ProjectName }}/{{ .Version }}/" ,
Username : "deployuser" ,
} ,
} ,
} )
ctx . Version = "2.0.0"
ctx . Env = map [ string ] string {
"ARTIFACTORY_PRODUCTION_SECRET" : "deployuser-secret" ,
}
2019-08-12 17:44:48 -03:00
ctx . Artifacts . Add ( & artifact . Artifact {
2017-12-17 22:54:24 -02:00
Type : artifact . UploadableArchive ,
Name : "bin.tar.gz" ,
Path : tarfile . Name ( ) ,
} )
2019-11-18 10:34:17 -03:00
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
2018-10-16 22:29:02 -03:00
err = Pipe { } . Publish ( ctx )
2020-10-06 09:48:04 -03:00
require . Error ( t , err )
require . Contains ( t , err . Error ( ) , "connection refused" )
2017-12-17 22:54:24 -02:00
}
2017-12-09 23:34:25 +01:00
func TestRunPipe_TargetTemplateError ( t * testing . T ) {
2021-04-01 09:03:11 -03:00
folder := t . TempDir ( )
dist := filepath . Join ( folder , "dist" )
binPath := filepath . Join ( dist , "mybin" , "mybin" )
2017-12-09 23:34:25 +01:00
2021-04-01 09:03:11 -03:00
ctx := context . New ( config . Project {
2017-12-17 22:46:49 -02:00
ProjectName : "mybin" ,
Dist : dist ,
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload {
2017-12-17 22:46:49 -02:00
{
Name : "production" ,
Mode : "binary" ,
// This template is not correct and should fail
2022-08-06 18:44:23 -03:00
Target : "http://storage.company.com/example-repo-local/{{.Name}" ,
2017-12-17 22:46:49 -02:00
Username : "deployuser" ,
2017-12-09 23:34:25 +01:00
} ,
} ,
2019-12-29 15:02:15 -03:00
Archives : [ ] config . Archive {
{ } ,
} ,
2017-12-17 22:46:49 -02:00
} )
ctx . Env = map [ string ] string {
"ARTIFACTORY_PRODUCTION_SECRET" : "deployuser-secret" ,
2017-12-09 23:34:25 +01:00
}
2019-08-12 17:44:48 -03:00
ctx . Artifacts . Add ( & artifact . Artifact {
2017-12-17 19:19:44 -02:00
Name : "mybin" ,
Path : binPath ,
Goarch : "amd64" ,
Goos : "darwin" ,
Type : artifact . UploadableBinary ,
} )
2017-12-09 23:34:25 +01:00
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
2022-08-06 18:44:23 -03:00
testlib . RequireTemplateError ( t , Pipe { } . Publish ( ctx ) )
2017-12-09 23:34:25 +01:00
}
2017-12-02 21:40:14 +01:00
func TestRunPipe_BadCredentials ( t * testing . T ) {
setup ( )
defer teardown ( )
2021-04-01 09:03:11 -03:00
folder := t . TempDir ( )
dist := filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0 o755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "mybin" ) , 0 o755 ) )
binPath := filepath . Join ( dist , "mybin" , "mybin" )
2017-12-02 21:40:14 +01:00
d1 := [ ] byte ( "hello\ngo\n" )
2021-04-25 13:00:51 -03:00
require . NoError ( t , os . WriteFile ( binPath , d1 , 0 o666 ) )
2017-12-02 21:40:14 +01:00
// Dummy artifactories
mux . HandleFunc ( "/example-repo-local/mybin/darwin/amd64/mybin" , func ( w http . ResponseWriter , r * http . Request ) {
2021-04-25 14:20:49 -03:00
requireMethodPut ( t , r )
requireHeader ( t , r , "Content-Length" , "9" )
2017-12-02 21:40:14 +01:00
// Basic auth of user "deployuser" with secret "deployuser-secret"
2021-04-25 14:20:49 -03:00
requireHeader ( t , r , "Authorization" , "Basic ZGVwbG95dXNlcjpkZXBsb3l1c2VyLXNlY3JldA==" )
2017-12-02 21:40:14 +01:00
w . WriteHeader ( http . StatusUnauthorized )
fmt . Fprint ( w , ` {
"errors" : [ {
"status" : 401 ,
"message" : "Bad credentials"
} ]
} ` )
} )
2021-04-01 09:03:11 -03:00
ctx := context . New ( config . Project {
2017-12-17 22:46:49 -02:00
ProjectName : "mybin" ,
Dist : dist ,
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload {
2017-12-17 22:46:49 -02:00
{
Name : "production" ,
Mode : "binary" ,
Target : fmt . Sprintf ( "%s/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" , server . URL ) ,
Username : "deployuser" ,
2017-12-02 21:40:14 +01:00
} ,
} ,
2019-12-29 15:02:15 -03:00
Archives : [ ] config . Archive {
{ } ,
} ,
2017-12-17 22:46:49 -02:00
} )
ctx . Env = map [ string ] string {
"ARTIFACTORY_PRODUCTION_SECRET" : "deployuser-secret" ,
2017-12-02 21:40:14 +01:00
}
2019-08-12 17:44:48 -03:00
ctx . Artifacts . Add ( & artifact . Artifact {
2017-12-17 19:19:44 -02:00
Name : "mybin" ,
Path : binPath ,
Goarch : "amd64" ,
Goos : "darwin" ,
Type : artifact . UploadableBinary ,
} )
2017-12-09 22:25:03 +01:00
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
2021-04-01 09:03:11 -03:00
err := Pipe { } . Publish ( ctx )
2020-10-06 09:48:04 -03:00
require . Error ( t , err )
require . Contains ( t , err . Error ( ) , "Bad credentials" )
2017-12-09 23:34:25 +01:00
}
func TestRunPipe_UnparsableErrorResponse ( t * testing . T ) {
setup ( )
defer teardown ( )
2021-04-01 09:03:11 -03:00
folder := t . TempDir ( )
dist := filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0 o755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "mybin" ) , 0 o755 ) )
binPath := filepath . Join ( dist , "mybin" , "mybin" )
2017-12-09 23:34:25 +01:00
d1 := [ ] byte ( "hello\ngo\n" )
2021-04-25 13:00:51 -03:00
require . NoError ( t , os . WriteFile ( binPath , d1 , 0 o666 ) )
2017-12-09 23:34:25 +01:00
// Dummy artifactories
mux . HandleFunc ( "/example-repo-local/mybin/darwin/amd64/mybin" , func ( w http . ResponseWriter , r * http . Request ) {
2021-04-25 14:20:49 -03:00
requireMethodPut ( t , r )
requireHeader ( t , r , "Content-Length" , "9" )
2017-12-09 23:34:25 +01:00
// Basic auth of user "deployuser" with secret "deployuser-secret"
2021-04-25 14:20:49 -03:00
requireHeader ( t , r , "Authorization" , "Basic ZGVwbG95dXNlcjpkZXBsb3l1c2VyLXNlY3JldA==" )
2017-12-09 23:34:25 +01:00
w . WriteHeader ( http . StatusUnauthorized )
2022-11-12 00:49:08 -03:00
fmt . Fprint ( w , ` <body><h1>error</h1></body> ` )
2017-12-09 23:34:25 +01:00
} )
2021-04-01 09:03:11 -03:00
ctx := context . New ( config . Project {
2017-12-17 22:46:49 -02:00
ProjectName : "mybin" ,
Dist : dist ,
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload {
2017-12-17 22:46:49 -02:00
{
Name : "production" ,
Mode : "binary" ,
Target : fmt . Sprintf ( "%s/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" , server . URL ) ,
Username : "deployuser" ,
2017-12-09 23:34:25 +01:00
} ,
} ,
2019-12-29 15:02:15 -03:00
Archives : [ ] config . Archive {
{ } ,
} ,
2017-12-17 22:46:49 -02:00
} )
ctx . Env = map [ string ] string {
"ARTIFACTORY_PRODUCTION_SECRET" : "deployuser-secret" ,
2017-12-09 23:34:25 +01:00
}
2019-08-12 17:44:48 -03:00
ctx . Artifacts . Add ( & artifact . Artifact {
2017-12-17 19:19:44 -02:00
Name : "mybin" ,
Path : binPath ,
Goarch : "amd64" ,
Goos : "darwin" ,
Type : artifact . UploadableBinary ,
} )
2017-12-09 23:34:25 +01:00
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
2022-11-12 00:49:08 -03:00
require . EqualError ( t , Pipe { } . Publish ( ctx ) , ` artifactory: upload failed: unexpected error: invalid character '<' looking for beginning of value: <body><h1>error</h1></body> ` )
2017-12-09 22:25:03 +01:00
}
2017-12-17 21:57:24 -02:00
func TestRunPipe_FileNotFound ( t * testing . T ) {
2021-04-01 09:03:11 -03:00
ctx := context . New ( config . Project {
2017-12-17 22:46:49 -02:00
ProjectName : "mybin" ,
Dist : "archivetest/dist" ,
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload {
2017-12-17 22:46:49 -02:00
{
Name : "production" ,
Mode : "binary" ,
Target : "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" ,
Username : "deployuser" ,
2017-12-02 22:03:01 +01:00
} ,
} ,
2019-12-29 15:02:15 -03:00
Archives : [ ] config . Archive {
{ } ,
} ,
2017-12-17 22:46:49 -02:00
} )
ctx . Env = map [ string ] string {
"ARTIFACTORY_PRODUCTION_SECRET" : "deployuser-secret" ,
2017-12-02 22:03:01 +01:00
}
2019-08-12 17:44:48 -03:00
ctx . Artifacts . Add ( & artifact . Artifact {
2017-12-17 19:19:44 -02:00
Name : "mybin" ,
Path : "archivetest/dist/mybin/mybin" ,
Goarch : "amd64" ,
Goos : "darwin" ,
2017-12-17 21:57:24 -02:00
Type : artifact . UploadableBinary ,
2017-12-17 19:19:44 -02:00
} )
2017-12-02 22:03:01 +01:00
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
require . EqualError ( t , Pipe { } . Publish ( ctx ) , ` open archivetest/dist/mybin/mybin: no such file or directory ` )
2017-12-02 22:03:01 +01:00
}
func TestRunPipe_UnparsableTarget ( t * testing . T ) {
2021-04-01 09:03:11 -03:00
folder := t . TempDir ( )
dist := filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0 o755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "mybin" ) , 0 o755 ) )
binPath := filepath . Join ( dist , "mybin" , "mybin" )
2017-12-02 22:03:01 +01:00
d1 := [ ] byte ( "hello\ngo\n" )
2021-04-25 13:00:51 -03:00
require . NoError ( t , os . WriteFile ( binPath , d1 , 0 o666 ) )
2017-12-02 22:03:01 +01:00
2021-04-01 09:03:11 -03:00
ctx := context . New ( config . Project {
2017-12-17 22:46:49 -02:00
ProjectName : "mybin" ,
Dist : dist ,
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload {
2017-12-17 22:46:49 -02:00
{
Name : "production" ,
Mode : "binary" ,
Target : "://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" ,
Username : "deployuser" ,
2017-12-02 22:03:01 +01:00
} ,
} ,
2019-12-29 15:02:15 -03:00
Archives : [ ] config . Archive {
{ } ,
} ,
2017-12-17 22:46:49 -02:00
} )
ctx . Env = map [ string ] string {
"ARTIFACTORY_PRODUCTION_SECRET" : "deployuser-secret" ,
2017-12-02 22:03:01 +01:00
}
2019-08-12 17:44:48 -03:00
ctx . Artifacts . Add ( & artifact . Artifact {
2017-12-17 19:19:44 -02:00
Name : "mybin" ,
Path : binPath ,
Goarch : "amd64" ,
Goos : "darwin" ,
2017-12-17 21:57:24 -02:00
Type : artifact . UploadableBinary ,
2017-12-17 19:19:44 -02:00
} )
2017-12-02 22:03:01 +01:00
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
require . EqualError ( t , Pipe { } . Publish ( ctx ) , ` artifactory: upload failed: parse "://artifacts.company.com/example-repo-local/mybin/darwin/amd64/mybin": missing protocol scheme ` )
2017-12-02 22:03:01 +01:00
}
func TestRunPipe_DirUpload ( t * testing . T ) {
2021-04-01 09:03:11 -03:00
folder := t . TempDir ( )
dist := filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0 o755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "mybin" ) , 0 o755 ) )
binPath := filepath . Join ( dist , "mybin" )
2017-12-09 18:17:37 +01:00
2021-04-01 09:03:11 -03:00
ctx := context . New ( config . Project {
2017-12-17 22:46:49 -02:00
ProjectName : "mybin" ,
Dist : dist ,
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload {
2017-12-17 22:46:49 -02:00
{
Name : "production" ,
Mode : "binary" ,
Target : "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" ,
Username : "deployuser" ,
2017-12-02 22:03:01 +01:00
} ,
} ,
2019-12-29 15:02:15 -03:00
Archives : [ ] config . Archive {
{ } ,
} ,
2017-12-17 22:46:49 -02:00
} )
ctx . Env = map [ string ] string {
"ARTIFACTORY_PRODUCTION_SECRET" : "deployuser-secret" ,
2017-12-02 22:03:01 +01:00
}
2019-08-12 17:44:48 -03:00
ctx . Artifacts . Add ( & artifact . Artifact {
2017-12-17 19:19:44 -02:00
Name : "mybin" ,
2017-12-17 21:57:24 -02:00
Path : filepath . Dir ( binPath ) ,
2017-12-17 19:19:44 -02:00
Goarch : "amd64" ,
Goos : "darwin" ,
2017-12-17 21:57:24 -02:00
Type : artifact . UploadableBinary ,
2017-12-17 19:19:44 -02:00
} )
2017-12-02 22:03:01 +01:00
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
require . EqualError ( t , Pipe { } . Publish ( ctx ) , ` artifactory: upload failed: the asset to upload can't be a directory ` )
2017-12-02 22:03:01 +01:00
}
2017-12-02 19:41:05 +01:00
func TestDescription ( t * testing . T ) {
2020-10-06 09:48:04 -03:00
require . NotEmpty ( t , Pipe { } . String ( ) )
2017-12-02 19:41:05 +01:00
}
2017-12-09 22:25:03 +01:00
func TestArtifactoriesWithoutTarget ( t * testing . T ) {
2021-04-01 09:03:11 -03:00
ctx := & context . Context {
2017-12-10 14:41:27 +01:00
Env : map [ string ] string {
"ARTIFACTORY_PRODUCTION_SECRET" : "deployuser-secret" ,
} ,
Config : config . Project {
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload {
2017-12-10 14:41:27 +01:00
{
Name : "production" ,
Username : "deployuser" ,
} ,
2017-12-02 19:41:05 +01:00
} ,
} ,
2017-12-10 14:41:27 +01:00
}
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
2021-09-18 10:21:29 -03:00
testlib . AssertSkipped ( t , Pipe { } . Publish ( ctx ) )
2017-12-02 19:41:05 +01:00
}
2017-12-09 22:25:03 +01:00
func TestArtifactoriesWithoutUsername ( t * testing . T ) {
2021-04-01 09:03:11 -03:00
ctx := & context . Context {
2017-12-10 14:41:27 +01:00
Env : map [ string ] string {
"ARTIFACTORY_PRODUCTION_SECRET" : "deployuser-secret" ,
} ,
Config : config . Project {
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload {
2017-12-10 14:41:27 +01:00
{
Name : "production" ,
Target : "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" ,
} ,
2017-12-02 19:41:05 +01:00
} ,
} ,
2017-12-10 14:41:27 +01:00
}
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
2021-09-18 10:21:29 -03:00
testlib . AssertSkipped ( t , Pipe { } . Publish ( ctx ) )
2017-12-02 19:41:05 +01:00
}
2017-12-09 22:25:03 +01:00
func TestArtifactoriesWithoutName ( t * testing . T ) {
2021-04-01 09:03:11 -03:00
ctx := context . New ( config . Project {
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload {
2017-12-09 18:17:37 +01:00
{
Username : "deployuser" ,
Target : "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" ,
} ,
} ,
2019-11-18 10:34:17 -03:00
} )
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
2021-09-18 10:21:29 -03:00
testlib . AssertSkipped ( t , Pipe { } . Publish ( ctx ) )
2017-12-09 18:17:37 +01:00
}
2017-12-09 22:25:03 +01:00
func TestArtifactoriesWithoutSecret ( t * testing . T ) {
2021-04-01 09:03:11 -03:00
ctx := context . New ( config . Project {
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload {
2017-12-09 22:25:03 +01:00
{
Name : "production" ,
Target : "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" ,
Username : "deployuser" ,
} ,
} ,
2019-11-18 10:34:17 -03:00
} )
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
2021-09-18 10:21:29 -03:00
testlib . AssertSkipped ( t , Pipe { } . Publish ( ctx ) )
2017-12-09 22:25:03 +01:00
}
func TestArtifactoriesWithInvalidMode ( t * testing . T ) {
2021-04-01 09:03:11 -03:00
ctx := & context . Context {
2017-12-10 14:41:27 +01:00
Env : map [ string ] string {
"ARTIFACTORY_PRODUCTION_SECRET" : "deployuser-secret" ,
} ,
2017-12-09 22:53:44 +01:00
Config : config . Project {
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload {
2017-12-09 22:53:44 +01:00
{
Name : "production" ,
Mode : "does-not-exists" ,
Target : "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" ,
Username : "deployuser" ,
} ,
2017-12-02 19:41:05 +01:00
} ,
} ,
2017-12-09 22:53:44 +01:00
}
2019-11-18 10:34:17 -03:00
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
require . Error ( t , Pipe { } . Publish ( ctx ) )
2017-12-02 19:41:05 +01:00
}
2017-12-09 22:25:03 +01:00
func TestDefault ( t * testing . T ) {
2021-04-01 09:03:11 -03:00
ctx := & context . Context {
2017-12-09 22:25:03 +01:00
Config : config . Project {
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload {
2017-12-09 22:25:03 +01:00
{
Name : "production" ,
Target : "http://artifacts.company.com/example-repo-local/{{ .ProjectName }}/{{ .Os }}/{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" ,
Username : "deployuser" ,
} ,
} ,
} ,
}
2019-11-18 10:34:17 -03:00
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
require . Len ( t , ctx . Config . Artifactories , 1 )
2021-04-01 09:03:11 -03:00
artifactory := ctx . Config . Artifactories [ 0 ]
2020-10-06 09:48:04 -03:00
require . Equal ( t , "archive" , artifactory . Mode )
2017-12-09 22:25:03 +01:00
}
func TestDefaultNoArtifactories ( t * testing . T ) {
2021-04-01 09:03:11 -03:00
ctx := & context . Context {
2017-12-09 22:25:03 +01:00
Config : config . Project {
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload { } ,
2017-12-09 22:25:03 +01:00
} ,
}
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
require . Empty ( t , ctx . Config . Artifactories )
2017-12-09 22:25:03 +01:00
}
func TestDefaultSet ( t * testing . T ) {
2021-04-01 09:03:11 -03:00
ctx := & context . Context {
2017-12-09 22:25:03 +01:00
Config : config . Project {
2019-11-18 10:34:17 -03:00
Artifactories : [ ] config . Upload {
2017-12-09 22:25:03 +01:00
{
2021-04-01 09:03:11 -03:00
Mode : "custom" ,
ChecksumHeader : "foo" ,
2017-12-09 22:25:03 +01:00
} ,
} ,
} ,
}
2020-10-06 09:48:04 -03:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
require . Len ( t , ctx . Config . Artifactories , 1 )
2021-04-01 09:03:11 -03:00
artifactory := ctx . Config . Artifactories [ 0 ]
2020-10-06 09:48:04 -03:00
require . Equal ( t , "custom" , artifactory . Mode )
2021-04-01 09:03:11 -03:00
require . Equal ( t , "foo" , artifactory . ChecksumHeader )
2017-12-09 22:25:03 +01:00
}
2021-09-18 10:21:29 -03:00
func TestSkip ( t * testing . T ) {
t . Run ( "skip" , func ( t * testing . T ) {
require . True ( t , Pipe { } . Skip ( context . New ( config . Project { } ) ) )
} )
t . Run ( "dont skip" , func ( t * testing . T ) {
ctx := context . New ( config . Project {
Artifactories : [ ] config . Upload { { } } ,
} )
require . False ( t , Pipe { } . Skip ( ctx ) )
} )
}