1package files
2
3import (
4	"context"
5	"fmt"
6	"strings"
7	"testing"
8
9	"github.com/Azure/azure-sdk-for-go/profiles/latest/storage/mgmt/storage"
10	"github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/shares"
11	"github.com/tombuildsstuff/giovanni/storage/internal/auth"
12	"github.com/tombuildsstuff/giovanni/storage/internal/endpoints"
13	"github.com/tombuildsstuff/giovanni/testhelpers"
14)
15
16func TestFilesCopyAndWaitFromURL(t *testing.T) {
17	client, err := testhelpers.Build(t)
18	if err != nil {
19		t.Fatal(err)
20	}
21	ctx := context.TODO()
22	resourceGroup := fmt.Sprintf("acctestrg-%d", testhelpers.RandomInt())
23	accountName := fmt.Sprintf("acctestsa%s", testhelpers.RandomString())
24	shareName := fmt.Sprintf("share-%d", testhelpers.RandomInt())
25
26	testData, err := client.BuildTestResources(ctx, resourceGroup, accountName, storage.Storage)
27	if err != nil {
28		t.Fatal(err)
29	}
30	defer client.DestroyTestResources(ctx, resourceGroup, accountName)
31
32	storageAuth := auth.NewSharedKeyLiteAuthorizer(accountName, testData.StorageAccountKey)
33	sharesClient := shares.NewWithEnvironment(client.Environment)
34	sharesClient.Client = client.PrepareWithAuthorizer(sharesClient.Client, storageAuth)
35
36	input := shares.CreateInput{
37		QuotaInGB: 10,
38	}
39	_, err = sharesClient.Create(ctx, accountName, shareName, input)
40	if err != nil {
41		t.Fatalf("Error creating fileshare: %s", err)
42	}
43	defer sharesClient.Delete(ctx, accountName, shareName, false)
44
45	filesClient := NewWithEnvironment(client.Environment)
46	filesClient.Client = client.PrepareWithAuthorizer(filesClient.Client, storageAuth)
47
48	copiedFileName := "ubuntu.iso"
49	copyInput := CopyInput{
50		CopySource: "http://releases.ubuntu.com/14.04/ubuntu-14.04.6-desktop-amd64.iso",
51	}
52
53	t.Logf("[DEBUG] Copy And Waiting..")
54	if _, err := filesClient.CopyAndWait(ctx, accountName, shareName, "", copiedFileName, copyInput, DefaultCopyPollDuration); err != nil {
55		t.Fatalf("Error copy & waiting: %s", err)
56	}
57
58	t.Logf("[DEBUG] Asserting that the file's ready..")
59
60	props, err := filesClient.GetProperties(ctx, accountName, shareName, "", copiedFileName)
61	if err != nil {
62		t.Fatalf("Error retrieving file: %s", err)
63	}
64
65	if !strings.EqualFold(props.CopyStatus, "success") {
66		t.Fatalf("Expected the Copy Status to be `Success` but got %q", props.CopyStatus)
67	}
68}
69
70func TestFilesCopyAndWaitFromBlob(t *testing.T) {
71	client, err := testhelpers.Build(t)
72	if err != nil {
73		t.Fatal(err)
74	}
75	ctx := context.TODO()
76	resourceGroup := fmt.Sprintf("acctestrg-%d", testhelpers.RandomInt())
77	accountName := fmt.Sprintf("acctestsa%s", testhelpers.RandomString())
78	shareName := fmt.Sprintf("share-%d", testhelpers.RandomInt())
79
80	testData, err := client.BuildTestResources(ctx, resourceGroup, accountName, storage.Storage)
81	if err != nil {
82		t.Fatal(err)
83	}
84	defer client.DestroyTestResources(ctx, resourceGroup, accountName)
85
86	storageAuth := auth.NewSharedKeyLiteAuthorizer(accountName, testData.StorageAccountKey)
87	sharesClient := shares.NewWithEnvironment(client.Environment)
88	sharesClient.Client = client.PrepareWithAuthorizer(sharesClient.Client, storageAuth)
89
90	input := shares.CreateInput{
91		QuotaInGB: 10,
92	}
93	_, err = sharesClient.Create(ctx, accountName, shareName, input)
94	if err != nil {
95		t.Fatalf("Error creating fileshare: %s", err)
96	}
97	defer sharesClient.Delete(ctx, accountName, shareName, false)
98
99	filesClient := NewWithEnvironment(client.Environment)
100	filesClient.Client = client.PrepareWithAuthorizer(filesClient.Client, storageAuth)
101
102	originalFileName := "ubuntu.iso"
103	copiedFileName := "ubuntu-copied.iso"
104	copyInput := CopyInput{
105		CopySource: "http://releases.ubuntu.com/14.04/ubuntu-14.04.6-desktop-amd64.iso",
106	}
107	t.Logf("[DEBUG] Copy And Waiting the original file..")
108	if _, err := filesClient.CopyAndWait(ctx, accountName, shareName, "", originalFileName, copyInput, DefaultCopyPollDuration); err != nil {
109		t.Fatalf("Error copy & waiting: %s", err)
110	}
111
112	t.Logf("[DEBUG] Now copying that blob..")
113	duplicateInput := CopyInput{
114		CopySource: fmt.Sprintf("%s/%s/%s", endpoints.GetFileEndpoint(filesClient.BaseURI, accountName), shareName, originalFileName),
115	}
116	if _, err := filesClient.CopyAndWait(ctx, accountName, shareName, "", copiedFileName, duplicateInput, DefaultCopyPollDuration); err != nil {
117		t.Fatalf("Error copying duplicate: %s", err)
118	}
119
120	t.Logf("[DEBUG] Asserting that the file's ready..")
121	props, err := filesClient.GetProperties(ctx, accountName, shareName, "", copiedFileName)
122	if err != nil {
123		t.Fatalf("Error retrieving file: %s", err)
124	}
125
126	if !strings.EqualFold(props.CopyStatus, "success") {
127		t.Fatalf("Expected the Copy Status to be `Success` but got %q", props.CopyStatus)
128	}
129}
130