File: directory_test.go

package info (click to toggle)
golang-github-azure-azure-sdk-for-go 68.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 556,256 kB
  • sloc: javascript: 196; sh: 96; makefile: 7
file content (172 lines) | stat: -rw-r--r-- 4,753 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package storage

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

import chk "gopkg.in/check.v1"

type StorageDirSuite struct{}

var _ = chk.Suite(&StorageDirSuite{})

func (s *StorageDirSuite) TestListZeroDirsAndFiles(c *chk.C) {
	// create share
	cli := getFileClient(c)
	rec := cli.client.appendRecorder(c)
	defer rec.Stop()

	share := cli.GetShareReference(shareName(c))
	c.Assert(share.Create(nil), chk.IsNil)
	defer share.Delete(nil)

	// list contents, should be empty
	root := share.GetRootDirectoryReference()
	resp, err := root.ListDirsAndFiles(ListDirsAndFilesParameters{})
	c.Assert(err, chk.IsNil)
	c.Assert(resp.Directories, chk.IsNil)
	c.Assert(resp.Files, chk.IsNil)
}

func (s *StorageDirSuite) TestListDirsAndFiles(c *chk.C) {
	// create share
	cli := getFileClient(c)
	rec := cli.client.appendRecorder(c)
	defer rec.Stop()

	share := cli.GetShareReference(shareName(c))
	c.Assert(share.Create(nil), chk.IsNil)
	defer share.Delete(nil)

	// create a directory and a file
	root := share.GetRootDirectoryReference()
	dir := root.GetDirectoryReference("SomeDirectory")
	file := root.GetFileReference("lol.file")
	c.Assert(dir.Create(nil), chk.IsNil)
	c.Assert(file.Create(512, nil), chk.IsNil)

	// list contents
	resp, err := root.ListDirsAndFiles(ListDirsAndFilesParameters{})
	c.Assert(err, chk.IsNil)
	c.Assert(len(resp.Directories), chk.Equals, 1)
	c.Assert(len(resp.Files), chk.Equals, 1)
	c.Assert(resp.Directories[0].Name, chk.Equals, dir.Name)
	c.Assert(resp.Files[0].Name, chk.Equals, file.Name)

	// delete file
	del, err := file.DeleteIfExists(nil)
	c.Assert(err, chk.IsNil)
	c.Assert(del, chk.Equals, true)

	ok, err := file.Exists()
	c.Assert(err, chk.IsNil)
	c.Assert(ok, chk.Equals, false)
}

func (s *StorageDirSuite) TestCreateDirectory(c *chk.C) {
	cli := getFileClient(c)
	rec := cli.client.appendRecorder(c)
	defer rec.Stop()

	share := cli.GetShareReference(shareName(c))
	c.Assert(share.Create(nil), chk.IsNil)
	defer share.Delete(nil)

	root := share.GetRootDirectoryReference()
	dir := root.GetDirectoryReference("dir")
	err := dir.Create(nil)
	c.Assert(err, chk.IsNil)

	// check properties
	c.Assert(dir.Properties.Etag, chk.Not(chk.Equals), "")
	c.Assert(dir.Properties.LastModified, chk.Not(chk.Equals), "")

	// delete directory and verify
	c.Assert(dir.Delete(nil), chk.IsNil)
	exists, err := dir.Exists()
	c.Assert(err, chk.IsNil)
	c.Assert(exists, chk.Equals, false)
}

func (s *StorageDirSuite) TestCreateDirectoryIfNotExists(c *chk.C) {
	cli := getFileClient(c)
	rec := cli.client.appendRecorder(c)
	defer rec.Stop()

	// create share
	share := cli.GetShareReference(shareName(c))
	share.Create(nil)
	defer share.Delete(nil)

	// create non exisiting directory
	root := share.GetRootDirectoryReference()
	dir := root.GetDirectoryReference("dir")
	exists, err := dir.CreateIfNotExists(nil)
	c.Assert(err, chk.IsNil)
	c.Assert(exists, chk.Equals, true)

	c.Assert(dir.Properties.Etag, chk.Not(chk.Equals), "")
	c.Assert(dir.Properties.LastModified, chk.Not(chk.Equals), "")

	c.Assert(dir.Delete(nil), chk.IsNil)
	exists, err = dir.Exists()
	c.Assert(err, chk.IsNil)
	c.Assert(exists, chk.Equals, false)
}

func (s *StorageDirSuite) TestCreateDirectoryIfExists(c *chk.C) {
	// create share
	cli := getFileClient(c)
	rec := cli.client.appendRecorder(c)
	defer rec.Stop()

	share := cli.GetShareReference(shareName(c))
	share.Create(nil)
	defer share.Delete(nil)

	// create directory
	root := share.GetRootDirectoryReference()
	dir := root.GetDirectoryReference("dir")
	dir.Create(nil)

	// try to create directory
	exists, err := dir.CreateIfNotExists(nil)
	c.Assert(err, chk.IsNil)
	c.Assert(exists, chk.Equals, false)

	// check properties
	c.Assert(dir.Properties.Etag, chk.Not(chk.Equals), "")
	c.Assert(dir.Properties.LastModified, chk.Not(chk.Equals), "")

	// delete directory
	c.Assert(dir.Delete(nil), chk.IsNil)
}

func (s *StorageDirSuite) TestDirectoryMetadata(c *chk.C) {
	// create share
	cli := getFileClient(c)
	rec := cli.client.appendRecorder(c)
	defer rec.Stop()

	share := cli.GetShareReference(shareName(c))
	c.Assert(share.Create(nil), chk.IsNil)
	defer share.Delete(nil)
	root := share.GetRootDirectoryReference()

	dir := root.GetDirectoryReference("testdir")
	c.Assert(dir.Create(nil), chk.IsNil)

	// get metadata, shouldn't be any
	c.Assert(dir.Metadata, chk.IsNil)

	// set some custom metadata
	md := map[string]string{
		"something": "somethingvalue",
		"another":   "anothervalue",
	}
	dir.Metadata = md
	c.Assert(dir.SetMetadata(nil), chk.IsNil)

	// retrieve and verify
	c.Assert(dir.FetchAttributes(nil), chk.IsNil)
	c.Assert(dir.Metadata, chk.DeepEquals, md)
}