Repository: goreleaser/goreleaser
Pull request #1021 (2019-05-22 03:12:10)

test: improved s3 pipe tests
- random port for each minio instance
- different name for each minio instance
- no more shared dirs in the repo
- updated to latest minio version
- improved test time by overriding healcheck interval that defaults to 1m to 1s

codecov-io: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1021?src=pr&el=h1) Report
> Merging [#1021](https://codecov.io/gh/goreleaser/goreleaser/pull/1021?src=pr&el=desc) into [master](https://codecov.io/gh/goreleaser/goreleaser/commit/d9e89f69c72cec19014fdcee327eedc92950b92a?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/1021/graphs/tree.svg?width=650&token=3bJvv6b7hN&height=150&src=pr)](https://codecov.io/gh/goreleaser/goreleaser/pull/1021?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1021      +/-   ##
==========================================
+ Coverage   88.46%   88.46%   +<.01%     
==========================================
  Files          53       53              
  Lines        2591     2593       +2     
==========================================
+ Hits         2292     2294       +2     
  Misses        236      236              
  Partials       63       63
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/1021?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [internal/pipe/s3/s3.go](https://codecov.io/gh/goreleaser/goreleaser/pull/1021/diff?src=pr&el=tree#diff-aW50ZXJuYWwvcGlwZS9zMy9zMy5nbw==) | `94.36% <100%> (+0.16%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1021?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1021?src=pr&el=footer). Last update [d9e89f6...36e8eaf](https://codecov.io/gh/goreleaser/goreleaser/pull/1021?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/internal/pipe/s3/s3.go b/internal/pipe/s3/s3.go
index a4d02cdf..37fd8a02 100644
--- a/internal/pipe/s3/s3.go
+++ b/internal/pipe/s3/s3.go
@@ -59,7 +59,7 @@ func (Pipe) Publish(ctx *context.Context) error {
 	return g.Wait()
 }
 
-func upload(ctx *context.Context, conf config.S3) error {
+func newS3Svc(conf config.S3) *s3.S3 {
 	builder := newSessionBuilder()
 	builder.Profile(conf.Profile)
 	if conf.Endpoint != "" {
@@ -68,9 +68,13 @@ func upload(ctx *context.Context, conf config.S3) error {
 	}
 	sess := builder.Build()
 
-	svc := s3.New(sess, &aws.Config{
+	return s3.New(sess, &aws.Config{
 		Region: aws.String(conf.Region),
 	})
+}
+
+func upload(ctx *context.Context, conf config.S3) error {
+	var svc = newS3Svc(conf)
 
 	template := tmpl.New(ctx)
 	bucket, err := template.Apply(conf.Bucket)


Test patch:
diff --git a/internal/pipe/s3/s3_test.go b/internal/pipe/s3/s3_test.go
index 7c5c65a0..2d4c2c8f 100644
--- a/internal/pipe/s3/s3_test.go
+++ b/internal/pipe/s3/s3_test.go
@@ -2,6 +2,7 @@ package s3
 
 import (
 	"io/ioutil"
+	"net"
 	"os"
 	"os/exec"
 	"path/filepath"
@@ -9,12 +10,14 @@ import (
 	"testing"
 	"time"
 
-	"github.com/apex/log"
+	"github.com/aws/aws-sdk-go/aws"
+	"github.com/aws/aws-sdk-go/service/s3"
 	"github.com/goreleaser/goreleaser/internal/artifact"
 	"github.com/goreleaser/goreleaser/internal/testlib"
 	"github.com/goreleaser/goreleaser/pkg/config"
 	"github.com/goreleaser/goreleaser/pkg/context"
 	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
 )
 
 func TestDescription(t *testing.T) {
@@ -55,6 +58,7 @@ func TestDefaults(t *testing.T) {
 }
 
 func TestUpload(t *testing.T) {
+	var listen = randomListen(t)
 	folder, err := ioutil.TempDir("", "goreleasertest")
 	assert.NoError(t, err)
 	tgzpath := filepath.Join(folder, "bin.tar.gz")
@@ -67,7 +71,7 @@ func TestUpload(t *testing.T) {
 		S3: []config.S3{
 			{
 				Bucket:   "test",
-				Endpoint: "http://localhost:9000",
+				Endpoint: "http://" + listen,
 			},
 		},
 	})
@@ -82,14 +86,16 @@ func TestUpload(t *testing.T) {
 		Name: "bin.deb",
 		Path: debpath,
 	})
-	start(t)
-	defer stop(t)
-	setCredentials()
+	var name = "test_upload"
+	defer stop(t, name)
+	start(t, name, listen)
+	prepareEnv(t, listen)
 	assert.NoError(t, Pipe{}.Default(ctx))
 	assert.NoError(t, Pipe{}.Publish(ctx))
 }
 
 func TestUploadCustomBucketID(t *testing.T) {
+	var listen = randomListen(t)
 	folder, err := ioutil.TempDir("", "goreleasertest")
 	assert.NoError(t, err)
 	tgzpath := filepath.Join(folder, "bin.tar.gz")
@@ -105,7 +111,7 @@ func TestUploadCustomBucketID(t *testing.T) {
 		S3: []config.S3{
 			{
 				Bucket:   "{{.Env.BUCKET_ID}}",
-				Endpoint: "http://localhost:9000",
+				Endpoint: "http://" + listen,
 			},
 		},
 	})
@@ -120,14 +126,16 @@ func TestUploadCustomBucketID(t *testing.T) {
 		Name: "bin.deb",
 		Path: debpath,
 	})
-	start(t)
-	defer stop(t)
-	setCredentials()
+	var name = "custom_bucket_id"
+	defer stop(t, name)
+	start(t, name, listen)
+	prepareEnv(t, listen)
 	assert.NoError(t, Pipe{}.Default(ctx))
 	assert.NoError(t, Pipe{}.Publish(ctx))
 }
 
 func TestUploadInvalidCustomBucketID(t *testing.T) {
+	var listen = randomListen(t)
 	folder, err := ioutil.TempDir("", "goreleasertest")
 	assert.NoError(t, err)
 	tgzpath := filepath.Join(folder, "bin.tar.gz")
@@ -142,7 +150,7 @@ func TestUploadInvalidCustomBucketID(t *testing.T) {
 		S3: []config.S3{
 			{
 				Bucket:   "{{.Bad}}",
-				Endpoint: "http://localhost:9000",
+				Endpoint: "http://" + listen,
 			},
 		},
 	})
@@ -157,53 +165,65 @@ func TestUploadInvalidCustomBucketID(t *testing.T) {
 		Name: "bin.deb",
 		Path: debpath,
 	})
-	start(t)
-	defer stop(t)
-	setCredentials()
+	var name = "invalid_bucket_id"
+	defer stop(t, name)
+	start(t, name, listen)
+	prepareEnv(t, listen)
 	assert.NoError(t, Pipe{}.Default(ctx))
 	assert.Error(t, Pipe{}.Publish(ctx))
 }
 
-func setCredentials() {
-	// 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")
+func randomListen(t *testing.T) string {
+	listener, err := net.Listen("tcp", "127.0.0.1:0")
+	require.NoError(t, err)
+	listener.Close()
+	return listener.Addr().String()
+}
+
+func prepareEnv(t *testing.T, listen string) {
+	os.Setenv("AWS_ACCESS_KEY_ID", "minio")
+	os.Setenv("AWS_SECRET_ACCESS_KEY", "miniostorage")
 	os.Setenv("AWS_REGION", "us-east-1")
+
+	t.Log("creating test bucket")
+	_, err := newS3Svc(config.S3{
+		Endpoint: "http://" + listen,
+		Region:   "us-east-1",
+	}).CreateBucket(&s3.CreateBucketInput{
+		Bucket: aws.String("test"),
+	})
+	require.NoError(t, err)
 }
 
-func start(t *testing.T) {
-	dir, err := os.Getwd()
-	assert.NoError(t, err)
+func start(t *testing.T, name, listen string) {
 	if out, err := exec.Command(
 		"docker", "run", "-d", "--rm",
-		"--name", "minio",
-		"-p", "9000:9000",
-		"-v", dir+"/testdata/data:/data",
-		"-v", dir+"/testdata/config:/root/.minio",
-		"minio/minio:RELEASE.2018-06-09T03-43-35Z",
+		"--name", name,
+		"-p", listen+":9000",
+		"-e", "MINIO_ACCESS_KEY=minio",
+		"-e", "MINIO_SECRET_KEY=miniostorage",
+		"--health-interval", "1s",
+		"minio/minio:RELEASE.2019-05-14T23-57-45Z",
 		"server", "/data",
 	).CombinedOutput(); err != nil {
-		log.WithError(err).Errorf("failed to start minio: %s", string(out))
-		t.FailNow()
+		t.Fatalf("failed to start minio: %s", string(out))
 	}
 
 	for range time.Tick(time.Second) {
-		out, err := exec.Command("docker", "inspect", "--format='{{json .State.Health}}'", "minio").CombinedOutput()
+		out, err := exec.Command("docker", "inspect", "--format='{{json .State.Health}}'", name).CombinedOutput()
 		if err != nil {
-			log.WithError(err).Errorf("failed to check minio status: %s", string(out))
-			t.FailNow()
+			t.Fatalf("failed to check minio status: %s", string(out))
 		}
 		if strings.Contains(string(out), `"Status":"healthy"`) {
-			log.Info("minio is healthy")
+			t.Log("minio is healthy")
 			break
 		}
-		log.Info("waiting for minio to be healthy")
+		t.Log("waiting for minio to be healthy")
 	}
 }
 
-func stop(t *testing.T) {
-	if out, err := exec.Command("docker", "stop", "minio").CombinedOutput(); err != nil {
-		log.WithError(err).Errorf("failed to stop minio: %s", string(out))
-		t.FailNow()
+func stop(t *testing.T, name string) {
+	if out, err := exec.Command("docker", "stop", name).CombinedOutput(); err != nil {
+		t.Fatalf("failed to stop minio: %s", string(out))
 	}
 }
diff --git a/internal/pipe/s3/testdata/config/certs/CAs/.gitkeep b/internal/pipe/s3/testdata/config/certs/CAs/.gitkeep
deleted file mode 100644
index e69de29b..00000000
diff --git a/internal/pipe/s3/testdata/config/config.json b/internal/pipe/s3/testdata/config/config.json
deleted file mode 100644
index 7df1cc5b..00000000
--- a/internal/pipe/s3/testdata/config/config.json
+++ /dev/null
@@ -1,126 +0,0 @@
-{
-	"version": "25",
-	"credential": {
-		"accessKey": "WPXKJC7CZQCFPKY5727N",
-		"secretKey": "eHCSajxLvl94l36gIMlzZ/oW2O0rYYK+cVn5jNT2"
-	},
-	"region": "",
-	"browser": "on",
-	"worm": "off",
-	"domain": "",
-	"storageclass": {
-		"standard": "",
-		"rrs": ""
-	},
-	"cache": {
-		"drives": [],
-		"expiry": 90,
-		"exclude": []
-	},
-	"notify": {
-		"amqp": {
-			"1": {
-				"enable": false,
-				"url": "",
-				"exchange": "",
-				"routingKey": "",
-				"exchangeType": "",
-				"deliveryMode": 0,
-				"mandatory": false,
-				"immediate": false,
-				"durable": false,
-				"internal": false,
-				"noWait": false,
-				"autoDeleted": false
-			}
-		},
-		"elasticsearch": {
-			"1": {
-				"enable": false,
-				"format": "",
-				"url": "",
-				"index": ""
-			}
-		},
-		"kafka": {
-			"1": {
-				"enable": false,
-				"brokers": null,
-				"topic": ""
-			}
-		},
-		"mqtt": {
-			"1": {
-				"enable": false,
-				"broker": "",
-				"topic": "",
-				"qos": 0,
-				"clientId": "",
-				"username": "",
-				"password": "",
-				"reconnectInterval": 0,
-				"keepAliveInterval": 0
-			}
-		},
-		"mysql": {
-			"1": {
-				"enable": false,
-				"format": "",
-				"dsnString": "",
-				"table": "",
-				"host": "",
-				"port": "",
-				"user": "",
-				"password": "",
-				"database": ""
-			}
-		},
-		"nats": {
-			"1": {
-				"enable": false,
-				"address": "",
-				"subject": "",
-				"username": "",
-				"password": "",
-				"token": "",
-				"secure": false,
-				"pingInterval": 0,
-				"streaming": {
-					"enable": false,
-					"clusterID": "",
-					"clientID": "",
-					"async": false,
-					"maxPubAcksInflight": 0
-				}
-			}
-		},
-		"postgresql": {
-			"1": {
-				"enable": false,
-				"format": "",
-				"connectionString": "",
-				"table": "",
-				"host": "",
-				"port": "",
-				"user": "",
-				"password": "",
-				"database": ""
-			}
-		},
-		"redis": {
-			"1": {
-				"enable": false,
-				"format": "",
-				"address": "",
-				"password": "",
-				"key": ""
-			}
-		},
-		"webhook": {
-			"1": {
-				"enable": false,
-				"endpoint": ""
-			}
-		}
-	}
-}
\ No newline at end of file
diff --git a/internal/pipe/s3/testdata/data/.gitignore b/internal/pipe/s3/testdata/data/.gitignore
deleted file mode 100644
index 3efd4908..00000000
--- a/internal/pipe/s3/testdata/data/.gitignore
+++ /dev/null
@@ -1,1 +0,0 @@
-.minio.sys
diff --git a/internal/pipe/s3/testdata/data/test/.gitignore b/internal/pipe/s3/testdata/data/test/.gitignore
deleted file mode 100644
index 1fa23401..00000000
--- a/internal/pipe/s3/testdata/data/test/.gitignore
+++ /dev/null
@@ -1,1 +0,0 @@
-testupload