| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package mirrors |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"reflect" |
| 9 |
"strconv" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
"unsafe" |
| 13 |
|
| 14 |
"github.com/etix/mirrorbits/filesystem" |
| 15 |
"github.com/etix/mirrorbits/network" |
| 16 |
. "github.com/etix/mirrorbits/testing" |
| 17 |
_ "github.com/rafaeljusto/redigomock" |
| 18 |
) |
| 19 |
|
| 20 |
func TestNewCache(t *testing.T) { |
| 21 |
_, conn := PrepareRedisTest() |
| 22 |
conn.ConnectPubsub() |
| 23 |
|
| 24 |
c := NewCache(nil) |
| 25 |
if c != nil { |
| 26 |
t.Fatalf("Expected invalid instance") |
| 27 |
} |
| 28 |
|
| 29 |
c = NewCache(conn) |
| 30 |
if c == nil { |
| 31 |
t.Fatalf("No valid instance returned") |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
type TestValue struct { |
| 36 |
value string |
| 37 |
} |
| 38 |
|
| 39 |
func (f *TestValue) Size() int { |
| 40 |
return int(unsafe.Sizeof(f.value)) |
| 41 |
} |
| 42 |
|
| 43 |
func TestCache_Clear(t *testing.T) { |
| 44 |
_, conn := PrepareRedisTest() |
| 45 |
conn.ConnectPubsub() |
| 46 |
|
| 47 |
c := NewCache(conn) |
| 48 |
|
| 49 |
c.fiCache.Set("test", &TestValue{"42"}) |
| 50 |
c.fmCache.Set("test", &TestValue{"42"}) |
| 51 |
c.mCache.Set("test", &TestValue{"42"}) |
| 52 |
c.fimCache.Set("test", &TestValue{"42"}) |
| 53 |
|
| 54 |
c.Clear() |
| 55 |
|
| 56 |
if _, ok := c.fiCache.Get("test"); ok { |
| 57 |
t.Fatalf("Value shouldn't be present") |
| 58 |
} |
| 59 |
if _, ok := c.fmCache.Get("test"); ok { |
| 60 |
t.Fatalf("Value shouldn't be present") |
| 61 |
} |
| 62 |
if _, ok := c.mCache.Get("test"); ok { |
| 63 |
t.Fatalf("Value shouldn't be present") |
| 64 |
} |
| 65 |
if _, ok := c.fimCache.Get("test"); ok { |
| 66 |
t.Fatalf("Value shouldn't be present") |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func assertFileInfoEqual(t *testing.T, actual *filesystem.FileInfo, expected *filesystem.FileInfo) { |
| 71 |
t.Helper() |
| 72 |
if actual.Path != expected.Path { |
| 73 |
t.Fatalf("Path doesn't match, expected %#v got %#v", expected.Path, actual.Path) |
| 74 |
} |
| 75 |
if actual.Size != expected.Size { |
| 76 |
t.Fatalf("Size doesn't match, expected %#v got %#v", expected.Size, actual.Size) |
| 77 |
} |
| 78 |
if !actual.ModTime.Equal(expected.ModTime) { |
| 79 |
t.Fatalf("ModTime doesn't match, expected %s got %s", expected.ModTime.String(), actual.ModTime.String()) |
| 80 |
} |
| 81 |
if actual.Sha1 != expected.Sha1 { |
| 82 |
t.Fatalf("Sha1 doesn't match, expected %#v got %#v", expected.Sha1, actual.Sha1) |
| 83 |
} |
| 84 |
if actual.Sha256 != expected.Sha256 { |
| 85 |
t.Fatalf("Sha256 doesn't match, expected %#v got %#v", expected.Sha256, actual.Sha256) |
| 86 |
} |
| 87 |
if actual.Md5 != expected.Md5 { |
| 88 |
t.Fatalf("Md5 doesn't match, expected %#v got %#v", expected.Md5, actual.Md5) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func TestCache_fetchFileInfo(t *testing.T) { |
| 93 |
mock, conn := PrepareRedisTest() |
| 94 |
conn.ConnectPubsub() |
| 95 |
|
| 96 |
c := NewCache(conn) |
| 97 |
|
| 98 |
testfile := filesystem.FileInfo{ |
| 99 |
Path: "/test/file.tgz", |
| 100 |
Size: 43000, |
| 101 |
ModTime: time.Now(), |
| 102 |
Sha1: "3ce963aea2d6f23fe915063f8bba21888db0ddfa", |
| 103 |
Sha256: "1c8e38c7e03e4d117eba4f82afaf6631a9b79f4c1e9dec144d4faf1d109aacda", |
| 104 |
Md5: "2c98ec39f49da6ddd9cfa7b1d7342afe", |
| 105 |
} |
| 106 |
|
| 107 |
f, err := c.fetchFileInfo(testfile.Path) |
| 108 |
if err == nil { |
| 109 |
t.Fatalf("Error expected, mock command not yet registered") |
| 110 |
} |
| 111 |
|
| 112 |
cmdGetFileinfo := mock.Command("HMGET", "FILE_"+testfile.Path, "size", "modTime", "sha1", "sha256", "md5").Expect([]any{ |
| 113 |
[]byte(strconv.FormatInt(testfile.Size, 10)), |
| 114 |
[]byte(testfile.ModTime.Format("2006-01-02 15:04:05.999999999 -0700 MST")), |
| 115 |
[]byte(testfile.Sha1), |
| 116 |
[]byte(testfile.Sha256), |
| 117 |
[]byte(testfile.Md5), |
| 118 |
}) |
| 119 |
|
| 120 |
f, err = c.fetchFileInfo(testfile.Path) |
| 121 |
if err != nil { |
| 122 |
t.Fatalf("Unexpected error: %s", err.Error()) |
| 123 |
} |
| 124 |
|
| 125 |
if mock.Stats(cmdGetFileinfo) < 1 { |
| 126 |
t.Fatalf("HMGET not executed") |
| 127 |
} |
| 128 |
|
| 129 |
assertFileInfoEqual(t, &f, &testfile) |
| 130 |
|
| 131 |
_, ok := c.fiCache.Get(testfile.Path) |
| 132 |
if !ok { |
| 133 |
t.Fatalf("Not stored in cache") |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
func TestCache_fetchFileInfo_non_existing(t *testing.T) { |
| 138 |
mock, conn := PrepareRedisTest() |
| 139 |
conn.ConnectPubsub() |
| 140 |
|
| 141 |
c := NewCache(conn) |
| 142 |
|
| 143 |
testfile := filesystem.FileInfo{ |
| 144 |
Path: "/test/file.tgz", |
| 145 |
Size: 0, |
| 146 |
ModTime: time.Time{}, |
| 147 |
Sha1: "", |
| 148 |
Sha256: "", |
| 149 |
Md5: "", |
| 150 |
} |
| 151 |
|
| 152 |
f, err := c.fetchFileInfo(testfile.Path) |
| 153 |
if err == nil { |
| 154 |
t.Fatalf("Error expected, mock command not yet registered") |
| 155 |
} |
| 156 |
|
| 157 |
cmdGetFileinfo := mock.Command("HMGET", "FILE_"+testfile.Path, "size", "modTime", "sha1", "sha256", "md5").Expect([]any{ |
| 158 |
[]byte(""), |
| 159 |
[]byte(""), |
| 160 |
[]byte(""), |
| 161 |
[]byte(""), |
| 162 |
[]byte(""), |
| 163 |
}) |
| 164 |
|
| 165 |
f, err = c.fetchFileInfo(testfile.Path) |
| 166 |
// fetchFileInfo on a non-existing file doesn't yield Redis.ErrNil |
| 167 |
if err != nil { |
| 168 |
t.Fatalf("Unexpected error: %s", err.Error()) |
| 169 |
} |
| 170 |
|
| 171 |
if mock.Stats(cmdGetFileinfo) < 1 { |
| 172 |
t.Fatalf("HMGET not executed") |
| 173 |
} |
| 174 |
|
| 175 |
assertFileInfoEqual(t, &f, &testfile) |
| 176 |
|
| 177 |
// Non-existing file are also stored in cache |
| 178 |
_, ok := c.fiCache.Get(testfile.Path) |
| 179 |
if !ok { |
| 180 |
t.Fatalf("Not stored in cache") |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
func TestCache_GetFileInfo(t *testing.T) { |
| 185 |
mock, conn := PrepareRedisTest() |
| 186 |
conn.ConnectPubsub() |
| 187 |
|
| 188 |
c := NewCache(conn) |
| 189 |
|
| 190 |
testfile := filesystem.FileInfo{ |
| 191 |
Path: "/test/file.tgz", |
| 192 |
Size: 43000, |
| 193 |
ModTime: time.Now(), |
| 194 |
Sha1: "3ce963aea2d6f23fe915063f8bba21888db0ddfa", |
| 195 |
Sha256: "1c8e38c7e03e4d117eba4f82afaf6631a9b79f4c1e9dec144d4faf1d109aacda", |
| 196 |
Md5: "2c98ec39f49da6ddd9cfa7b1d7342afe", |
| 197 |
} |
| 198 |
|
| 199 |
_, err := c.GetFileInfo(testfile.Path) |
| 200 |
if err == nil { |
| 201 |
t.Fatalf("Error expected, mock command not yet registered") |
| 202 |
} |
| 203 |
|
| 204 |
cmdGetFileinfo := mock.Command("HMGET", "FILE_"+testfile.Path, "size", "modTime", "sha1", "sha256", "md5").Expect([]any{ |
| 205 |
[]byte(strconv.FormatInt(testfile.Size, 10)), |
| 206 |
[]byte(testfile.ModTime.Format("2006-01-02 15:04:05.999999999 -0700 MST")), |
| 207 |
[]byte(testfile.Sha1), |
| 208 |
[]byte(testfile.Sha256), |
| 209 |
[]byte(testfile.Md5), |
| 210 |
}) |
| 211 |
|
| 212 |
f, err := c.GetFileInfo(testfile.Path) |
| 213 |
if err != nil { |
| 214 |
t.Fatalf("Unexpected error: %s", err.Error()) |
| 215 |
} |
| 216 |
|
| 217 |
if mock.Stats(cmdGetFileinfo) < 1 { |
| 218 |
t.Fatalf("HMGET not executed") |
| 219 |
} |
| 220 |
|
| 221 |
assertFileInfoEqual(t, &f, &testfile) |
| 222 |
|
| 223 |
f, err = c.GetFileInfo(testfile.Path) |
| 224 |
if err != nil { |
| 225 |
t.Fatalf("Unexpected error: %s", err.Error()) |
| 226 |
} |
| 227 |
if mock.Stats(cmdGetFileinfo) > 1 { |
| 228 |
t.Fatalf("Cache not used, request expected to be done once") |
| 229 |
} |
| 230 |
|
| 231 |
assertFileInfoEqual(t, &f, &testfile) |
| 232 |
} |
| 233 |
|
| 234 |
func TestCache_GetFileInfo_non_existing(t *testing.T) { |
| 235 |
mock, conn := PrepareRedisTest() |
| 236 |
conn.ConnectPubsub() |
| 237 |
|
| 238 |
c := NewCache(conn) |
| 239 |
|
| 240 |
testfile := filesystem.FileInfo{ |
| 241 |
Path: "/test/file.tgz", |
| 242 |
Size: 0, |
| 243 |
ModTime: time.Time{}, |
| 244 |
Sha1: "", |
| 245 |
Sha256: "", |
| 246 |
Md5: "", |
| 247 |
} |
| 248 |
|
| 249 |
_, err := c.GetFileInfo(testfile.Path) |
| 250 |
if err == nil { |
| 251 |
t.Fatalf("Error expected, mock command not yet registered") |
| 252 |
} |
| 253 |
|
| 254 |
cmdGetFileinfo := mock.Command("HMGET", "FILE_"+testfile.Path, "size", "modTime", "sha1", "sha256", "md5").Expect([]any{ |
| 255 |
[]byte(""), |
| 256 |
[]byte(""), |
| 257 |
[]byte(""), |
| 258 |
[]byte(""), |
| 259 |
[]byte(""), |
| 260 |
}) |
| 261 |
|
| 262 |
f, err := c.GetFileInfo(testfile.Path) |
| 263 |
// GetFileInfo on a non-existing file doesn't yield Redis.ErrNil |
| 264 |
if err != nil { |
| 265 |
t.Fatalf("Unexpected error: %s", err.Error()) |
| 266 |
} |
| 267 |
|
| 268 |
if mock.Stats(cmdGetFileinfo) < 1 { |
| 269 |
t.Fatalf("HMGET not executed") |
| 270 |
} |
| 271 |
|
| 272 |
assertFileInfoEqual(t, &f, &testfile) |
| 273 |
|
| 274 |
f, err = c.GetFileInfo(testfile.Path) |
| 275 |
if err != nil { |
| 276 |
t.Fatalf("Unexpected error: %s", err.Error()) |
| 277 |
} |
| 278 |
// Non-existing file are also stored in cache |
| 279 |
if mock.Stats(cmdGetFileinfo) > 1 { |
| 280 |
t.Fatalf("Cache not used, request expected to be done once") |
| 281 |
} |
| 282 |
|
| 283 |
assertFileInfoEqual(t, &f, &testfile) |
| 284 |
} |
| 285 |
|
| 286 |
func TestCache_fetchFileMirrors(t *testing.T) { |
| 287 |
mock, conn := PrepareRedisTest() |
| 288 |
conn.ConnectPubsub() |
| 289 |
|
| 290 |
c := NewCache(conn) |
| 291 |
filename := "/test/file.tgz" |
| 292 |
|
| 293 |
_, err := c.fetchFileMirrors(filename) |
| 294 |
if err == nil { |
| 295 |
t.Fatalf("Error expected, mock command not yet registered") |
| 296 |
} |
| 297 |
|
| 298 |
cmdGetFilemirrors := mock.Command("SMEMBERS", "FILEMIRRORS_"+filename).Expect([]any{ |
| 299 |
[]byte("9"), |
| 300 |
[]byte("2"), |
| 301 |
[]byte("5"), |
| 302 |
}) |
| 303 |
|
| 304 |
ids, err := c.fetchFileMirrors(filename) |
| 305 |
if err != nil { |
| 306 |
t.Fatalf("Unexpected error: %s", err.Error()) |
| 307 |
} |
| 308 |
|
| 309 |
if mock.Stats(cmdGetFilemirrors) < 1 { |
| 310 |
t.Fatalf("SMEMBERS not executed") |
| 311 |
} |
| 312 |
|
| 313 |
if len(ids) != 3 { |
| 314 |
t.Fatalf("Invalid number of items returned") |
| 315 |
} |
| 316 |
|
| 317 |
_, ok := c.fmCache.Get(filename) |
| 318 |
if !ok { |
| 319 |
t.Fatalf("Not stored in cache") |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
func TestCache_fetchMirror(t *testing.T) { |
| 324 |
mock, conn := PrepareRedisTest() |
| 325 |
conn.ConnectPubsub() |
| 326 |
|
| 327 |
c := NewCache(conn) |
| 328 |
|
| 329 |
testmirror := Mirror{ |
| 330 |
ID: 1, |
| 331 |
Name: "m1", |
| 332 |
HttpURL: "http://m1.mirror", |
| 333 |
RsyncURL: "rsync://m1.mirror", |
| 334 |
FtpURL: "ftp://m1.mirror", |
| 335 |
SponsorName: "m1sponsor", |
| 336 |
SponsorURL: "m1sponsorurl", |
| 337 |
SponsorLogoURL: "m1sponsorlogourl", |
| 338 |
AdminName: "m1adminname", |
| 339 |
AdminEmail: "m1adminemail", |
| 340 |
CustomData: "m1customdata", |
| 341 |
ContinentOnly: true, |
| 342 |
CountryOnly: false, |
| 343 |
ASOnly: true, |
| 344 |
Score: 0, |
| 345 |
Latitude: -20.0, |
| 346 |
Longitude: 55.0, |
| 347 |
ContinentCode: "EU", |
| 348 |
CountryCodes: "FR UK", |
| 349 |
Asnum: 444, |
| 350 |
Comment: "m1comment", |
| 351 |
Enabled: true, |
| 352 |
HttpUp: true, |
| 353 |
HttpsUp: true, |
| 354 |
} |
| 355 |
|
| 356 |
_, err := c.fetchMirror(testmirror.ID) |
| 357 |
if err == nil { |
| 358 |
t.Fatalf("Error expected, mock command not yet registered") |
| 359 |
} |
| 360 |
|
| 361 |
cmdGetMirror := mock.Command("HGETALL", "MIRROR_1").ExpectMap(map[string]string{ |
| 362 |
"ID": strconv.Itoa(testmirror.ID), |
| 363 |
"name": testmirror.Name, |
| 364 |
"http": testmirror.HttpURL, |
| 365 |
"rsync": testmirror.RsyncURL, |
| 366 |
"ftp": testmirror.FtpURL, |
| 367 |
"sponsorName": testmirror.SponsorName, |
| 368 |
"sponsorURL": testmirror.SponsorURL, |
| 369 |
"sponsorLogo": testmirror.SponsorLogoURL, |
| 370 |
"adminName": testmirror.AdminName, |
| 371 |
"adminEmail": testmirror.AdminEmail, |
| 372 |
"customData": testmirror.CustomData, |
| 373 |
"continentOnly": strconv.FormatBool(testmirror.ContinentOnly), |
| 374 |
"countryOnly": strconv.FormatBool(testmirror.CountryOnly), |
| 375 |
"asOnly": strconv.FormatBool(testmirror.ASOnly), |
| 376 |
"score": strconv.FormatInt(int64(testmirror.Score), 10), |
| 377 |
"latitude": fmt.Sprintf("%f", testmirror.Latitude), |
| 378 |
"longitude": fmt.Sprintf("%f", testmirror.Longitude), |
| 379 |
"continentCode": testmirror.ContinentCode, |
| 380 |
"countryCodes": testmirror.CountryCodes, |
| 381 |
"asnum": strconv.FormatInt(int64(testmirror.Asnum), 10), |
| 382 |
"comment": testmirror.Comment, |
| 383 |
"enabled": strconv.FormatBool(testmirror.Enabled), |
| 384 |
"httpUp": strconv.FormatBool(testmirror.HttpUp), |
| 385 |
"httpsUp": strconv.FormatBool(testmirror.HttpsUp), |
| 386 |
}) |
| 387 |
|
| 388 |
m, err := c.fetchMirror(testmirror.ID) |
| 389 |
if err != nil { |
| 390 |
t.Fatalf("Unexpected error: %s", err.Error()) |
| 391 |
} |
| 392 |
|
| 393 |
if mock.Stats(cmdGetMirror) < 1 { |
| 394 |
t.Fatalf("HGETALL not executed") |
| 395 |
} |
| 396 |
|
| 397 |
// This is required to reach DeepEqual(ity) |
| 398 |
testmirror.Prepare() |
| 399 |
|
| 400 |
if !reflect.DeepEqual(testmirror, m) { |
| 401 |
t.Fatalf("Result is different") |
| 402 |
} |
| 403 |
|
| 404 |
_, ok := c.mCache.Get(strconv.Itoa(testmirror.ID)) |
| 405 |
if !ok { |
| 406 |
t.Fatalf("Not stored in cache") |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
func TestCache_fetchFileInfoMirror(t *testing.T) { |
| 411 |
mock, conn := PrepareRedisTest() |
| 412 |
conn.ConnectPubsub() |
| 413 |
|
| 414 |
c := NewCache(conn) |
| 415 |
|
| 416 |
testfile := filesystem.FileInfo{ |
| 417 |
Path: "/test/file.tgz", |
| 418 |
Size: 44000, |
| 419 |
ModTime: time.Now(), |
| 420 |
Sha1: "3ce963aea2d6f23fe915063f8bba21888db0ddfa", |
| 421 |
Sha256: "1c8e38c7e03e4d117eba4f82afaf6631a9b79f4c1e9dec144d4faf1d109aacda", |
| 422 |
Md5: "2c98ec39f49da6ddd9cfa7b1d7342afe", |
| 423 |
} |
| 424 |
|
| 425 |
_, err := c.fetchFileInfoMirror(1, testfile.Path) |
| 426 |
if err == nil { |
| 427 |
t.Fatalf("Error expected, mock command not yet registered") |
| 428 |
} |
| 429 |
|
| 430 |
cmdGetFileinfomirror := mock.Command("HMGET", "FILEINFO_1_"+testfile.Path, "size", "modTime", "sha1", "sha256", "md5").Expect([]any{ |
| 431 |
[]byte(strconv.FormatInt(testfile.Size, 10)), |
| 432 |
[]byte(testfile.ModTime.String()), |
| 433 |
[]byte(testfile.Sha1), |
| 434 |
[]byte(testfile.Sha256), |
| 435 |
[]byte(testfile.Md5), |
| 436 |
}) |
| 437 |
|
| 438 |
_, err = c.fetchFileInfoMirror(1, testfile.Path) |
| 439 |
if err != nil { |
| 440 |
t.Fatalf("Unexpected error: %s", err.Error()) |
| 441 |
} |
| 442 |
|
| 443 |
if mock.Stats(cmdGetFileinfomirror) < 1 { |
| 444 |
t.Fatalf("HMGET not executed") |
| 445 |
} |
| 446 |
|
| 447 |
_, ok := c.fimCache.Get("1|" + testfile.Path) |
| 448 |
if !ok { |
| 449 |
t.Fatalf("Not stored in cache") |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
func TestCache_GetMirror(t *testing.T) { |
| 454 |
mock, conn := PrepareRedisTest() |
| 455 |
conn.ConnectPubsub() |
| 456 |
|
| 457 |
c := NewCache(conn) |
| 458 |
|
| 459 |
testmirror := 1 |
| 460 |
|
| 461 |
_, err := c.GetMirror(testmirror) |
| 462 |
if err == nil { |
| 463 |
t.Fatalf("Error expected, mock command not yet registered") |
| 464 |
} |
| 465 |
|
| 466 |
cmdGetMirror := mock.Command("HGETALL", "MIRROR_1").ExpectMap(map[string]string{ |
| 467 |
"ID": strconv.Itoa(testmirror), |
| 468 |
}) |
| 469 |
|
| 470 |
m, err := c.GetMirror(testmirror) |
| 471 |
if err != nil { |
| 472 |
t.Fatalf("Unexpected error: %s", err.Error()) |
| 473 |
} |
| 474 |
|
| 475 |
if mock.Stats(cmdGetMirror) < 1 { |
| 476 |
t.Fatalf("HGETALL not executed") |
| 477 |
} |
| 478 |
|
| 479 |
// Results are already checked by TestCache_fetchMirror |
| 480 |
// We only need to check one of them |
| 481 |
if m.ID != testmirror { |
| 482 |
t.Fatalf("Result is different") |
| 483 |
} |
| 484 |
|
| 485 |
_, ok := c.mCache.Get(strconv.Itoa(testmirror)) |
| 486 |
if !ok { |
| 487 |
t.Fatalf("Not stored in cache") |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
func TestCache_GetMirrors(t *testing.T) { |
| 492 |
mock, conn := PrepareRedisTest() |
| 493 |
conn.ConnectPubsub() |
| 494 |
|
| 495 |
c := NewCache(conn) |
| 496 |
|
| 497 |
filename := "/test/file.tgz" |
| 498 |
|
| 499 |
clientInfo := network.GeoIPRecord{ |
| 500 |
CountryCode: "FR", |
| 501 |
Latitude: 48.8567, |
| 502 |
Longitude: 2.3508, |
| 503 |
} |
| 504 |
|
| 505 |
_, err := c.GetMirrors(filename, clientInfo) |
| 506 |
if err == nil { |
| 507 |
t.Fatalf("Error expected, mock command not yet registered") |
| 508 |
} |
| 509 |
|
| 510 |
cmdGetFilemirrors := mock.Command("SMEMBERS", "FILEMIRRORS_"+filename).Expect([]any{ |
| 511 |
[]byte("1"), |
| 512 |
[]byte("2"), |
| 513 |
}) |
| 514 |
|
| 515 |
cmdGetMirrorM1 := mock.Command("HGETALL", "MIRROR_1").ExpectMap(map[string]string{ |
| 516 |
"ID": "1", |
| 517 |
"latitude": "52.5167", |
| 518 |
"longitude": "13.3833", |
| 519 |
}) |
| 520 |
|
| 521 |
cmdGetMirrorM2 := mock.Command("HGETALL", "MIRROR_2").ExpectMap(map[string]string{ |
| 522 |
"ID": "2", |
| 523 |
"latitude": "51.5072", |
| 524 |
"longitude": "0.1275", |
| 525 |
}) |
| 526 |
|
| 527 |
cmdGetFileinfomirrorM1 := mock.Command("HMGET", "FILEINFO_1_"+filename, "size", "modTime", "sha1", "sha256", "md5").Expect([]any{ |
| 528 |
[]byte("44000"), |
| 529 |
[]byte(""), |
| 530 |
[]byte(""), |
| 531 |
[]byte(""), |
| 532 |
[]byte(""), |
| 533 |
}) |
| 534 |
|
| 535 |
cmdGetFileinfomirrorM2 := mock.Command("HMGET", "FILEINFO_2_"+filename, "size", "modTime", "sha1", "sha256", "md5").Expect([]any{ |
| 536 |
[]byte("44000"), |
| 537 |
[]byte(""), |
| 538 |
[]byte(""), |
| 539 |
[]byte(""), |
| 540 |
[]byte(""), |
| 541 |
}) |
| 542 |
|
| 543 |
mirrors, err := c.GetMirrors(filename, clientInfo) |
| 544 |
if err != nil { |
| 545 |
t.Fatalf("Unexpected error: %s", err.Error()) |
| 546 |
} |
| 547 |
|
| 548 |
if mock.Stats(cmdGetFilemirrors) < 1 { |
| 549 |
t.Fatalf("cmdGetFilemirrors not called") |
| 550 |
} |
| 551 |
if mock.Stats(cmdGetMirrorM1) < 1 { |
| 552 |
t.Fatalf("cmdGetMirrorM1 not called") |
| 553 |
} |
| 554 |
if mock.Stats(cmdGetMirrorM2) < 1 { |
| 555 |
t.Fatalf("cmdGetMirrorM2 not called") |
| 556 |
} |
| 557 |
if mock.Stats(cmdGetFileinfomirrorM1) < 1 { |
| 558 |
t.Fatalf("cmdGetFileinfomirrorM1 not called") |
| 559 |
} |
| 560 |
if mock.Stats(cmdGetFileinfomirrorM2) < 1 { |
| 561 |
t.Fatalf("cmdGetFileinfomirrorM2 not called") |
| 562 |
} |
| 563 |
|
| 564 |
if len(mirrors) != 2 { |
| 565 |
t.Fatalf("Invalid number of mirrors returned") |
| 566 |
} |
| 567 |
|
| 568 |
if int(mirrors[0].Distance) != int(876) { |
| 569 |
t.Fatalf("Distance between user and m1 is wrong, got %d, expected 876", int(mirrors[0].Distance)) |
| 570 |
} |
| 571 |
|
| 572 |
if int(mirrors[1].Distance) != int(334) { |
| 573 |
t.Fatalf("Distance between user and m2 is wrong, got %d, expected 334", int(mirrors[1].Distance)) |
| 574 |
} |
| 575 |
} |