github.com/joselitofilho/goreleaser@v0.155.1-0.20210123221854-e4891856c593/internal/pipe/blob/blob_test.go (about)

     1  package blob
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/goreleaser/goreleaser/internal/artifact"
    11  	"github.com/goreleaser/goreleaser/internal/testlib"
    12  	"github.com/goreleaser/goreleaser/pkg/config"
    13  	"github.com/goreleaser/goreleaser/pkg/context"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestDescription(t *testing.T) {
    18  	require.NotEmpty(t, Pipe{}.String())
    19  }
    20  
    21  func TestNoBlob(t *testing.T) {
    22  	testlib.AssertSkipped(t, Pipe{}.Publish(context.New(config.Project{})))
    23  }
    24  
    25  func TestDefaultsNoConfig(t *testing.T) {
    26  	errorString := "bucket or provider cannot be empty"
    27  	var ctx = context.New(config.Project{
    28  		Blobs: []config.Blob{
    29  			{},
    30  		},
    31  	})
    32  	require.EqualError(t, Pipe{}.Default(ctx), errorString)
    33  }
    34  
    35  func TestDefaultsNoBucket(t *testing.T) {
    36  	errorString := "bucket or provider cannot be empty"
    37  	var ctx = context.New(config.Project{
    38  		Blobs: []config.Blob{
    39  			{
    40  				Provider: "azblob",
    41  			},
    42  		},
    43  	})
    44  	require.EqualError(t, Pipe{}.Default(ctx), errorString)
    45  }
    46  
    47  func TestDefaultsNoProvider(t *testing.T) {
    48  	errorString := "bucket or provider cannot be empty"
    49  	var ctx = context.New(config.Project{
    50  		Blobs: []config.Blob{
    51  			{
    52  				Bucket: "goreleaser-bucket",
    53  			},
    54  		},
    55  	})
    56  	require.EqualError(t, Pipe{}.Default(ctx), errorString)
    57  }
    58  
    59  func TestDefaults(t *testing.T) {
    60  	var ctx = context.New(config.Project{
    61  		Blobs: []config.Blob{
    62  			{
    63  				Bucket:   "foo",
    64  				Provider: "azblob",
    65  				IDs:      []string{"foo", "bar"},
    66  			},
    67  			{
    68  				Bucket:   "foobar",
    69  				Provider: "gcs",
    70  			},
    71  		},
    72  	})
    73  	require.NoError(t, Pipe{}.Default(ctx))
    74  	require.Equal(t, []config.Blob{
    75  		{
    76  			Bucket:   "foo",
    77  			Provider: "azblob",
    78  			Folder:   "{{ .ProjectName }}/{{ .Tag }}",
    79  			IDs:      []string{"foo", "bar"},
    80  		},
    81  		{
    82  			Bucket:   "foobar",
    83  			Provider: "gcs",
    84  			Folder:   "{{ .ProjectName }}/{{ .Tag }}",
    85  		},
    86  	}, ctx.Config.Blobs)
    87  }
    88  
    89  func TestDefaultsWithProvider(t *testing.T) {
    90  	var ctx = context.New(config.Project{
    91  		Blobs: []config.Blob{
    92  			{
    93  				Bucket:   "foo",
    94  				Provider: "azblob",
    95  			},
    96  			{
    97  				Bucket:   "foo",
    98  				Provider: "s3",
    99  			},
   100  			{
   101  				Bucket:   "foo",
   102  				Provider: "gs",
   103  			},
   104  		},
   105  	})
   106  	require.Nil(t, Pipe{}.Default(ctx))
   107  }
   108  
   109  func TestPipe_Publish(t *testing.T) {
   110  	pipePublish(t, []config.ExtraFile{})
   111  }
   112  
   113  func TestPipe_PublishExtraFiles(t *testing.T) {
   114  	var extra = []config.ExtraFile{
   115  		{
   116  			Glob: "./testdata/file.golden",
   117  		},
   118  	}
   119  	pipePublish(t, extra)
   120  }
   121  
   122  func pipePublish(t *testing.T, extra []config.ExtraFile) {
   123  	t.Helper()
   124  	gcloudCredentials, _ := filepath.Abs("./testdata/credentials.json")
   125  
   126  	var folder = t.TempDir()
   127  	tgzpath := filepath.Join(folder, "bin.tar.gz")
   128  	debpath := filepath.Join(folder, "bin.deb")
   129  	require.NoError(t, ioutil.WriteFile(tgzpath, []byte("fake\ntargz"), 0744))
   130  	require.NoError(t, ioutil.WriteFile(debpath, []byte("fake\ndeb"), 0744))
   131  
   132  	// Azure Blob Context
   133  	var azblobctx = context.New(config.Project{
   134  		Dist:        folder,
   135  		ProjectName: "testupload",
   136  		Blobs: []config.Blob{
   137  			{
   138  				Bucket:     "foo",
   139  				Provider:   "azblob",
   140  				ExtraFiles: extra,
   141  			},
   142  		},
   143  	})
   144  
   145  	azblobctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   146  	azblobctx.Artifacts.Add(&artifact.Artifact{
   147  		Type: artifact.UploadableArchive,
   148  		Name: "bin.tar.gz",
   149  		Path: tgzpath,
   150  	})
   151  	azblobctx.Artifacts.Add(&artifact.Artifact{
   152  		Type: artifact.LinuxPackage,
   153  		Name: "bin.deb",
   154  		Path: debpath,
   155  	})
   156  
   157  	// Google Cloud Storage Context
   158  	var gsctx = context.New(config.Project{
   159  		Dist:        folder,
   160  		ProjectName: "testupload",
   161  		Blobs: []config.Blob{
   162  			{
   163  				Bucket:     "foo",
   164  				Provider:   "gs",
   165  				ExtraFiles: extra,
   166  			},
   167  		},
   168  	})
   169  
   170  	gsctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   171  
   172  	gsctx.Artifacts.Add(&artifact.Artifact{
   173  		Type: artifact.UploadableArchive,
   174  		Name: "bin.tar.gz",
   175  		Path: tgzpath,
   176  	})
   177  	gsctx.Artifacts.Add(&artifact.Artifact{
   178  		Type: artifact.LinuxPackage,
   179  		Name: "bin.deb",
   180  		Path: debpath,
   181  	})
   182  
   183  	// AWS S3 Context
   184  	var s3ctx = context.New(config.Project{
   185  		Dist:        folder,
   186  		ProjectName: "testupload",
   187  		Blobs: []config.Blob{
   188  			{
   189  				Bucket:     "foo",
   190  				Provider:   "s3",
   191  				ExtraFiles: extra,
   192  			},
   193  		},
   194  	})
   195  	s3ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   196  	s3ctx.Artifacts.Add(&artifact.Artifact{
   197  		Type: artifact.UploadableArchive,
   198  		Name: "bin.tar.gz",
   199  		Path: tgzpath,
   200  	})
   201  	s3ctx.Artifacts.Add(&artifact.Artifact{
   202  		Type: artifact.LinuxPackage,
   203  		Name: "bin.deb",
   204  		Path: debpath,
   205  	})
   206  
   207  	type args struct {
   208  		ctx *context.Context
   209  	}
   210  	tests := []struct {
   211  		name          string
   212  		args          args
   213  		env           map[string]string
   214  		wantErr       bool
   215  		wantErrString string
   216  	}{
   217  		{
   218  			name:          "Azure Blob Bucket Test Publish",
   219  			args:          args{azblobctx},
   220  			env:           map[string]string{"AZURE_STORAGE_ACCOUNT": "hjsdhjsdhs", "AZURE_STORAGE_KEY": "eHCSajxLvl94l36gIMlzZ/oW2O0rYYK+cVn5jNT2"},
   221  			wantErr:       false,
   222  			wantErrString: "azure storage account you provided is not valid",
   223  		},
   224  		{
   225  			name:          "Google Cloud Storage Bucket Test Publish",
   226  			args:          args{gsctx},
   227  			env:           map[string]string{"GOOGLE_APPLICATION_CREDENTIALS": gcloudCredentials},
   228  			wantErr:       false,
   229  			wantErrString: "google app credentials you provided is not valid",
   230  		},
   231  		{
   232  			name:          "AWS S3 Bucket Test Publish",
   233  			args:          args{s3ctx},
   234  			env:           map[string]string{"AWS_ACCESS_KEY": "WPXKJC7CZQCFPKY5727N", "AWS_SECRET_KEY": "eHCSajxLvl94l36gIMlzZ/oW2O0rYYK+cVn5jNT2", "AWS_REGION": "us-east-1"},
   235  			wantErr:       false,
   236  			wantErrString: "aws access key id you provided does not exist in our records",
   237  		},
   238  	}
   239  	for _, tt := range tests {
   240  		t.Run(tt.name, func(t *testing.T) {
   241  			p := Pipe{}
   242  			setEnv(tt.env)
   243  			defer unsetEnv(tt.env)
   244  			if err := p.Publish(tt.args.ctx); (err != nil) != tt.wantErr {
   245  				if !strings.HasPrefix(err.Error(), tt.wantErrString) {
   246  					t.Errorf("Pipe.Publish() error = %v, wantErr %v", err, tt.wantErrString)
   247  				}
   248  			}
   249  		})
   250  	}
   251  }
   252  
   253  func TestURL(t *testing.T) {
   254  	t.Run("s3 with opts", func(t *testing.T) {
   255  		url, err := urlFor(context.New(config.Project{}), config.Blob{
   256  			Bucket:     "foo",
   257  			Provider:   "s3",
   258  			Region:     "us-west-1",
   259  			Folder:     "foo",
   260  			Endpoint:   "s3.foobar.com",
   261  			DisableSSL: true,
   262  		})
   263  		require.NoError(t, err)
   264  		require.Equal(t, "s3://foo?disableSSL=true&endpoint=s3.foobar.com&region=us-west-1&s3ForcePathStyle=true", url)
   265  	})
   266  
   267  	t.Run("s3 with some opts", func(t *testing.T) {
   268  		url, err := urlFor(context.New(config.Project{}), config.Blob{
   269  			Bucket:     "foo",
   270  			Provider:   "s3",
   271  			Region:     "us-west-1",
   272  			DisableSSL: true,
   273  		})
   274  		require.NoError(t, err)
   275  		require.Equal(t, "s3://foo?disableSSL=true&region=us-west-1", url)
   276  	})
   277  
   278  	t.Run("gs with opts", func(t *testing.T) {
   279  		url, err := urlFor(context.New(config.Project{}), config.Blob{
   280  			Bucket:     "foo",
   281  			Provider:   "gs",
   282  			Region:     "us-west-1",
   283  			Folder:     "foo",
   284  			Endpoint:   "s3.foobar.com",
   285  			DisableSSL: true,
   286  		})
   287  		require.NoError(t, err)
   288  		require.Equal(t, "gs://foo", url)
   289  	})
   290  
   291  	t.Run("s3 no opts", func(t *testing.T) {
   292  		url, err := urlFor(context.New(config.Project{}), config.Blob{
   293  			Bucket:   "foo",
   294  			Provider: "s3",
   295  		})
   296  		require.NoError(t, err)
   297  		require.Equal(t, "s3://foo", url)
   298  	})
   299  
   300  	t.Run("gs no opts", func(t *testing.T) {
   301  		url, err := urlFor(context.New(config.Project{}), config.Blob{
   302  			Bucket:   "foo",
   303  			Provider: "gs",
   304  		})
   305  		require.NoError(t, err)
   306  		require.Equal(t, "gs://foo", url)
   307  	})
   308  }
   309  
   310  func setEnv(env map[string]string) {
   311  	for k, v := range env {
   312  		os.Setenv(k, v)
   313  	}
   314  }
   315  
   316  func unsetEnv(env map[string]string) {
   317  	for k := range env {
   318  		os.Unsetenv(k)
   319  	}
   320  }