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 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
package tracker
import (
"context"
"errors"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/redistool"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/matcher"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/mock_modshared"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/mock_redis"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/testing/testhelpers"
"go.uber.org/zap/zaptest"
)
var (
_ Registerer = &RedisTracker{}
_ Tracker = &RedisTracker{}
_ Querier = &RedisTracker{}
)
const (
selfUrl = "grpc://1.1.1.1:10"
)
func TestRegisterConnection(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r, hash, _ := setupTracker(t)
gomock.InOrder(
hash.EXPECT().
Set(testhelpers.AgentId, selfUrl, gomock.Any()).
Return(func(ctx context.Context) error {
cancel()
return nil
}),
hash.EXPECT().
Clear(gomock.Any()),
)
go func() {
assert.NoError(t, r.RegisterTunnel(context.Background(), testhelpers.AgentId))
}()
require.NoError(t, r.Run(ctx))
}
func TestRegisterConnection_TwoConnections(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r, hash, _ := setupTracker(t)
gomock.InOrder(
hash.EXPECT().
Set(testhelpers.AgentId, selfUrl, gomock.Any()).
Return(func(ctx context.Context) error {
cancel()
return nil
}),
hash.EXPECT().
Clear(gomock.Any()),
)
go func() {
// Two registrations result in a single Set() call
assert.NoError(t, r.RegisterTunnel(context.Background(), testhelpers.AgentId)) // first
assert.NoError(t, r.RegisterTunnel(context.Background(), testhelpers.AgentId)) // second
}()
require.NoError(t, r.Run(ctx))
}
func TestUnregisterConnection(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r, hash, _ := setupTracker(t)
gomock.InOrder(
hash.EXPECT().
Set(testhelpers.AgentId, selfUrl, gomock.Any()).
Return(nopIOFunc),
hash.EXPECT().
Unset(testhelpers.AgentId, selfUrl).
Return(func(ctx context.Context) error {
cancel()
return nil
}),
hash.EXPECT().
Clear(gomock.Any()),
)
go func() {
assert.NoError(t, r.RegisterTunnel(context.Background(), testhelpers.AgentId))
assert.NoError(t, r.UnregisterTunnel(context.Background(), testhelpers.AgentId))
}()
require.NoError(t, r.Run(ctx))
}
func TestUnregisterConnection_TwoConnections(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r, hash, _ := setupTracker(t)
gomock.InOrder(
hash.EXPECT().
Set(testhelpers.AgentId, selfUrl, gomock.Any()).
Return(nopIOFunc),
hash.EXPECT().
Unset(testhelpers.AgentId, selfUrl).
Return(func(ctx context.Context) error {
cancel()
return nil
}),
hash.EXPECT().
Clear(gomock.Any()),
)
go func() {
assert.NoError(t, r.RegisterTunnel(context.Background(), testhelpers.AgentId))
assert.NoError(t, r.RegisterTunnel(context.Background(), testhelpers.AgentId))
assert.NoError(t, r.UnregisterTunnel(context.Background(), testhelpers.AgentId))
assert.NoError(t, r.UnregisterTunnel(context.Background(), testhelpers.AgentId))
}()
require.NoError(t, r.Run(ctx))
}
// This test ensures Unset() is only called when there are no registered connections i.e. it is NOT called
// for two RegisterTunnel() and a single UnregisterTunnel().
func TestUnregisterConnection_TwoConnections_OneSet(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r, hash, _ := setupTracker(t)
gomock.InOrder(
hash.EXPECT().
Set(testhelpers.AgentId, selfUrl, gomock.Any()).
Return(nopIOFunc),
hash.EXPECT().
Clear(gomock.Any()),
)
go func() {
assert.NoError(t, r.RegisterTunnel(context.Background(), testhelpers.AgentId))
assert.NoError(t, r.RegisterTunnel(context.Background(), testhelpers.AgentId))
assert.NoError(t, r.UnregisterTunnel(context.Background(), testhelpers.AgentId))
cancel()
}()
require.NoError(t, r.Run(ctx))
}
func TestGC(t *testing.T) {
r, hash, _ := setupTracker(t)
wasCalled := false
hash.EXPECT().
GC().
Return(func(_ context.Context) (int, error) {
wasCalled = true
return 3, nil
})
deleted, err := r.runGC(context.Background())
require.NoError(t, err)
assert.EqualValues(t, 3, deleted)
assert.True(t, wasCalled)
}
func TestRefreshRegistrations(t *testing.T) {
r, hash, _ := setupTracker(t)
hash.EXPECT().
Refresh(gomock.Any()).
Return(nopIOFunc)
assert.NoError(t, r.refreshRegistrations(context.Background(), time.Now()))
}
func TestKasUrlsByAgentId_HappyPath(t *testing.T) {
r, hash, _ := setupTracker(t)
hash.EXPECT().
Scan(gomock.Any(), testhelpers.AgentId, gomock.Any()).
Do(func(ctx context.Context, key interface{}, cb redistool.ScanCallback) (int, error) {
var done bool
done, err := cb(selfUrl, nil, nil)
if err != nil || done {
return 0, err
}
return 0, nil
})
var cbCalled int
err := r.KasUrlsByAgentId(context.Background(), testhelpers.AgentId, func(kasUrl string) (bool, error) {
cbCalled++
assert.Equal(t, selfUrl, kasUrl)
return false, nil
})
require.NoError(t, err)
assert.EqualValues(t, 1, cbCalled)
}
func TestKasUrlsByAgentId_ScanError(t *testing.T) {
r, hash, api := setupTracker(t)
gomock.InOrder(
hash.EXPECT().
Scan(gomock.Any(), testhelpers.AgentId, gomock.Any()).
Do(func(ctx context.Context, key interface{}, cb redistool.ScanCallback) (int, error) {
done, err := cb("", nil, errors.New("intended error"))
require.NoError(t, err)
assert.False(t, done)
return 0, nil
}),
api.EXPECT().
HandleProcessingError(gomock.Any(), gomock.Any(), testhelpers.AgentId, "Redis hash scan", matcher.ErrorEq("intended error")),
)
err := r.KasUrlsByAgentId(context.Background(), testhelpers.AgentId, func(kasUrl string) (bool, error) {
require.FailNow(t, "unexpected call")
return false, nil
})
require.NoError(t, err)
}
func setupTracker(t *testing.T) (*RedisTracker, *mock_redis.MockExpiringHashInterface[int64, string], *mock_modshared.MockApi) {
ctrl := gomock.NewController(t)
api := mock_modshared.NewMockApi(ctrl)
hash := mock_redis.NewMockExpiringHashInterface[int64, string](ctrl)
return &RedisTracker{
log: zaptest.NewLogger(t),
api: api,
refreshPeriod: time.Minute,
gcPeriod: time.Minute,
ownPrivateApiUrl: selfUrl,
tunnelsByAgentIdCount: make(map[int64]uint16),
tunnelsByAgentId: hash,
}, hash, api
}
func nopIOFunc(ctx context.Context) error {
return nil
}
|