mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-14 09:29:07 +02:00
Add tests
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
|||||||
|
|
||||||
"github.com/netbirdio/netbird/management/server"
|
"github.com/netbirdio/netbird/management/server"
|
||||||
"github.com/netbirdio/netbird/management/server/migration"
|
"github.com/netbirdio/netbird/management/server/migration"
|
||||||
|
nbpeer "github.com/netbirdio/netbird/management/server/peer"
|
||||||
"github.com/netbirdio/netbird/route"
|
"github.com/netbirdio/netbird/route"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -89,3 +90,71 @@ func TestMigrateFieldFromGobToJSON_WithJSONData(t *testing.T) {
|
|||||||
db.Model(&server.Account{}).Select("network_net").First(&jsonStr)
|
db.Model(&server.Account{}).Select("network_net").First(&jsonStr)
|
||||||
assert.JSONEq(t, `{"IP":"10.0.0.0","Mask":"////AA=="}`, jsonStr, "Data should be unchanged")
|
assert.JSONEq(t, `{"IP":"10.0.0.0","Mask":"////AA=="}`, jsonStr, "Data should be unchanged")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMigrateNetIPFieldFromBlobToJSON_EmptyDB(t *testing.T) {
|
||||||
|
db := setupDatabase(t)
|
||||||
|
err := migration.MigrateNetIPFieldFromBlobToJSON[nbpeer.Peer](db, "ip", "idx_peers_account_id_ip")
|
||||||
|
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")
|
||||||
|
|
||||||
|
err = migration.MigrateNetIPFieldFromBlobToJSON[nbpeer.Peer](db, "location_connection_ip", "")
|
||||||
|
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{
|
||||||
|
PeersG: []nbpeer.Peer{
|
||||||
|
{Location: nbpeer.Location{ConnectionIP: net.IP{10, 0, 0, 1}}},
|
||||||
|
}},
|
||||||
|
).Error
|
||||||
|
require.NoError(t, err, "Failed to insert JSON data")
|
||||||
|
|
||||||
|
err = migration.MigrateNetIPFieldFromBlobToJSON[nbpeer.Peer](db, "location_connection_ip", "")
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
nbdns "github.com/netbirdio/netbird/dns"
|
|
||||||
nbgroup "github.com/netbirdio/netbird/management/server/group"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
@@ -12,6 +10,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
nbdns "github.com/netbirdio/netbird/dns"
|
||||||
|
nbgroup "github.com/netbirdio/netbird/management/server/group"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@@ -518,15 +519,29 @@ func TestMigrate(t *testing.T) {
|
|||||||
Net net.IPNet `gorm:"serializer:gob"`
|
Net net.IPNet `gorm:"serializer:gob"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type location struct {
|
||||||
|
nbpeer.Location
|
||||||
|
ConnectionIP net.IP
|
||||||
|
}
|
||||||
|
|
||||||
|
type peer struct {
|
||||||
|
nbpeer.Peer
|
||||||
|
Location location `gorm:"embedded;embeddedPrefix:location_"`
|
||||||
|
}
|
||||||
|
|
||||||
type account struct {
|
type account struct {
|
||||||
Account
|
Account
|
||||||
Network *network `gorm:"embedded;embeddedPrefix:network_"`
|
Network *network `gorm:"embedded;embeddedPrefix:network_"`
|
||||||
|
Peers []peer `gorm:"foreignKey:AccountID;references:id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
act := &account{
|
act := &account{
|
||||||
Network: &network{
|
Network: &network{
|
||||||
Net: *ipnet,
|
Net: *ipnet,
|
||||||
},
|
},
|
||||||
|
Peers: []peer{
|
||||||
|
{Location: location{ConnectionIP: net.IP{10, 0, 0, 1}}},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
err = store.db.Save(act).Error
|
err = store.db.Save(act).Error
|
||||||
|
|||||||
Reference in New Issue
Block a user