1
|
|
|
package files |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"io/ioutil" |
5
|
|
|
"os" |
6
|
|
|
"testing" |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
func validateLength(t *testing.T, fsr FileSystemRepository, expectedLength uint8) { |
10
|
|
|
fsList, e := fsr.List() |
11
|
|
|
if e != nil { |
12
|
|
|
t.Errorf("Could not get listing: %v", e.Error()) |
13
|
|
|
} |
14
|
|
|
if len(fsList) != int(expectedLength) { |
15
|
|
|
t.Errorf("Should have a length of %d but was %d", expectedLength, len(fsList)) |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
func validateRecordFound(t *testing.T, fsr FileSystemRepository) { |
20
|
|
|
fs, err := fsr.FindByName("My FS") |
21
|
|
|
if err != nil { |
22
|
|
|
t.Errorf("Unable to find record: %v", err.Error()) |
23
|
|
|
t.Fail() |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if fs.Description != "This is a description" { |
27
|
|
|
t.Errorf("Invalid description: %v", fs.Description) |
28
|
|
|
t.Fail() |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
func createTestFile(t *testing.T) string { |
33
|
|
|
file, err := ioutil.TempFile("", "file*.repo") |
34
|
|
|
var path string |
35
|
|
|
if err == nil { |
36
|
|
|
path = file.Name() |
37
|
|
|
file.Close() |
38
|
|
|
os.Remove(file.Name()) |
39
|
|
|
return path |
40
|
|
|
} |
41
|
|
|
t.Error("Unable to create temp file") |
42
|
|
|
t.Fail() |
43
|
|
|
return path |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
func getRepository(t *testing.T, path string) FileSystemRepository { |
47
|
|
|
fsr, err := GetFileSystemRepositoryFromPath(path) |
48
|
|
|
if err != nil { |
49
|
|
|
t.Errorf("Unable to build file system repository: %v", err.Error()) |
50
|
|
|
t.Fail() |
51
|
|
|
} |
52
|
|
|
return fsr |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
func TestFileSystemRepository(t *testing.T) { |
56
|
|
|
path := createTestFile(t) |
57
|
|
|
fsr := getRepository(t, path) |
58
|
|
|
|
59
|
|
|
validateLength(t, fsr, 0) |
60
|
|
|
|
61
|
|
|
if b := fsr.Store(FileSystem{ |
62
|
|
|
Name: "My FS", |
63
|
|
|
Description: "This is a description", |
64
|
|
|
Addresses: []string{"localhost:9000"}, |
65
|
|
|
}); b != nil { |
66
|
|
|
t.Errorf("Could not store record: %v", b.Error()) |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
validateLength(t, fsr, 1) |
70
|
|
|
validateRecordFound(t, fsr) |
71
|
|
|
|
72
|
|
|
// re-initialize |
73
|
|
|
fsr = getRepository(t, path) |
74
|
|
|
validateLength(t, fsr, 1) |
75
|
|
|
validateRecordFound(t, fsr) |
76
|
|
|
|
77
|
|
|
if b := fsr.Remove("My FS"); b != nil { |
78
|
|
|
t.Errorf("Error removing record: %v", b.Error()) |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// re-initialize |
82
|
|
|
fsr = getRepository(t, path) |
83
|
|
|
validateLength(t, fsr, 0) |
84
|
|
|
} |
85
|
|
|
|