-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial azure backend implementationn * fix golangci-lint, docker-compose * [CI SKIP] excluded specific linter for magic numbers * Fixed error message in creating new container
- Loading branch information
1 parent
a690b4f
commit 254d34d
Showing
13 changed files
with
263 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package backend | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/Azure/azure-storage-blob-go/azblob" | ||
"github.com/meltwater/drone-cache/cache" | ||
) | ||
|
||
type azureBackend struct { | ||
containerURL azblob.ContainerURL | ||
ctx context.Context | ||
} | ||
|
||
func newAzure(ctx context.Context, containerURL azblob.ContainerURL) cache.Backend { | ||
return &azureBackend{ | ||
containerURL: containerURL, | ||
ctx: ctx, | ||
} | ||
} | ||
|
||
func (c *azureBackend) Get(p string) (io.ReadCloser, error) { | ||
blobURL := c.containerURL.NewBlockBlobURL(p) | ||
|
||
downloadResponse, err := blobURL.Download(c.ctx, 0, azblob.CountToEnd, azblob.BlobAccessConditions{}, false) | ||
if err != nil { | ||
return nil, fmt.Errorf("get the object %w", err) | ||
} | ||
|
||
//nolint:mnd // NOTE: automatically retries are performed if the connection fails, not magic number | ||
bodyStream := downloadResponse.Body(azblob.RetryReaderOptions{MaxRetryRequests: 4}) | ||
|
||
return bodyStream, nil | ||
} | ||
|
||
// Put uploads the contents of the io.ReadSeeker | ||
func (c *azureBackend) Put(p string, src io.ReadSeeker) error { | ||
blobURL := c.containerURL.NewBlockBlobURL(p) | ||
|
||
fmt.Printf("uploading the file with blob name: %s\n", p) | ||
|
||
_, err := blobURL.Upload(c.ctx, src, azblob.BlobHTTPHeaders{}, azblob.Metadata{}, azblob.BlobAccessConditions{}) | ||
if err != nil { | ||
return fmt.Errorf("put the object %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package backend | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/go-kit/kit/log" | ||
) | ||
|
||
const defaultBlobStorageURL = "127.0.0.1:10000" | ||
|
||
var blobURL = getEnv("TEST_AZURITE_URL", defaultBlobStorageURL) | ||
|
||
func TestAzureTruth(t *testing.T) { | ||
|
||
b, err := InitializeAzureBackend(log.NewNopLogger(), | ||
AzureConfig{ | ||
AccountName: "devstoreaccount1", | ||
AccountKey: "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==", | ||
ContainerName: "testcontainer", | ||
BlobStorageURL: blobURL, | ||
Azurite: true, | ||
}, true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
token := make([]byte, 32) | ||
rand.Read(token) | ||
testData := bytes.NewReader(token) | ||
|
||
// PUT TEST | ||
err = b.Put("test_key", testData) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// GET TEST | ||
readCloser, err := b.Get("test_key") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Check the validity of returned bytes | ||
readData, _ := ioutil.ReadAll(readCloser) | ||
|
||
if !bytes.Equal(readData, token) { | ||
t.Fatal(string(readData), "!=", token) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.