Repository navigation
Expand file tree
/
Copy pathtouch_test.go
More file actions
232 lines (202 loc) · 6.79 KB
/
Copy pathtouch_test.go
File metadata and controls
232 lines (202 loc) · 6.79 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"context"
"encoding/json"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/tracyhatemice/who/internal/storage"
)
func readTouchRecord(t *testing.T, path string) TouchRecord {
t.Helper()
raw, err := os.ReadFile(path)
if err != nil {
t.Fatalf("reading touch file: %v", err)
}
var rec TouchRecord
if err := json.Unmarshal(raw, &rec); err != nil {
t.Fatalf("touch file is not valid JSON: %v\n%s", err, raw)
}
return rec
}
func TestWriteTouchFilePlainName(t *testing.T) {
path := filepath.Join(t.TempDir(), "alice.json")
srv := &Server{store: NewStore(), aliases: map[string][]string{}}
srv.store.Load("alice", "203.0.113.50", 12345)
srv.writeTouchFile(TouchEntry{IAM: "alice", Path: path})
rec := readTouchRecord(t, path)
want := TouchRecord{IAM: "alice", IP: []string{"203.0.113.50"}, LastUpdate: 12345}
if !reflect.DeepEqual(rec, want) {
t.Errorf("record = %+v, want %+v", rec, want)
}
}
func TestWriteTouchFileUnknownNameWritesEmptyRecord(t *testing.T) {
path := filepath.Join(t.TempDir(), "ghost.json")
srv := &Server{store: NewStore(), aliases: map[string][]string{}}
srv.writeTouchFile(TouchEntry{IAM: "ghost", Path: path})
raw, err := os.ReadFile(path)
if err != nil {
t.Fatalf("touch file should exist on write even without data: %v", err)
}
rec := readTouchRecord(t, path)
if rec.IAM != "ghost" || len(rec.IP) != 0 || rec.LastUpdate != 0 {
t.Errorf("record = %+v, want empty record for ghost", rec)
}
// ip must serialize as [], not null
var asMap map[string]any
if err := json.Unmarshal(raw, &asMap); err != nil {
t.Fatal(err)
}
if asMap["ip"] == nil {
t.Errorf("ip should be [], got null in:\n%s", raw)
}
}
func TestWriteTouchFileAliasFlattensMembers(t *testing.T) {
path := filepath.Join(t.TempDir(), "julia.json")
srv := &Server{
store: NewStore(),
aliases: map[string][]string{"julia": {"juliav4", "juliav6"}},
}
srv.store.Load("juliav4", "111.111.111.111", 100)
srv.store.Load("juliav6", "2001:db8::1", 200)
srv.writeTouchFile(TouchEntry{IAM: "julia", Path: path})
rec := readTouchRecord(t, path)
want := TouchRecord{
IAM: "julia",
IP: []string{"111.111.111.111", "2001:db8::1"},
LastUpdate: 200,
}
if !reflect.DeepEqual(rec, want) {
t.Errorf("record = %+v, want %+v", rec, want)
}
}
func TestWriteTouchFileAliasSkipsMembersWithoutIP(t *testing.T) {
path := filepath.Join(t.TempDir(), "julia.json")
srv := &Server{
store: NewStore(),
aliases: map[string][]string{"julia": {"juliav4", "juliav6"}},
}
srv.store.Load("juliav4", "111.111.111.111", 100)
srv.writeTouchFile(TouchEntry{IAM: "julia", Path: path})
rec := readTouchRecord(t, path)
want := TouchRecord{IAM: "julia", IP: []string{"111.111.111.111"}, LastUpdate: 100}
if !reflect.DeepEqual(rec, want) {
t.Errorf("record = %+v, want %+v", rec, want)
}
}
func TestWriteAllTouchFilesSkipsInvalidEntries(t *testing.T) {
dir := t.TempDir()
alicePath := filepath.Join(dir, "alice.json")
srv := &Server{
store: NewStore(),
aliases: map[string][]string{},
touch: []TouchEntry{
{IAM: "alice", Path: alicePath},
{IAM: "", Path: filepath.Join(dir, "noname.json")},
{IAM: "bob", Path: ""},
},
}
srv.store.Load("alice", "203.0.113.50", 12345)
srv.writeAllTouchFiles()
if _, err := os.Stat(alicePath); err != nil {
t.Errorf("alice touch file should be created on startup: %v", err)
}
if _, err := os.Stat(filepath.Join(dir, "noname.json")); err == nil {
t.Error("entry without iam should be skipped")
}
}
func TestTouchEntriesForName(t *testing.T) {
srv := &Server{
aliases: map[string][]string{"julia": {"juliav4", "juliav6"}},
touch: []TouchEntry{
{IAM: "julia", Path: "/tmp/julia.json"},
{IAM: "juliav4", Path: "/tmp/juliav4.json"},
{IAM: "other", Path: "/tmp/other.json"},
},
}
got := srv.touchEntriesFor("juliav4")
var paths []string
for _, e := range got {
paths = append(paths, e.Path)
}
want := []string{"/tmp/julia.json", "/tmp/juliav4.json"}
if !reflect.DeepEqual(paths, want) {
t.Errorf("touchEntriesFor(juliav4) paths = %v, want %v", paths, want)
}
}
func assertLatestDelivery(t *testing.T, db *storage.DB, key, wantStatus string) storage.DeliveryLogEntry {
t.Helper()
latest, err := db.LatestDeliveriesByTarget(context.Background())
if err != nil {
t.Fatalf("LatestDeliveriesByTarget: %v", err)
}
got, ok := latest[key]
if !ok || got.Status != wantStatus {
t.Fatalf("delivery[%q] = %+v (exists=%v), want status %q", key, got, ok, wantStatus)
}
return got
}
func TestTouchSkipsDisabledAndWriteTouchSingleRecordsDelivery(t *testing.T) {
ctx := context.Background()
db, _ := openAccessLogTestDB(t)
dir := t.TempDir()
enabledPath := filepath.Join(dir, "enabled.json")
disabledPath := filepath.Join(dir, "disabled.json")
row, err := db.CreateTouchRecord(ctx, storage.TouchRecordRow{
IAM: "alice",
Path: disabledPath,
Enabled: false,
})
if err != nil {
t.Fatalf("CreateTouchRecord: %v", err)
}
srv := &Server{
db: db,
store: NewStore(),
aliases: map[string][]string{},
touch: []TouchEntry{
{ID: 10, IAM: "alice", Path: enabledPath, Disabled: false},
{ID: row.ID, IAM: "alice", Path: disabledPath, Disabled: true},
},
}
srv.store.Load("alice", "203.0.113.50", 12345)
// Disabled entries are skipped in startup writeAllTouchFiles and touchEntriesFor.
srv.writeAllTouchFiles()
if _, statErr := os.Stat(enabledPath); statErr != nil {
t.Fatalf("enabled touch file should exist: %v", statErr)
}
if _, statErr := os.Stat(disabledPath); statErr == nil {
t.Fatal("disabled touch file should be skipped by writeAllTouchFiles")
}
affected := srv.touchEntriesFor("alice")
if len(affected) != 1 || affected[0].Path != enabledPath {
t.Fatalf("touchEntriesFor(alice) = %+v, want only enabled entry", affected)
}
// WriteTouchSingle writes on demand even when Disabled and records delivery_logs.
if writeErr := srv.WriteTouchSingle(TouchEntry{
ID: row.ID,
IAM: "alice",
Path: disabledPath,
Disabled: true,
}, 0); writeErr != nil {
t.Fatalf("WriteTouchSingle: %v", writeErr)
}
rec := readTouchRecord(t, disabledPath)
if !reflect.DeepEqual(rec.IP, []string{"203.0.113.50"}) {
t.Fatalf("unexpected touch record: %+v", rec)
}
gotDeliv := assertLatestDelivery(t, db, "touch:1", "ok")
if gotDeliv.Target != disabledPath {
t.Fatalf("delivery target = %q, want %q", gotDeliv.Target, disabledPath)
}
// WriteTouchSingle records error status when file write fails.
badPath := filepath.Join(dir, "missing-dir", "fail.json")
if writeErr := srv.WriteTouchSingle(TouchEntry{ID: 99, IAM: "alice", Path: badPath}, 0); writeErr == nil {
t.Fatal("expected error writing to missing parent directory")
}
errDeliv := assertLatestDelivery(t, db, "touch:99", "error")
if errDeliv.Error == "" {
t.Fatalf("expected non-empty error message on failed touch delivery: %+v", errDeliv)
}
}