Debian Codegraph @

storage/2017-07-29/file/files/range_get_file_test.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 package files
2
3 import (
4 "context"
5 "fmt"
6 "os"
7 "testing"
8
9 "github.com/Azure/azure-sdk-for-go/profiles/latest/storage/mgmt/storage"
10 "github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/shares"
11 "github.com/tombuildsstuff/giovanni/storage/internal/auth"
12 "github.com/tombuildsstuff/giovanni/testhelpers"
13 )
14
15 func TestGetSmallFile(t *testing.T) {
16 // the purpose of this test is to verify that the small, single-chunked file gets downloaded correctly
17 testGetFile(t, "small-file.png", "image/png")
18 }
19
20 func TestGetLargeFile(t *testing.T) {
21 // the purpose of this test is to verify that the large, multi-chunked file gets downloaded correctly
22 testGetFile(t, "blank-large-file.dmg", "application/x-apple-diskimage")
23 }
24
25 func testGetFile(t *testing.T, fileName string, contentType string) {
26 client, err := testhelpers.Build(t)
27 if err != nil {
28 t.Fatal(err)
29 }
30 ctx := context.TODO()
31 resourceGroup := fmt.Sprintf("acctestrg-%d", testhelpers.RandomInt())
32 accountName := fmt.Sprintf("acctestsa%s", testhelpers.RandomString())
33 shareName := fmt.Sprintf("share-%d", testhelpers.RandomInt())
34
35 testData, err := client.BuildTestResources(ctx, resourceGroup, accountName, storage.KindStorage)
36 if err != nil {
37 t.Fatal(err)
38 }
39 defer client.DestroyTestResources(ctx, resourceGroup, accountName)
40
41 storageAuth := auth.NewSharedKeyLiteAuthorizer(accountName, testData.StorageAccountKey)
42 sharesClient := shares.NewWithEnvironment(client.Environment)
43 sharesClient.Client = client.PrepareWithAuthorizer(sharesClient.Client, storageAuth)
44
45 input := shares.CreateInput{
46 QuotaInGB: 10,
47 }
48 _, err = sharesClient.Create(ctx, accountName, shareName, input)
49 if err != nil {
50 t.Fatalf("Error creating fileshare: %s", err)
51 }
52 defer sharesClient.Delete(ctx, accountName, shareName, false)
53
54 filesClient := NewWithEnvironment(client.Environment)
55 filesClient.Client = client.PrepareWithAuthorizer(filesClient.Client, storageAuth)
56
57 // store files outside of this directory, since they're reused
58 file, err := os.Open("../../../testdata/" + fileName)
59 if err != nil {
60 t.Fatalf("Error opening: %s", err)
61 }
62
63 info, err := file.Stat()
64 if err != nil {
65 t.Fatalf("Error 'stat'-ing: %s", err)
66 }
67
68 t.Logf("[DEBUG] Creating Top Level File..")
69 createFileInput := CreateInput{
70 ContentLength: info.Size(),
71 ContentType: &contentType,
72 }
73 if _, err := filesClient.Create(ctx, accountName, shareName, "", fileName, createFileInput); err != nil {
74 t.Fatalf("Error creating Top-Level File: %s", err)
75 }
76
77 t.Logf("[DEBUG] Uploading File..")
78 if err := filesClient.PutFile(ctx, accountName, shareName, "", fileName, file, 4); err != nil {
79 t.Fatalf("Error uploading File: %s", err)
80 }
81
82 t.Logf("[DEBUG] Downloading file..")
83 _, downloadedBytes, err := filesClient.GetFile(ctx, accountName, shareName, "", fileName, 4)
84 if err != nil {
85 t.Fatalf("Error downloading file: %s", err)
86 }
87
88 t.Logf("[DEBUG] Asserting the files are the same size..")
89 expectedBytes := make([]byte, info.Size())
90 file.Read(expectedBytes)
91 if len(expectedBytes) != len(downloadedBytes) {
92 t.Fatalf("Expected %d bytes but got %d", len(expectedBytes), len(downloadedBytes))
93 }
94
95 t.Logf("[DEBUG] Asserting the files are the same content-wise..")
96 // overkill, but it's this or shasum-ing
97 for i := int64(0); i < info.Size(); i++ {
98 if expectedBytes[i] != downloadedBytes[i] {
99 t.Fatalf("Expected byte %d to be %q but got %q", i, expectedBytes[i], downloadedBytes[i])
100 }
101 }
102
103 t.Logf("[DEBUG] Deleting Top Level File..")
104 if _, err := filesClient.Delete(ctx, accountName, shareName, "", fileName); err != nil {
105 t.Fatalf("Error deleting Top-Level File: %s", err)
106 }
107
108 }