This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
/
config_steps.go
244 lines (214 loc) · 5.96 KB
/
config_steps.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
233
234
235
236
237
238
239
240
241
242
243
244
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package bddtests
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/cucumber/godog"
"github.com/pkg/errors"
"github.com/trustbloc/fabric-peer-test-common/bddtests"
)
type configSteps struct {
BDDContext *bddtests.BDDContext
}
// NewConfigSteps returns ledger config steps
func NewConfigSteps(context *bddtests.BDDContext) *configSteps {
return &configSteps{BDDContext: context}
}
// loadConfigFromFile loads configuration data from a file and sets the value to the given variable
func (d *configSteps) loadConfigFromFile(varName, filePath string) error {
logger.Infof("Loading config from file [%s] to variable [%s]", filePath, varName)
cfgBytes, err := readFile(filePath)
if err != nil {
return err
}
cfg := &Config{}
if err := json.Unmarshal(cfgBytes, cfg); err != nil {
return err
}
// Replace all of the file references with actual config
newCfg, err := newConfigPreProcessor(filePath).preProcess(cfg)
if err != nil {
return err
}
cfgBytes, err = json.Marshal(newCfg)
if err != nil {
return err
}
bddtests.SetVar(varName, string(cfgBytes))
return nil
}
// RegisterSteps registers config steps
func (d *configSteps) RegisterSteps(s *godog.Suite) {
s.Step(`^variable "([^"]*)" is assigned config from file "([^"]*)"$`, d.loadConfigFromFile)
}
type configPreprocessor struct {
basePath string
}
func newConfigPreProcessor(basePath string) *configPreprocessor {
return &configPreprocessor{basePath: basePath}
}
func (cp *configPreprocessor) preProcess(cfg *Config) (*Config, error) {
peers, err := cp.visitPeers(cfg.Peers)
if err != nil {
return nil, err
}
apps, err := cp.visitApps(cfg.Apps)
if err != nil {
return nil, err
}
return &Config{
MspID: cfg.MspID,
Peers: peers,
Apps: apps,
}, nil
}
func (cp *configPreprocessor) visitPeers(srcPeers []*Peer) ([]*Peer, error) {
peers := make([]*Peer, len(srcPeers))
for i, p := range srcPeers {
apps, err := cp.visitApps(p.Apps)
if err != nil {
return nil, err
}
peers[i] = &Peer{
PeerID: p.PeerID,
Apps: apps,
}
}
return peers, nil
}
func (cp *configPreprocessor) visitApps(srcApps []*App) ([]*App, error) {
apps := make([]*App, len(srcApps))
for i, a := range srcApps {
var config string
var components []*Component
var err error
if a.Config != "" {
config, err = cp.visitConfigString(a.Config)
if err != nil {
return nil, err
}
} else {
components, err = cp.visitComponents(a.Components)
if err != nil {
return nil, err
}
}
apps[i] = &App{
AppName: a.AppName,
Version: a.Version,
Format: a.Format,
Tags: a.Tags,
Config: config,
Components: components,
}
}
return apps, nil
}
func (cp *configPreprocessor) visitComponents(srcComponents []*Component) ([]*Component, error) {
components := make([]*Component, len(srcComponents))
for i, c := range srcComponents {
config, err := cp.visitConfigString(c.Config)
if err != nil {
return nil, err
}
components[i] = &Component{
Name: c.Name,
Version: c.Version,
Format: c.Format,
Tags: c.Tags,
Config: config,
}
}
return components, nil
}
func (cp *configPreprocessor) visitConfigString(srcConfig string) (string, error) {
// Substitute all of the file refs with the actual contents of the file
if !strings.HasPrefix(srcConfig, "file://") {
return srcConfig, nil
}
refFilePath := srcConfig[7:]
contents, err := cp.readFileRef(refFilePath)
if err != nil {
return "", errors.Wrapf(err, "error retrieving contents of file [%s]", refFilePath)
}
return string(contents), nil
}
func (cp *configPreprocessor) readFileRef(refPath string) ([]byte, error) {
var path string
if filepath.IsAbs(refPath) || cp.basePath == "" {
path = refPath
} else {
// The path is relative to the source config file
path = filepath.Join(filepath.Dir(cp.basePath), refPath)
}
return readFile(path)
}
func readFile(path string) ([]byte, error) {
file, err := os.Open(filepath.Clean(path))
if err != nil {
return nil, errors.WithMessagef(err, "error opening file [%s]", path)
}
defer func() {
if e := file.Close(); e != nil {
// This shouldn't happen
panic(err.Error())
}
}()
configBytes, err := ioutil.ReadAll(file)
if err != nil {
return nil, errors.WithMessagef(err, "error reading config file [%s]", path)
}
return configBytes, nil
}
// Format specifies the format of the configuration
type Format string
// Config contains zero or more application configurations and zero or more peer-specific application configurations
type Config struct {
// MspID is the ID of the MSP
MspID string
// Peers contains configuration for zero or more peers
Peers []*Peer `json:",omitempty"`
// Apps contains configuration for zero or more application
Apps []*App `json:",omitempty"`
}
// Peer contains a collection of application configurations for a given peer
type Peer struct {
// PeerID is the unique ID of the peer
PeerID string
// Apps contains configuration for one or more application
Apps []*App
}
// App contains the configuration for an application and/or multiple sub-components.
type App struct {
// Name is the name of the application
AppName string
// Version is the version of the config
Version string
// Format describes the format of the data
Format Format
// Config contains the actual configuration
Config string
// Tags contains optional tags that describe the data
Tags []string `json:",omitempty"`
// Components zero or more component configs
Components []*Component `json:",omitempty"`
}
// Component contains the configuration for an application component.
type Component struct {
// Name is the name of the component
Name string
// Version is the version of the config
Version string
// Format describes the format of the data
Format Format
// Config contains the actual configuration
Config string
// Tags contains optional tags that describe the data
Tags []string `json:",omitempty"`
}