|
15 | 15 | package configv2 |
16 | 16 |
|
17 | 17 | import ( |
18 | | - "os" |
19 | 18 | "path" |
20 | 19 | "testing" |
21 | 20 |
|
22 | | - "github.com/spf13/viper" |
23 | 21 | "github.com/stretchr/testify/assert" |
24 | 22 |
|
25 | 23 | "github.com/census-instrumentation/opencensus-service/internal/configmodels" |
26 | | - "github.com/census-instrumentation/opencensus-service/internal/factories" |
27 | 24 | ) |
28 | 25 |
|
29 | | -// ExampleReceiver: for testing purposes we are defining an example config and factory |
30 | | -// for "examplereceiver" receiver type. |
31 | | -type ExampleReceiver struct { |
32 | | - configmodels.ReceiverSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct |
33 | | - ExtraSetting string `mapstructure:"extra"` |
34 | | -} |
35 | | - |
36 | | -type ExampleReceiverFactory struct { |
37 | | -} |
38 | | - |
39 | | -// Type gets the type of the Receiver config created by this factory. |
40 | | -func (f *ExampleReceiverFactory) Type() string { |
41 | | - return "examplereceiver" |
42 | | -} |
43 | | - |
44 | | -// CreateDefaultConfig creates the default configuration for the Receiver. |
45 | | -func (f *ExampleReceiverFactory) CreateDefaultConfig() configmodels.Receiver { |
46 | | - return &ExampleReceiver{ |
47 | | - ReceiverSettings: configmodels.ReceiverSettings{ |
48 | | - Endpoint: "localhost:1000", |
49 | | - Enabled: false, |
50 | | - }, |
51 | | - ExtraSetting: "some string", |
52 | | - } |
53 | | -} |
54 | | - |
55 | | -// MultiProtoReceiver: for testing purposes we are defining an example multi protocol |
56 | | -// config and factory for "multireceiver" receiver type. |
57 | | -type MultiProtoReceiver struct { |
58 | | - Protocols map[string]MultiProtoReceiverOneCfg `mapstructure:"protocols"` |
59 | | -} |
60 | | - |
61 | | -var _ configmodels.Receiver = (*MultiProtoReceiver)(nil) |
62 | | - |
63 | | -type MultiProtoReceiverOneCfg struct { |
64 | | - Enabled bool `mapstructure:"enabled"` |
65 | | - Endpoint string `mapstructure:"endpoint"` |
66 | | - ExtraSetting string `mapstructure:"extra"` |
67 | | -} |
68 | | - |
69 | | -type MultiProtoReceiverFactory struct { |
70 | | -} |
71 | | - |
72 | | -// Type gets the type of the Receiver config created by this factory. |
73 | | -func (f *MultiProtoReceiverFactory) Type() string { |
74 | | - return "multireceiver" |
75 | | -} |
76 | | - |
77 | | -// CreateDefaultConfig creates the default configuration for the Receiver. |
78 | | -func (f *MultiProtoReceiverFactory) CreateDefaultConfig() configmodels.Receiver { |
79 | | - return &MultiProtoReceiver{ |
80 | | - Protocols: map[string]MultiProtoReceiverOneCfg{ |
81 | | - "http": { |
82 | | - Enabled: false, |
83 | | - Endpoint: "example.com:8888", |
84 | | - ExtraSetting: "extra string 1", |
85 | | - }, |
86 | | - "tcp": { |
87 | | - Enabled: false, |
88 | | - Endpoint: "omnition.com:9999", |
89 | | - ExtraSetting: "extra string 2", |
90 | | - }, |
91 | | - }, |
92 | | - } |
93 | | -} |
94 | | - |
95 | | -// ExampleExporter: for testing purposes we are defining an example config and factory |
96 | | -// for "exampleexporter" exporter type. |
97 | | -type ExampleExporter struct { |
98 | | - configmodels.ExporterSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct |
99 | | - ExtraSetting string `mapstructure:"extra"` |
100 | | -} |
101 | | - |
102 | | -type ExampleExporterFactory struct { |
103 | | -} |
104 | | - |
105 | | -// Type gets the type of the Exporter config created by this factory. |
106 | | -func (f *ExampleExporterFactory) Type() string { |
107 | | - return "exampleexporter" |
108 | | -} |
109 | | - |
110 | | -// CreateDefaultConfig creates the default configuration for the Exporter. |
111 | | -func (f *ExampleExporterFactory) CreateDefaultConfig() configmodels.Exporter { |
112 | | - return &ExampleExporter{ |
113 | | - ExporterSettings: configmodels.ExporterSettings{ |
114 | | - Enabled: false, |
115 | | - }, |
116 | | - ExtraSetting: "some export string", |
117 | | - } |
118 | | -} |
119 | | - |
120 | | -// ExampleProcessor: for testing purposes we are defining an example config and factory |
121 | | -// for "exampleprocessor" processor type. |
122 | | -type ExampleProcessor struct { |
123 | | - configmodels.ProcessorSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct |
124 | | - ExtraSetting string `mapstructure:"extra"` |
125 | | -} |
126 | | - |
127 | | -type ExampleProcessorFactory struct { |
128 | | -} |
129 | | - |
130 | | -// Type gets the type of the Processor config created by this factory. |
131 | | -func (f *ExampleProcessorFactory) Type() string { |
132 | | - return "exampleprocessor" |
133 | | -} |
134 | | - |
135 | | -// CreateDefaultConfig creates the default configuration for the Processor. |
136 | | -func (f *ExampleProcessorFactory) CreateDefaultConfig() configmodels.Processor { |
137 | | - return &ExampleProcessor{ |
138 | | - ProcessorSettings: configmodels.ProcessorSettings{ |
139 | | - Enabled: false, |
140 | | - }, |
141 | | - ExtraSetting: "some export string", |
142 | | - } |
143 | | -} |
144 | | - |
145 | | -// Register all factories |
146 | | -var _ = factories.RegisterReceiverFactory(&ExampleReceiverFactory{}) |
147 | | -var _ = factories.RegisterReceiverFactory(&MultiProtoReceiverFactory{}) |
148 | | -var _ = factories.RegisterExporterFactory(&ExampleExporterFactory{}) |
149 | | -var _ = factories.RegisterProcessorFactory(&ExampleProcessorFactory{}) |
150 | | - |
151 | | -func loadConfigFile(t *testing.T, fileName string) (*configmodels.ConfigV2, error) { |
152 | | - // Open the file for reading. |
153 | | - file, err := os.Open(fileName) |
154 | | - if err != nil { |
155 | | - t.Error(err) |
156 | | - return nil, err |
157 | | - } |
158 | | - |
159 | | - // Read yaml config from file |
160 | | - v := viper.New() |
161 | | - v.SetConfigType("yaml") |
162 | | - err = v.ReadConfig(file) |
163 | | - if err != nil { |
164 | | - t.Errorf("unable to read yaml, %v", err) |
165 | | - return nil, err |
166 | | - } |
167 | | - |
168 | | - // Load the config from viper |
169 | | - return Load(v) |
170 | | -} |
| 26 | +var _ = RegisterTestFactories() |
171 | 27 |
|
172 | 28 | func TestDecodeConfig(t *testing.T) { |
173 | 29 |
|
174 | 30 | // Load the config |
175 | | - config, err := loadConfigFile(t, path.Join(".", "testdata", "valid-config.yaml")) |
| 31 | + config, err := LoadConfigFile(t, path.Join(".", "testdata", "valid-config.yaml")) |
176 | 32 | if err != nil { |
177 | 33 | t.Fatalf("unable to load config, %v", err) |
178 | 34 | } |
@@ -244,7 +100,7 @@ func TestDecodeConfig(t *testing.T) { |
244 | 100 | func TestDecodeConfig_MultiProto(t *testing.T) { |
245 | 101 |
|
246 | 102 | // Load the config |
247 | | - config, err := loadConfigFile(t, path.Join(".", "testdata", "multiproto-config.yaml")) |
| 103 | + config, err := LoadConfigFile(t, path.Join(".", "testdata", "multiproto-config.yaml")) |
248 | 104 | if err != nil { |
249 | 105 | t.Fatalf("unable to load config, %v", err) |
250 | 106 | } |
@@ -325,7 +181,7 @@ func TestDecodeConfig_Invalid(t *testing.T) { |
325 | 181 | } |
326 | 182 |
|
327 | 183 | for _, test := range testCases { |
328 | | - _, err := loadConfigFile(t, path.Join(".", "testdata", test.name+".yaml")) |
| 184 | + _, err := LoadConfigFile(t, path.Join(".", "testdata", test.name+".yaml")) |
329 | 185 | if err == nil { |
330 | 186 | t.Errorf("expected error but succedded on invalid config case: %s", test.name) |
331 | 187 | } else if test.expected != 0 { |
|
0 commit comments