-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathconnectionlog.go
More file actions
129 lines (113 loc) · 4.45 KB
/
connectionlog.go
File metadata and controls
129 lines (113 loc) · 4.45 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
package connectionlog
import (
"context"
"sync"
"testing"
"github.com/google/uuid"
"github.com/coder/coder/v2/coderd/database"
)
type ConnectionLogger interface {
Upsert(ctx context.Context, clog database.UpsertConnectionLogParams) error
}
type nop struct{}
func NewNop() ConnectionLogger {
return nop{}
}
func (nop) Upsert(context.Context, database.UpsertConnectionLogParams) error {
return nil
}
func NewFake() *FakeConnectionLogger {
return &FakeConnectionLogger{}
}
type FakeConnectionLogger struct {
mu sync.Mutex
upsertions []database.UpsertConnectionLogParams
}
func (m *FakeConnectionLogger) Reset() {
m.mu.Lock()
defer m.mu.Unlock()
m.upsertions = make([]database.UpsertConnectionLogParams, 0)
}
func (m *FakeConnectionLogger) ConnectionLogs() []database.UpsertConnectionLogParams {
m.mu.Lock()
defer m.mu.Unlock()
return m.upsertions
}
func (m *FakeConnectionLogger) Upsert(_ context.Context, clog database.UpsertConnectionLogParams) error {
m.mu.Lock()
defer m.mu.Unlock()
m.upsertions = append(m.upsertions, clog)
return nil
}
func (m *FakeConnectionLogger) Contains(t testing.TB, expected database.UpsertConnectionLogParams) bool {
m.mu.Lock()
defer m.mu.Unlock()
for idx, cl := range m.upsertions {
if expected.ID != uuid.Nil && cl.ID != expected.ID {
t.Logf("connection log %d: expected ID %s, got %s", idx+1, expected.ID, cl.ID)
continue
}
if expected.OrganizationID != uuid.Nil && cl.OrganizationID != expected.OrganizationID {
t.Logf("connection log %d: expected OrganizationID %s, got %s", idx+1, expected.OrganizationID, cl.OrganizationID)
continue
}
if expected.WorkspaceOwnerID != uuid.Nil && cl.WorkspaceOwnerID != expected.WorkspaceOwnerID {
t.Logf("connection log %d: expected WorkspaceOwnerID %s, got %s", idx+1, expected.WorkspaceOwnerID, cl.WorkspaceOwnerID)
continue
}
if expected.WorkspaceID != uuid.Nil && cl.WorkspaceID != expected.WorkspaceID {
t.Logf("connection log %d: expected WorkspaceID %s, got %s", idx+1, expected.WorkspaceID, cl.WorkspaceID)
continue
}
if expected.WorkspaceName != "" && cl.WorkspaceName != expected.WorkspaceName {
t.Logf("connection log %d: expected WorkspaceName %s, got %s", idx+1, expected.WorkspaceName, cl.WorkspaceName)
continue
}
if expected.AgentName != "" && cl.AgentName != expected.AgentName {
t.Logf("connection log %d: expected AgentName %s, got %s", idx+1, expected.AgentName, cl.AgentName)
continue
}
if expected.Type != "" && cl.Type != expected.Type {
t.Logf("connection log %d: expected Type %s, got %s", idx+1, expected.Type, cl.Type)
continue
}
if expected.Code.Valid && cl.Code.Int32 != expected.Code.Int32 {
t.Logf("connection log %d: expected Code %d, got %d", idx+1, expected.Code.Int32, cl.Code.Int32)
continue
}
if expected.IP.Valid && cl.IP.IPNet.String() != expected.IP.IPNet.String() {
t.Logf("connection log %d: expected IP %s, got %s", idx+1, expected.IP.IPNet, cl.IP.IPNet)
continue
}
if expected.UserAgent.Valid && cl.UserAgent.String != expected.UserAgent.String {
t.Logf("connection log %d: expected UserAgent %s, got %s", idx+1, expected.UserAgent.String, cl.UserAgent.String)
continue
}
if expected.UserID.Valid && cl.UserID.UUID != expected.UserID.UUID {
t.Logf("connection log %d: expected UserID %s, got %s", idx+1, expected.UserID.UUID, cl.UserID.UUID)
continue
}
if expected.SlugOrPort.Valid && cl.SlugOrPort.String != expected.SlugOrPort.String {
t.Logf("connection log %d: expected SlugOrPort %s, got %s", idx+1, expected.SlugOrPort.String, cl.SlugOrPort.String)
continue
}
if expected.ConnectionID.Valid && cl.ConnectionID.UUID != expected.ConnectionID.UUID {
t.Logf("connection log %d: expected ConnectionID %s, got %s", idx+1, expected.ConnectionID.UUID, cl.ConnectionID.UUID)
continue
}
if expected.DisconnectReason.Valid && cl.DisconnectReason.String != expected.DisconnectReason.String {
t.Logf("connection log %d: expected DisconnectReason %s, got %s", idx+1, expected.DisconnectReason.String, cl.DisconnectReason.String)
continue
}
if !expected.Time.IsZero() && expected.Time != cl.Time {
t.Logf("connection log %d: expected Time %s, got %s", idx+1, expected.Time, cl.Time)
continue
}
if expected.ConnectionStatus != "" && expected.ConnectionStatus != cl.ConnectionStatus {
t.Logf("connection log %d: expected ConnectionStatus %s, got %s", idx+1, expected.ConnectionStatus, cl.ConnectionStatus)
continue
}
return true
}
return false
}