github.com/amane3/goreleaser@v0.182.0/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/amane3/goreleaser/internal/artifact"
    11  	"github.com/amane3/goreleaser/internal/testlib"
    12  	"github.com/amane3/goreleaser/pkg/config"
    13  	"github.com/amane3/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  	gcloudCredentials, _ := filepath.Abs("./testdata/credentials.json")
   124  
   125  	var folder = t.TempDir()
   126  	tgzpath := filepath.Join(folder, "bin.tar.gz")
   127  	debpath := filepath.Join(folder, "bin.deb")
   128  	require.NoError(t, ioutil.WriteFile(tgzpath, []byte("fake\ntargz"), 0744))
   129  	require.NoError(t, ioutil.WriteFile(debpath, []byte("fake\ndeb"), 0744))
   130  
   131  	// Azure Blob Context
   132  	var azblobctx = context.New(config.Project{
   133  		Dist:        folder,
   134  		ProjectName: "testupload",
   135  		Blobs: []config.Blob{
   136  			{
   137  				Bucket:     "foo",
   138  				Provider:   "azblob",
   139  				ExtraFiles: extra,
   140  			},
   141  		},
   142  	})
   143  
   144  	azblobctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   145  	azblobctx.Artifacts.Add(&artifact.Artifact{
   146  		Type: artifact.UploadableArchive,
   147  		Name: "bin.tar.gz",
   148  		Path: tgzpath,
   149  	})
   150  	azblobctx.Artifacts.Add(&artifact.Artifact{
   151  		Type: artifact.LinuxPackage,
   152  		Name: "bin.deb",
   153  		Path: debpath,
   154  	})
   155  
   156  	// Google Cloud Storage Context
   157  	var gsctx = context.New(config.Project{
   158  		Dist:        folder,
   159  		ProjectName: "testupload",
   160  		Blobs: []config.Blob{
   161  			{
   162  				Bucket:     "foo",
   163  				Provider:   "gs",
   164  				ExtraFiles: extra,
   165  			},
   166  		},
   167  	})
   168  
   169  	gsctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   170  
   171  	gsctx.Artifacts.Add(&artifact.Artifact{
   172  		Type: artifact.UploadableArchive,
   173  		Name: "bin.tar.gz",
   174  		Path: tgzpath,
   175  	})
   176  	gsctx.Artifacts.Add(&artifact.Artifact{
   177  		Type: artifact.LinuxPackage,
   178  		Name: "bin.deb",
   179  		Path: debpath,
   180  	})
   181  
   182  	// AWS S3 Context
   183  	var s3ctx = context.New(config.Project{
   184  		Dist:        folder,
   185  		ProjectName: "testupload",
   186  		Blobs: []config.Blob{
   187  			{
   188  				Bucket:     "foo",
   189  				Provider:   "s3",
   190  				ExtraFiles: extra,
   191  			},
   192  		},
   193  	})
   194  	s3ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   195  	s3ctx.Artifacts.Add(&artifact.Artifact{
   196  		Type: artifact.UploadableArchive,
   197  		Name: "bin.tar.gz",
   198  		Path: tgzpath,
   199  	})
   200  	s3ctx.Artifacts.Add(&artifact.Artifact{
   201  		Type: artifact.LinuxPackage,
   202  		Name: "bin.deb",
   203  		Path: debpath,
   204  	})
   205  
   206  	type args struct {
   207  		ctx *context.Context
   208  	}
   209  	tests := []struct {
   210  		name          string
   211  		args          args
   212  		env           map[string]string
   213  		wantErr       bool
   214  		wantErrString string
   215  	}{
   216  		{
   217  			name:          "Azure Blob Bucket Test Publish",
   218  			args:          args{azblobctx},
   219  			env:           map[string]string{"AZURE_STORAGE_ACCOUNT": "hjsdhjsdhs", "AZURE_STORAGE_KEY": "eHCSajxLvl94l36gIMlzZ/oW2O0rYYK+cVn5jNT2"},
   220  			wantErr:       false,
   221  			wantErrString: "azure storage account you provided is not valid",
   222  		},
   223  		{
   224  			name:          "Google Cloud Storage Bucket Test Publish",
   225  			args:          args{gsctx},
   226  			env:           map[string]string{"GOOGLE_APPLICATION_CREDENTIALS": gcloudCredentials},
   227  			wantErr:       false,
   228  			wantErrString: "google app credentials you provided is not valid",
   229  		},
   230  		{
   231  			name:          "AWS S3 Bucket Test Publish",
   232  			args:          args{s3ctx},
   233  			env:           map[string]string{"AWS_ACCESS_KEY": "WPXKJC7CZQCFPKY5727N", "AWS_SECRET_KEY": "eHCSajxLvl94l36gIMlzZ/oW2O0rYYK+cVn5jNT2", "AWS_REGION": "us-east-1"},
   234  			wantErr:       false,
   235  			wantErrString: "aws access key id you provided does not exist in our records",
   236  		},
   237  	}
   238  	for _, tt := range tests {
   239  		t.Run(tt.name, func(t *testing.T) {
   240  			p := Pipe{}
   241  			setEnv(tt.env)
   242  			defer unsetEnv(tt.env)
   243  			if err := p.Publish(tt.args.ctx); (err != nil) != tt.wantErr {
   244  				if !strings.HasPrefix(err.Error(), tt.wantErrString) {
   245  					t.Errorf("Pipe.Publish() error = %v, wantErr %v", err, tt.wantErrString)
   246  				}
   247  			}
   248  		})
   249  	}
   250  }
   251  
   252  func TestURL(t *testing.T) {
   253  	t.Run("s3 with opts", func(t *testing.T) {
   254  		url, err := urlFor(context.New(config.Project{}), config.Blob{
   255  			Bucket:     "foo",
   256  			Provider:   "s3",
   257  			Region:     "us-west-1",
   258  			Folder:     "foo",
   259  			Endpoint:   "s3.foobar.com",
   260  			DisableSSL: true,
   261  		})
   262  		require.NoError(t, err)
   263  		require.Equal(t, "s3://foo?disableSSL=true&endpoint=s3.foobar.com&region=us-west-1&s3ForcePathStyle=true", url)
   264  	})
   265  
   266  	t.Run("s3 with some opts", func(t *testing.T) {
   267  		url, err := urlFor(context.New(config.Project{}), config.Blob{
   268  			Bucket:     "foo",
   269  			Provider:   "s3",
   270  			Region:     "us-west-1",
   271  			DisableSSL: true,
   272  		})
   273  		require.NoError(t, err)
   274  		require.Equal(t, "s3://foo?disableSSL=true&region=us-west-1", url)
   275  	})
   276  
   277  	t.Run("gs with opts", func(t *testing.T) {
   278  		url, err := urlFor(context.New(config.Project{}), config.Blob{
   279  			Bucket:     "foo",
   280  			Provider:   "gs",
   281  			Region:     "us-west-1",
   282  			Folder:     "foo",
   283  			Endpoint:   "s3.foobar.com",
   284  			DisableSSL: true,
   285  		})
   286  		require.NoError(t, err)
   287  		require.Equal(t, "gs://foo", url)
   288  	})
   289  
   290  	t.Run("s3 no opts", func(t *testing.T) {
   291  		url, err := urlFor(context.New(config.Project{}), config.Blob{
   292  			Bucket:   "foo",
   293  			Provider: "s3",
   294  		})
   295  		require.NoError(t, err)
   296  		require.Equal(t, "s3://foo", url)
   297  	})
   298  
   299  	t.Run("gs no opts", func(t *testing.T) {
   300  		url, err := urlFor(context.New(config.Project{}), config.Blob{
   301  			Bucket:   "foo",
   302  			Provider: "gs",
   303  		})
   304  		require.NoError(t, err)
   305  		require.Equal(t, "gs://foo", url)
   306  	})
   307  }
   308  
   309  func setEnv(env map[string]string) {
   310  	for k, v := range env {
   311  		os.Setenv(k, v)
   312  	}
   313  }
   314  
   315  func unsetEnv(env map[string]string) {
   316  	for k := range env {
   317  		os.Unsetenv(k)
   318  	}
   319  }