예제 #1
0
func TestByExcludeReason_Less(t *testing.T) {
	m := Mirrors{
		Mirror{
			ID:            "M0",
			ExcludeReason: "x42",
		},
		Mirror{
			ID:            "M1",
			ExcludeReason: "x43",
		},
		Mirror{
			ID:            "M2",
			ExcludeReason: "Test one",
		},
		Mirror{
			ID:            "M3",
			ExcludeReason: "Test two",
		},
		Mirror{
			ID:            "M4",
			ExcludeReason: "test three",
		},
	}

	sort.Sort(ByExcludeReason{m})

	if !matchingMirrorOrder(m, []string{"M2", "M3", "M4", "M0", "M1"}) {
		t.Fatalf("Order doesn't seem right: %s, expected M2, M3, M4, M0, M1", formatMirrorOrder(m))
	}
}
예제 #2
0
func TestMarkMirrorDown(t *testing.T) {
	_, conn := PrepareRedisTest()

	if err := MarkMirrorDown(conn, "m1", "test1"); err == nil {
		t.Fatalf("Error expected but nil returned")
	}
}
예제 #3
0
func TestByComputedScore_Less(t *testing.T) {
	m := Mirrors{
		Mirror{
			ID:            "M0",
			ComputedScore: 50,
		},
		Mirror{
			ID:            "M1",
			ComputedScore: 0,
		},
		Mirror{
			ID:            "M2",
			ComputedScore: 2500,
		},
		Mirror{
			ID:            "M3",
			ComputedScore: 21,
		},
	}

	sort.Sort(ByComputedScore{m})

	if !matchingMirrorOrder(m, []string{"M2", "M0", "M3", "M1"}) {
		t.Fatalf("Order doesn't seem right: %s, expected M2, M0, M3, M1", formatMirrorOrder(m))
	}
}
예제 #4
0
func TestMirrors_Len(t *testing.T) {
	m := Mirrors{}
	if m.Len() != 0 {
		t.Fatalf("Expected 0, got %d", m.Len())
	}

	m = generateSimpleMirrorList(2)
	if m.Len() != len(m) {
		t.Fatalf("Expected %d, got %d", len(m), m.Len())
	}
}
예제 #5
0
func TestNewCache(t *testing.T) {
	_, conn := PrepareRedisTest()
	conn.ConnectPubsub()

	c := NewCache(nil)
	if c != nil {
		t.Fatalf("Expected invalid instance")
	}

	c = NewCache(conn)
	if c == nil {
		t.Fatalf("No valid instance returned")
	}
}
예제 #6
0
func TestCache_Clear(t *testing.T) {
	_, conn := PrepareRedisTest()
	conn.ConnectPubsub()

	c := NewCache(conn)

	c.fiCache.Set("test", &TestValue{"42"})
	c.fmCache.Set("test", &TestValue{"42"})
	c.mCache.Set("test", &TestValue{"42"})
	c.fimCache.Set("test", &TestValue{"42"})

	c.Clear()

	if _, ok := c.fiCache.Get("test"); ok {
		t.Fatalf("Value shouldn't be present")
	}
	if _, ok := c.fmCache.Get("test"); ok {
		t.Fatalf("Value shouldn't be present")
	}
	if _, ok := c.mCache.Get("test"); ok {
		t.Fatalf("Value shouldn't be present")
	}
	if _, ok := c.fimCache.Get("test"); ok {
		t.Fatalf("Value shouldn't be present")
	}
}
예제 #7
0
func TestDisableMirror(t *testing.T) {
	mock, conn := PrepareRedisTest()

	cmd_disable := mock.Command("HMSET", "MIRROR_m1", "enabled", false).Expect("ok")
	DisableMirror(conn, "m1")

	if mock.Stats(cmd_disable) != 1 {
		t.Fatalf("Mirror not enabled")
	}

	mock.Command("HMSET", "MIRROR_m1", "enabled", false).ExpectError(redis.Error("blah"))
	if DisableMirror(conn, "m1") == nil {
		t.Fatalf("Error expected")
	}
}
예제 #8
0
func TestCache_GetMirror(t *testing.T) {
	mock, conn := PrepareRedisTest()
	conn.ConnectPubsub()

	c := NewCache(conn)

	testmirror := "m1"

	_, err := c.GetMirror(testmirror)
	if err == nil {
		t.Fatalf("Error expected, mock command not yet registered")
	}

	cmd_get_mirror := mock.Command("HGETALL", "MIRROR_m1").ExpectMap(map[string]string{
		"ID": testmirror,
	})

	m, err := c.GetMirror(testmirror)
	if err != nil {
		t.Fatalf("Unexpected error: %s", err.Error())
	}

	if mock.Stats(cmd_get_mirror) < 1 {
		t.Fatalf("HGETALL not executed")
	}

	// Results are already checked by TestCache_fetchMirror
	// We only need to check one of them
	if m.ID != testmirror {
		t.Fatalf("Result is different")
	}

	_, ok := c.mCache.Get(testmirror)
	if !ok {
		t.Fatalf("Not stored in cache")
	}
}
예제 #9
0
func TestCache_fetchFileMirrors(t *testing.T) {
	mock, conn := PrepareRedisTest()
	conn.ConnectPubsub()

	c := NewCache(conn)
	filename := "/test/file.tgz"

	_, err := c.fetchFileMirrors(filename)
	if err == nil {
		t.Fatalf("Error expected, mock command not yet registered")
	}

	cmd_get_filemirrors := mock.Command("SMEMBERS", "FILEMIRRORS_"+filename).Expect([]interface{}{
		[]byte("m9"),
		[]byte("m2"),
		[]byte("m5"),
	})

	ids, err := c.fetchFileMirrors(filename)
	if err != nil {
		t.Fatalf("Unexpected error: %s", err.Error())
	}

	if mock.Stats(cmd_get_filemirrors) < 1 {
		t.Fatalf("SMEMBERS not executed")
	}

	if len(ids) != 3 {
		t.Fatalf("Invalid number of items returned")
	}

	_, ok := c.fmCache.Get(filename)
	if !ok {
		t.Fatalf("Not stored in cache")
	}
}
예제 #10
0
func TestCache_fetchFileInfoMirror(t *testing.T) {
	mock, conn := PrepareRedisTest()
	conn.ConnectPubsub()

	c := NewCache(conn)

	testfile := filesystem.FileInfo{
		Path:    "/test/file.tgz",
		Size:    44000,
		ModTime: time.Now(),
		Sha1:    "3ce963aea2d6f23fe915063f8bba21888db0ddfa",
		Sha256:  "1c8e38c7e03e4d117eba4f82afaf6631a9b79f4c1e9dec144d4faf1d109aacda",
		Md5:     "2c98ec39f49da6ddd9cfa7b1d7342afe",
	}

	_, err := c.fetchFileInfoMirror("m1", testfile.Path)
	if err == nil {
		t.Fatalf("Error expected, mock command not yet registered")
	}

	cmd_get_fileinfomirror := mock.Command("HMGET", "FILEINFO_m1_"+testfile.Path, "size", "modTime", "sha1", "sha256", "md5").ExpectMap(map[string]string{
		"size":    strconv.FormatInt(testfile.Size, 10),
		"modTime": testfile.ModTime.String(),
		"sha1":    testfile.Sha1,
		"sha256":  testfile.Sha256,
		"md5":     testfile.Md5,
	})

	_, err = c.fetchFileInfoMirror("m1", testfile.Path)
	if err != nil {
		t.Fatalf("Unexpected error: %s", err.Error())
	}

	if mock.Stats(cmd_get_fileinfomirror) < 1 {
		t.Fatalf("HGETALL not executed")
	}

	_, ok := c.fimCache.Get("m1|" + testfile.Path)
	if !ok {
		t.Fatalf("Not stored in cache")
	}
}
예제 #11
0
func TestGetMirrorMapUrl(t *testing.T) {
	m := Mirrors{
		Mirror{
			ID:        "M0",
			Latitude:  -80.0,
			Longitude: 80.0,
		},
		Mirror{
			ID:        "M1",
			Latitude:  -60.0,
			Longitude: 60.0,
		},
		Mirror{
			ID:        "M2",
			Latitude:  -40.0,
			Longitude: 40.0,
		},
		Mirror{
			ID:        "M3",
			Latitude:  -20.0,
			Longitude: 20.0,
		},
	}

	c := network.GeoIPRecord{
		GeoIPRecord: &geoip.GeoIPRecord{
			Latitude:  -10.0,
			Longitude: 10.0,
		},
		ASNum: 4444,
	}

	result := GetMirrorMapUrl(m, c)

	if !strings.HasPrefix(result, "//maps.googleapis.com") {
		t.Fatalf("Bad format")
	}

	if !strings.Contains(result, "color:red") {
		t.Fatalf("Missing client marker?")
	}

	if strings.Count(result, "label:") != len(m) {
		t.Fatalf("Missing some mirror markers?")
	}
}
예제 #12
0
func TestMirrors_Swap(t *testing.T) {
	m := generateSimpleMirrorList(5)

	if !matchingMirrorOrder(m, []string{"M0", "M1", "M2", "M3", "M4"}) {
		t.Fatalf("Expected M0 before M1, got %s", formatMirrorOrder(m))
	}

	m.Swap(0, 1)

	if !matchingMirrorOrder(m, []string{"M1", "M0", "M2", "M3", "M4"}) {
		t.Fatalf("Expected M1 before M0, got %s", formatMirrorOrder(m))
	}

	m.Swap(2, 4)

	if !matchingMirrorOrder(m, []string{"M1", "M0", "M4", "M3", "M2"}) {
		t.Fatalf("Expected M4 at position 2 and M2 at position 4", m)
	}
}
예제 #13
0
func TestCache_fetchFileInfo(t *testing.T) {
	mock, conn := PrepareRedisTest()
	conn.ConnectPubsub()

	c := NewCache(conn)

	testfile := filesystem.FileInfo{
		Path:    "/test/file.tgz",
		Size:    43000,
		ModTime: time.Now(),
		Sha1:    "3ce963aea2d6f23fe915063f8bba21888db0ddfa",
		Sha256:  "1c8e38c7e03e4d117eba4f82afaf6631a9b79f4c1e9dec144d4faf1d109aacda",
		Md5:     "2c98ec39f49da6ddd9cfa7b1d7342afe",
	}

	f, err := c.fetchFileInfo(testfile.Path)
	if err == nil {
		t.Fatalf("Error expected, mock command not yet registered")
	}

	cmd_get_fileinfo := mock.Command("HMGET", "FILE_"+testfile.Path, "size", "modTime", "sha1", "sha256", "md5").Expect([]interface{}{
		[]byte(strconv.FormatInt(testfile.Size, 10)),
		[]byte(testfile.ModTime.String()),
		[]byte(testfile.Sha1),
		[]byte(testfile.Sha256),
		[]byte(testfile.Md5),
	})

	f, err = c.fetchFileInfo(testfile.Path)
	if err != nil {
		t.Fatalf("Unexpected error: %s", err.Error())
	}

	if mock.Stats(cmd_get_fileinfo) < 1 {
		t.Fatalf("HMGET not executed")
	}

	if f.Path != testfile.Path {
		t.Fatalf("Path doesn't match, expected %#v got %#v", testfile.Path, f.Path)
	}
	if f.Size != testfile.Size {
		t.Fatalf("Size doesn't match, expected %#v got %#v", testfile.Size, f.Size)
	}
	if f.ModTime != testfile.ModTime {
		t.Fatalf("ModTime doesn't match, expected %#v got %#v", testfile.ModTime, f.ModTime)
	}
	if f.Sha1 != testfile.Sha1 {
		t.Fatalf("Sha1 doesn't match, expected %#v got %#v", testfile.Sha1, f.Sha1)
	}
	if f.Sha256 != testfile.Sha256 {
		t.Fatalf("Sha256 doesn't match, expected %#v got %#v", testfile.Sha256, f.Sha256)
	}
	if f.Md5 != testfile.Md5 {
		t.Fatalf("Md5 doesn't match, expected %#v got %#v", testfile.Md5, f.Md5)
	}

	_, ok := c.fiCache.Get(testfile.Path)
	if !ok {
		t.Fatalf("Not stored in cache")
	}
}
예제 #14
0
func TestSetMirrorState(t *testing.T) {
	mock, conn := PrepareRedisTest()

	if err := SetMirrorState(conn, "m1", true, "test1"); err == nil {
		t.Fatalf("Error expected but nil returned")
	}

	cmd_publish := mock.Command("PUBLISH", string(database.MIRROR_UPDATE), redigomock.NewAnyData()).Expect("ok")

	/* */

	cmd_previous_state := mock.Command("HGET", "MIRROR_m1", "up").Expect(int64(0)).Expect(int64(1))
	cmd_state_since := mock.Command("HMSET", "MIRROR_m1", "up", true, "excludeReason", "test1", "stateSince", redigomock.NewAnyInt()).Expect("ok")
	cmd_state := mock.Command("HMSET", "MIRROR_m1", "up", true, "excludeReason", "test2").Expect("ok")

	if err := SetMirrorState(conn, "m1", true, "test1"); err != nil {
		t.Fatalf("Unexpected error: %s", err)
	}

	if mock.Stats(cmd_previous_state) < 1 {
		t.Fatalf("Previous state not tested")
	}

	if mock.Stats(cmd_state_since) < 1 {
		t.Fatalf("New state not set")
	} else if mock.Stats(cmd_state_since) > 1 {
		t.Fatalf("State set more than once")
	}

	if mock.Stats(cmd_publish) < 1 {
		t.Fatalf("Event MIRROR_UPDATE not published")
	}

	/* */

	if err := SetMirrorState(conn, "m1", true, "test2"); err != nil {
		t.Fatalf("Unexpected error: %s", err)
	}

	if mock.Stats(cmd_state_since) > 1 || mock.Stats(cmd_state) < 1 {
		t.Fatalf("The value stateSince isn't supposed to be set")
	}

	if mock.Stats(cmd_publish) != 1 {
		t.Fatalf("Event MIRROR_UPDATE should not be sent")
	}

	/* */

	cmd_previous_state = mock.Command("HGET", "MIRROR_m1", "up").Expect(int64(1))
	cmd_state_since = mock.Command("HMSET", "MIRROR_m1", "up", false, "excludeReason", "test3", "stateSince", redigomock.NewAnyInt()).Expect("ok")

	if err := SetMirrorState(conn, "m1", false, "test3"); err != nil {
		t.Fatalf("Unexpected error: %s", err)
	}

	if mock.Stats(cmd_previous_state) < 1 {
		t.Fatalf("Previous state not tested")
	}

	if mock.Stats(cmd_state_since) < 1 {
		t.Fatalf("New state not set")
	} else if mock.Stats(cmd_state_since) > 1 {
		t.Fatalf("State set more than once")
	}

	if mock.Stats(cmd_publish) < 2 {
		t.Fatalf("Event MIRROR_UPDATE not published")
	}
}
예제 #15
0
func TestSetMirrorEnabled(t *testing.T) {
	mock, conn := PrepareRedisTest()

	cmd_publish := mock.Command("PUBLISH", string(database.MIRROR_UPDATE), redigomock.NewAnyData()).Expect("ok")

	cmd_enable := mock.Command("HMSET", "MIRROR_m1", "enabled", true).Expect("ok")
	SetMirrorEnabled(conn, "m1", true)

	if mock.Stats(cmd_enable) < 1 {
		t.Fatalf("Mirror not enabled")
	} else if mock.Stats(cmd_enable) > 1 {
		t.Fatalf("Mirror enabled more than once")
	}

	if mock.Stats(cmd_publish) < 1 {
		t.Fatalf("Event MIRROR_UPDATE not published")
	}

	mock.Command("HMSET", "MIRROR_m1", "enabled", true).ExpectError(redis.Error("blah"))
	if SetMirrorEnabled(conn, "m1", true) == nil {
		t.Fatalf("Error expected")
	}

	cmd_disable := mock.Command("HMSET", "MIRROR_m1", "enabled", false).Expect("ok")
	SetMirrorEnabled(conn, "m1", false)

	if mock.Stats(cmd_disable) != 1 {
		t.Fatalf("Mirror not disabled")
	} else if mock.Stats(cmd_disable) > 1 {
		t.Fatalf("Mirror disabled more than once")
	}

	if mock.Stats(cmd_publish) < 2 {
		t.Fatalf("Event MIRROR_UPDATE not published")
	}

	mock.Command("HMSET", "MIRROR_m1", "enabled", false).ExpectError(redis.Error("blah"))
	if SetMirrorEnabled(conn, "m1", false) == nil {
		t.Fatalf("Error expected")
	}
}
예제 #16
0
func TestCache_fetchMirror(t *testing.T) {
	mock, conn := PrepareRedisTest()
	conn.ConnectPubsub()

	c := NewCache(conn)

	testmirror := Mirror{
		ID:             "m1",
		HttpURL:        "http://m1.mirror",
		RsyncURL:       "rsync://m1.mirror",
		FtpURL:         "ftp://m1.mirror",
		SponsorName:    "m1sponsor",
		SponsorURL:     "m1sponsorurl",
		SponsorLogoURL: "m1sponsorlogourl",
		AdminName:      "m1adminname",
		AdminEmail:     "m1adminemail",
		CustomData:     "m1customdata",
		ContinentOnly:  true,
		CountryOnly:    false,
		ASOnly:         true,
		Score:          0,
		Latitude:       -20.0,
		Longitude:      55.0,
		ContinentCode:  "EU",
		CountryCodes:   "FR UK",
		Asnum:          444,
		Comment:        "m1comment",
		Enabled:        true,
		Up:             true,
	}

	_, err := c.fetchMirror(testmirror.ID)
	if err == nil {
		t.Fatalf("Error expected, mock command not yet registered")
	}

	cmd_get_mirror := mock.Command("HGETALL", "MIRROR_m1").ExpectMap(map[string]string{
		"ID":            testmirror.ID,
		"http":          testmirror.HttpURL,
		"rsync":         testmirror.RsyncURL,
		"ftp":           testmirror.FtpURL,
		"sponsorName":   testmirror.SponsorName,
		"sponsorURL":    testmirror.SponsorURL,
		"sponsorLogo":   testmirror.SponsorLogoURL,
		"adminName":     testmirror.AdminName,
		"adminEmail":    testmirror.AdminEmail,
		"customData":    testmirror.CustomData,
		"continentOnly": strconv.FormatBool(testmirror.ContinentOnly),
		"countryOnly":   strconv.FormatBool(testmirror.CountryOnly),
		"asOnly":        strconv.FormatBool(testmirror.ASOnly),
		"score":         strconv.FormatInt(int64(testmirror.Score), 10),
		"latitude":      fmt.Sprintf("%f", testmirror.Latitude),
		"longitude":     fmt.Sprintf("%f", testmirror.Longitude),
		"continentCode": testmirror.ContinentCode,
		"countryCodes":  testmirror.CountryCodes,
		"asnum":         strconv.FormatInt(int64(testmirror.Asnum), 10),
		"comment":       testmirror.Comment,
		"enabled":       strconv.FormatBool(testmirror.Enabled),
		"up":            strconv.FormatBool(testmirror.Up),
	})

	m, err := c.fetchMirror(testmirror.ID)
	if err != nil {
		t.Fatalf("Unexpected error: %s", err.Error())
	}

	if mock.Stats(cmd_get_mirror) < 1 {
		t.Fatalf("HGETALL not executed")
	}

	// This is required to reach DeepEqual(ity)
	testmirror.CountryFields = strings.Fields(testmirror.CountryCodes)

	if !reflect.DeepEqual(testmirror, m) {
		t.Fatalf("Result is different")
	}

	_, ok := c.mCache.Get(testmirror.ID)
	if !ok {
		t.Fatalf("Not stored in cache")
	}
}
예제 #17
0
func TestCache_GetMirrors(t *testing.T) {
	mock, conn := PrepareRedisTest()
	conn.ConnectPubsub()

	c := NewCache(conn)

	filename := "/test/file.tgz"

	clientInfo := network.GeoIPRecord{
		GeoIPRecord: &geoip.GeoIPRecord{
			CountryCode: "FR",
			Latitude:    48.8567,
			Longitude:   2.3508,
		},
	}

	_, err := c.GetMirrors(filename, clientInfo)
	if err == nil {
		t.Fatalf("Error expected, mock command not yet registered")
	}

	cmd_get_filemirrors := mock.Command("SMEMBERS", "FILEMIRRORS_"+filename).Expect([]interface{}{
		[]byte("m1"),
		[]byte("m2"),
	})

	cmd_get_mirror_m1 := mock.Command("HGETALL", "MIRROR_m1").ExpectMap(map[string]string{
		"ID":        "m1",
		"latitude":  "52.5167",
		"longitude": "13.3833",
	})

	cmd_get_mirror_m2 := mock.Command("HGETALL", "MIRROR_m2").ExpectMap(map[string]string{
		"ID":        "m2",
		"latitude":  "51.5072",
		"longitude": "0.1275",
	})

	cmd_get_fileinfomirror_m1 := mock.Command("HMGET", "FILEINFO_m1_"+filename, "size", "modTime", "sha1", "sha256", "md5").ExpectMap(map[string]string{
		"size":    "44000",
		"modTime": "",
		"sha1":    "",
		"sha256":  "",
		"md5":     "",
	})

	cmd_get_fileinfomirror_m2 := mock.Command("HMGET", "FILEINFO_m2_"+filename, "size", "modTime", "sha1", "sha256", "md5").ExpectMap(map[string]string{
		"size":    "44000",
		"modTime": "",
		"sha1":    "",
		"sha256":  "",
		"md5":     "",
	})

	mirrors, err := c.GetMirrors(filename, clientInfo)
	if err != nil {
		t.Fatalf("Unexpected error: %s", err.Error())
	}

	if mock.Stats(cmd_get_filemirrors) < 1 {
		t.Fatalf("cmd_get_filemirrors not called")
	}
	if mock.Stats(cmd_get_mirror_m1) < 1 {
		t.Fatalf("cmd_get_mirror_m1 not called")
	}
	if mock.Stats(cmd_get_mirror_m2) < 1 {
		t.Fatalf("cmd_get_mirror_m2 not called")
	}
	if mock.Stats(cmd_get_fileinfomirror_m1) < 1 {
		t.Fatalf("cmd_get_fileinfomirror_m1 not called")
	}
	if mock.Stats(cmd_get_fileinfomirror_m2) < 1 {
		t.Fatalf("cmd_get_fileinfomirror_m2 not called")
	}

	if len(mirrors) != 2 {
		t.Fatalf("Invalid number of mirrors returned")
	}

	if int(mirrors[0].Distance) != int(876) {
		t.Fatalf("Distance between user and m1 is wrong, got %d, expected 876", int(mirrors[0].Distance))
	}

	if int(mirrors[1].Distance) != int(334) {
		t.Fatalf("Distance between user and m2 is wrong, got %d, expected 334", int(mirrors[1].Distance))
	}
}
예제 #18
0
func TestByRank_Less(t *testing.T) {
	rand.Seed(time.Now().UnixNano())

	/* */

	c := network.GeoIPRecord{}
	if c.IsValid() {
		t.Fatalf("GeoIPRecord is supposed to be invalid")
	}

	/* */

	// Generate two identical slices
	m1 := generateSimpleMirrorList(50)
	m2 := generateSimpleMirrorList(50)

	// Mirrors are indentical (besides name) so ByRank is expected
	// to randomize their order.
	sort.Sort(ByRank{m1, c})

	differences := 0
	for i, m := range m1 {
		if m.ID != m2[i].ID {
			differences++
		}
	}

	if differences == 0 {
		t.Fatalf("Result is supposed to be randomized")
	} else if differences < 10 {
		t.Fatalf("Too many similarities, something's wrong?")
	}

	// Sort again, just to be sure the result is different
	m3 := generateSimpleMirrorList(50)
	sort.Sort(ByRank{m3, c})

	differences = 0
	for i, m := range m3 {
		if m.ID != m1[i].ID {
			differences++
		}
	}

	if differences == 0 {
		t.Fatalf("Result is supposed to be different from previous run")
	} else if differences < 10 {
		t.Fatalf("Too many similarities, something's wrong?")
	}

	/* */

	c = network.GeoIPRecord{
		GeoIPRecord: &geoip.GeoIPRecord{
			CountryCode:   "FR",
			ContinentCode: "EU",
		},
		ASNum: 4444,
	}
	if !c.IsValid() {
		t.Fatalf("GeoIPRecord is supposed to be valid")
	}

	/* asnum */

	m := Mirrors{
		Mirror{
			ID:    "M0",
			Asnum: 6666,
		},
		Mirror{
			ID:    "M1",
			Asnum: 5555,
		},
		Mirror{
			ID:    "M2",
			Asnum: 4444,
		},
		Mirror{
			ID:    "M3",
			Asnum: 6666,
		},
	}

	sort.Sort(ByRank{m, c})

	if !matchingMirrorOrder(m, []string{"M2", "M0", "M1", "M3"}) {
		t.Fatalf("Order doesn't seem right: %s, expected M2, M0, M1, M3", formatMirrorOrder(m))
	}

	/* distance */

	m = Mirrors{
		Mirror{
			ID:       "M0",
			Distance: 1000.0,
		},
		Mirror{
			ID:       "M1",
			Distance: 999.0,
		},
		Mirror{
			ID:       "M2",
			Distance: 1000.0,
		},
		Mirror{
			ID:       "M3",
			Distance: 888.0,
		},
	}

	sort.Sort(ByRank{m, c})

	if !matchingMirrorOrder(m, []string{"M3", "M1", "M0", "M2"}) {
		t.Fatalf("Order doesn't seem right: %s, expected M3, M1, M0, M2", formatMirrorOrder(m))
	}

	/* countrycode */

	m = Mirrors{
		Mirror{
			ID:            "M0",
			CountryFields: []string{"IT", "UK"},
		},
		Mirror{
			ID:            "M1",
			CountryFields: []string{"IT", "UK"},
		},
		Mirror{
			ID:            "M2",
			CountryFields: []string{"IT", "FR"},
		},
		Mirror{
			ID:            "M3",
			CountryFields: []string{"FR", "UK"},
		},
	}

	sort.Sort(ByRank{m, c})

	if !matchingMirrorOrder(m, []string{"M2", "M3", "M0", "M1"}) {
		t.Fatalf("Order doesn't seem right: %s, expected M2, M3, M0, M1", formatMirrorOrder(m))
	}

	/* continentcode */

	c = network.GeoIPRecord{
		GeoIPRecord: &geoip.GeoIPRecord{
			ContinentCode: "EU",
		},
		ASNum: 4444,
	}

	m = Mirrors{
		Mirror{
			ID:            "M0",
			ContinentCode: "NA",
		},
		Mirror{
			ID:            "M1",
			ContinentCode: "NA",
		},
		Mirror{
			ID:            "M2",
			ContinentCode: "EU",
		},
		Mirror{
			ID:            "M3",
			ContinentCode: "NA",
		},
	}

	sort.Sort(ByRank{m, c})

	if !matchingMirrorOrder(m, []string{"M2", "M0", "M1", "M3"}) {
		t.Fatalf("Order doesn't seem right: %s, expected M2, M0, M1, M3", formatMirrorOrder(m))
	}

	/* */

	c = network.GeoIPRecord{
		GeoIPRecord: &geoip.GeoIPRecord{
			CountryCode:   "FR",
			ContinentCode: "EU",
		},
		ASNum: 4444,
	}

	m = Mirrors{
		Mirror{
			ID:            "M0",
			Distance:      100.0,
			CountryFields: []string{"IT", "FR"},
			ContinentCode: "EU",
		},
		Mirror{
			ID:            "M1",
			Distance:      200.0,
			CountryFields: []string{"FR", "CH"},
			ContinentCode: "EU",
		},
		Mirror{
			ID:            "M2",
			Distance:      1000.0,
			CountryFields: []string{"UK", "DE"},
			Asnum:         4444,
		},
	}

	sort.Sort(ByRank{m, c})

	if !matchingMirrorOrder(m, []string{"M2", "M0", "M1"}) {
		t.Fatalf("Order doesn't seem right: %s, expected M2, M0, M1", formatMirrorOrder(m))
	}
}
예제 #19
0
func TestCache_GetFileInfo(t *testing.T) {
	mock, conn := PrepareRedisTest()
	conn.ConnectPubsub()

	c := NewCache(conn)

	testfile := filesystem.FileInfo{
		Path:    "/test/file.tgz",
		Size:    43000,
		ModTime: time.Now(),
		Sha1:    "3ce963aea2d6f23fe915063f8bba21888db0ddfa",
		Sha256:  "1c8e38c7e03e4d117eba4f82afaf6631a9b79f4c1e9dec144d4faf1d109aacda",
		Md5:     "2c98ec39f49da6ddd9cfa7b1d7342afe",
	}

	_, err := c.GetFileInfo(testfile.Path)
	if err == nil {
		t.Fatalf("Error expected, mock command not yet registered")
	}

	cmd_get_fileinfo := mock.Command("HMGET", "FILE_"+testfile.Path, "size", "modTime", "sha1", "sha256", "md5").Expect([]interface{}{
		[]byte(strconv.FormatInt(testfile.Size, 10)),
		[]byte(testfile.ModTime.String()),
		[]byte(testfile.Sha1),
		[]byte(testfile.Sha256),
		[]byte(testfile.Md5),
	})

	f, err := c.GetFileInfo(testfile.Path)
	if err != nil {
		t.Fatalf("Unexpected error: %s", err.Error())
	}

	if mock.Stats(cmd_get_fileinfo) < 1 {
		t.Fatalf("HMGET not executed")
	}

	// Results are already checked by TestCache_fetchFileInfo
	// We only need to check one of them
	if f.ModTime != testfile.ModTime {
		t.Fatalf("One or more values do not match")
	}

	_, err = c.GetFileInfo(testfile.Path)
	if err == redis.ErrNil {
		t.Fatalf("Cache not used, request expected to be done once")
	} else if err != nil {
		t.Fatalf("Unexpected error: %s", err.Error())
	}
}