> Related issue: #9956 # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/Committing-Changes.md#changes-files) for more information. - [x] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements) - [x] Added/updated tests - [x] If paths of existing endpoints are modified without backwards compatibility, checked the frontend/CLI for any necessary changes - [x] If database migrations are included, checked table schema to confirm autoupdate - For database migrations: - [x] Checked schema for all modified table for columns that will auto-update timestamps during migration. - [x] Confirmed that updating the timestamps is acceptable, and will not cause unwanted side effects. - [x] Ensured the correct collation is explicitly set for character columns (`COLLATE utf8mb4_unicode_ci`). - [x] Manual QA for all new/changed functionality --------- Co-authored-by: Martin Angers <martin.n.angers@gmail.com> Co-authored-by: Gabriel Hernandez <ghernandez345@gmail.com> Co-authored-by: Roberto Dip <rroperzh@gmail.com> Co-authored-by: Sarah Gillespie <73313222+gillespi314@users.noreply.github.com> Co-authored-by: Dante Catalfamo <43040593+dantecatalfamo@users.noreply.github.com> Co-authored-by: Roberto Dip <dip.jesusr@gmail.com>
138 lines
3.7 KiB
Go
138 lines
3.7 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/test"
|
|
kitlog "github.com/go-kit/log"
|
|
"github.com/jmoiron/sqlx"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestDBMigrationsVPPToken(t *testing.T) {
|
|
// FIXME
|
|
t.Skip()
|
|
ctx := context.Background()
|
|
|
|
ds := mysql.CreateMySQLDS(t)
|
|
// call TruncateTables immediately as a DB migration may have created jobs
|
|
mysql.TruncateTables(t, ds)
|
|
|
|
nopLog := kitlog.NewNopLogger()
|
|
// use this to debug/verify details of calls
|
|
// nopLog := kitlog.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.CreateVPPTokenEncoded(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,
|
|
team_id,
|
|
null_team_type
|
|
)
|
|
VALUES
|
|
('', '', DATE('2000-01-01'), ?, NULL, 'allteams')
|
|
`
|
|
|
|
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)
|
|
require.Empty(t, jobs)
|
|
|
|
// 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.Equal(t, string(tok), vppTok.Token)
|
|
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")
|
|
}
|