Commit 707ddc5b authored by Thomas Markwalder's avatar Thomas Markwalder

[#252] Removed optional RPS flag from NewStatsPuller()

NewStatsPuller() now always creates an RpsWorker instance.
parent af7e915a
......@@ -98,7 +98,7 @@ func (rpsWorker *RpsWorker) AgeOffRpsIntervals() error {
// Appends the statistic-get command for DHCP4 to the given commond list. It returns
// an instance of the expected response type.
func (rpsWorker *RpsWorker) AddCmd4(cmds *[]*agentcomm.KeaCommand, dhcp4Daemons *agentcomm.KeaDaemons) interface{} {
func RpsAddCmd4(cmds *[]*agentcomm.KeaCommand, dhcp4Daemons *agentcomm.KeaDaemons) interface{} {
dhcp4Arguments := RpsGetDhcp4Arguments()
*cmds = append(*cmds, &agentcomm.KeaCommand{
Command: "statistic-get",
......@@ -109,7 +109,7 @@ func (rpsWorker *RpsWorker) AddCmd4(cmds *[]*agentcomm.KeaCommand, dhcp4Daemons
// Appends the statistic-get command for DHCP4 to the given commond list. It returns
// an instance of the expected response type.
func (rpsWorker *RpsWorker) AddCmd6(cmds *[]*agentcomm.KeaCommand, dhcp6Daemons *agentcomm.KeaDaemons) interface{} {
func RpsAddCmd6(cmds *[]*agentcomm.KeaCommand, dhcp6Daemons *agentcomm.KeaDaemons) interface{} {
dhcp6Arguments := RpsGetDhcp6Arguments()
*cmds = append(*cmds, &agentcomm.KeaCommand{
Command: "statistic-get",
......
......@@ -308,7 +308,7 @@ func rpsTestInvokeResponse4Handler(rps *RpsWorker, daemon *dbmodel.Daemon, jsonR
responses := []interface{}{}
dhcp4Daemons, _ := agentcomm.NewKeaDaemons(dhcp4)
responses = append(responses, rps.AddCmd4(&cmds, dhcp4Daemons))
responses = append(responses, RpsAddCmd4(&cmds, dhcp4Daemons))
agentcomm.UnmarshalKeaResponseList(cmds[0], jsonResponse, responses[0])
err := rps.Response4Handler(daemon, responses[0])
......@@ -321,7 +321,7 @@ func rpsTestInvokeResponse6Handler(rps *RpsWorker, daemon *dbmodel.Daemon, jsonR
responses := []interface{}{}
dhcp6Daemons, _ := agentcomm.NewKeaDaemons(dhcp6)
responses = append(responses, rps.AddCmd6(&cmds, dhcp6Daemons))
responses = append(responses, RpsAddCmd6(&cmds, dhcp6Daemons))
agentcomm.UnmarshalKeaResponseList(cmds[0], jsonResponse, responses[0])
err := rps.Response6Handler(daemon, responses[0])
......
......@@ -19,7 +19,7 @@ type StatsPuller struct {
// Create a StatsPuller object that in background pulls Kea stats about leases.
// Beneath it spawns a goroutine that pulls stats periodically from Kea apps (that are stored in database).
func NewStatsPuller(db *pg.DB, agents agentcomm.ConnectedAgents, includeRpsWorker bool) (*StatsPuller, error) {
func NewStatsPuller(db *pg.DB, agents agentcomm.ConnectedAgents) (*StatsPuller, error) {
statsPuller := &StatsPuller{}
periodicPuller, err := agentcomm.NewPeriodicPuller(db, agents, "Kea Stats", "kea_stats_puller_interval",
statsPuller.pullStats)
......@@ -28,15 +28,12 @@ func NewStatsPuller(db *pg.DB, agents agentcomm.ConnectedAgents, includeRpsWorke
}
statsPuller.PeriodicPuller = periodicPuller
if includeRpsWorker {
// Create RpsWorker instance without its own PeriodicPuller
rpsWorker, err := NewRpsWorker(db)
if err != nil {
return nil, err
}
statsPuller.RpsWorker = rpsWorker
// Create RpsWorker instance
rpsWorker, err := NewRpsWorker(db)
if err != nil {
return nil, err
}
statsPuller.RpsWorker = rpsWorker
return statsPuller, nil
}
......@@ -333,7 +330,7 @@ func (statsPuller *StatsPuller) getStatsFromApp(dbApp *dbmodel.App) error {
// Add daemon, cmd and response for DHCP4 RPS stats if we have an RpsWorker
if statsPuller.RpsWorker != nil {
cmdDaemons = append(cmdDaemons, d)
responses = append(responses, statsPuller.RpsWorker.AddCmd4(&cmds, dhcp4Daemons))
responses = append(responses, RpsAddCmd4(&cmds, dhcp4Daemons))
}
case dhcp6:
......@@ -350,7 +347,7 @@ func (statsPuller *StatsPuller) getStatsFromApp(dbApp *dbmodel.App) error {
// Add daemon, cmd and response for DHCP6 RPS stats if we have an RpsWorker
if statsPuller.RpsWorker != nil {
cmdDaemons = append(cmdDaemons, d)
responses = append(responses, statsPuller.RpsWorker.AddCmd6(&cmds, dhcp6Daemons))
responses = append(responses, RpsAddCmd6(&cmds, dhcp6Daemons))
}
}
}
......
......@@ -29,150 +29,15 @@ func TestStatsPullerBasic(t *testing.T) {
// prepare fake agents
fa := storktest.NewFakeAgents(nil, nil)
sp, _ := NewStatsPuller(db, fa, true)
sp, _ := NewStatsPuller(db, fa)
require.NotEmpty(t, sp.RpsWorker)
sp.Shutdown()
}
// Check if pulling stats works.
func TestStatsPullerPullStats(t *testing.T) {
db, _, teardown := dbtest.SetupDatabaseTestCase(t)
defer teardown()
// prepare fake agents
keaMock := func(callNo int, cmdResponses []interface{}) {
// DHCPv4
daemons, _ := agentcomm.NewKeaDaemons("dhcp4")
command, _ := agentcomm.NewKeaCommand("stat-lease4-get", daemons, nil)
json := `[{
"result": 0,
"text": "Everything is fine",
"arguments": {
"result-set": {
"columns": [ "subnet-id", "total-addresses", "assigned-addresses", "declined-addresses" ],
"rows": [
[ 10, 256, 111, 0 ],
[ 20, 4098, 2034, 4 ]
],
"timestamp": "2018-05-04 15:03:37.000000"
}
}
}]`
agentcomm.UnmarshalKeaResponseList(command, json, cmdResponses[0])
// DHCPv6
daemons, _ = agentcomm.NewKeaDaemons("dhcp6")
command, _ = agentcomm.NewKeaCommand("stat-lease6-get", daemons, nil)
json = `[{
"result": 0,
"text": "Everything is fine",
"arguments": {
"result-set": {
"columns": [ "subnet-id", "total-nas", "assigned-nas", "declined-nas", "total-pds", "assigned-pds" ],
"rows": [
[ 30, 4096, 2400, 3, 0, 0],
[ 40, 0, 0, 0, 1048, 233 ],
[ 50, 256, 60, 0, 1048, 15 ]
],
"timestamp": "2018-05-04 15:03:37.000000"
}
}
}]`
agentcomm.UnmarshalKeaResponseList(command, json, cmdResponses[1])
}
fa := storktest.NewFakeAgents(keaMock, nil)
// prepare apps with subnets and local subnets
v4Config := `
{
"Dhcp4": {
"subnet4": [{"id": 10, "subnet": "192.0.2.0/24"},
{"id": 20, "subnet": "192.0.3.0/24"}]
}
}`
v6Config := `
{
"Dhcp6": {
"subnet6": [{"id": 30, "subnet": "2001:db8:1::/64"},
{"id": 40, "subnet": "2001:db8:2::/64"},
{"id": 50, "subnet": "2001:db8:3::/64"}]
}
}`
app := createAppWithSubnets(t, db, 0, v4Config, v6Config)
nets, snets, err := DetectNetworks(db, app)
require.NoError(t, err)
_, err = dbmodel.CommitNetworksIntoDB(db, nets, snets, app, 1)
require.NoError(t, err)
// set one setting that is needed by puller
setting := dbmodel.Setting{
Name: "kea_stats_puller_interval",
ValType: dbmodel.SettingValTypeInt,
Value: "60",
}
err = db.Insert(&setting)
require.NoError(t, err)
// prepare stats puller without RpsWorker
sp, err := NewStatsPuller(db, fa, false)
require.NoError(t, err)
// shutdown stats puller at the end
defer sp.Shutdown()
// invoke pulling stats
appsOkCnt, err := sp.pullStats()
require.NoError(t, err)
require.Equal(t, 1, appsOkCnt)
// check collected stats
subnets := []*dbmodel.LocalSubnet{}
q := db.Model(&subnets)
q = q.Column("local_subnet_id", "stats", "stats_collected_at")
q = q.Where("local_subnet.app_id = ?", app.ID)
err = q.Select()
require.NoError(t, err)
snCnt := 0
for _, sn := range subnets {
switch sn.LocalSubnetID {
case 10:
require.Equal(t, 111.0, sn.Stats["assigned-addresses"])
require.Equal(t, 0.0, sn.Stats["declined-addresses"])
require.Equal(t, 256.0, sn.Stats["total-addresses"])
snCnt++
case 20:
require.Equal(t, 2034.0, sn.Stats["assigned-addresses"])
require.Equal(t, 4.0, sn.Stats["declined-addresses"])
require.Equal(t, 4098.0, sn.Stats["total-addresses"])
snCnt++
case 30:
require.Equal(t, 2400.0, sn.Stats["assigned-nas"])
require.Equal(t, 0.0, sn.Stats["assigned-pds"])
require.Equal(t, 3.0, sn.Stats["declined-nas"])
require.Equal(t, 0.0, sn.Stats["total-pds"])
snCnt++
case 40:
require.Equal(t, 0.0, sn.Stats["assigned-nas"])
require.Equal(t, 233.0, sn.Stats["assigned-pds"])
require.Equal(t, 0.0, sn.Stats["declined-nas"])
require.Equal(t, 0.0, sn.Stats["total-nas"])
require.Equal(t, 1048.0, sn.Stats["total-pds"])
snCnt++
case 50:
require.Equal(t, 60.0, sn.Stats["assigned-nas"])
require.Equal(t, 15.0, sn.Stats["assigned-pds"])
require.Equal(t, 0.0, sn.Stats["declined-nas"])
require.Equal(t, 256.0, sn.Stats["total-nas"])
require.Equal(t, 1048.0, sn.Stats["total-pds"])
snCnt++
}
}
require.Equal(t, 5, snCnt)
}
// Check if Kea response to stat-leaseX-get command is handled correctly when it is
// empty or when stats hooks library is not loaded.
// empty or when stats hooks library is not loaded. The RPS responses are valid,
// they have their own unit tests in rps_test.go
func TestStatsPullerEmptyResponse(t *testing.T) {
db, _, teardown := dbtest.SetupDatabaseTestCase(t)
defer teardown()
......@@ -190,6 +55,16 @@ func TestStatsPullerEmptyResponse(t *testing.T) {
}]`
agentcomm.UnmarshalKeaResponseList(command, json, cmdResponses[0])
// DHCPv4 RSP response
json = `[{ "result": 0, "text": "Everything is fine",
"arguments": {
"pkt4-ack-sent": [ [ 0, "2019-07-30 10:13:00.000000" ] ]
}}]`
rpsCmd := []*agentcomm.KeaCommand{}
_ = RpsAddCmd4(&rpsCmd, daemons)
agentcomm.UnmarshalKeaResponseList(rpsCmd[0], json, cmdResponses[1])
// DHCPv6
daemons, _ = agentcomm.NewKeaDaemons("dhcp6")
command, _ = agentcomm.NewKeaCommand("stat-lease6-get", daemons, nil)
......@@ -198,7 +73,17 @@ func TestStatsPullerEmptyResponse(t *testing.T) {
"result": 2,
"text": "'stat-lease6-get' command not supported."
}]`
agentcomm.UnmarshalKeaResponseList(command, json, cmdResponses[1])
agentcomm.UnmarshalKeaResponseList(command, json, cmdResponses[2])
// DHCPv6 RSP response
json = `[{ "result": 0, "text": "Everything is fine",
"arguments": {
"pkt6-reply-sent": [ [ 0, "2019-07-30 10:13:00.000000" ] ]
}}]`
rpsCmd = []*agentcomm.KeaCommand{}
_ = RpsAddCmd6(&rpsCmd, daemons)
agentcomm.UnmarshalKeaResponseList(rpsCmd[0], json, cmdResponses[3])
}
fa := storktest.NewFakeAgents(keaMock, nil)
......@@ -222,14 +107,18 @@ func TestStatsPullerEmptyResponse(t *testing.T) {
AccessPoints: accessPoints,
Daemons: []*dbmodel.Daemon{
{
Active: true,
Name: "dhcp4",
KeaDaemon: &dbmodel.KeaDaemon{},
Active: true,
Name: "dhcp4",
KeaDaemon: &dbmodel.KeaDaemon{
KeaDHCPDaemon: &dbmodel.KeaDHCPDaemon{},
},
},
{
Active: true,
Name: "dhcp6",
KeaDaemon: &dbmodel.KeaDaemon{},
Active: true,
Name: "dhcp6",
KeaDaemon: &dbmodel.KeaDaemon{
KeaDHCPDaemon: &dbmodel.KeaDHCPDaemon{},
},
},
},
}
......@@ -246,8 +135,8 @@ func TestStatsPullerEmptyResponse(t *testing.T) {
err = db.Insert(&setting)
require.NoError(t, err)
// prepare stats puller without RpsWorker
sp, err := NewStatsPuller(db, fa, false)
// prepare stats puller
sp, err := NewStatsPuller(db, fa)
require.NoError(t, err)
// shutdown stats puller at the end
defer sp.Shutdown()
......@@ -262,9 +151,8 @@ func TestStatsPullerEmptyResponse(t *testing.T) {
// RpsWorker has a thorough set of unit tests so in this
// we verify only that it has entries in its internal
// Map of statistics fetched. This is enough to demonstrate
// that it is operational. We repeat the lease stats
// checks to make sure they have not been interfered with.
func TestStatsPullerPullStatsWithRps(t *testing.T) {
// that it is operational.
func TestStatsPullerPullStats(t *testing.T) {
db, _, teardown := dbtest.SetupDatabaseTestCase(t)
defer teardown()
......@@ -290,7 +178,8 @@ func TestStatsPullerPullStatsWithRps(t *testing.T) {
agentcomm.UnmarshalKeaResponseList(command, json, cmdResponses[0])
// Command and response for DHCP4 RPS statistic pull
command, _ = agentcomm.NewKeaCommand("statistic-get", daemons, nil)
rpsCmd := []*agentcomm.KeaCommand{}
_ = RpsAddCmd4(&rpsCmd, daemons)
json = `[{
"result": 0,
"text": "Everything is fine",
......@@ -298,7 +187,7 @@ func TestStatsPullerPullStatsWithRps(t *testing.T) {
"pkt4-ack-sent": [ [ 44, "2019-07-30 10:13:00.000000" ] ]
}
}]`
agentcomm.UnmarshalKeaResponseList(command, json, cmdResponses[1])
agentcomm.UnmarshalKeaResponseList(rpsCmd[0], json, cmdResponses[1])
// DHCPv6
daemons, _ = agentcomm.NewKeaDaemons("dhcp6")
......@@ -321,7 +210,8 @@ func TestStatsPullerPullStatsWithRps(t *testing.T) {
agentcomm.UnmarshalKeaResponseList(command, json, cmdResponses[2])
// Command and response for DHCP6 RPS statistic pull
command, _ = agentcomm.NewKeaCommand("statistic-get", daemons, nil)
rpsCmd = []*agentcomm.KeaCommand{}
_ = RpsAddCmd4(&rpsCmd, daemons)
json = `[{
"result": 0,
"text": "Everything is fine",
......@@ -329,8 +219,7 @@ func TestStatsPullerPullStatsWithRps(t *testing.T) {
"pkt6-reply-sent": [ [ 66, "2019-07-30 10:13:00.000000" ] ]
}
}]`
agentcomm.UnmarshalKeaResponseList(command, json, cmdResponses[3])
agentcomm.UnmarshalKeaResponseList(rpsCmd[0], json, cmdResponses[3])
}
fa := storktest.NewFakeAgents(keaMock, nil)
......@@ -365,8 +254,8 @@ func TestStatsPullerPullStatsWithRps(t *testing.T) {
err = db.Insert(&setting)
require.NoError(t, err)
// prepare stats puller with RpsWorker
sp, err := NewStatsPuller(db, fa, true)
// prepare stats puller
sp, err := NewStatsPuller(db, fa)
require.NoError(t, err)
// shutdown stats puller at the end
......
......@@ -127,7 +127,7 @@ func NewStorkServer() (ss *StorkServer, err error) {
}
// setup kea stats puller
ss.KeaStatsPuller, err = kea.NewStatsPuller(ss.Db, ss.Agents, true)
ss.KeaStatsPuller, err = kea.NewStatsPuller(ss.Db, ss.Agents)
if err != nil {
return nil, err
}
......
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