<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #38889 PLEASE READ BELOW before looking at file changes Before converting individual files/packages to slog, we generally need to make these 2 changes to make the conversion easier: - Replace uses of `kitlog.With` since they are not fully compatible with our kitlog adapter - Directly use the kitlog adapter logger type instead of the kitlog interface, which will let us have direct access to the underlying slog logger: `*logging.Logger` Note: that I did not replace absolutely all uses of `kitlog.Logger`, but I did remove all uses of `kitlog.With` except for these due to complexity: - server/logging/filesystem.go and the other log writers (webhook, firehose, kinesis, lambda, pubsub, nats) - server/datastore/mysql/nanomdm_storage.go (adapter pattern) - server/vulnerabilities/nvd/* (cascades to CLI tools) - server/service/osquery_utils/queries.go (callback type signatures cascade broadly) - cmd/maintained-apps/ (standalone, so can be transitioned later all at once) Most of the changes in this PR follow these patterns: - `kitlog.Logger` type → `*logging.Logger` - `kitlog.With(logger, ...)` → `logger.With(...)` - `kitlog.NewNopLogger() → logging.NewNopLogger()`, including similar variations such as `logging.NewLogfmtLogger(w)` and `logging.NewJSONLogger(w)` - removed many now-unused kitlog imports Unique changes that the PR review should focus on: - server/platform/logging/kitlog_adapter.go: Core adapter changes - server/platform/logging/logging.go: New convenience functions - server/service/integration_logger_test.go: Test changes for slog # Checklist for submitter If some of the following don't apply, delete the relevant line. - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. - Was added 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** * Migrated the codebase to a unified internal structured logging system for more consistent, reliable logs and observability. * No user-facing functionality changed; runtime behavior and APIs remain compatible. * **Tests** * Updated tests to use the new logging helpers to ensure consistent test logging and validation. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
137 lines
3.8 KiB
Go
137 lines
3.8 KiB
Go
package worker
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/datastore/mysql"
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
"github.com/fleetdm/fleet/v4/server/platform/logging"
|
|
"github.com/fleetdm/fleet/v4/server/test"
|
|
"github.com/jmoiron/sqlx"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestDBMigrationsVPPToken(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
ds := mysql.CreateMySQLDS(t)
|
|
// call TruncateTables immediately as a DB migration may have created jobs
|
|
mysql.TruncateTables(t, ds)
|
|
|
|
nopLog := logging.NewNopLogger()
|
|
// use this to debug/verify details of calls
|
|
// nopLog := logging.NewJSONLogger(os.Stdout)
|
|
|
|
// create and register the worker
|
|
processor := &DBMigration{
|
|
Datastore: ds,
|
|
Log: nopLog,
|
|
}
|
|
w := NewWorker(ds, nopLog)
|
|
w.Register(processor)
|
|
|
|
// create the migrated token and enqueue the job
|
|
expDate := time.Date(2024, 8, 27, 0, 0, 0, 0, time.UTC)
|
|
tok, err := test.CreateVPPTokenEncodedAfterMigration(expDate, "test-org", "test-loc")
|
|
require.NoError(t, err)
|
|
encTok, err := mysql.EncryptWithPrivateKey(t, ds, tok)
|
|
require.NoError(t, err)
|
|
|
|
const insVPP = `
|
|
INSERT INTO vpp_tokens
|
|
(
|
|
organization_name,
|
|
location,
|
|
renew_at,
|
|
token
|
|
)
|
|
VALUES
|
|
('', '', DATE('2000-01-01'), ?)
|
|
`
|
|
|
|
const insJob = `
|
|
INSERT INTO jobs (
|
|
name,
|
|
args,
|
|
state,
|
|
error,
|
|
not_before,
|
|
created_at,
|
|
updated_at
|
|
)
|
|
VALUES (?, ?, ?, '', ?, ?, ?)
|
|
`
|
|
mysql.ExecAdhocSQL(t, ds, func(q sqlx.ExtContext) error {
|
|
_, err := q.ExecContext(ctx, insVPP, encTok)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
argsJSON, err := json.Marshal(dbMigrationArgs{Task: DBMigrateVPPTokenTask})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to JSON marshal the job arguments: %w", err)
|
|
}
|
|
ts := time.Date(2024, 8, 26, 0, 0, 0, 0, time.UTC)
|
|
if _, err := q.ExecContext(ctx, insJob, dbMigrationJobName, argsJSON, fleet.JobStateQueued, ts, ts, ts); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
|
|
// run the worker, should mark the job as done
|
|
err = w.ProcessJobs(ctx)
|
|
require.NoError(t, err)
|
|
|
|
// nothing more to run
|
|
jobs, err := ds.GetQueuedJobs(ctx, 1, time.Now().UTC().Add(time.Minute)) // look in the future to catch any delayed job
|
|
require.NoError(t, err)
|
|
if !assert.Empty(t, jobs) {
|
|
t.Logf(">>> %#+v", jobs[0])
|
|
}
|
|
|
|
// token should've been updated
|
|
vppTok, err := ds.GetVPPTokenByLocation(ctx, "test-loc")
|
|
require.NoError(t, err)
|
|
require.Equal(t, "test-org", vppTok.OrgName)
|
|
require.Equal(t, "test-loc", vppTok.Location)
|
|
require.Equal(t, expDate, vppTok.RenewDate)
|
|
require.Contains(t, string(tok), `"token":"`+vppTok.Token+`"`) // the DB-stored token is the "token" JSON field in the raw tok
|
|
require.NotNil(t, vppTok.Teams)
|
|
require.Len(t, vppTok.Teams, 0)
|
|
|
|
// empty-location token should not exist anymore
|
|
_, err = ds.GetVPPTokenByLocation(ctx, "")
|
|
require.Error(t, err)
|
|
var nfe fleet.NotFoundError
|
|
require.ErrorAs(t, err, &nfe)
|
|
|
|
// enqueue a DB migration job with an unknown task
|
|
mysql.ExecAdhocSQL(t, ds, func(q sqlx.ExtContext) error {
|
|
argsJSON, err := json.Marshal(dbMigrationArgs{Task: DBMigrationTask("no-such-task")})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to JSON marshal the job arguments: %w", err)
|
|
}
|
|
ts := time.Date(2024, 8, 26, 0, 0, 0, 0, time.UTC)
|
|
if _, err := q.ExecContext(ctx, insJob, dbMigrationJobName, argsJSON, fleet.JobStateQueued, ts, ts, ts); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
|
|
// run the worker, will fail but still queued for a retry
|
|
err = w.ProcessJobs(ctx)
|
|
require.NoError(t, err)
|
|
|
|
jobs, err = ds.GetQueuedJobs(ctx, 1, time.Now().UTC().Add(time.Minute)) // look in the future to catch any delayed job
|
|
require.NoError(t, err)
|
|
require.Len(t, jobs, 1)
|
|
require.Equal(t, fleet.JobStateQueued, jobs[0].State)
|
|
require.Equal(t, 1, jobs[0].Retries)
|
|
require.Contains(t, jobs[0].Error, "unknown task: no-such-task")
|
|
}
|