forked from ChainSafe/ChainBridge
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig_test.go
150 lines (129 loc) · 2.88 KB
/
config_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
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: LGPL-3.0-only
package config
import (
"flag"
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
"github.com/urfave/cli/v2"
)
func createTempConfigFile() (*os.File, *Config) {
testConfig := NewConfig()
ethCfg := RawChainConfig{
Name: "chain",
Type: "ethereum",
Id: "1",
Endpoint: "endpoint",
From: "0x0",
Opts: map[string]string{"key": "value"},
}
testConfig.Chains = []RawChainConfig{ethCfg}
tmpFile, err := ioutil.TempFile(os.TempDir(), "*.json")
if err != nil {
fmt.Println("Cannot create temporary file", "err", err)
os.Exit(1)
}
f := testConfig.ToJSON(tmpFile.Name())
return f, testConfig
}
// Creates a cli context for a test given a set of flags and values
func createCliContext(description string, flags []string, values []interface{}) (*cli.Context, error) {
set := flag.NewFlagSet(description, 0)
for i := range values {
switch v := values[i].(type) {
case bool:
set.Bool(flags[i], v, "")
case string:
set.String(flags[i], v, "")
case uint:
set.Uint(flags[i], v, "")
default:
return nil, fmt.Errorf("unexpected cli value type: %T", values[i])
}
}
context := cli.NewContext(nil, set, nil)
return context, nil
}
func TestLoadJSONConfig(t *testing.T) {
file, cfg := createTempConfigFile()
ctx, err := createCliContext("", []string{"config"}, []interface{}{file.Name()})
if err != nil {
t.Fatal(err)
}
res, err := GetConfig(ctx)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(res, cfg) {
t.Errorf("did not match\ngot: %+v\nexpected: %+v", res.Chains[0], cfg.Chains[0])
}
}
func TestValdiateConfig(t *testing.T) {
valid := RawChainConfig{
Name: "chain",
Type: "ethereum",
Id: "1",
Endpoint: "endpoint",
From: "0x0",
Opts: nil,
}
missingType := RawChainConfig{
Name: "chain",
Type: "",
Id: "1",
Endpoint: "endpoint",
From: "0x0",
Opts: nil,
}
missingEndpoint := RawChainConfig{
Name: "chain",
Type: "ethereum",
Id: "1",
Endpoint: "",
From: "0x0",
Opts: nil,
}
missingName := RawChainConfig{
Name: "",
Type: "ethereum",
Id: "1",
Endpoint: "endpoint",
From: "0x0",
Opts: nil,
}
cfg := Config{
Chains: []RawChainConfig{valid},
KeystorePath: "",
}
err := cfg.validate()
if err != nil {
t.Fatal(err)
}
cfg = Config{
Chains: []RawChainConfig{missingType},
KeystorePath: "",
}
err = cfg.validate()
if err == nil {
t.Fatal("must require type field")
}
cfg = Config{
Chains: []RawChainConfig{missingEndpoint},
KeystorePath: "",
}
err = cfg.validate()
if err == nil {
t.Fatal("must require endpoint field")
}
cfg = Config{
Chains: []RawChainConfig{missingName},
KeystorePath: "",
}
err = cfg.validate()
if err == nil {
t.Fatal("must require name field")
}
}