github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/netconf/config_test.go (about) 1 package netconf_test 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/spf13/viper" 9 "github.com/stretchr/testify/require" 10 11 "github.com/onflow/flow-go/config" 12 "github.com/onflow/flow-go/network/netconf" 13 ) 14 15 // TestSetAliases ensures every network configuration key prefixed with "network" has an alias without the "network" prefix. 16 func TestSetAliases(t *testing.T) { 17 c := viper.New() 18 for _, key := range netconf.AllFlagNames() { 19 c.Set(fmt.Sprintf("network.%s", key), "not aliased") 20 c.Set(key, "aliased") 21 } 22 23 // ensure network prefixed keys do not point to non-prefixed alias 24 for _, key := range c.AllKeys() { 25 parts := strings.Split(key, ".") 26 if len(parts) != 2 { 27 continue 28 } 29 require.NotEqual(t, c.GetString(parts[1]), c.GetString(key)) 30 } 31 32 err := netconf.SetAliases(c) 33 require.NoError(t, err) 34 35 // ensure each network prefixed key now points to the non-prefixed alias 36 for _, key := range c.AllKeys() { 37 parts := strings.Split(key, ".") 38 if len(parts) != 2 { 39 continue 40 } 41 require.Equal(t, c.GetString(parts[1]), c.GetString(key)) 42 } 43 } 44 45 // TestCrossReferenceFlagsWithConfigs ensures that each flag is cross-referenced with the config file, i.e., that each 46 // flag has a corresponding config key. 47 func TestCrossReferenceFlagsWithConfigs(t *testing.T) { 48 // reads the default config file 49 c := config.RawViperConfig() 50 err := netconf.SetAliases(c) 51 require.NoError(t, err) 52 } 53 54 // TestCrossReferenceConfigsWithFlags ensures that each config is cross-referenced with the flags, i.e., that each config 55 // key has a corresponding flag. 56 func TestCrossReferenceConfigsWithFlags(t *testing.T) { 57 c := config.RawViperConfig() 58 // keeps all flag names 59 m := make(map[string]struct{}) 60 61 // each flag name should correspond to exactly one key in our config store after it is loaded with the default config 62 for _, flagName := range netconf.AllFlagNames() { 63 m[flagName] = struct{}{} 64 } 65 66 for _, key := range c.AllKeys() { 67 s := strings.Split(key, ".") 68 flag := strings.Join(s[1:], "-") 69 if len(flag) == 0 { 70 continue 71 } 72 _, ok := m[flag] 73 require.Truef(t, ok, "config key %s does not have a corresponding flag", flag) 74 } 75 }