-
Notifications
You must be signed in to change notification settings - Fork 378
/
clitestutil.go
144 lines (107 loc) · 3.11 KB
/
clitestutil.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
// Package clitestutil contains utilities for
package clitestutil
import (
"strings"
"testing"
"time"
)
// SourceInfo reprents a single source (user@host:/path) with its snapshots.
type SourceInfo struct {
User string
Host string
Path string
Snapshots []SnapshotInfo
}
// SnapshotInfo represents a single snapshot information.
type SnapshotInfo struct {
ObjectID string
SnapshotID string
Time time.Time
}
// MustParseSnapshots parsers the output of 'snapshot list'.
func MustParseSnapshots(t *testing.T, lines []string) []SourceInfo {
t.Helper()
var (
result []SourceInfo
currentSource *SourceInfo
)
for _, l := range lines {
if l == "" {
continue
}
if strings.HasPrefix(l, " ") {
if currentSource == nil {
t.Errorf("snapshot without a source: %q", l)
return nil
}
currentSource.Snapshots = append(currentSource.Snapshots, mustParseSnaphotInfo(t, l[2:]))
continue
}
s := mustParseSourceInfo(t, l)
result = append(result, s)
currentSource = &result[len(result)-1]
}
return result
}
func mustParseSnaphotInfo(t *testing.T, l string) SnapshotInfo {
t.Helper()
parts := strings.Split(l, " ")
ts, err := time.Parse("2006-01-02 15:04:05 MST", strings.Join(parts[0:3], " "))
if err != nil {
t.Fatalf("err: %v", err)
}
manifestField := parts[7]
snapID := strings.TrimPrefix(manifestField, "manifest:")
return SnapshotInfo{
Time: ts,
ObjectID: parts[3],
SnapshotID: snapID,
}
}
func mustParseSourceInfo(t *testing.T, l string) SourceInfo {
t.Helper()
p1 := strings.Index(l, "@")
p2 := strings.Index(l, ":")
if p1 >= 0 && p2 > p1 {
return SourceInfo{User: l[0:p1], Host: l[p1+1 : p2], Path: l[p2+1:]}
}
t.Fatalf("can't parse source info: %q", l)
return SourceInfo{}
}
// DirEntry represents directory entry.
type DirEntry struct {
Name string
ObjectID string
}
func mustParseDirectoryEntries(lines []string) []DirEntry {
var result []DirEntry
for _, l := range lines {
parts := strings.Fields(l)
result = append(result, DirEntry{
Name: parts[6],
ObjectID: parts[5],
})
}
return result
}
type testEnv interface {
RunAndExpectSuccess(t *testing.T, args ...string) []string
}
// ListSnapshotsAndExpectSuccess lists given snapshots and parses the output.
func ListSnapshotsAndExpectSuccess(t *testing.T, e testEnv, targets ...string) []SourceInfo {
t.Helper()
lines := e.RunAndExpectSuccess(t, append([]string{"snapshot", "list", "-l", "--manifest-id"}, targets...)...)
return MustParseSnapshots(t, lines)
}
// ListDirectory lists a given directory and returns directory entries.
func ListDirectory(t *testing.T, e testEnv, targets ...string) []DirEntry {
t.Helper()
lines := e.RunAndExpectSuccess(t, append([]string{"ls", "-l"}, targets...)...)
return mustParseDirectoryEntries(lines)
}
// ListDirectoryRecursive lists a given directory recursively and returns directory entries.
func ListDirectoryRecursive(t *testing.T, e testEnv, targets ...string) []DirEntry {
t.Helper()
lines := e.RunAndExpectSuccess(t, append([]string{"ls", "-lr"}, targets...)...)
return mustParseDirectoryEntries(lines)
}