Commit bc28af69 authored by Marcin Siodelski's avatar Marcin Siodelski

[#137] Improved services detection

Service is not detected when it is blank. Also, avoid null pointer
dereference when non HA service present.
parent 3a5feed6
......@@ -13,9 +13,9 @@ import (
// the peers' configurations the server names, URLs and roles must
// match.
func appBelongsToHAService(app *dbmodel.App, service *dbmodel.Service) bool {
// If the service or app is nil, or if the app is not Kea then the app
// If the service or app is nil, service is blank or if the app is not Kea then the app
// surely doesn't belong to the service.
if service.HAService == nil || app == nil || app.Type != "kea" {
if service.HAService == nil || len(service.Apps) == 0 || app == nil || app.Type != "kea" {
return false
}
......@@ -30,7 +30,7 @@ func appBelongsToHAService(app *dbmodel.App, service *dbmodel.Service) bool {
var index int = -1
for i, d := range appKea.Daemons {
if (d.Name == "dhcp4" || d.Name == "dhcp6") && (d.Name == service.HAService.HAType) {
if d.Name == service.HAService.HAType {
index = i
}
}
......@@ -174,7 +174,9 @@ func DetectHAServices(db *dbops.PgDB, dbApp *dbmodel.App) (services []dbmodel.Se
index = -1
for i, service := range dbServices {
s := service
if service.HAService.HAType == d.Name && appBelongsToHAService(dbApp, &s) {
if (service.HAService != nil) &&
(service.HAService.HAType == d.Name) &&
appBelongsToHAService(dbApp, &s) {
index = i
break
}
......
......@@ -363,3 +363,37 @@ func TestDetectHAServices(t *testing.T) {
require.Len(t, services[0].Apps, 3)
}
// Test that an app doesn't belong to a blank service , i.e. a
// service that comprises no apps.
func TestAppBelongsToHAServiceBlankService(t *testing.T) {
// Create blank service.
service := &dbmodel.Service{
BaseService: dbmodel.BaseService{
ServiceType: "ha_dhcp",
},
HAService: &dbmodel.BaseHAService{
HAType: "dhcp4",
},
}
// Create an app.
app := &dbmodel.App{
Type: dbmodel.KeaAppType,
CtrlAddress: "192.0.2.66",
CtrlPort: 8000,
Details: dbmodel.AppKea{
Daemons: []*dbmodel.KeaDaemon{
{
Name: "dhcp4",
Config: getTestConfig("Dhcp4", "server2", "load-balancing",
"server1", "server2", "server4"),
},
},
},
}
// The app doesn't belong to the service because the service includes
// no meaningful information to make such determination. In that case
// it is up to the administrator to explicitly add the app to the service.
require.False(t, appBelongsToHAService(app, service))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment