Skip to content

Commit

Permalink
issues: update test and implement forwarding through status
Browse files Browse the repository at this point in the history
  • Loading branch information
sechmann committed Jun 12, 2024
1 parent ee7486e commit b9d114e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
5 changes: 5 additions & 0 deletions internal/device-agent/states/connected/connected.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Connected struct {
statusUpdates chan<- *pb.AgentStatus

gateways []*pb.Gateway
issues []*pb.DeviceIssue
connectedSince *timestamppb.Timestamp
unhealthy bool

Expand Down Expand Up @@ -90,6 +91,7 @@ func (c *Connected) Enter(ctx context.Context) state.EventWithSpan {
_, err = c.deviceHelper.Teardown(ctx, &pb.TeardownRequest{})
cancel()
c.gateways = nil
c.issues = nil
c.unhealthy = false
}()

Expand Down Expand Up @@ -230,6 +232,7 @@ func (c *Connected) defaultSyncConfigLoop(ctx context.Context) error {

c.unhealthy = true
c.gateways = nil
c.issues = cfg.Issues

c.triggerStatusUpdate()
return nil
Expand All @@ -253,6 +256,7 @@ func (c *Connected) defaultSyncConfigLoop(ctx context.Context) error {
}

c.gateways = pb.MergeGatewayHealth(c.gateways, cfg.Gateways)
c.issues = cfg.Issues
c.triggerStatusUpdate()
healthCheckCancel = c.launchHealthCheck(ctx)
}
Expand Down Expand Up @@ -322,6 +326,7 @@ func (c Connected) Status() *pb.AgentStatus {
return &pb.AgentStatus{
ConnectedSince: c.connectedSince,
Gateways: c.gateways,
Issues: c.issues,
ConnectionState: state,
}
}
Expand Down
77 changes: 40 additions & 37 deletions internal/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ func TestIntegration(t *testing.T) {
now := time.Now()

type testCase struct {
name string
device *pb.Device
deviceFailures []kolide.DeviceFailure
endState pb.AgentState
expectedGateways map[string]*pb.Gateway
expectedIssues []*pb.DeviceIssue
name string
device *pb.Device
deviceFailures []kolide.DeviceFailure

Check failure on line 34 in internal/integration_test/integration_test.go

View workflow job for this annotation

GitHub Actions / Test

field deviceFailures is unused (U1000)

Check failure on line 34 in internal/integration_test/integration_test.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04)

field deviceFailures is unused (U1000)

Check failure on line 34 in internal/integration_test/integration_test.go

View workflow job for this annotation

GitHub Actions / test (macOS-latest)

field deviceFailures is unused (U1000)
endState pb.AgentState
expectedGateways map[string]*pb.Gateway
expectedKolideFailures []kolide.DeviceFailure
expectedIssues []*pb.DeviceIssue
}
tests := []testCase{
{
Expand All @@ -48,14 +49,24 @@ func TestIntegration(t *testing.T) {
},
endState: pb.AgentState_Unhealthy,
expectedGateways: nil,
expectedKolideFailures: []kolide.DeviceFailure{
{
Title: "Issue used in integration test",
Check: kolide.Check{
Tags: []string{pb.Severity_Critical.String()},
Description: "This is just a fake issue used in integration test",
},
Timestamp: ptrTo(now.Add(-(2 * time.Hour))),
LastUpdated: now,
},
},
expectedIssues: []*pb.DeviceIssue{
{
Title: "Issue used in integration test",
Message: "This is just a fake issue used in integration test",
Severity: pb.Severity_Critical,
DetectedAt: timestamppb.New(now.Add(-(2 * time.Hour))),
ResolveBefore: timestamppb.New(now.Add(-time.Second)),
LastUpdated: timestamppb.New(now),
Title: "Issue used in integration test",
Message: "This is just a fake issue used in integration test",
Severity: pb.Severity_Critical,
DetectedAt: timestamppb.New(now.Add(-(2 * time.Hour))),
LastUpdated: timestamppb.New(now),
},
},
},
Expand Down Expand Up @@ -92,34 +103,19 @@ func TestIntegration(t *testing.T) {
},
}
log := logger.WithField("component", "test")
tableTest(t, log, now, test.device, test.endState, test.expectedGateways, test.expectedIssues)
tableTest(t, log, now, test.device, test.endState, test.expectedGateways, test.expectedIssues, test.expectedKolideFailures)
}
}
t.Run(test.name, wrap(test))
}
}

