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