Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix network timeouts in template tests #10272

Merged
merged 7 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion charger/openwb.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func NewOpenWB(log *util.Logger, mqttconf mqtt.Config, id int, topic string, p1p
// check if loadpoint configured
configured := boolG(fmt.Sprintf("%s/lp/%d/%s", topic, id, openwb.ConfiguredTopic))
if isConfigured, err := configured(); err != nil || !isConfigured {
return nil, fmt.Errorf("openWB loadpoint %d is not configured", id)
return nil, fmt.Errorf("loadpoint %d is not configured", id)
}

// adapt plugged/charging to status
Expand Down
2 changes: 2 additions & 0 deletions charger/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ var acceptable = []string{
"hciconfig provided no response",
"connect: no route to host",
"connect: connection refused",
"connector already registered: 1", // ocpp
"error connecting: Network Error",
"i/o timeout",
"loadpoint 1 is not configured", // openWB
"recv timeout",
"(Client.Timeout exceeded while awaiting headers)",
"can only have either uri or device", // modbus
Expand Down
74 changes: 39 additions & 35 deletions util/templates/render_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,51 +35,55 @@ func TestClass(t *testing.T, class Class, instantiate func(t *testing.T, values
for _, tmpl := range ByClass(class) {
tmpl := tmpl

t.Run(tmpl.Template, func(t *testing.T) {
// set default values for all params
values := tmpl.Defaults(TemplateRenderModeUnitTest)
// set default values for all params
values := tmpl.Defaults(TemplateRenderModeUnitTest)

// set the template value which is needed for rendering
values["template"] = tmpl.Template

// set modbus default test values
if values[ParamModbus] != nil {
modbusChoices := tmpl.ModbusChoices()
// we only test one modbus setup
if slices.Contains(modbusChoices, ModbusChoiceTCPIP) {
values[ModbusKeyTCPIP] = true
} else {
values[ModbusKeyRS485TCPIP] = true
}
tmpl.ModbusValues(TemplateRenderModeUnitTest, values)
// set modbus default test values
if values[ParamModbus] != nil {
modbusChoices := tmpl.ModbusChoices()
// we only test one modbus setup
if slices.Contains(modbusChoices, ModbusChoiceTCPIP) {
values[ModbusKeyTCPIP] = true
} else {
values[ModbusKeyRS485TCPIP] = true
}
tmpl.ModbusValues(TemplateRenderModeUnitTest, values)
}

// set the template value which is needed for rendering
values["template"] = tmpl.Template
// https://github.com/evcc-io/evcc/pull/10272 - override example IP (192.0.2.2)
values["host"] = "localhost"

usages := tmpl.Usages()
if len(usages) == 0 {
t.Run(tmpl.Template, func(t *testing.T) {
t.Parallel()

usages := tmpl.Usages()
if len(usages) == 0 {
test(t, tmpl, values, func(values map[string]interface{}) {
instantiate(t, values)
})
})

return
}
return
}

for _, u := range usages {
// create a copy of the map for parallel execution
usageValues := make(map[string]interface{}, len(values)+1)
if err := copier.Copy(&usageValues, values); err != nil {
panic(err)
}
usageValues[ParamUsage] = u
for _, u := range usages {
// create a copy of the map for parallel execution
usageValues := make(map[string]interface{}, len(values)+1)
if err := copier.Copy(&usageValues, values); err != nil {
panic(err)
}
usageValues[ParamUsage] = u

// subtest for each usage
t.Run(u, func(t *testing.T) {
t.Parallel()
// subtest for each usage
t.Run(u, func(t *testing.T) {
t.Parallel()

test(t, tmpl, usageValues, func(values map[string]interface{}) {
instantiate(t, values)
})
test(t, tmpl, usageValues, func(values map[string]interface{}) {
instantiate(t, values)
})
}
})
})
}
}
}