github.com/goreleaser/goreleaser@v1.25.1/internal/pipe/blob/blob_test.go (about)

     1  package blob
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/goreleaser/goreleaser/internal/testctx"
     8  	"github.com/goreleaser/goreleaser/internal/testlib"
     9  	"github.com/goreleaser/goreleaser/pkg/config"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestDescription(t *testing.T) {
    14  	require.NotEmpty(t, Pipe{}.String())
    15  }
    16  
    17  func TestErrors(t *testing.T) {
    18  	for k, v := range map[string]string{
    19  		"NoSuchBucket":                 "provided bucket does not exist: someurl: NoSuchBucket",
    20  		"ContainerNotFound":            "provided bucket does not exist: someurl: ContainerNotFound",
    21  		"notFound":                     "provided bucket does not exist: someurl: notFound",
    22  		"NoCredentialProviders":        "check credentials and access to bucket: someurl: NoCredentialProviders",
    23  		"InvalidAccessKeyId":           "aws access key id you provided does not exist in our records: InvalidAccessKeyId",
    24  		"AuthenticationFailed":         "azure storage key you provided is not valid: AuthenticationFailed",
    25  		"invalid_grant":                "google app credentials you provided is not valid: invalid_grant",
    26  		"no such host":                 "azure storage account you provided is not valid: no such host",
    27  		"ServiceCode=ResourceNotFound": "missing azure storage key for provided bucket someurl: ServiceCode=ResourceNotFound",
    28  		"other":                        "failed to write to bucket: other",
    29  	} {
    30  		t.Run(k, func(t *testing.T) {
    31  			require.EqualError(t, handleError(fmt.Errorf(k), "someurl"), v)
    32  		})
    33  	}
    34  }
    35  
    36  func TestDefaultsNoConfig(t *testing.T) {
    37  	errorString := "bucket or provider cannot be empty"
    38  	ctx := testctx.NewWithCfg(config.Project{
    39  		Blobs: []config.Blob{{}},
    40  	})
    41  	require.EqualError(t, Pipe{}.Default(ctx), errorString)
    42  }
    43  
    44  func TestDefaultsNoBucket(t *testing.T) {
    45  	errorString := "bucket or provider cannot be empty"
    46  	ctx := testctx.NewWithCfg(config.Project{
    47  		Blobs: []config.Blob{
    48  			{
    49  				Provider: "azblob",
    50  			},
    51  		},
    52  	})
    53  	require.EqualError(t, Pipe{}.Default(ctx), errorString)
    54  }
    55  
    56  func TestDefaultsNoProvider(t *testing.T) {
    57  	errorString := "bucket or provider cannot be empty"
    58  	ctx := testctx.NewWithCfg(config.Project{
    59  		Blobs: []config.Blob{
    60  			{
    61  				Bucket: "goreleaser-bucket",
    62  			},
    63  		},
    64  	})
    65  	require.EqualError(t, Pipe{}.Default(ctx), errorString)
    66  }
    67  
    68  func TestDefaults(t *testing.T) {
    69  	ctx := testctx.NewWithCfg(config.Project{
    70  		Blobs: []config.Blob{
    71  			{
    72  				Bucket:             "foo",
    73  				Provider:           "azblob",
    74  				IDs:                []string{"foo", "bar"},
    75  				ContentDisposition: "inline",
    76  			},
    77  			{
    78  				Bucket:   "foobar",
    79  				Provider: "gcs",
    80  			},
    81  			{
    82  				Bucket:        "deprecated",
    83  				Provider:      "s3",
    84  				Folder:        "static",
    85  				OldDisableSSL: true,
    86  				OldKMSKey:     "fake",
    87  			},
    88  		},
    89  	})
    90  	require.NoError(t, Pipe{}.Default(ctx))
    91  	require.Equal(t, []config.Blob{
    92  		{
    93  			Bucket:             "foo",
    94  			Provider:           "azblob",
    95  			Directory:          "{{ .ProjectName }}/{{ .Tag }}",
    96  			IDs:                []string{"foo", "bar"},
    97  			ContentDisposition: "inline",
    98  		},
    99  		{
   100  			Bucket:             "foobar",
   101  			Provider:           "gcs",
   102  			Directory:          "{{ .ProjectName }}/{{ .Tag }}",
   103  			ContentDisposition: "attachment;filename={{.Filename}}",
   104  		},
   105  		{
   106  			Bucket:             "deprecated",
   107  			Provider:           "s3",
   108  			Folder:             "static",
   109  			Directory:          "static",
   110  			OldDisableSSL:      true,
   111  			DisableSSL:         true,
   112  			OldKMSKey:          "fake",
   113  			KMSKey:             "fake",
   114  			ContentDisposition: "attachment;filename={{.Filename}}",
   115  		},
   116  	}, ctx.Config.Blobs)
   117  }
   118  
   119  func TestDefaultsWithProvider(t *testing.T) {
   120  	ctx := testctx.NewWithCfg(config.Project{
   121  		Blobs: []config.Blob{
   122  			{
   123  				Bucket:   "foo",
   124  				Provider: "azblob",
   125  			},
   126  			{
   127  				Bucket:   "foo",
   128  				Provider: "s3",
   129  			},
   130  			{
   131  				Bucket:   "foo",
   132  				Provider: "gs",
   133  			},
   134  		},
   135  	})
   136  	require.NoError(t, Pipe{}.Default(ctx))
   137  }
   138  
   139  func TestURL(t *testing.T) {
   140  	t.Run("s3 with opts", func(t *testing.T) {
   141  		url, err := urlFor(testctx.New(), config.Blob{
   142  			Bucket:     "foo",
   143  			Provider:   "s3",
   144  			Region:     "us-west-1",
   145  			Folder:     "foo",
   146  			Endpoint:   "s3.foobar.com",
   147  			DisableSSL: true,
   148  		})
   149  		require.NoError(t, err)
   150  		require.Equal(t, "s3://foo?disableSSL=true&endpoint=s3.foobar.com&region=us-west-1&s3ForcePathStyle=true", url)
   151  	})
   152  
   153  	t.Run("s3 with some opts", func(t *testing.T) {
   154  		url, err := urlFor(testctx.New(), config.Blob{
   155  			Bucket:     "foo",
   156  			Provider:   "s3",
   157  			Region:     "us-west-1",
   158  			DisableSSL: true,
   159  		})
   160  		require.NoError(t, err)
   161  		require.Equal(t, "s3://foo?disableSSL=true&region=us-west-1", url)
   162  	})
   163  
   164  	t.Run("gs with opts", func(t *testing.T) {
   165  		url, err := urlFor(testctx.New(), config.Blob{
   166  			Bucket:     "foo",
   167  			Provider:   "gs",
   168  			Region:     "us-west-1",
   169  			Folder:     "foo",
   170  			Endpoint:   "s3.foobar.com",
   171  			DisableSSL: true,
   172  		})
   173  		require.NoError(t, err)
   174  		require.Equal(t, "gs://foo", url)
   175  	})
   176  
   177  	t.Run("s3 no opts", func(t *testing.T) {
   178  		url, err := urlFor(testctx.New(), config.Blob{
   179  			Bucket:   "foo",
   180  			Provider: "s3",
   181  		})
   182  		require.NoError(t, err)
   183  		require.Equal(t, "s3://foo", url)
   184  	})
   185  
   186  	t.Run("gs no opts", func(t *testing.T) {
   187  		url, err := urlFor(testctx.New(), config.Blob{
   188  			Bucket:   "foo",
   189  			Provider: "gs",
   190  		})
   191  		require.NoError(t, err)
   192  		require.Equal(t, "gs://foo", url)
   193  	})
   194  
   195  	t.Run("template errors", func(t *testing.T) {
   196  		t.Run("provider", func(t *testing.T) {
   197  			_, err := urlFor(testctx.New(), config.Blob{
   198  				Provider: "{{ .Nope }}",
   199  			})
   200  			testlib.RequireTemplateError(t, err)
   201  		})
   202  		t.Run("bucket", func(t *testing.T) {
   203  			_, err := urlFor(testctx.New(), config.Blob{
   204  				Bucket:   "{{ .Nope }}",
   205  				Provider: "gs",
   206  			})
   207  			testlib.RequireTemplateError(t, err)
   208  		})
   209  		t.Run("endpoint", func(t *testing.T) {
   210  			_, err := urlFor(testctx.New(), config.Blob{
   211  				Bucket:   "foobar",
   212  				Endpoint: "{{.Env.NOPE}}",
   213  				Provider: "s3",
   214  			})
   215  			testlib.RequireTemplateError(t, err)
   216  		})
   217  		t.Run("region", func(t *testing.T) {
   218  			_, err := urlFor(testctx.New(), config.Blob{
   219  				Bucket:   "foobar",
   220  				Region:   "{{.Env.NOPE}}",
   221  				Provider: "s3",
   222  			})
   223  			testlib.RequireTemplateError(t, err)
   224  		})
   225  	})
   226  }
   227  
   228  func TestSkip(t *testing.T) {
   229  	t.Run("skip", func(t *testing.T) {
   230  		require.True(t, Pipe{}.Skip(testctx.New()))
   231  	})
   232  
   233  	t.Run("dont skip", func(t *testing.T) {
   234  		ctx := testctx.NewWithCfg(config.Project{
   235  			Blobs: []config.Blob{{}},
   236  		})
   237  		require.False(t, Pipe{}.Skip(ctx))
   238  	})
   239  }