mirror of
https://github.com/fleetdm/fleet
synced 2026-04-21 21:47:20 +00:00
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #40540 # Checklist for submitter - [ ] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. - Changes present in previous PR ## Testing - [x] Added/updated automated tests - [x] QA'd all new/changed functionality manually <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Switched the application logging to Go's standard slog with context-aware logging, improving structured logs and observability across services (status, audit, result, integrations). * Replaced legacy logging implementations and updated runtime wiring to propagate contextual loggers for more consistent, searchable log output. * **Tests** * Updated test suites to use the new slog discard/logger setup. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
84 lines
2.7 KiB
Go
84 lines
2.7 KiB
Go
package logging
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"testing"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
"github.com/aws/aws-sdk-go-v2/service/lambda"
|
|
"github.com/aws/aws-sdk-go-v2/service/lambda/types"
|
|
"github.com/fleetdm/fleet/v4/server/logging/mock"
|
|
"github.com/fleetdm/fleet/v4/server/test"
|
|
"github.com/stretchr/testify/assert"
|
|
tmock "github.com/stretchr/testify/mock"
|
|
)
|
|
|
|
func makeLambdaWriterWithMock(client LambdaAPI, functionName string) *lambdaLogWriter {
|
|
return &lambdaLogWriter{
|
|
client: client,
|
|
functionName: functionName,
|
|
logger: slog.New(slog.DiscardHandler),
|
|
}
|
|
}
|
|
|
|
func TestLambdaValidateFunctionError(t *testing.T) {
|
|
m := &mock.LambdaMock{}
|
|
ctx := context.Background()
|
|
m.On("Invoke", &lambda.InvokeInput{FunctionName: aws.String("foobar"), InvocationType: types.InvocationTypeDryRun}).
|
|
Return(nil, errors.New("failed"))
|
|
writer := makeLambdaWriterWithMock(m, "foobar")
|
|
err := writer.validateFunction(ctx)
|
|
assert.Error(t, err)
|
|
m.AssertExpectations(test.Quiet(t))
|
|
}
|
|
|
|
func TestLambdaValidateFunctionErrorFunction(t *testing.T) {
|
|
m := &mock.LambdaMock{}
|
|
ctx := context.Background()
|
|
m.On("Invoke", &lambda.InvokeInput{FunctionName: aws.String("foobar"), InvocationType: types.InvocationTypeDryRun}).
|
|
Return(&lambda.InvokeOutput{FunctionError: aws.String("failed")}, nil)
|
|
writer := makeLambdaWriterWithMock(m, "foobar")
|
|
err := writer.validateFunction(ctx)
|
|
assert.Error(t, err)
|
|
m.AssertExpectations(test.Quiet(t))
|
|
}
|
|
|
|
func TestLambdaValidateFunctionSuccess(t *testing.T) {
|
|
m := &mock.LambdaMock{}
|
|
ctx := context.Background()
|
|
m.On("Invoke", &lambda.InvokeInput{FunctionName: aws.String("foobar"), InvocationType: types.InvocationTypeDryRun}).
|
|
Return(&lambda.InvokeOutput{}, nil)
|
|
writer := makeLambdaWriterWithMock(m, "foobar")
|
|
err := writer.validateFunction(ctx)
|
|
assert.NoError(t, err)
|
|
m.AssertExpectations(test.Quiet(t))
|
|
}
|
|
|
|
func TestLambdaError(t *testing.T) {
|
|
m := &mock.LambdaMock{}
|
|
m.On("Invoke", tmock.MatchedBy(
|
|
func(in *lambda.InvokeInput) bool {
|
|
return *in.FunctionName == "foobar" && in.InvocationType == ""
|
|
},
|
|
)).Return(nil, errors.New("failed"))
|
|
writer := makeLambdaWriterWithMock(m, "foobar")
|
|
err := writer.Write(context.Background(), logs)
|
|
assert.Error(t, err)
|
|
m.AssertExpectations(test.Quiet(t))
|
|
}
|
|
|
|
func TestLambdaSuccess(t *testing.T) {
|
|
m := &mock.LambdaMock{}
|
|
m.On("Invoke", tmock.MatchedBy(
|
|
func(in *lambda.InvokeInput) bool {
|
|
return len(in.Payload) > 0 && *in.FunctionName == "foobar" && in.InvocationType == ""
|
|
},
|
|
)).Return(&lambda.InvokeOutput{}, nil).
|
|
Times(len(logs))
|
|
writer := makeLambdaWriterWithMock(m, "foobar")
|
|
err := writer.Write(context.Background(), logs)
|
|
assert.NoError(t, err)
|
|
m.AssertExpectations(test.Quiet(t))
|
|
}
|