1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/internal/pipeline/s3/s3_test.go

165 lines
4.2 KiB
Go
Raw Normal View History

2018-05-13 18:08:14 +02:00
package s3
import (
2018-05-13 20:40:14 +02:00
"io/ioutil"
"os"
"os/exec"
"path/filepath"
2018-05-13 21:41:45 +02:00
"strings"
2018-05-13 18:08:14 +02:00
"testing"
2018-05-13 20:40:14 +02:00
"time"
2018-05-13 18:08:14 +02:00
2018-05-13 21:41:45 +02:00
"github.com/apex/log"
2018-05-13 20:40:14 +02:00
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/pipeline"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
2018-05-13 18:08:14 +02:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2018-05-13 18:08:14 +02:00
)
func TestDescription(t *testing.T) {
assert.NotEmpty(t, Pipe{}.String())
}
func TestNoS3(t *testing.T) {
assert.NoError(t, Pipe{}.Run(context.New(config.Project{})))
}
2018-05-13 19:44:49 +02:00
func TestDefaultsNoS3(t *testing.T) {
var assert = assert.New(t)
var ctx = context.New(config.Project{
S3: []config.S3{
{},
2018-05-13 19:44:49 +02:00
},
})
assert.NoError(Pipe{}.Default(ctx))
assert.Equal([]config.S3{{}}, ctx.Config.S3)
2018-05-13 19:44:49 +02:00
}
func TestDefaults(t *testing.T) {
var assert = assert.New(t)
var ctx = context.New(config.Project{
S3: []config.S3{
{
Bucket: "foo",
},
},
})
assert.NoError(Pipe{}.Default(ctx))
assert.Equal([]config.S3{{
2018-05-13 19:44:49 +02:00
Bucket: "foo",
Region: "us-east-1",
Folder: "{{ .ProjectName }}/{{ .Tag }}",
2018-07-26 15:04:25 +02:00
ACL: "private",
2018-05-13 19:44:49 +02:00
}}, ctx.Config.S3)
}
2018-05-13 20:40:14 +02:00
func TestSkipPublish(t *testing.T) {
folder, err := ioutil.TempDir("", "goreleasertest")
require.NoError(t, err)
artifactPath := filepath.Join(folder, "foo.tar.gz")
require.NoError(t, ioutil.WriteFile(artifactPath, []byte("fake\ntargz"), 0744))
var ctx = context.New(config.Project{
Dist: folder,
ProjectName: "testupload",
S3: []config.S3{
{
Bucket: "test",
Endpoint: "http://fake.s3.example",
},
},
})
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx.Artifacts.Add(artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "foo.tar.gz",
Path: artifactPath,
})
ctx.SkipPublish = true
require.NoError(t, Pipe{}.Default(ctx))
err = Pipe{}.Run(ctx)
assert.True(t, pipeline.IsSkip(err))
assert.EqualError(t, err, pipeline.ErrSkipPublishEnabled.Error())
}
2018-05-13 20:40:14 +02:00
func TestUpload(t *testing.T) {
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(t, err)
tgzpath := filepath.Join(folder, "bin.tar.gz")
debpath := filepath.Join(folder, "bin.deb")
assert.NoError(t, ioutil.WriteFile(tgzpath, []byte("fake\ntargz"), 0744))
assert.NoError(t, ioutil.WriteFile(debpath, []byte("fake\ndeb"), 0744))
var ctx = context.New(config.Project{
Dist: folder,
ProjectName: "testupload",
S3: []config.S3{
{
Bucket: "test",
Endpoint: "http://localhost:9000",
2018-05-13 20:40:14 +02:00
},
},
})
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx.Artifacts.Add(artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
Path: tgzpath,
})
ctx.Artifacts.Add(artifact.Artifact{
Type: artifact.LinuxPackage,
Name: "bin.deb",
Path: debpath,
})
2018-05-13 21:32:55 +02:00
start(t)
defer stop(t)
2018-05-13 21:09:37 +02:00
setCredentials(t)
2018-05-13 20:40:14 +02:00
assert.NoError(t, Pipe{}.Default(ctx))
assert.NoError(t, Pipe{}.Run(ctx))
}
2018-05-13 21:09:37 +02:00
func setCredentials(t *testing.T) {
// this comes from the testdata/config/config.json file - not real aws keys
os.Setenv("AWS_ACCESS_KEY_ID", "WPXKJC7CZQCFPKY5727N")
os.Setenv("AWS_SECRET_ACCESS_KEY", "eHCSajxLvl94l36gIMlzZ/oW2O0rYYK+cVn5jNT2")
2018-05-13 21:09:37 +02:00
os.Setenv("AWS_REGION", "us-east-1")
}
2018-05-13 20:40:14 +02:00
func start(t *testing.T) {
dir, err := os.Getwd()
assert.NoError(t, err)
2018-05-13 21:41:45 +02:00
log.Info("wd: " + dir)
2018-05-13 20:40:14 +02:00
if out, err := exec.Command(
"docker", "run", "-d", "--rm",
"--name", "minio",
"-p", "9000:9000",
2018-05-13 20:40:14 +02:00
"-v", dir+"/testdata/data:/data",
"-v", dir+"/testdata/config:/root/.minio",
2018-06-15 15:54:50 +02:00
"minio/minio:RELEASE.2018-06-09T03-43-35Z",
2018-05-13 20:40:14 +02:00
"server", "/data",
).CombinedOutput(); err != nil {
2018-05-13 21:41:45 +02:00
log.WithError(err).Errorf("failed to start minio: %s", string(out))
2018-05-13 20:40:14 +02:00
t.FailNow()
}
2018-05-13 21:41:45 +02:00
for range time.Tick(time.Second) {
out, err := exec.Command("docker", "inspect", "--format='{{json .State.Health}}'", "minio").CombinedOutput()
if err != nil {
log.WithError(err).Errorf("failed to check minio status: %s", string(out))
t.FailNow()
}
if strings.Contains(string(out), `"Status":"healthy"`) {
log.Info("minio is healthy")
break
}
log.Info("waiting for minio to be healthy")
}
2018-05-13 20:40:14 +02:00
}
func stop(t *testing.T) {
if out, err := exec.Command("docker", "stop", "minio").CombinedOutput(); err != nil {
2018-05-13 21:41:45 +02:00
log.WithError(err).Errorf("failed to stop minio: %s", string(out))
2018-05-13 20:40:14 +02:00
t.FailNow()
}
}