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