storj/satellite/geoip/example_testplanet_test.go
Kaloyan Raev 98b82486bd satellite/overlay: update country code on every node check-in
We have a specific issue that a user uploaded a file to a bucket
geo-fenced to EU and one of the pieces appeared to be on a node in the
US. The country code of this node is set to SE (Sweden) in the satellite
DB. It turns out that some time ago MaxMind changed the country code of
this node's IP from Sweden to US, but this change hasn't been reflected
in the satellite's database.

So far the satellite updates the country code of a node only if its IP
changes. It was assumed that if the IP does not change, its country code
shouldn't change too. This turned to be a wrong assumption.

With this change, the satellite will look up the MaxMindDB on every
check-in to see if the country code of the node's IP has changed.

Change-Id: Icdf659b09be9fc6ad14601902032b35ba5ea78c4
2023-03-22 08:38:51 +00:00

79 lines
2.3 KiB
Go

// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information
package geoip_test
import (
"fmt"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"storj.io/common/storj/location"
"storj.io/common/testcontext"
"storj.io/storj/private/testplanet"
"storj.io/storj/satellite"
"storj.io/storj/satellite/geoip"
"storj.io/storj/storagenode"
)
func TestGeoIPMock(t *testing.T) {
if runtime.GOOS == "darwin" {
t.Skip("Test does not work with macOS")
}
testplanet.Run(t,
testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 3, UplinkCount: 0,
Reconfigure: testplanet.Reconfigure{
Satellite: func(logger *zap.Logger, index int, config *satellite.Config) {
config.Overlay.GeoIP.MockCountries = []string{"US", "GB"}
},
StorageNode: func(index int, config *storagenode.Config) {
config.Server.Address = fmt.Sprintf("127.0.201.%d:0", index+1)
},
},
},
func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
// ensure storage nodes checked in with satellite
for _, node := range planet.StorageNodes {
node.Contact.Chore.TriggerWait(ctx)
}
// expected country codes per node index
countryCodes := map[int]location.CountryCode{
0: location.UnitedKingdom,
1: location.UnitedStates,
2: location.UnitedKingdom,
}
// check the country code for each storage nodes
for i, node := range planet.StorageNodes {
dossier, err := planet.Satellites[0].API.Overlay.DB.Get(ctx, node.ID())
require.NoError(t, err)
assert.Equal(t, countryCodes[i], dossier.CountryCode)
}
// change country in the mock GeoIP service from US to CA
planet.Satellites[0].Overlay.Service.GeoIP = geoip.NewMockIPToCountry([]string{"CA", "GB"})
// wait for storage nodes checked in again with satellite
for _, node := range planet.StorageNodes {
node.Contact.Chore.TriggerWait(ctx)
}
// adjust expected country codes for node 1
countryCodes[1] = location.Canada
// check the country code for each storage nodes
for i, node := range planet.StorageNodes {
dossier, err := planet.Satellites[0].API.Overlay.DB.Get(ctx, node.ID())
require.NoError(t, err)
assert.Equal(t, countryCodes[i], dossier.CountryCode)
}
},
)
}