2022-03-13 15:17:18 +01:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2022-04-15 17:30:12 +02:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/util"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-03-13 15:17:18 +01:00
|
|
|
)
|
|
|
|
|
2022-04-15 17:30:12 +02:00
|
|
|
func TestGetConfig(t *testing.T) {
|
2023-02-07 11:40:05 +01:00
|
|
|
// case 1: new default config has to be generated
|
|
|
|
config, err := GetConfig(ConfigInput{
|
|
|
|
ConfigPath: filepath.Join(t.TempDir(), "config.json"),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, config.ManagementURL.String(), DefaultManagementURL)
|
|
|
|
assert.Equal(t, config.AdminURL.String(), DefaultAdminURL)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-03-13 15:17:18 +01:00
|
|
|
managementURL := "https://test.management.url:33071"
|
2023-02-07 11:40:05 +01:00
|
|
|
adminURL := "https://app.admin.url:443"
|
2022-03-13 15:17:18 +01:00
|
|
|
path := filepath.Join(t.TempDir(), "config.json")
|
|
|
|
preSharedKey := "preSharedKey"
|
|
|
|
|
2023-02-07 11:40:05 +01:00
|
|
|
// case 2: new config has to be generated
|
|
|
|
config, err = GetConfig(ConfigInput{
|
2023-01-08 12:57:28 +01:00
|
|
|
ManagementURL: managementURL,
|
|
|
|
AdminURL: adminURL,
|
|
|
|
ConfigPath: path,
|
|
|
|
PreSharedKey: &preSharedKey,
|
|
|
|
})
|
2022-03-13 15:17:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, config.ManagementURL.String(), managementURL)
|
|
|
|
assert.Equal(t, config.PreSharedKey, preSharedKey)
|
|
|
|
|
|
|
|
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
|
|
|
|
t.Errorf("config file was expected to be created under path %s", path)
|
|
|
|
}
|
|
|
|
|
2023-02-07 11:40:05 +01:00
|
|
|
// case 3: existing config -> fetch it
|
2023-01-08 12:57:28 +01:00
|
|
|
config, err = GetConfig(ConfigInput{
|
|
|
|
ManagementURL: managementURL,
|
|
|
|
AdminURL: adminURL,
|
|
|
|
ConfigPath: path,
|
|
|
|
PreSharedKey: &preSharedKey,
|
|
|
|
})
|
2022-03-13 15:17:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, config.ManagementURL.String(), managementURL)
|
|
|
|
assert.Equal(t, config.PreSharedKey, preSharedKey)
|
|
|
|
|
2023-02-07 11:40:05 +01:00
|
|
|
// case 4: existing config, but new managementURL has been provided -> update config
|
2022-03-13 15:17:18 +01:00
|
|
|
newManagementURL := "https://test.newManagement.url:33071"
|
2023-01-08 12:57:28 +01:00
|
|
|
config, err = GetConfig(ConfigInput{
|
|
|
|
ManagementURL: newManagementURL,
|
|
|
|
AdminURL: adminURL,
|
|
|
|
ConfigPath: path,
|
|
|
|
PreSharedKey: &preSharedKey,
|
|
|
|
})
|
2022-03-13 15:17:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, config.ManagementURL.String(), newManagementURL)
|
|
|
|
assert.Equal(t, config.PreSharedKey, preSharedKey)
|
|
|
|
|
|
|
|
// read once more to make sure that config file has been updated with the new management URL
|
|
|
|
readConf, err := util.ReadJson(path, config)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(t, readConf.(*Config).ManagementURL.String(), newManagementURL)
|
|
|
|
}
|