github.com/szyn/goreleaser@v0.76.1-0.20180517112710-333da09a1297/pipeline/s3/s3_test.go (about)

     1  package s3
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/apex/log"
    13  	"github.com/goreleaser/goreleaser/config"
    14  	"github.com/goreleaser/goreleaser/context"
    15  	"github.com/goreleaser/goreleaser/internal/artifact"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func TestDescription(t *testing.T) {
    20  	assert.NotEmpty(t, Pipe{}.String())
    21  }
    22  
    23  func TestNoS3(t *testing.T) {
    24  	assert.NoError(t, Pipe{}.Run(context.New(config.Project{})))
    25  }
    26  
    27  func TestDefaultsNoS3(t *testing.T) {
    28  	var assert = assert.New(t)
    29  	var ctx = context.New(config.Project{
    30  		S3: []config.S3{
    31  			config.S3{},
    32  		},
    33  	})
    34  	assert.NoError(Pipe{}.Default(ctx))
    35  	assert.Equal([]config.S3{config.S3{}}, ctx.Config.S3)
    36  }
    37  
    38  func TestDefaults(t *testing.T) {
    39  	var assert = assert.New(t)
    40  	var ctx = context.New(config.Project{
    41  		S3: []config.S3{
    42  			{
    43  				Bucket: "foo",
    44  			},
    45  		},
    46  	})
    47  	assert.NoError(Pipe{}.Default(ctx))
    48  	assert.Equal([]config.S3{config.S3{
    49  		Bucket: "foo",
    50  		Region: "us-east-1",
    51  		Folder: "{{ .ProjectName }}/{{ .Tag }}",
    52  	}}, ctx.Config.S3)
    53  }
    54  
    55  func TestUpload(t *testing.T) {
    56  	folder, err := ioutil.TempDir("", "goreleasertest")
    57  	assert.NoError(t, err)
    58  	tgzpath := filepath.Join(folder, "bin.tar.gz")
    59  	debpath := filepath.Join(folder, "bin.deb")
    60  	assert.NoError(t, ioutil.WriteFile(tgzpath, []byte("fake\ntargz"), 0744))
    61  	assert.NoError(t, ioutil.WriteFile(debpath, []byte("fake\ndeb"), 0744))
    62  	var ctx = context.New(config.Project{
    63  		Dist:        folder,
    64  		ProjectName: "testupload",
    65  		S3: []config.S3{
    66  			{
    67  				Bucket:   "test",
    68  				Endpoint: "http://localhost:9000",
    69  			},
    70  		},
    71  	})
    72  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
    73  	ctx.Artifacts.Add(artifact.Artifact{
    74  		Type: artifact.UploadableArchive,
    75  		Name: "bin.tar.gz",
    76  		Path: tgzpath,
    77  	})
    78  	ctx.Artifacts.Add(artifact.Artifact{
    79  		Type: artifact.LinuxPackage,
    80  		Name: "bin.deb",
    81  		Path: debpath,
    82  	})
    83  	start(t)
    84  	defer stop(t)
    85  	setCredentials(t)
    86  	assert.NoError(t, Pipe{}.Default(ctx))
    87  	assert.NoError(t, Pipe{}.Run(ctx))
    88  }
    89  
    90  func setCredentials(t *testing.T) {
    91  	// this comes from the testdata/config/config.json file - not real aws keys
    92  	os.Setenv("AWS_ACCESS_KEY_ID", "IWA0WZQ1QJ2I8I1ALW64")
    93  	os.Setenv("AWS_SECRET_ACCESS_KEY", "zcK4QQegvYwVGJaBm2E6k20WRLIQZqHOKOPP2npT")
    94  	os.Setenv("AWS_REGION", "us-east-1")
    95  }
    96  
    97  func start(t *testing.T) {
    98  	dir, err := os.Getwd()
    99  	assert.NoError(t, err)
   100  	log.Info("wd: " + dir)
   101  	if out, err := exec.Command(
   102  		"docker", "run", "-d", "--rm",
   103  		"--name", "minio",
   104  		"-p", "9000:9000",
   105  		"-v", dir+"/testdata/data:/data",
   106  		"-v", dir+"/testdata/config:/root/.minio",
   107  		"minio/minio",
   108  		"server", "/data",
   109  	).CombinedOutput(); err != nil {
   110  		log.WithError(err).Errorf("failed to start minio: %s", string(out))
   111  		t.FailNow()
   112  	}
   113  
   114  	for range time.Tick(time.Second) {
   115  		out, err := exec.Command("docker", "inspect", "--format='{{json .State.Health}}'", "minio").CombinedOutput()
   116  		if err != nil {
   117  			log.WithError(err).Errorf("failed to check minio status: %s", string(out))
   118  			t.FailNow()
   119  		}
   120  		if strings.Contains(string(out), `"Status":"healthy"`) {
   121  			log.Info("minio is healthy")
   122  			break
   123  		}
   124  		log.Info("waiting for minio to be healthy")
   125  	}
   126  }
   127  
   128  func stop(t *testing.T) {
   129  	if out, err := exec.Command("docker", "stop", "minio").CombinedOutput(); err != nil {
   130  		log.WithError(err).Errorf("failed to stop minio: %s", string(out))
   131  		t.FailNow()
   132  	}
   133  }