func tableTest(t *testing.T, log *logrus.Entry, now time.Time, testDevice *pb.Device, endState pb.AgentState, expectedGateways map[string]*pb.Gateway, expectedIssues []*pb.DeviceIssue) {
func tableTest(t *testing.T, log *logrus.Entry, now time.Time, testDevice *pb.Device, endState pb.AgentState, expectedGateways map[string]*pb.Gateway, expectedIssues []*pb.DeviceIssue, expectedKolideFailures []kolide.DeviceFailure) {
wg := &sync.WaitGroup{}
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

deviceFailures := []kolide.DeviceFailure{}
for _, issue := range expectedIssues {
deviceFailures = append(deviceFailures, kolide.DeviceFailure{
Title: issue.Title,
Check: kolide.Check{
Tags: []string{pb.Severity_Critical.String()},
Description: issue.Message,
DisplayName: "fake check: field not used by anything",
},
LastUpdated: now,
Timestamp: &now,
},
)
}

devicePrivateKey := "devicePrivateKey"
kolideDevice := kolide.Device{
LastSeenAt: &now,
Expand All @@ -128,8 +124,8 @@ func tableTest(t *testing.T, log *logrus.Entry, now time.Time, testDevice *pb.De
AssignedOwner: kolide.DeviceOwner{
Email: testDevice.Username,
},
Failures: deviceFailures,
FailureCount: len(deviceFailures),
Failures: expectedKolideFailures,
FailureCount: len(expectedKolideFailures),
}
kolideClient := kolide.NewFakeClient().WithDevice(kolideDevice).Build()

Expand Down Expand Up @@ -370,7 +366,8 @@ func tableTest(t *testing.T, log *logrus.Entry, now time.Time, testDevice *pb.De
lastKnownState = status.ConnectionState
lastKnownGateways = status.Gateways
if status.ConnectionState == endState &&
matchExactGateways(expectedGateways)(append(status.Gateways, apiserverPeer)) {
matchExactGateways(expectedGateways)(append(status.Gateways, apiserverPeer)) &&
len(expectedIssues) == len(status.Issues) {
t.Logf("agent reached expected end state: %v", endState)

// Verify all gateways have the exact expected parameters
Expand All @@ -391,7 +388,7 @@ func tableTest(t *testing.T, log *logrus.Entry, now time.Time, testDevice *pb.De
wg.Wait() // TODO make sure cleanup works as expected
return
} else {
t.Logf("received non final status: %+v, with gateways: %+v", status.String(), status.Gateways)
t.Logf("received non final status: %+v, with gateways: %+v, issues: %+v", status.String(), status.Gateways, status.Issues)
}
}
}
Expand All @@ -400,27 +397,29 @@ func tableTest(t *testing.T, log *logrus.Entry, now time.Time, testDevice *pb.De
func assertEqualIssueLists(t *testing.T, expected, actual []*pb.DeviceIssue) {
t.Helper()
equalIssues := func(a, b *pb.DeviceIssue) bool {
t.Logf("comparing issues (%v,%v,%v,%v,%v)", a.Title == b.Title, a.Message == b.Message, a.Severity == b.Severity, a.DetectedAt.AsTime().Equal(b.DetectedAt.AsTime()), a.LastUpdated.AsTime().Equal(b.LastUpdated.AsTime()))
return a.Title == b.Title &&
a.Message == b.Message &&
a.Severity == b.Severity &&
a.DetectedAt.AsTime().Equal(b.DetectedAt.AsTime()) &&
a.LastUpdated.AsTime().Equal(b.LastUpdated.AsTime()) &&
a.ResolveBefore.AsTime().Equal(b.ResolveBefore.AsTime())
a.LastUpdated.AsTime().Equal(b.LastUpdated.AsTime())
}

for _, expectedIssue := range expected {
found := false
for _, actualIssue := range actual {
assert.False(t, actualIssue.ResolveBefore.AsTime().IsZero())
if equalIssues(expectedIssue, actualIssue) {
found = true
break
}
}

if !found {
t.Errorf("FAIL: expected issue (%+v) not found in actual issues: %+v", expectedIssue.Title, actual)
t.Errorf("FAIL: expected issue (%+v) not found in actual issues: %+v", expectedIssue, actual)
}
}

for _, actualIssue := range actual {
found := false
for _, expectedIssue := range expected {
Expand All @@ -431,7 +430,7 @@ func assertEqualIssueLists(t *testing.T, expected, actual []*pb.DeviceIssue) {
}

if !found {
t.Errorf("FAIL: unexpected issue (%+v) found in actual issues. Expected: %+v", actualIssue.Title, expected)
t.Errorf("FAIL: unexpected issue (%+v) found in actual issues. Expected: %+v", actualIssue, expected)
}
}
}
Expand Down Expand Up @@ -553,3 +552,7 @@ func (t *testLogWriter) Write(p []byte) (n int, err error) {
t.t.Logf("%s", p)
return len(p), nil
}

func ptrTo[A any](v A) *A {
return &v
}

0 comments on commit b9d114e

Please sign in to comment.