forked from ooni/probe-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_experiment.go
186 lines (176 loc) · 4.94 KB
/
generate_experiment.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
// +build ignore
package main
import (
"log"
"os"
"text/template"
)
var newMeasurementTemplate = template.Must(template.New("open_report").Parse(`
func TestExperimentNewMeasurement{{ .Name }}WorksAsIntended(t *testing.T) {
sess := &Session{location: &model.LocationInfo{
{{ .LocationInfoName }}: {{ .LocationInfoValue }},
}}
newExperiment := func() *Experiment {
builder, err := sess.NewExperimentBuilder("example")
if err != nil {
t.Fatal(err)
}
return builder.NewExperiment()
}
t.Run("with false setting", func(t *testing.T) {
sess.privacySettings.{{ .Setting }} = false
exp := newExperiment()
m := exp.newMeasurement("")
if m.{{ .NameForTesting }} != model.Default{{ .Name }} {
t.Fatal("not the value we expected")
}
})
t.Run("with true setting", func(t *testing.T) {
sess.privacySettings.{{ .Setting }} = true
exp := newExperiment()
m := exp.newMeasurement("")
if m.{{ .NameForTesting }} != {{ .ValueForTesting }} {
t.Fatal("not the value we expected")
}
})
}`))
var openReportTemplate = template.Must(template.New("new_measurement").Parse(`
func TestExperimentOpenReport{{ .Name }}WorksAsIntended(t *testing.T) {
sess := &Session{location: &model.LocationInfo{
{{ .LocationInfoName }}: {{ .LocationInfoValue }},
}}
newExperiment := func() *Experiment {
builder, err := sess.NewExperimentBuilder("example")
if err != nil {
t.Fatal(err)
}
return builder.NewExperiment()
}
t.Run("with false setting", func(t *testing.T) {
sess.privacySettings.{{ .Setting }} = false
exp := newExperiment()
rt := exp.newReportTemplate()
if rt.{{ .NameForTesting }} != model.Default{{ .Name }} {
t.Fatal("not the value we expected")
}
})
t.Run("with true setting", func(t *testing.T) {
sess.privacySettings.{{ .Setting }} = true
exp := newExperiment()
rt := exp.newReportTemplate()
if rt.{{ .NameForTesting }} != {{ .ValueForTesting }} {
t.Fatal("not the value we expected")
}
})
}`))
type Variable struct {
LocationInfoName string
LocationInfoValue string
Name string
NameForTesting string
NoOpenReport bool
Setting string
ValueForTesting string
}
var Variables = []Variable{{
LocationInfoName: "ProbeIP",
LocationInfoValue: `"8.8.8.8"`,
Name: "ProbeIP",
NameForTesting: "ProbeIP",
NoOpenReport: true,
Setting: "IncludeIP",
ValueForTesting: `"8.8.8.8"`,
}, {
LocationInfoName: "ASN",
LocationInfoValue: `30722`,
Name: "ProbeASNString",
NameForTesting: "ProbeASN",
Setting: "IncludeASN",
ValueForTesting: `"AS30722"`,
}, {
LocationInfoName: "CountryCode",
LocationInfoValue: `"IT"`,
Name: "ProbeCC",
NameForTesting: "ProbeCC",
Setting: "IncludeCountry",
ValueForTesting: `"IT"`,
}, {
LocationInfoName: "NetworkName",
LocationInfoValue: `"Vodafone Italia"`,
Name: "ProbeNetworkName",
NameForTesting: "ProbeNetworkName",
NoOpenReport: true,
Setting: "IncludeASN",
ValueForTesting: `"Vodafone Italia"`,
}, {
LocationInfoName: "ResolverIP",
LocationInfoValue: `"9.9.9.9"`,
Name: "ResolverIP",
NameForTesting: "ResolverIP",
NoOpenReport: true,
Setting: "IncludeIP",
ValueForTesting: `"9.9.9.9"`,
}, {
LocationInfoName: "ResolverASN",
LocationInfoValue: `44`,
Name: "ResolverASNString",
NameForTesting: "ResolverASN",
NoOpenReport: true,
Setting: "IncludeASN",
ValueForTesting: `"AS44"`,
}, {
LocationInfoName: "ResolverNetworkName",
LocationInfoValue: `"Google LLC"`,
Name: "ResolverNetworkName",
NameForTesting: "ResolverNetworkName",
NoOpenReport: true,
Setting: "IncludeASN",
ValueForTesting: `"Google LLC"`,
}}
func writestring(fp *os.File, s string) {
if _, err := fp.Write([]byte(s)); err != nil {
log.Fatal(err)
}
}
func withFile(filepath string, do func(fp *os.File)) {
fp, err := os.Create(filepath)
if err != nil {
log.Fatal(err)
}
do(fp)
if err := fp.Close(); err != nil {
log.Fatal(err)
}
}
func writeExperimentGeneratedTestGo() {
withFile("experiment_generated_test.go", func(fp *os.File) {
writestring(fp, "// Code generated by go generate; DO NOT EDIT.\n")
writestring(fp, "\n")
writestring(fp, "package engine\n")
writestring(fp, "\n")
writestring(fp, "import (\n")
writestring(fp, "\t\"testing\"\n")
writestring(fp, "\n")
writestring(fp, "\t\"github.com/ooni/probe-engine/model\"\n")
writestring(fp, ")\n")
writestring(fp, "\n")
writestring(fp, "//go:generate go run generate_experiment.go")
writestring(fp, "\n")
for _, variable := range Variables {
if err := newMeasurementTemplate.Execute(fp, variable); err != nil {
log.Fatal(err)
}
writestring(fp, "\n")
if variable.NoOpenReport {
continue
}
if err := openReportTemplate.Execute(fp, variable); err != nil {
log.Fatal(err)
}
writestring(fp, "\n")
}
})
}
func main() {
writeExperimentGeneratedTestGo()
}