forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolder_branch_status_test.go
178 lines (152 loc) · 5.08 KB
/
folder_branch_status_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
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
173
174
175
176
177
178
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libkbfs
import (
"errors"
"fmt"
"reflect"
"sort"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/keybase/kbfs/kbfsblock"
"github.com/keybase/kbfs/kbfscodec"
"github.com/keybase/kbfs/kbfscrypto"
"github.com/keybase/kbfs/kbfsmd"
"github.com/keybase/kbfs/tlf"
"github.com/stretchr/testify/require"
"golang.org/x/net/context"
)
func fbStatusTestInit(t *testing.T) (*gomock.Controller, *ConfigMock,
*folderBranchStatusKeeper, *MockNodeCache) {
ctr := NewSafeTestReporter(t)
mockCtrl := gomock.NewController(ctr)
config := NewConfigMock(mockCtrl, ctr)
config.mockKbpki.EXPECT().ResolveImplicitTeam(
gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().
Return(ImplicitTeamInfo{}, errors.New("No such team"))
nodeCache := NewMockNodeCache(mockCtrl)
fbsk := newFolderBranchStatusKeeper(config, nodeCache)
interposeDaemonKBPKI(config, "alice", "bob")
return mockCtrl, config, fbsk, nodeCache
}
func fbStatusTestShutdown(mockCtrl *gomock.Controller, config *ConfigMock) {
config.ctr.CheckForFailures()
mockCtrl.Finish()
}
func newMockNode(mockCtrl *gomock.Controller) *MockNode {
n := NewMockNode(mockCtrl)
id := NewMockNodeID(mockCtrl)
n.EXPECT().GetID().AnyTimes().Return(id)
return n
}
func TestFBStatusSignal(t *testing.T) {
mockCtrl, config, fbsk, nodeCache := fbStatusTestInit(t)
defer fbStatusTestShutdown(mockCtrl, config)
ctx := context.Background()
_, c, err := fbsk.getStatus(ctx, nil)
if err != nil {
t.Fatalf("Couldn't get status: %v", err)
}
n := newMockNode(mockCtrl)
p1 := path{path: []pathNode{{Name: "a1"}, {Name: "b1"}}}
nodeCache.EXPECT().PathFromNode(mockNodeMatcher{n}).AnyTimes().Return(p1)
fbsk.addDirtyNode(n)
<-c
_, c, err = fbsk.getStatus(ctx, nil)
if err != nil {
t.Fatalf("Couldn't get status: %v", err)
}
// no change should result in no signal
fbsk.addDirtyNode(n)
select {
case <-c:
t.Fatalf("Status should not have signalled a change")
default:
}
}
// mockNodeMatcher is needed to compare mock nodes -- for some reason
// the default equality comparison doesn't work in gomock.
type mockNodeMatcher struct {
node *MockNode
}
func (m mockNodeMatcher) Matches(x interface{}) bool {
n, ok := x.(*MockNode)
if !ok {
return false
}
return n == m.node
}
func (m mockNodeMatcher) String() string {
return fmt.Sprintf("Matches node %v", m.node)
}
func checkStringSlices(t *testing.T, expected, got []string) {
sort.Strings(expected)
sort.Strings(got)
if !reflect.DeepEqual(expected, got) {
t.Errorf("Expected %v; got %v", expected, got)
}
}
func TestFBStatusAllFields(t *testing.T) {
mockCtrl, config, fbsk, nodeCache := fbStatusTestInit(t)
defer fbStatusTestShutdown(mockCtrl, config)
ctx := context.Background()
// make a new root metadata
id := tlf.FakeID(1, tlf.Private)
h := parseTlfHandleOrBust(t, config, "alice", tlf.Private, id)
u := h.FirstResolvedWriter()
rmd, err := makeInitialRootMetadata(config.MetadataVersion(), id, h)
require.NoError(t, err)
rmd.SetUnmerged()
rmd.SetLastModifyingWriter(u.AsUserOrBust())
signingKey := kbfscrypto.MakeFakeSigningKeyOrBust("fake seed")
signer := kbfscrypto.SigningKeySigner{Key: signingKey}
err = rmd.bareMd.SignWriterMetadataInternally(
ctx, kbfscodec.NewMsgpack(), signer)
require.NoError(t, err)
// make two nodes with expected PathFromNode calls
n1 := newMockNode(mockCtrl)
p1 := path{path: []pathNode{{Name: "a1"}, {Name: "b1"}}}
nodeCache.EXPECT().PathFromNode(mockNodeMatcher{n1}).AnyTimes().Return(p1)
n2 := newMockNode(mockCtrl)
p2 := path{path: []pathNode{{Name: "a2"}, {Name: "b2"}}}
nodeCache.EXPECT().PathFromNode(mockNodeMatcher{n2}).AnyTimes().Return(p2)
fbsk.setRootMetadata(
MakeImmutableRootMetadata(rmd, signingKey.GetVerifyingKey(),
kbfsmd.FakeID(1), time.Now(), true))
fbsk.addDirtyNode(n1)
fbsk.addDirtyNode(n2)
config.mockRekeyQueue.EXPECT().IsRekeyPending(id)
config.mockBcache.EXPECT().GetWithPrefetch(gomock.Any()).
Return(nil, NoPrefetch, NoCacheEntry, nil)
config.mockClock.EXPECT().Now().AnyTimes().Return(time.Now())
config.mockBserv.EXPECT().GetUserQuotaInfo(gomock.Any()).AnyTimes().Return(
&kbfsblock.QuotaInfo{
Total: &kbfsblock.UsageStat{
Bytes: map[kbfsblock.UsageType]int64{
kbfsblock.UsageWrite: 10,
kbfsblock.UsageGitWrite: 20,
},
},
Limit: 1000,
GitLimit: 2000,
}, nil)
// check the returned status for accuracy
status, _, err := fbsk.getStatus(ctx, nil)
if err != nil {
t.Fatalf("Couldn't get status: %v", err)
}
if !status.Staged {
t.Errorf("Status does not show staged changes")
}
if string(status.HeadWriter) != "alice" {
t.Errorf("Unexpected head writer in status: %s", status.HeadWriter)
}
expectedDirtyPaths := []string{p1.String(), p2.String()}
checkStringSlices(t, expectedDirtyPaths, status.DirtyPaths)
require.Equal(t, int64(10), status.UsageBytes)
require.Equal(t, int64(1000), status.LimitBytes)
require.Equal(t, int64(20), status.GitUsageBytes)
require.Equal(t, int64(2000), status.GitLimitBytes)
}