forked from keploy/keploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
228 lines (206 loc) · 11.2 KB
/
config.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
// Package config provides configuration structures for the application.
package config
import (
"errors"
"fmt"
"strings"
"time"
)
type Config struct {
Path string `json:"path" yaml:"path" mapstructure:"path"`
AppID uint64 `json:"appId" yaml:"appId" mapstructure:"appId"`
AppName string `json:"appName" yaml:"appName" mapstructure:"appName"`
Command string `json:"command" yaml:"command" mapstructure:"command"`
Templatize Templatize `json:"templatize" yaml:"templatize" mapstructure:"templatize"`
Port uint32 `json:"port" yaml:"port" mapstructure:"port"`
DNSPort uint32 `json:"dnsPort" yaml:"dnsPort" mapstructure:"dnsPort"`
ProxyPort uint32 `json:"proxyPort" yaml:"proxyPort" mapstructure:"proxyPort"`
Debug bool `json:"debug" yaml:"debug" mapstructure:"debug"`
DisableTele bool `json:"disableTele" yaml:"disableTele" mapstructure:"disableTele"`
DisableANSI bool `json:"disableANSI" yaml:"disableANSI" mapstructure:"disableANSI"`
InDocker bool `json:"inDocker" yaml:"-" mapstructure:"inDocker"`
ContainerName string `json:"containerName" yaml:"containerName" mapstructure:"containerName"`
NetworkName string `json:"networkName" yaml:"networkName" mapstructure:"networkName"`
BuildDelay uint64 `json:"buildDelay" yaml:"buildDelay" mapstructure:"buildDelay"`
Test Test `json:"test" yaml:"test" mapstructure:"test"`
Record Record `json:"record" yaml:"record" mapstructure:"record"`
Gen UtGen `json:"gen" yaml:"-" mapstructure:"gen"`
Normalize Normalize `json:"normalize" yaml:"-" mapstructure:"normalize"`
ReRecord ReRecord `json:"rerecord" yaml:"-" mapstructure:"rerecord"`
ConfigPath string `json:"configPath" yaml:"configPath" mapstructure:"configPath"`
BypassRules []BypassRule `json:"bypassRules" yaml:"bypassRules" mapstructure:"bypassRules"`
EnableTesting bool `json:"enableTesting" yaml:"-" mapstructure:"enableTesting"`
GenerateGithubActions bool `json:"generateGithubActions" yaml:"generateGithubActions" mapstructure:"generateGithubActions"`
KeployContainer string `json:"keployContainer" yaml:"keployContainer" mapstructure:"keployContainer"`
KeployNetwork string `json:"keployNetwork" yaml:"keployNetwork" mapstructure:"keployNetwork"`
CommandType string `json:"cmdType" yaml:"cmdType" mapstructure:"cmdType"`
Contract Contract `json:"contract" yaml:"contract" mapstructure:"contract"`
InCi bool `json:"inCi" yaml:"inCi" mapstructure:"inCi"`
InstallationID string `json:"-" yaml:"-" mapstructure:"-"`
Version string `json:"-" yaml:"-" mapstructure:"-"`
APIServerURL string `json:"-" yaml:"-" mapstructure:"-"`
GitHubClientID string `json:"-" yaml:"-" mapstructure:"-"`
}
type UtGen struct {
SourceFilePath string `json:"sourceFilePath" yaml:"sourceFilePath" mapstructure:"sourceFilePath"`
TestFilePath string `json:"testFilePath" yaml:"testFilePath" mapstructure:"testFilePath"`
CoverageReportPath string `json:"coverageReportPath" yaml:"coverageReportPath" mapstructure:"coverageReportPath"`
TestCommand string `json:"testCommand" yaml:"testCommand" mapstructure:"testCommand"`
CoverageFormat string `json:"coverageFormat" yaml:"coverageFormat" mapstructure:"coverageFormat"`
DesiredCoverage float64 `json:"expectedCoverage" yaml:"expectedCoverage" mapstructure:"expectedCoverage"`
MaxIterations int `json:"maxIterations" yaml:"maxIterations" mapstructure:"maxIterations"`
TestDir string `json:"testDir" yaml:"testDir" mapstructure:"testDir"`
APIBaseURL string `json:"llmBaseUrl" yaml:"llmBaseUrl" mapstructure:"llmBaseUrl"`
Model string `json:"model" yaml:"model" mapstructure:"model"`
APIVersion string `json:"llmApiVersion" yaml:"llmApiVersion" mapstructure:"llmApiVersion"`
AdditionalPrompt string `json:"additionalPrompt" yaml:"additionalPrompt" mapstructure:"additionalPrompt"`
FunctionUnderTest string `json:"functionUnderTest" yaml:"-" mapstructure:"functionUnderTest"`
Flakiness bool `json:"flakiness" yaml:"flakiness" mapstructure:"flakiness"`
}
type Templatize struct {
TestSets []string `json:"testSets" yaml:"testSets" mapstructure:"testSets"`
}
type Record struct {
Filters []Filter `json:"filters" yaml:"filters" mapstructure:"filters"`
RecordTimer time.Duration `json:"recordTimer" yaml:"recordTimer" mapstructure:"recordTimer"`
}
type ReRecord struct {
SelectedTests []string `json:"selectedTests" yaml:"selectedTests" mapstructure:"selectedTests"`
Filters []Filter `json:"filters" yaml:"filters" mapstructure:"filters"`
Host string `json:"host" yaml:"host" mapstructure:"host"`
Port uint32 `json:"port" yaml:"port" mapstructure:"port"`
}
type Contract struct {
Services []string `json:"services" yaml:"services" mapstructure:"services"`
Tests []string `json:"tests" yaml:"tests" mapstructure:"tests"`
Path string `json:"path" yaml:"path" mapstructure:"path"`
Download bool `json:"download" yaml:"download" mapstructure:"download"`
Generate bool `json:"generate" yaml:"generate" mapstructure:"generate"`
Driven string `json:"driven" yaml:"driven" mapstructure:"driven"`
Mappings Mappings `json:"mappings" yaml:"mappings" mapstructure:"mappings"`
}
type Mappings struct {
ServicesMapping map[string][]string `json:"servicesMapping" yaml:"servicesMapping" mapstructure:"servicesMapping"`
Self string `json:"self" yaml:"self" mapstructure:"self"`
}
type Normalize struct {
SelectedTests []SelectedTests `json:"selectedTests" yaml:"selectedTests" mapstructure:"selectedTests"`
TestRun string `json:"testReport" yaml:"testReport" mapstructure:"testReport"`
}
type BypassRule struct {
Path string `json:"path" yaml:"path" mapstructure:"path"`
Host string `json:"host" yaml:"host" mapstructure:"host"`
Port uint `json:"port" yaml:"port" mapstructure:"port"`
}
type Filter struct {
BypassRule `mapstructure:",squash"`
URLMethods []string `json:"urlMethods" yaml:"urlMethods" mapstructure:"urlMethods"`
Headers map[string]string `json:"headers" yaml:"headers" mapstructure:"headers"`
}
type Test struct {
SelectedTests map[string][]string `json:"selectedTests" yaml:"selectedTests" mapstructure:"selectedTests"`
GlobalNoise Globalnoise `json:"globalNoise" yaml:"globalNoise" mapstructure:"globalNoise"`
Delay uint64 `json:"delay" yaml:"delay" mapstructure:"delay"`
Host string `json:"host" yaml:"host" mapstructure:"host"`
Port uint32 `json:"port" yaml:"port" mapstructure:"port"`
APITimeout uint64 `json:"apiTimeout" yaml:"apiTimeout" mapstructure:"apiTimeout"`
SkipCoverage bool `json:"skipCoverage" yaml:"skipCoverage" mapstructure:"skipCoverage"` // boolean to capture the coverage in test
CoverageReportPath string `json:"coverageReportPath" yaml:"coverageReportPath" mapstructure:"coverageReportPath"` // directory path to store the coverage files
IgnoreOrdering bool `json:"ignoreOrdering" yaml:"ignoreOrdering" mapstructure:"ignoreOrdering"`
MongoPassword string `json:"mongoPassword" yaml:"mongoPassword" mapstructure:"mongoPassword"`
Language Language `json:"language" yaml:"language" mapstructure:"language"`
RemoveUnusedMocks bool `json:"removeUnusedMocks" yaml:"removeUnusedMocks" mapstructure:"removeUnusedMocks"`
FallBackOnMiss bool `json:"fallBackOnMiss" yaml:"fallBackOnMiss" mapstructure:"fallBackOnMiss"`
JacocoAgentPath string `json:"jacocoAgentPath" yaml:"jacocoAgentPath" mapstructure:"jacocoAgentPath"`
BasePath string `json:"basePath" yaml:"basePath" mapstructure:"basePath"`
Mocking bool `json:"mocking" yaml:"mocking" mapstructure:"mocking"`
IgnoredTests map[string][]string `json:"ignoredTests" yaml:"ignoredTests" mapstructure:"ignoredTests"`
DisableLineCoverage bool `json:"disableLineCoverage" yaml:"disableLineCoverage" mapstructure:"disableLineCoverage"`
DisableMockUpload bool `json:"disableMockUpload" yaml:"disableMockUpload" mapstructure:"disableMockUpload"`
UseLocalMock bool `json:"useLocalMock" yaml:"useLocalMock" mapstructure:"useLocalMock"`
UpdateTemplate bool `json:"updateTemplate" yaml:"updateTemplate" mapstructure:"updateTemplate"`
}
type Language string
// String is used both by fmt.Print and by Cobra in help text
func (e *Language) String() string {
return string(*e)
}
// Set must have pointer receiver so it doesn't change the value of a copy
func (e *Language) Set(v string) error {
switch v {
case "go", "java", "python", "javascript":
*e = Language(v)
return nil
default:
return errors.New(`must be one of "go", "java", "python" or "javascript"`)
}
}
// Type is only used in help text
func (e *Language) Type() string {
return "myEnum"
}
type Globalnoise struct {
Global GlobalNoise `json:"global" yaml:"global" mapstructure:"global"`
Testsets TestsetNoise `json:"test-sets" yaml:"test-sets" mapstructure:"test-sets"`
}
type SelectedTests struct {
TestSet string `json:"testSet" yaml:"testSet" mapstructure:"testSet"`
Tests []string `json:"tests" yaml:"tests" mapstructure:"tests"`
}
type (
Noise map[string][]string
GlobalNoise map[string]map[string][]string
TestsetNoise map[string]map[string]map[string][]string
)
func SetByPassPorts(conf *Config, ports []uint) {
for _, port := range ports {
conf.BypassRules = append(conf.BypassRules, BypassRule{
Path: "",
Host: "",
Port: port,
})
}
}
func GetByPassPorts(conf *Config) []uint {
var ports []uint
for _, rule := range conf.BypassRules {
ports = append(ports, rule.Port)
}
return ports
}
func SetSelectedTests(conf *Config, testSets []string) {
conf.Test.SelectedTests = make(map[string][]string)
for _, testSet := range testSets {
conf.Test.SelectedTests[testSet] = []string{}
}
}
func SetSelectedServices(conf *Config, services []string) {
// string is "s1,s2" so i want to get s1,s2
conf.Contract.Services = services
}
func SetSelectedContractTests(conf *Config, tests []string) {
conf.Contract.Tests = tests
}
func SetSelectedTestsNormalize(conf *Config, value string) error {
testSets := strings.FieldsFunc(value, func(r rune) bool {
return r == ',' || r == ' '
})
var tests []SelectedTests
if len(testSets) == 0 {
conf.Normalize.SelectedTests = tests
return nil
}
for _, ts := range testSets {
parts := strings.Split(ts, ":")
if len(parts) != 2 {
return fmt.Errorf("invalid format: %s", ts)
}
testCases := strings.Split(parts[1], " ")
tests = append(tests, SelectedTests{
TestSet: parts[0],
Tests: testCases,
})
}
conf.Normalize.SelectedTests = tests
return nil
}