@@ -25,7 +25,6 @@ import (
2525 "github.com/elastic/fleet-server/v7/internal/pkg/dl"
2626 "github.com/elastic/fleet-server/v7/internal/pkg/es"
2727 "github.com/elastic/fleet-server/v7/internal/pkg/model"
28- mockmonitor "github.com/elastic/fleet-server/v7/internal/pkg/monitor/mock"
2928 "github.com/elastic/fleet-server/v7/internal/pkg/policy"
3029 "github.com/elastic/fleet-server/v7/internal/pkg/sqn"
3130 ftesting "github.com/elastic/fleet-server/v7/internal/pkg/testing"
@@ -39,6 +38,30 @@ import (
3938 "github.com/stretchr/testify/require"
4039)
4140
41+ type mockPolicyMonitor struct {
42+ mock.Mock
43+ }
44+
45+ func (m * mockPolicyMonitor ) Run (ctx context.Context ) error {
46+ args := m .Called (ctx )
47+ return args .Error (0 )
48+ }
49+
50+ func (m * mockPolicyMonitor ) Subscribe (agentID , policyID string , revIDX int64 ) (policy.Subscription , error ) {
51+ args := m .Called (agentID , policyID , revIDX )
52+ return args .Get (0 ).(policy.Subscription ), args .Error (1 )
53+ }
54+
55+ func (m * mockPolicyMonitor ) Unsubscribe (sub policy.Subscription ) error {
56+ args := m .Called (sub )
57+ return args .Error (0 )
58+ }
59+
60+ func (m * mockPolicyMonitor ) LatestRev (ctx context.Context , id string ) int64 {
61+ args := m .Called (ctx , id )
62+ return args .Get (0 ).(int64 )
63+ }
64+
4265func TestConvertActionData (t * testing.T ) {
4366 tests := []struct {
4467 name string
@@ -339,14 +362,13 @@ func TestResolveSeqNo(t *testing.T) {
339362 cfg := & config.Server {}
340363 c , _ := cache .New (config.Cache {NumCounters : 100 , MaxCost : 100000 })
341364 bc := checkin .NewBulk (nil )
342- bulker := ftesting .NewMockBulk ()
343- pim := mockmonitor .NewMockMonitor ()
344- pm := policy .NewMonitor (bulker , pim , config.ServerLimits {PolicyLimit : config.Limit {Interval : 5 * time .Millisecond , Burst : 1 }})
365+ pm := & mockPolicyMonitor {}
345366 ct , err := NewCheckinT (verCon , cfg , c , bc , pm , nil , nil , nil )
346367 assert .NoError (t , err )
347368
348369 resp , _ := ct .resolveSeqNo (ctx , logger , tc .req , tc .agent )
349370 assert .Equal (t , tc .resp , resp )
371+ pm .AssertExpectations (t )
350372 })
351373 }
352374
@@ -1123,18 +1145,22 @@ func TestProcessPolicyDetails(t *testing.T) {
11231145 policyID := "policy-id"
11241146 revIDX2 := int64 (2 )
11251147 tests := []struct {
1126- name string
1127- agent * model.Agent
1128- req * CheckinRequest
1129- revIDX int64
1130- returnsOps bool
1131- err error
1148+ name string
1149+ agent * model.Agent
1150+ req * CheckinRequest
1151+ getPolicyMonitor func () * mockPolicyMonitor
1152+ revIDX int64
1153+ returnsOps bool
1154+ err error
11321155 }{{
11331156 name : "request has no policy details" ,
11341157 agent : & model.Agent {
11351158 PolicyRevisionIdx : 1 ,
11361159 },
1137- req : & CheckinRequest {},
1160+ req : & CheckinRequest {},
1161+ getPolicyMonitor : func () * mockPolicyMonitor {
1162+ return & mockPolicyMonitor {}
1163+ },
11381164 revIDX : 1 ,
11391165 returnsOps : false ,
11401166 err : nil ,
@@ -1152,8 +1178,11 @@ func TestProcessPolicyDetails(t *testing.T) {
11521178 AgentPolicyId : & policyID ,
11531179 PolicyRevisionIdx : & revIDX2 ,
11541180 },
1181+ getPolicyMonitor : func () * mockPolicyMonitor {
1182+ return & mockPolicyMonitor {}
1183+ },
11551184 revIDX : 0 ,
1156- returnsOps : true ,
1185+ returnsOps : false ,
11571186 err : nil ,
11581187 }, {
11591188 name : "revision updated" ,
@@ -1169,9 +1198,36 @@ func TestProcessPolicyDetails(t *testing.T) {
11691198 AgentPolicyId : & policyID ,
11701199 PolicyRevisionIdx : & revIDX2 ,
11711200 },
1201+ getPolicyMonitor : func () * mockPolicyMonitor {
1202+ pm := & mockPolicyMonitor {}
1203+ pm .On ("LatestRev" , mock .Anything , policyID ).Return (int64 (2 )).Once ()
1204+ return pm
1205+ },
11721206 revIDX : 2 ,
11731207 returnsOps : true ,
11741208 err : nil ,
1209+ }, {
1210+ name : "checkin revision is greater than the policy's latest revision" ,
1211+ agent : & model.Agent {
1212+ Agent : & model.AgentMetadata {
1213+ ID : "agent-id" ,
1214+ },
1215+ PolicyID : policyID ,
1216+ AgentPolicyID : policyID ,
1217+ PolicyRevisionIdx : 1 ,
1218+ },
1219+ req : & CheckinRequest {
1220+ AgentPolicyId : & policyID ,
1221+ PolicyRevisionIdx : & revIDX2 ,
1222+ },
1223+ getPolicyMonitor : func () * mockPolicyMonitor {
1224+ pm := & mockPolicyMonitor {}
1225+ pm .On ("LatestRev" , mock .Anything , policyID ).Return (int64 (1 )).Once ()
1226+ return pm
1227+ },
1228+ revIDX : 0 ,
1229+ returnsOps : true ,
1230+ err : nil ,
11751231 }, {
11761232 name : "agent_policy_id has changed" ,
11771233 agent : & model.Agent {
@@ -1186,6 +1242,11 @@ func TestProcessPolicyDetails(t *testing.T) {
11861242 AgentPolicyId : & policyID ,
11871243 PolicyRevisionIdx : & revIDX2 ,
11881244 },
1245+ getPolicyMonitor : func () * mockPolicyMonitor {
1246+ pm := & mockPolicyMonitor {}
1247+ pm .On ("LatestRev" , mock .Anything , policyID ).Return (int64 (2 )).Once ()
1248+ return pm
1249+ },
11891250 revIDX : 2 ,
11901251 returnsOps : true ,
11911252 err : nil ,
@@ -1202,8 +1263,13 @@ func TestProcessPolicyDetails(t *testing.T) {
12021263 AgentPolicyId : & policyID ,
12031264 PolicyRevisionIdx : & revIDX2 ,
12041265 },
1266+ getPolicyMonitor : func () * mockPolicyMonitor {
1267+ pm := & mockPolicyMonitor {}
1268+ pm .On ("LatestRev" , mock .Anything , policyID ).Return (int64 (2 )).Once ()
1269+ return pm
1270+ },
12051271 revIDX : 2 ,
1206- returnsOps : false ,
1272+ returnsOps : true ,
12071273 err : nil ,
12081274 }, {
12091275 name : "details present with no changes from agent doc" ,
@@ -1219,6 +1285,11 @@ func TestProcessPolicyDetails(t *testing.T) {
12191285 AgentPolicyId : & policyID ,
12201286 PolicyRevisionIdx : & revIDX2 ,
12211287 },
1288+ getPolicyMonitor : func () * mockPolicyMonitor {
1289+ pm := & mockPolicyMonitor {}
1290+ pm .On ("LatestRev" , mock .Anything , policyID ).Return (int64 (2 )).Once ()
1291+ return pm
1292+ },
12221293 revIDX : 2 ,
12231294 returnsOps : false ,
12241295 err : nil ,
@@ -1227,8 +1298,10 @@ func TestProcessPolicyDetails(t *testing.T) {
12271298 for _ , tc := range tests {
12281299 t .Run (tc .name , func (t * testing.T ) {
12291300 logger := testlog .SetLogger (t )
1301+ pm := tc .getPolicyMonitor ()
12301302 checkin := & CheckinT {
12311303 bulker : ftesting .NewMockBulk (),
1304+ pm : pm ,
12321305 }
12331306
12341307 revIDX , opts , err := checkin .processPolicyDetails (t .Context (), logger , tc .agent , tc .req )
@@ -1243,6 +1316,7 @@ func TestProcessPolicyDetails(t *testing.T) {
12431316 } else {
12441317 assert .NoError (t , err )
12451318 }
1319+ pm .AssertExpectations (t )
12461320 })
12471321 }
12481322}
0 commit comments