-
Notifications
You must be signed in to change notification settings - Fork 27
/
repository_test.go
117 lines (96 loc) · 2.51 KB
/
repository_test.go
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
/*
* knoxite
* Copyright (c) 2016-2017, Christian Muehlhaeuser <muesli@gmail.com>
*
* For license see LICENSE
*/
package knoxite
import (
"io/ioutil"
"os"
"testing"
)
func TestRepositoryCreate(t *testing.T) {
testPassword := "this_is_a_password"
dir, err := ioutil.TempDir("", "knoxite")
if err != nil {
t.Errorf("Failed creating temporary dir for repository: %s", err)
return
}
defer os.RemoveAll(dir)
_, err = NewRepository(dir, testPassword)
if err != nil {
t.Errorf("Failed creating repository: %s", err)
return
}
_, err = OpenRepository(dir, testPassword)
if err != nil {
t.Errorf("Failed opening repository: %s", err)
return
}
}
func TestRepositoryCreateError(t *testing.T) {
testPassword := "this_is_a_password"
_, err := NewRepository("invalidprotocol://foo", testPassword)
if err != ErrInvalidRepositoryURL {
t.Errorf("Expected %v, got %v", ErrInvalidRepositoryURL, err)
}
}
func TestRepositoryIsEmpty(t *testing.T) {
testPassword := "this_is_a_password"
dir, err := ioutil.TempDir("", "knoxite")
if err != nil {
t.Errorf("Failed creating temporary dir for repository: %s", err)
return
}
defer os.RemoveAll(dir)
r, _ := NewRepository(dir, testPassword)
if !r.IsEmpty() {
t.Error("Repository should be empty")
}
vol, _ := NewVolume("test", "")
_ = r.AddVolume(vol)
if !r.IsEmpty() {
t.Error("Repository should be empty")
}
snapshot, _ := NewSnapshot("test_snapshot")
_ = snapshot.Save(&r)
_ = vol.AddSnapshot(snapshot.ID)
if r.IsEmpty() {
t.Error("Repository should not be empty")
}
}
func TestRepositoryChangePassword(t *testing.T) {
testPassword := "this_is_a_password"
newPassword := "this_is_another_password"
dir, err := ioutil.TempDir("", "knoxite")
if err != nil {
t.Errorf("Failed creating temporary dir for repository: %s", err)
return
}
defer os.RemoveAll(dir)
_, err = NewRepository(dir, testPassword)
if err != nil {
t.Errorf("Failed creating repository: %s", err)
return
}
repo, err := OpenRepository(dir, testPassword)
if err != nil {
t.Errorf("Failed opening repository: %s", err)
return
}
if repo.ChangePassword(newPassword) != nil {
t.Errorf("Failed to change repository password: %s", err)
return
}
_, err = OpenRepository(dir, testPassword)
if err == nil {
t.Errorf("Repository can still be opened with the old password after changing it: %s", err)
return
}
_, err = OpenRepository(dir, newPassword)
if err != nil {
t.Errorf("Failed opening repository with new password after changing it: %s", err)
return
}
}