2024-04-18 18:14:21 +02:00
|
|
|
package migration_test
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2024-04-18 18:14:21 +02:00
|
|
|
"encoding/gob"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/migration"
|
2024-05-07 13:01:45 +02:00
|
|
|
nbpeer "github.com/netbirdio/netbird/management/server/peer"
|
2024-04-18 18:14:21 +02:00
|
|
|
"github.com/netbirdio/netbird/route"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupDatabase(t *testing.T) *gorm.DB {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{
|
|
|
|
PrepareStmt: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err, "Failed to open database")
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMigrateFieldFromGobToJSON_EmptyDB(t *testing.T) {
|
|
|
|
db := setupDatabase(t)
|
2024-07-03 11:33:02 +02:00
|
|
|
err := migration.MigrateFieldFromGobToJSON[server.Account, net.IPNet](context.Background(), db, "network_net")
|
2024-04-18 18:14:21 +02:00
|
|
|
require.NoError(t, err, "Migration should not fail for an empty database")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMigrateFieldFromGobToJSON_WithGobData(t *testing.T) {
|
|
|
|
db := setupDatabase(t)
|
|
|
|
|
|
|
|
err := db.AutoMigrate(&server.Account{}, &route.Route{})
|
|
|
|
require.NoError(t, err, "Failed to auto-migrate tables")
|
|
|
|
|
|
|
|
_, ipnet, err := net.ParseCIDR("10.0.0.0/24")
|
|
|
|
require.NoError(t, err, "Failed to parse CIDR")
|
|
|
|
|
|
|
|
type network struct {
|
|
|
|
server.Network
|
|
|
|
Net net.IPNet `gorm:"serializer:gob"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type account struct {
|
|
|
|
server.Account
|
|
|
|
Network *network `gorm:"embedded;embeddedPrefix:network_"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Save(&account{Account: server.Account{Id: "123"}, Network: &network{Net: *ipnet}}).Error
|
|
|
|
require.NoError(t, err, "Failed to insert Gob data")
|
|
|
|
|
|
|
|
var gobStr string
|
|
|
|
err = db.Model(&server.Account{}).Select("network_net").First(&gobStr).Error
|
|
|
|
assert.NoError(t, err, "Failed to fetch Gob data")
|
|
|
|
|
|
|
|
err = gob.NewDecoder(strings.NewReader(gobStr)).Decode(&ipnet)
|
|
|
|
require.NoError(t, err, "Failed to decode Gob data")
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = migration.MigrateFieldFromGobToJSON[server.Account, net.IPNet](context.Background(), db, "network_net")
|
2024-04-18 18:14:21 +02:00
|
|
|
require.NoError(t, err, "Migration should not fail with Gob data")
|
|
|
|
|
|
|
|
var jsonStr string
|
|
|
|
db.Model(&server.Account{}).Select("network_net").First(&jsonStr)
|
|
|
|
assert.JSONEq(t, `{"IP":"10.0.0.0","Mask":"////AA=="}`, jsonStr, "Data should be migrated")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMigrateFieldFromGobToJSON_WithJSONData(t *testing.T) {
|
|
|
|
db := setupDatabase(t)
|
|
|
|
|
|
|
|
err := db.AutoMigrate(&server.Account{}, &route.Route{})
|
|
|
|
require.NoError(t, err, "Failed to auto-migrate tables")
|
|
|
|
|
|
|
|
_, ipnet, err := net.ParseCIDR("10.0.0.0/24")
|
|
|
|
require.NoError(t, err, "Failed to parse CIDR")
|
|
|
|
|
|
|
|
err = db.Save(&server.Account{Network: &server.Network{Net: *ipnet}}).Error
|
|
|
|
require.NoError(t, err, "Failed to insert JSON data")
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = migration.MigrateFieldFromGobToJSON[server.Account, net.IPNet](context.Background(), db, "network_net")
|
2024-04-18 18:14:21 +02:00
|
|
|
require.NoError(t, err, "Migration should not fail with JSON data")
|
|
|
|
|
|
|
|
var jsonStr string
|
|
|
|
db.Model(&server.Account{}).Select("network_net").First(&jsonStr)
|
|
|
|
assert.JSONEq(t, `{"IP":"10.0.0.0","Mask":"////AA=="}`, jsonStr, "Data should be unchanged")
|
|
|
|
}
|
2024-05-07 13:01:45 +02:00
|
|
|
|
|
|
|
func TestMigrateNetIPFieldFromBlobToJSON_EmptyDB(t *testing.T) {
|
|
|
|
db := setupDatabase(t)
|
2024-07-03 11:33:02 +02:00
|
|
|
err := migration.MigrateNetIPFieldFromBlobToJSON[nbpeer.Peer](context.Background(), db, "ip", "idx_peers_account_id_ip")
|
2024-05-07 13:01:45 +02:00
|
|
|
require.NoError(t, err, "Migration should not fail for an empty database")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMigrateNetIPFieldFromBlobToJSON_WithBlobData(t *testing.T) {
|
|
|
|
db := setupDatabase(t)
|
|
|
|
|
|
|
|
err := db.AutoMigrate(&server.Account{}, &nbpeer.Peer{})
|
|
|
|
require.NoError(t, err, "Failed to auto-migrate tables")
|
|
|
|
|
|
|
|
type location struct {
|
|
|
|
nbpeer.Location
|
|
|
|
ConnectionIP net.IP
|
|
|
|
}
|
|
|
|
|
|
|
|
type peer struct {
|
|
|
|
nbpeer.Peer
|
|
|
|
Location location `gorm:"embedded;embeddedPrefix:location_"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type account struct {
|
|
|
|
server.Account
|
|
|
|
Peers []peer `gorm:"foreignKey:AccountID;references:id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Save(&account{
|
|
|
|
Account: server.Account{Id: "123"},
|
|
|
|
Peers: []peer{
|
|
|
|
{Location: location{ConnectionIP: net.IP{10, 0, 0, 1}}},
|
|
|
|
}},
|
|
|
|
).Error
|
|
|
|
require.NoError(t, err, "Failed to insert blob data")
|
|
|
|
|
|
|
|
var blobValue string
|
|
|
|
err = db.Model(&nbpeer.Peer{}).Select("location_connection_ip").First(&blobValue).Error
|
|
|
|
assert.NoError(t, err, "Failed to fetch blob data")
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = migration.MigrateNetIPFieldFromBlobToJSON[nbpeer.Peer](context.Background(), db, "location_connection_ip", "")
|
2024-05-07 13:01:45 +02:00
|
|
|
require.NoError(t, err, "Migration should not fail with net.IP blob data")
|
|
|
|
|
|
|
|
var jsonStr string
|
|
|
|
db.Model(&nbpeer.Peer{}).Select("location_connection_ip").First(&jsonStr)
|
|
|
|
assert.JSONEq(t, `"10.0.0.1"`, jsonStr, "Data should be migrated")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMigrateNetIPFieldFromBlobToJSON_WithJSONData(t *testing.T) {
|
|
|
|
db := setupDatabase(t)
|
|
|
|
|
|
|
|
err := db.AutoMigrate(&server.Account{}, &nbpeer.Peer{})
|
|
|
|
require.NoError(t, err, "Failed to auto-migrate tables")
|
|
|
|
|
|
|
|
err = db.Save(&server.Account{
|
|
|
|
Id: "1234",
|
|
|
|
PeersG: []nbpeer.Peer{
|
|
|
|
{Location: nbpeer.Location{ConnectionIP: net.IP{10, 0, 0, 1}}},
|
|
|
|
}},
|
|
|
|
).Error
|
|
|
|
require.NoError(t, err, "Failed to insert JSON data")
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = migration.MigrateNetIPFieldFromBlobToJSON[nbpeer.Peer](context.Background(), db, "location_connection_ip", "")
|
2024-05-07 13:01:45 +02:00
|
|
|
require.NoError(t, err, "Migration should not fail with net.IP JSON data")
|
|
|
|
|
|
|
|
var jsonStr string
|
|
|
|
db.Model(&nbpeer.Peer{}).Select("location_connection_ip").First(&jsonStr)
|
|
|
|
assert.JSONEq(t, `"10.0.0.1"`, jsonStr, "Data should be unchanged")
|
|
|
|
}
|
2024-10-28 17:52:23 +01:00
|
|
|
|
|
|
|
func TestMigrateSetupKeyToHashedSetupKey_ForPlainKey(t *testing.T) {
|
|
|
|
db := setupDatabase(t)
|
|
|
|
|
|
|
|
err := db.AutoMigrate(&server.SetupKey{})
|
|
|
|
require.NoError(t, err, "Failed to auto-migrate tables")
|
|
|
|
|
|
|
|
err = db.Save(&server.SetupKey{
|
|
|
|
Id: "1",
|
|
|
|
Key: "EEFDAB47-C1A5-4472-8C05-71DE9A1E8382",
|
|
|
|
}).Error
|
|
|
|
require.NoError(t, err, "Failed to insert setup key")
|
|
|
|
|
|
|
|
err = migration.MigrateSetupKeyToHashedSetupKey[server.SetupKey](context.Background(), db)
|
|
|
|
require.NoError(t, err, "Migration should not fail to migrate setup key")
|
|
|
|
|
|
|
|
var key server.SetupKey
|
|
|
|
err = db.Model(&server.SetupKey{}).First(&key).Error
|
|
|
|
assert.NoError(t, err, "Failed to fetch setup key")
|
|
|
|
|
|
|
|
assert.Equal(t, "EEFDA****", key.KeySecret, "Key should be secret")
|
|
|
|
assert.Equal(t, "9+FQcmNd2GCxIK+SvHmtp6PPGV4MKEicDS+xuSQmvlE=", key.Key, "Key should be hashed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMigrateSetupKeyToHashedSetupKey_ForAlreadyMigratedKey_Case1(t *testing.T) {
|
|
|
|
db := setupDatabase(t)
|
|
|
|
|
|
|
|
err := db.AutoMigrate(&server.SetupKey{})
|
|
|
|
require.NoError(t, err, "Failed to auto-migrate tables")
|
|
|
|
|
|
|
|
err = db.Save(&server.SetupKey{
|
|
|
|
Id: "1",
|
|
|
|
Key: "9+FQcmNd2GCxIK+SvHmtp6PPGV4MKEicDS+xuSQmvlE=",
|
|
|
|
KeySecret: "EEFDA****",
|
|
|
|
}).Error
|
|
|
|
require.NoError(t, err, "Failed to insert setup key")
|
|
|
|
|
|
|
|
err = migration.MigrateSetupKeyToHashedSetupKey[server.SetupKey](context.Background(), db)
|
|
|
|
require.NoError(t, err, "Migration should not fail to migrate setup key")
|
|
|
|
|
|
|
|
var key server.SetupKey
|
|
|
|
err = db.Model(&server.SetupKey{}).First(&key).Error
|
|
|
|
assert.NoError(t, err, "Failed to fetch setup key")
|
|
|
|
|
|
|
|
assert.Equal(t, "EEFDA****", key.KeySecret, "Key should be secret")
|
|
|
|
assert.Equal(t, "9+FQcmNd2GCxIK+SvHmtp6PPGV4MKEicDS+xuSQmvlE=", key.Key, "Key should be hashed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMigrateSetupKeyToHashedSetupKey_ForAlreadyMigratedKey_Case2(t *testing.T) {
|
|
|
|
db := setupDatabase(t)
|
|
|
|
|
|
|
|
err := db.AutoMigrate(&server.SetupKey{})
|
|
|
|
require.NoError(t, err, "Failed to auto-migrate tables")
|
|
|
|
|
|
|
|
err = db.Save(&server.SetupKey{
|
|
|
|
Id: "1",
|
|
|
|
Key: "9+FQcmNd2GCxIK+SvHmtp6PPGV4MKEicDS+xuSQmvlE=",
|
|
|
|
}).Error
|
|
|
|
require.NoError(t, err, "Failed to insert setup key")
|
|
|
|
|
|
|
|
err = migration.MigrateSetupKeyToHashedSetupKey[server.SetupKey](context.Background(), db)
|
|
|
|
require.NoError(t, err, "Migration should not fail to migrate setup key")
|
|
|
|
|
|
|
|
var key server.SetupKey
|
|
|
|
err = db.Model(&server.SetupKey{}).First(&key).Error
|
|
|
|
assert.NoError(t, err, "Failed to fetch setup key")
|
|
|
|
|
|
|
|
assert.Equal(t, "9+FQcmNd2GCxIK+SvHmtp6PPGV4MKEicDS+xuSQmvlE=", key.Key, "Key should be hashed")
|
|
|
|
}
|