-
Notifications
You must be signed in to change notification settings - Fork 8
/
env_test.go
232 lines (206 loc) · 13.1 KB
/
env_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package config_test
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/chia-network/go-chia-libs/pkg/config"
"github.com/chia-network/go-chia-libs/pkg/types"
)
func TestChiaConfig_SetFieldByPath(t *testing.T) {
defaultConfig, err := config.LoadDefaultConfig()
assert.NoError(t, err)
// Make assertions about the default state, to ensure the assumed initial values are correct
assert.Equal(t, uint16(8444), defaultConfig.FullNode.Port)
assert.Equal(t, uint16(8555), defaultConfig.FullNode.RPCPort)
assert.NotNil(t, defaultConfig.NetworkOverrides.Constants["mainnet"])
assert.Equal(t, defaultConfig.NetworkOverrides.Constants["mainnet"].DifficultyConstantFactor, types.Uint128{})
assert.Equal(t, *defaultConfig.SelectedNetwork, "mainnet")
assert.Equal(t, defaultConfig.Logging.LogLevel, "WARNING")
err = defaultConfig.SetFieldByPath([]string{"full_node", "port"}, "1234")
assert.NoError(t, err)
assert.Equal(t, uint16(1234), defaultConfig.FullNode.Port)
err = defaultConfig.SetFieldByPath([]string{"full_node", "rpc_port"}, "5678")
assert.NoError(t, err)
assert.Equal(t, uint16(5678), defaultConfig.FullNode.RPCPort)
err = defaultConfig.SetFieldByPath([]string{"network_overrides", "constants", "mainnet", "DIFFICULTY_CONSTANT_FACTOR"}, "44445555")
assert.NoError(t, err)
assert.NotNil(t, defaultConfig.NetworkOverrides.Constants["mainnet"])
assert.Equal(t, types.Uint128From64(44445555), defaultConfig.NetworkOverrides.Constants["mainnet"].DifficultyConstantFactor)
err = defaultConfig.SetFieldByPath([]string{"selected_network"}, "unittestnet")
assert.NoError(t, err)
assert.Equal(t, *defaultConfig.SelectedNetwork, "unittestnet")
// Ensure all the other selected networks got set too
assert.Equal(t, *defaultConfig.Seeder.SelectedNetwork, "unittestnet")
assert.Equal(t, *defaultConfig.Harvester.SelectedNetwork, "unittestnet")
assert.Equal(t, *defaultConfig.Pool.SelectedNetwork, "unittestnet")
assert.Equal(t, *defaultConfig.Farmer.SelectedNetwork, "unittestnet")
assert.Equal(t, *defaultConfig.Timelord.SelectedNetwork, "unittestnet")
assert.Equal(t, *defaultConfig.FullNode.SelectedNetwork, "unittestnet")
assert.Equal(t, *defaultConfig.UI.SelectedNetwork, "unittestnet")
assert.Equal(t, *defaultConfig.Introducer.SelectedNetwork, "unittestnet")
assert.Equal(t, *defaultConfig.Wallet.SelectedNetwork, "unittestnet")
assert.Equal(t, *defaultConfig.DataLayer.SelectedNetwork, "unittestnet")
err = defaultConfig.SetFieldByPath([]string{"logging", "log_level"}, "INFO")
assert.NoError(t, err)
assert.Equal(t, defaultConfig.Logging.LogLevel, "INFO")
}
// TestChiaConfig_SetFieldByPath_FullObjects Tests that we can pass in and correctly parse a whole section of config
// as json or yaml and that it gets set properly
func TestChiaConfig_SetFieldByPath_FullObjects(t *testing.T) {
defaultConfig, err := config.LoadDefaultConfig()
assert.NoError(t, err)
// Make assertions about the default state, to ensure the assumed initial values are correct
assert.Equal(t, uint16(8444), defaultConfig.FullNode.Port)
assert.Equal(t, uint16(8555), defaultConfig.FullNode.RPCPort)
assert.NotNil(t, defaultConfig.NetworkOverrides.Constants["mainnet"])
assert.Equal(t, defaultConfig.NetworkOverrides.Constants["mainnet"].DifficultyConstantFactor, types.Uint128{})
assert.Equal(t, *defaultConfig.SelectedNetwork, "mainnet")
assert.Equal(t, defaultConfig.Logging.LogLevel, "WARNING")
// Test passing in json blobs
err = defaultConfig.SetFieldByPath([]string{"network_overrides", "constants"}, `{"jsonnet":{"DIFFICULTY_CONSTANT_FACTOR":44445555,"GENESIS_CHALLENGE":e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806}}`)
assert.NoError(t, err)
assert.NotNil(t, defaultConfig.NetworkOverrides.Constants["jsonnet"])
assert.Equal(t, types.Uint128From64(44445555), defaultConfig.NetworkOverrides.Constants["jsonnet"].DifficultyConstantFactor)
assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.NetworkOverrides.Constants["jsonnet"].GenesisChallenge)
// Ensure this applied to the other areas of config as well
assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.Seeder.NetworkOverrides.Constants["jsonnet"].GenesisChallenge)
assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.Harvester.NetworkOverrides.Constants["jsonnet"].GenesisChallenge)
assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.Pool.NetworkOverrides.Constants["jsonnet"].GenesisChallenge)
assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.Farmer.NetworkOverrides.Constants["jsonnet"].GenesisChallenge)
assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.Timelord.NetworkOverrides.Constants["jsonnet"].GenesisChallenge)
assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.FullNode.NetworkOverrides.Constants["jsonnet"].GenesisChallenge)
assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.UI.NetworkOverrides.Constants["jsonnet"].GenesisChallenge)
assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.Introducer.NetworkOverrides.Constants["jsonnet"].GenesisChallenge)
assert.Equal(t, "e739da31bcc4ab1767d9f1ca99eb3cec765fb3b3508f82e090374d5913d24806", defaultConfig.Wallet.NetworkOverrides.Constants["jsonnet"].GenesisChallenge)
// Test passing in yaml blobs
err = defaultConfig.SetFieldByPath([]string{"network_overrides", "constants"}, `yamlnet:
DIFFICULTY_CONSTANT_FACTOR: 44445555
GENESIS_CHALLENGE: 9eb3cec765fb3b3508f82e090374d5913d24806e739da31bcc4ab1767d9f1ca9`)
assert.NoError(t, err)
assert.NotNil(t, defaultConfig.NetworkOverrides.Constants["yamlnet"])
assert.Equal(t, types.Uint128From64(44445555), defaultConfig.NetworkOverrides.Constants["yamlnet"].DifficultyConstantFactor)
assert.Equal(t, "9eb3cec765fb3b3508f82e090374d5913d24806e739da31bcc4ab1767d9f1ca9", defaultConfig.NetworkOverrides.Constants["yamlnet"].GenesisChallenge)
}
// TestChiaConfig_SetFieldByPath_FullObjects Tests that we can pass in and correctly parse a whole section of config
// as json or yaml and that it gets set properly
func TestChiaConfig_SetFieldByPath_Lists(t *testing.T) {
defaultConfig, err := config.LoadDefaultConfig()
assert.NoError(t, err)
// Make assertions about the default state, to ensure the assumed initial values are correct
assert.Equal(t, []string{}, defaultConfig.Seeder.StaticPeers)
assert.Equal(t, []config.Peer{}, defaultConfig.FullNode.FullNodePeers)
// Test json encoded version
err = defaultConfig.SetFieldByPath([]string{"seeder", "static_peers"}, `["node-test.chia.net","node-test-2.chia.net"]`)
assert.NoError(t, err)
assert.Equal(t, []string{"node-test.chia.net", "node-test-2.chia.net"}, defaultConfig.Seeder.StaticPeers)
// Test with the actual type as the data to set
// First reset
defaultConfig.Seeder.StaticPeers = []string{}
assert.Equal(t, []string{}, defaultConfig.Seeder.StaticPeers)
err = defaultConfig.SetFieldByPath([]string{"seeder", "static_peers"}, []string{"node-test.chia.net", "node-test-2.chia.net"})
assert.NoError(t, err)
assert.Equal(t, []string{"node-test.chia.net", "node-test-2.chia.net"}, defaultConfig.Seeder.StaticPeers)
err = defaultConfig.SetFieldByPath([]string{"full_node", "full_node_peers"}, `[{"host":"testnode.example.com","port":1234},{"host":"testnode2.example.com","port":5678}]`)
assert.NoError(t, err)
assert.Equal(t, []config.Peer{
{Host: "testnode.example.com", Port: 1234},
{Host: "testnode2.example.com", Port: 5678},
}, defaultConfig.FullNode.FullNodePeers)
defaultConfig.FullNode.FullNodePeers = []config.Peer{}
assert.Equal(t, []config.Peer{}, defaultConfig.FullNode.FullNodePeers)
err = defaultConfig.SetFieldByPath([]string{"full_node", "full_node_peers"}, []config.Peer{
{Host: "testnode.example.com", Port: 1234},
{Host: "testnode2.example.com", Port: 5678},
})
assert.NoError(t, err)
assert.Equal(t, []config.Peer{
{Host: "testnode.example.com", Port: 1234},
{Host: "testnode2.example.com", Port: 5678},
}, defaultConfig.FullNode.FullNodePeers)
}
func TestChiaConfig_FillValuesFromEnvironment(t *testing.T) {
defaultConfig, err := config.LoadDefaultConfig()
assert.NoError(t, err)
// Make assertions about the default state, to ensure the assumed initial values are correct
assert.Equal(t, uint16(8444), defaultConfig.FullNode.Port)
assert.Equal(t, uint16(8555), defaultConfig.FullNode.RPCPort)
assert.NotNil(t, defaultConfig.NetworkOverrides.Constants["mainnet"])
assert.Equal(t, defaultConfig.NetworkOverrides.Constants["mainnet"].DifficultyConstantFactor, types.Uint128{})
assert.Equal(t, *defaultConfig.SelectedNetwork, "mainnet")
assert.Equal(t, defaultConfig.Logging.LogLevel, "WARNING")
assert.NoError(t, os.Setenv("chia.full_node.port", "1234"))
assert.NoError(t, os.Setenv("chia__full_node__rpc_port", "5678"))
assert.NoError(t, os.Setenv("chia.network_overrides.constants.mainnet.DIFFICULTY_CONSTANT_FACTOR", "44445555"))
assert.NoError(t, os.Setenv("chia.selected_network", "unittestnet"))
assert.NoError(t, os.Setenv("chia__logging__log_level", "INFO"))
assert.NoError(t, defaultConfig.FillValuesFromEnvironment())
assert.Equal(t, uint16(1234), defaultConfig.FullNode.Port)
assert.Equal(t, uint16(5678), defaultConfig.FullNode.RPCPort)
assert.Equal(t, types.Uint128From64(44445555), defaultConfig.NetworkOverrides.Constants["mainnet"].DifficultyConstantFactor)
assert.Equal(t, *defaultConfig.SelectedNetwork, "unittestnet")
assert.Equal(t, defaultConfig.Logging.LogLevel, "INFO")
}
func TestChiaConfig_ParsePathsAndValuesFromStrings(t *testing.T) {
// A mix of paths with and without prefixes with both separators
strings := []string{
"chia.full_node.port=8444",
"chia__full_node__db_sync=auto",
"full_node.db_readers=4",
"full_node__database_path=testing.db",
}
// Test that both strings with prefixes are matched with requirePrefix
result := config.ParsePathsAndValuesFromStrings(strings, true)
assert.Len(t, result, 2)
assert.Contains(t, result, "full_node.port")
assert.Equal(t, []string{"full_node", "port"}, result["full_node.port"].Path)
assert.Equal(t, "8444", result["full_node.port"].Value)
assert.Contains(t, result, "full_node__db_sync")
assert.Equal(t, []string{"full_node", "db_sync"}, result["full_node__db_sync"].Path)
assert.Equal(t, "auto", result["full_node__db_sync"].Value)
assert.NotContains(t, result, "full_node.db_readers")
assert.NotContains(t, result, "full_node__database_path")
// Test that both strings with prefixes are matched with requirePrefix
result = config.ParsePathsAndValuesFromStrings(strings, false)
assert.Len(t, result, 4) // 4 because it won't strip chia prefix if its found
assert.Contains(t, result, "chia.full_node.port")
assert.Equal(t, []string{"chia", "full_node", "port"}, result["chia.full_node.port"].Path)
assert.Equal(t, "8444", result["chia.full_node.port"].Value)
assert.Contains(t, result, "chia__full_node__db_sync")
assert.Equal(t, []string{"chia", "full_node", "db_sync"}, result["chia__full_node__db_sync"].Path)
assert.Equal(t, "auto", result["chia__full_node__db_sync"].Value)
assert.Contains(t, result, "full_node.db_readers")
assert.Equal(t, []string{"full_node", "db_readers"}, result["full_node.db_readers"].Path)
assert.Equal(t, "4", result["full_node.db_readers"].Value)
assert.Contains(t, result, "full_node__database_path")
assert.Equal(t, []string{"full_node", "database_path"}, result["full_node__database_path"].Path)
assert.Equal(t, "testing.db", result["full_node__database_path"].Value)
}
func TestChiaConfig_ParsePathsFromStrings(t *testing.T) {
// A mix of paths with and without prefixes with both separators
strings := []string{
"chia.full_node.port",
"chia__full_node__db_sync",
"full_node.db_readers",
"full_node__database_path",
}
// Test that both strings with prefixes are matched with requirePrefix
result := config.ParsePathsFromStrings(strings, true)
assert.Len(t, result, 2)
assert.Contains(t, result, "full_node.port")
assert.Equal(t, []string{"full_node", "port"}, result["full_node.port"])
assert.Contains(t, result, "full_node__db_sync")
assert.Equal(t, []string{"full_node", "db_sync"}, result["full_node__db_sync"])
assert.NotContains(t, result, "full_node.db_readers")
assert.NotContains(t, result, "full_node__database_path")
// Test that both strings with prefixes are matched with requirePrefix
result = config.ParsePathsFromStrings(strings, false)
assert.Len(t, result, 4) // 4 because it won't strip chia prefix if its found
assert.Contains(t, result, "chia.full_node.port")
assert.Equal(t, []string{"chia", "full_node", "port"}, result["chia.full_node.port"])
assert.Contains(t, result, "chia__full_node__db_sync")
assert.Equal(t, []string{"chia", "full_node", "db_sync"}, result["chia__full_node__db_sync"])
assert.Contains(t, result, "full_node.db_readers")
assert.Equal(t, []string{"full_node", "db_readers"}, result["full_node.db_readers"])
assert.Contains(t, result, "full_node__database_path")
assert.Equal(t, []string{"full_node", "database_path"}, result["full_node__database_path"])
}