1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-24 04:16:27 +02:00
goreleaser/pipeline/s3/s3_test.go

134 lines
3.2 KiB
Go
Raw Normal View History

2018-05-13 13:08:14 -03:00
package s3
import (
2018-05-13 15:40:14 -03:00
"io/ioutil"
"os"
"os/exec"
"path/filepath"
2018-05-13 16:41:45 -03:00
"strings"
2018-05-13 13:08:14 -03:00
"testing"
2018-05-13 15:40:14 -03:00
"time"
2018-05-13 13:08:14 -03:00
2018-05-13 16:41:45 -03:00
"github.com/apex/log"
2018-05-13 13:08:14 -03:00
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
2018-05-13 15:40:14 -03:00
"github.com/goreleaser/goreleaser/internal/artifact"
2018-05-13 13:08:14 -03:00
"github.com/stretchr/testify/assert"
)
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 14:44:49 -03:00
func TestDefaultsNoS3(t *testing.T) {
var assert = assert.New(t)
var ctx = context.New(config.Project{
S3: []config.S3{
{},
2018-05-13 14:44:49 -03:00
},
})
assert.NoError(Pipe{}.Default(ctx))
assert.Equal([]config.S3{{}}, ctx.Config.S3)
2018-05-13 14:44:49 -03: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 14:44:49 -03:00
Bucket: "foo",
Region: "us-east-1",
Folder: "{{ .ProjectName }}/{{ .Tag }}",
}}, ctx.Config.S3)
}
2018-05-13 15:40:14 -03: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 15:40:14 -03: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 16:32:55 -03:00
start(t)
defer stop(t)
2018-05-13 16:09:37 -03:00
setCredentials(t)
2018-05-13 15:40:14 -03:00
assert.NoError(t, Pipe{}.Default(ctx))
assert.NoError(t, Pipe{}.Run(ctx))
}
2018-05-13 16:09:37 -03: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", "IWA0WZQ1QJ2I8I1ALW64")
os.Setenv("AWS_SECRET_ACCESS_KEY", "zcK4QQegvYwVGJaBm2E6k20WRLIQZqHOKOPP2npT")
os.Setenv("AWS_REGION", "us-east-1")
}
2018-05-13 15:40:14 -03:00
func start(t *testing.T) {
dir, err := os.Getwd()
assert.NoError(t, err)
2018-05-13 16:41:45 -03:00
log.Info("wd: " + dir)
2018-05-13 15:40:14 -03:00
if out, err := exec.Command(
"docker", "run", "-d", "--rm",
"--name", "minio",
"-p", "9000:9000",
2018-05-13 15:40:14 -03:00
"-v", dir+"/testdata/data:/data",
"-v", dir+"/testdata/config:/root/.minio",
"minio/minio",
"server", "/data",
).CombinedOutput(); err != nil {
2018-05-13 16:41:45 -03:00
log.WithError(err).Errorf("failed to start minio: %s", string(out))
2018-05-13 15:40:14 -03:00
t.FailNow()
}
2018-05-13 16:41:45 -03: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 15:40:14 -03:00
}
func stop(t *testing.T) {
if out, err := exec.Command("docker", "stop", "minio").CombinedOutput(); err != nil {
2018-05-13 16:41:45 -03:00
log.WithError(err).Errorf("failed to stop minio: %s", string(out))
2018-05-13 15:40:14 -03:00
t.FailNow()
}
}