mirror of
https://github.com/netbirdio/netbird.git
synced 2024-12-12 18:00:49 +01:00
bb40325977
This PR showcases the implementation of additional linter rules. I've updated the golangci-lint GitHub Actions to the latest available version. This update makes sure that the tool works the same way locally - assuming being updated regularly - and with the GitHub Actions. I've also taken care of keeping all the GitHub Actions up to date, which helps our code stay current. But there's one part, goreleaser that's a bit tricky to test on our computers. So, it's important to take a close look at that. To make it easier to understand what I've done, I've made separate changes for each thing that the new linters found. This should help the people reviewing the changes see what's going on more clearly. Some of the changes might not be obvious at first glance. Things to consider for the future CI runs on Ubuntu so the static analysis only happens for Linux. Consider running it for the rest: Darwin, Windows
137 lines
3.4 KiB
Go
137 lines
3.4 KiB
Go
package internal
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/netbirdio/netbird/util"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestGetConfig(t *testing.T) {
|
|
// case 1: new default config has to be generated
|
|
config, err := UpdateOrCreateConfig(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)
|
|
|
|
managementURL := "https://test.management.url:33071"
|
|
adminURL := "https://app.admin.url:443"
|
|
path := filepath.Join(t.TempDir(), "config.json")
|
|
preSharedKey := "preSharedKey"
|
|
|
|
// case 2: new config has to be generated
|
|
config, err = UpdateOrCreateConfig(ConfigInput{
|
|
ManagementURL: managementURL,
|
|
AdminURL: adminURL,
|
|
ConfigPath: path,
|
|
PreSharedKey: &preSharedKey,
|
|
})
|
|
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)
|
|
}
|
|
|
|
// case 3: existing config -> fetch it
|
|
config, err = UpdateOrCreateConfig(ConfigInput{
|
|
ManagementURL: managementURL,
|
|
AdminURL: adminURL,
|
|
ConfigPath: path,
|
|
PreSharedKey: &preSharedKey,
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
assert.Equal(t, config.ManagementURL.String(), managementURL)
|
|
assert.Equal(t, config.PreSharedKey, preSharedKey)
|
|
|
|
// case 4: new empty pre-shared key config -> fetch it
|
|
newPreSharedKey := ""
|
|
config, err = UpdateOrCreateConfig(ConfigInput{
|
|
ManagementURL: managementURL,
|
|
AdminURL: adminURL,
|
|
ConfigPath: path,
|
|
PreSharedKey: &newPreSharedKey,
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
assert.Equal(t, config.ManagementURL.String(), managementURL)
|
|
assert.Equal(t, config.PreSharedKey, preSharedKey)
|
|
|
|
// case 5: existing config, but new managementURL has been provided -> update config
|
|
newManagementURL := "https://test.newManagement.url:33071"
|
|
config, err = UpdateOrCreateConfig(ConfigInput{
|
|
ManagementURL: newManagementURL,
|
|
AdminURL: adminURL,
|
|
ConfigPath: path,
|
|
PreSharedKey: &preSharedKey,
|
|
})
|
|
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)
|
|
}
|
|
|
|
func TestHiddenPreSharedKey(t *testing.T) {
|
|
hidden := "**********"
|
|
samplePreSharedKey := "mysecretpresharedkey"
|
|
tests := []struct {
|
|
name string
|
|
preSharedKey *string
|
|
want string
|
|
}{
|
|
{"nil", nil, ""},
|
|
{"hidden", &hidden, ""},
|
|
{"filled", &samplePreSharedKey, samplePreSharedKey},
|
|
}
|
|
|
|
// generate default cfg
|
|
cfgFile := filepath.Join(t.TempDir(), "config.json")
|
|
_, _ = UpdateOrCreateConfig(ConfigInput{
|
|
ConfigPath: cfgFile,
|
|
})
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
cfg, err := UpdateOrCreateConfig(ConfigInput{
|
|
ConfigPath: cfgFile,
|
|
PreSharedKey: tt.preSharedKey,
|
|
})
|
|
|
|
if err != nil {
|
|
t.Fatalf("failed to get cfg: %s", err)
|
|
}
|
|
|
|
if cfg.PreSharedKey != tt.want {
|
|
t.Fatalf("invalid preshared key: '%s', expected: '%s' ", cfg.PreSharedKey, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|