Skip to content

Commit

Permalink
[chore] refactor test/cliparsing.sh into a go test below internal/con…
Browse files Browse the repository at this point in the history
…fig (#1036)

Also adds AddGlobalFlags and AddServerFlags as methods on ConfigState,
very useful for testing.
  • Loading branch information
LittleFox94 authored Nov 15, 2022
1 parent 5210977 commit 1f256e2
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 126 deletions.
1 change: 0 additions & 1 deletion .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ steps:
commands:
- apk update --no-cache && apk add git
- CGO_ENABLED=0 GTS_DB_TYPE="sqlite" GTS_DB_ADDRESS=":memory:" go test ./...
- CGO_ENABLED=0 ./test/cliparsing.sh
- CGO_ENABLED=0 ./test/envparsing.sh
when:
event:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ require (
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783
golang.org/x/text v0.4.0
gopkg.in/mcuadros/go-syslog.v2 v2.3.0
gopkg.in/yaml.v3 v3.0.1
modernc.org/sqlite v1.18.2
mvdan.cc/xurls/v2 v2.4.0
)
Expand Down Expand Up @@ -142,7 +143,6 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.38.1 // indirect
modernc.org/ccgo/v3 v3.16.9 // indirect
Expand Down
206 changes: 206 additions & 0 deletions internal/config/cliparsing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
GoToSocial
Copyright (C) 2022 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package config_test

import (
"os"
"strings"
"testing"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/superseriousbusiness/gotosocial/internal/config"
"gopkg.in/yaml.v3"
)

func expectedKV(kvpairs ...string) map[string]interface{} {
ret := make(map[string]interface{}, len(kvpairs)/2)

for i := 0; i < len(kvpairs)-1; i += 2 {
ret[kvpairs[i]] = kvpairs[i+1]
}

return ret
}

func expectedFile(t *testing.T, file string) map[string]interface{} {
expectedConfig, err := os.ReadFile(file)
if err != nil {
t.Errorf("error reading expected config from file %q: %v", file, err)
}

var ret map[string]interface{}
if err := yaml.Unmarshal(expectedConfig, &ret); err != nil {
t.Errorf("error parsing expected config from file %q: %v", file, err)
}

return ret
}

func TestCLIParsing(t *testing.T) {
type testcase struct {
cli []string
env []string
expected map[string]interface{}
}

defaults, _ := config.Defaults.MarshalMap()

testcases := map[string]testcase{
"Make sure defaults are set correctly": {
expected: defaults,
},

"Override db-address from default using cli flag": {
cli: []string{
"--db-address", "some.db.address",
},
expected: expectedKV(
"db-address", "some.db.address",
),
},

"Override db-address from default using env var": {
env: []string{
"GTS_DB_ADDRESS=some.db.address",
},
expected: expectedKV(
"db-address", "some.db.address",
),
},

"Override db-address from default using both env var and cli flag. The cli flag should take priority": {
cli: []string{
"--db-address", "some.db.address",
},
env: []string{
"GTS_DB_ADDRESS=some.other.db.address",
},
expected: expectedKV(
"db-address", "some.db.address",
),
},

"Loading a config file via env var": {
env: []string{
"GTS_CONFIG_PATH=testdata/test.yaml",
},
expected: expectedFile(t, "testdata/test.yaml"),
},

"Loading a config file via cli flag": {
cli: []string{
"--config-path", "testdata/test.yaml",
},
expected: expectedFile(t, "testdata/test.yaml"),
},

"Loading a config file and overriding one of the variables with a cli flag": {
cli: []string{
"--config-path", "testdata/test.yaml",
"--account-domain", "my.test.domain",
},
// only checking our overridden one and one non-default from the config file here instead of including all of test.yaml
expected: expectedKV(
"account-domain", "my.test.domain",
"host", "gts.example.org",
),
},

"Loading a config file and overriding one of the variables with an env var": {
cli: []string{
"--config-path", "testdata/test.yaml",
},
env: []string{
"GTS_ACCOUNT_DOMAIN=my.test.domain",
},
// only checking our overridden one and one non-default from the config file here instead of including all of test.yaml
expected: expectedKV(
"account-domain", "my.test.domain",
"host", "gts.example.org",
),
},

"Loading a config file and overriding one of the variables with both an env var and a cli flag. The cli flag should have priority": {
cli: []string{
"--config-path", "testdata/test.yaml",
"--account-domain", "my.test.domain",
},
env: []string{
"GTS_ACCOUNT_DOMAIN=my.wrong.test.domain",
},
// only checking our overridden one and one non-default from the config file here instead of including all of test.yaml
expected: expectedKV(
"account-domain", "my.test.domain",
"host", "gts.example.org",
),
},

"Loading a config file from json": {
cli: []string{
"--config-path", "testdata/test.json",
},
expected: expectedFile(t, "testdata/test.json"),
},

"Loading a partial config file. Default values should be used apart from those set in the config file": {
cli: []string{
"--config-path", "testdata/test2.yaml",
},
expected: expectedKV(
"log-level", "trace",
"account-domain", "peepee.poopoo",
"application-name", "gotosocial",
),
},
}

for desc, data := range testcases {
t.Run(desc, func(t *testing.T) {
os.Clearenv()

if data.env != nil {
for _, s := range data.env {
kv := strings.SplitN(s, "=", 2)
os.Setenv(kv[0], kv[1])
}
}

state := config.NewState()
cmd := cobra.Command{}
state.AddGlobalFlags(&cmd)
state.AddServerFlags(&cmd)

if data.cli != nil {
cmd.ParseFlags(data.cli)
}

state.BindFlags(&cmd)

state.Reload()

state.Viper(func(v *viper.Viper) {
for k, ev := range data.expected {
assert.EqualValues(t, ev, v.Get(k))
}
})
})
}
}
14 changes: 12 additions & 2 deletions internal/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ import (

// AddGlobalFlags will attach global configuration flags to given cobra command, loading defaults from global config.
func AddGlobalFlags(cmd *cobra.Command) {
Config(func(cfg *Configuration) {
global.AddGlobalFlags(cmd)
}

// AddGlobalFlags will attach global configuration flags to given cobra command, loading defaults from State.
func (s *ConfigState) AddGlobalFlags(cmd *cobra.Command) {
s.Config(func(cfg *Configuration) {
// General
cmd.PersistentFlags().String(ApplicationNameFlag(), cfg.ApplicationName, fieldtag("ApplicationName", "usage"))
cmd.PersistentFlags().String(LandingPageUserFlag(), cfg.LandingPageUser, fieldtag("LandingPageUser", "usage"))
Expand All @@ -51,7 +56,12 @@ func AddGlobalFlags(cmd *cobra.Command) {

// AddServerFlags will attach server configuration flags to given cobra command, loading defaults from global config.
func AddServerFlags(cmd *cobra.Command) {
Config(func(cfg *Configuration) {
global.AddServerFlags(cmd)
}

// AddServerFlags will attach server configuration flags to given cobra command, loading defaults from State.
func (s *ConfigState) AddServerFlags(cmd *cobra.Command) {
s.Config(func(cfg *Configuration) {
// Router
cmd.PersistentFlags().String(BindAddressFlag(), cfg.BindAddress, fieldtag("BindAddress", "usage"))
cmd.PersistentFlags().Int(PortFlag(), cfg.Port, fieldtag("Port", "usage"))
Expand Down
3 changes: 1 addition & 2 deletions test/test.json → internal/config/testdata/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"accounts-registration-open": true,
"application-name": "gotosocial",
"bind-address": "0.0.0.0",
"config-path": "./test/test.yaml",
"config-path": "testdata/test.json",
"db-address": "127.0.0.1",
"db-database": "postgres",
"db-password": "postgres",
Expand All @@ -14,7 +14,6 @@
"db-tls-mode": "disable",
"db-type": "postgres",
"db-user": "postgres",
"help": false,
"host": "gts.example.org",
"letsencrypt-cert-dir": "/gotosocial/storage/certs",
"letsencrypt-email-address": "",
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 1f256e2

Please sign in to comment.