mirror of
https://github.com/qdm12/gluetun.git
synced 2026-07-24 19:36:24 +02:00
d9cc7dcffb
- new directory structure containing manifest.json and one json file per provider, by default. - the manifest.json file can specify a filepath for each vpn provider - each vpn provider json data file can contain the `"preferred": true` field to enforce it is used even if outdated, unless there is a version mismatch - `STORAGE_SERVERS_DIRECTORY_PATH` replaces `STORAGE_FILEPATH` (which is now a migration source only). It sets the directory where server manifest and per-provider JSON files are stored (default: `/gluetun/servers/`). - First-run migration: On startup, gluetun checks for the old /gluetun/servers.json file; if found and no new manifest exists, it automatically migrates all data to /gluetun/servers/ directory structure - Silent fallback: If legacy file isn't found, uses the new directory path normally - Legacy cleanup: After successful migration, attempts to remove the old fat JSON file (logs warning only if removal fails, e.g., read-only bind mounts)
213 lines
4.5 KiB
Go
213 lines
4.5 KiB
Go
package ipvanish
|
|
|
|
import (
|
|
"net/netip"
|
|
"testing"
|
|
|
|
"github.com/qdm12/gluetun-servers/pkg/constants"
|
|
"github.com/qdm12/gluetun-servers/pkg/models"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func Test_hostToServer_add(t *testing.T) {
|
|
t.Parallel()
|
|
testCases := map[string]struct {
|
|
initialHTS hostToServer
|
|
host string
|
|
country string
|
|
city string
|
|
tcp bool
|
|
udp bool
|
|
expectedHTS hostToServer
|
|
}{
|
|
"empty host to server": {
|
|
initialHTS: hostToServer{},
|
|
host: "host",
|
|
country: "country",
|
|
city: "city",
|
|
tcp: true,
|
|
udp: true,
|
|
expectedHTS: hostToServer{
|
|
"host": {
|
|
VPN: constants.OpenVPN,
|
|
Hostname: "host",
|
|
Country: "country",
|
|
City: "city",
|
|
TCP: true,
|
|
UDP: true,
|
|
},
|
|
},
|
|
},
|
|
"add server": {
|
|
initialHTS: hostToServer{
|
|
"existing host": {},
|
|
},
|
|
host: "host",
|
|
country: "country",
|
|
city: "city",
|
|
tcp: true,
|
|
udp: true,
|
|
expectedHTS: hostToServer{
|
|
"existing host": {},
|
|
"host": models.Server{
|
|
VPN: constants.OpenVPN,
|
|
Hostname: "host",
|
|
Country: "country",
|
|
City: "city",
|
|
TCP: true,
|
|
UDP: true,
|
|
},
|
|
},
|
|
},
|
|
"extend existing server": {
|
|
initialHTS: hostToServer{
|
|
"host": models.Server{
|
|
VPN: constants.OpenVPN,
|
|
Hostname: "host",
|
|
Country: "country",
|
|
City: "city",
|
|
TCP: true,
|
|
},
|
|
},
|
|
host: "host",
|
|
country: "country",
|
|
city: "city",
|
|
tcp: false,
|
|
udp: true,
|
|
expectedHTS: hostToServer{
|
|
"host": models.Server{
|
|
VPN: constants.OpenVPN,
|
|
Hostname: "host",
|
|
Country: "country",
|
|
City: "city",
|
|
TCP: true,
|
|
UDP: true,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for name, testCase := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
t.Parallel()
|
|
testCase.initialHTS.add(testCase.host, testCase.country, testCase.city, testCase.tcp, testCase.udp)
|
|
assert.Equal(t, testCase.expectedHTS, testCase.initialHTS)
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_hostToServer_toHostsSlice(t *testing.T) {
|
|
t.Parallel()
|
|
testCases := map[string]struct {
|
|
hts hostToServer
|
|
hosts []string
|
|
}{
|
|
"empty host to server": {
|
|
hts: hostToServer{},
|
|
hosts: []string{},
|
|
},
|
|
"single host": {
|
|
hts: hostToServer{
|
|
"A": {},
|
|
},
|
|
hosts: []string{"A"},
|
|
},
|
|
"multiple hosts": {
|
|
hts: hostToServer{
|
|
"A": {},
|
|
"B": {},
|
|
},
|
|
hosts: []string{"A", "B"},
|
|
},
|
|
}
|
|
for name, testCase := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
t.Parallel()
|
|
hosts := testCase.hts.toHostsSlice()
|
|
assert.ElementsMatch(t, testCase.hosts, hosts)
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_hostToServer_adaptWithIPs(t *testing.T) {
|
|
t.Parallel()
|
|
testCases := map[string]struct {
|
|
initialHTS hostToServer
|
|
hostToIPs map[string][]netip.Addr
|
|
expectedHTS hostToServer
|
|
}{
|
|
"create server": {
|
|
initialHTS: hostToServer{},
|
|
hostToIPs: map[string][]netip.Addr{
|
|
"A": {netip.AddrFrom4([4]byte{1, 2, 3, 4})},
|
|
},
|
|
expectedHTS: hostToServer{
|
|
"A": models.Server{
|
|
IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 2, 3, 4})},
|
|
},
|
|
},
|
|
},
|
|
"add IPs to existing server": {
|
|
initialHTS: hostToServer{
|
|
"A": models.Server{
|
|
Country: "country",
|
|
},
|
|
},
|
|
hostToIPs: map[string][]netip.Addr{
|
|
"A": {netip.AddrFrom4([4]byte{1, 2, 3, 4})},
|
|
},
|
|
expectedHTS: hostToServer{
|
|
"A": models.Server{
|
|
Country: "country",
|
|
IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 2, 3, 4})},
|
|
},
|
|
},
|
|
},
|
|
"remove server without IP": {
|
|
initialHTS: hostToServer{
|
|
"A": models.Server{
|
|
Country: "country",
|
|
},
|
|
},
|
|
hostToIPs: map[string][]netip.Addr{},
|
|
expectedHTS: hostToServer{},
|
|
},
|
|
}
|
|
for name, testCase := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
t.Parallel()
|
|
testCase.initialHTS.adaptWithIPs(testCase.hostToIPs)
|
|
assert.Equal(t, testCase.expectedHTS, testCase.initialHTS)
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_hostToServer_toServersSlice(t *testing.T) {
|
|
t.Parallel()
|
|
testCases := map[string]struct {
|
|
hts hostToServer
|
|
servers []models.Server
|
|
}{
|
|
"empty host to server": {
|
|
hts: hostToServer{},
|
|
servers: []models.Server{},
|
|
},
|
|
"multiple servers": {
|
|
hts: hostToServer{
|
|
"A": {Country: "A"},
|
|
"B": {Country: "B"},
|
|
},
|
|
servers: []models.Server{
|
|
{Country: "A"},
|
|
{Country: "B"},
|
|
},
|
|
},
|
|
}
|
|
for name, testCase := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
t.Parallel()
|
|
servers := testCase.hts.toServersSlice()
|
|
assert.ElementsMatch(t, testCase.servers, servers)
|
|
})
|
|
}
|
|
}
|