* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
447 lines
12 KiB
Go
447 lines
12 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package scheduler
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
"github.com/hashicorp/boundary/internal/iam"
|
|
"github.com/hashicorp/boundary/internal/kms"
|
|
"github.com/hashicorp/boundary/internal/scheduler/job"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestScheduler_New(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
rw := db.New(conn)
|
|
wrapper := db.TestWrapper(t)
|
|
kmsCache := kms.TestKms(t, conn, wrapper)
|
|
iam.TestRepo(t, conn, wrapper)
|
|
|
|
jobRepoFn := func() (*job.Repository, error) {
|
|
return job.NewRepository(ctx, rw, rw, kmsCache)
|
|
}
|
|
|
|
type args struct {
|
|
serverId string
|
|
jobRepo jobRepoFactory
|
|
runLimit int
|
|
runInterval time.Duration
|
|
monitorInterval time.Duration
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
opts []Option
|
|
want args
|
|
wantErr bool
|
|
wantErrCode errors.Code
|
|
wantErrMsg string
|
|
}{
|
|
{
|
|
name: "with-no-server-id",
|
|
wantErr: true,
|
|
wantErrCode: errors.InvalidParameter,
|
|
wantErrMsg: "scheduler.New: missing server id: parameter violation: error #100",
|
|
},
|
|
{
|
|
name: "with-no-job-repo",
|
|
args: args{
|
|
serverId: "test-server",
|
|
},
|
|
wantErr: true,
|
|
wantErrCode: errors.InvalidParameter,
|
|
wantErrMsg: "scheduler.New: missing job repo function: parameter violation: error #100",
|
|
},
|
|
{
|
|
name: "valid",
|
|
args: args{
|
|
serverId: "test-server",
|
|
jobRepo: jobRepoFn,
|
|
},
|
|
want: args{
|
|
serverId: "test-server",
|
|
runLimit: defaultRunJobsLimit,
|
|
runInterval: defaultRunJobsInterval,
|
|
monitorInterval: defaultMonitorInterval,
|
|
},
|
|
},
|
|
{
|
|
name: "valid-with-interval",
|
|
args: args{
|
|
serverId: "test-server",
|
|
jobRepo: jobRepoFn,
|
|
},
|
|
opts: []Option{
|
|
WithRunJobsInterval(time.Hour),
|
|
},
|
|
want: args{
|
|
serverId: "test-server",
|
|
runLimit: defaultRunJobsLimit,
|
|
monitorInterval: defaultMonitorInterval,
|
|
runInterval: time.Hour,
|
|
},
|
|
},
|
|
{
|
|
name: "valid-with-unlimited",
|
|
args: args{
|
|
serverId: "test-server",
|
|
jobRepo: jobRepoFn,
|
|
},
|
|
opts: []Option{
|
|
WithRunJobsLimit(-1),
|
|
},
|
|
want: args{
|
|
serverId: "test-server",
|
|
runLimit: -1,
|
|
runInterval: defaultRunJobsInterval,
|
|
monitorInterval: defaultMonitorInterval,
|
|
},
|
|
},
|
|
{
|
|
name: "valid-with-limit",
|
|
args: args{
|
|
serverId: "test-server",
|
|
jobRepo: jobRepoFn,
|
|
},
|
|
opts: []Option{
|
|
WithRunJobsLimit(20),
|
|
},
|
|
want: args{
|
|
serverId: "test-server",
|
|
runLimit: 20,
|
|
runInterval: defaultRunJobsInterval,
|
|
monitorInterval: defaultMonitorInterval,
|
|
},
|
|
},
|
|
{
|
|
name: "valid-with-monitor",
|
|
args: args{
|
|
serverId: "test-server",
|
|
jobRepo: jobRepoFn,
|
|
},
|
|
opts: []Option{
|
|
WithMonitorInterval(time.Hour),
|
|
},
|
|
want: args{
|
|
serverId: "test-server",
|
|
runLimit: defaultRunJobsLimit,
|
|
runInterval: defaultRunJobsInterval,
|
|
monitorInterval: time.Hour,
|
|
},
|
|
},
|
|
{
|
|
name: "valid-with-all",
|
|
args: args{
|
|
serverId: "test-server",
|
|
jobRepo: jobRepoFn,
|
|
},
|
|
opts: []Option{
|
|
WithRunJobsInterval(time.Hour),
|
|
WithRunJobsLimit(20),
|
|
WithMonitorInterval(2 * time.Hour),
|
|
},
|
|
want: args{
|
|
serverId: "test-server",
|
|
runLimit: 20,
|
|
runInterval: time.Hour,
|
|
monitorInterval: 2 * time.Hour,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
got, err := New(ctx, tt.args.serverId, tt.args.jobRepo, tt.opts...)
|
|
if tt.wantErr {
|
|
require.Error(err)
|
|
assert.Truef(errors.Match(errors.T(tt.wantErrCode), err), "Unexpected error %s", err)
|
|
assert.Equal(tt.wantErrMsg, err.Error())
|
|
assert.Nil(got)
|
|
return
|
|
}
|
|
require.NoError(err)
|
|
|
|
assert.Equal(tt.want.serverId, got.serverId)
|
|
assert.Equal(tt.want.runLimit, got.runJobsLimit)
|
|
assert.Equal(tt.want.runInterval, got.runJobsInterval)
|
|
assert.Equal(tt.want.monitorInterval, got.monitorInterval)
|
|
assert.NotNil(got.jobRepoFn)
|
|
assert.NotNil(got.runningJobs)
|
|
assert.NotNil(got.registeredJobs)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestScheduler_RegisterJob(t *testing.T) {
|
|
t.Parallel()
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
rw := db.New(conn)
|
|
wrapper := db.TestWrapper(t)
|
|
iam.TestRepo(t, conn, wrapper)
|
|
|
|
sched := TestScheduler(t, conn, wrapper)
|
|
|
|
tests := []struct {
|
|
name string
|
|
job Job
|
|
wantErr bool
|
|
wantErrCode errors.Code
|
|
wantErrMsg string
|
|
}{
|
|
{
|
|
name: "nil-job",
|
|
wantErr: true,
|
|
wantErrCode: errors.InvalidParameter,
|
|
wantErrMsg: "scheduler.(Scheduler).RegisterJob: scheduler.validateJob: missing job: parameter violation: error #100",
|
|
},
|
|
{
|
|
name: "missing-job-name",
|
|
job: testJob{},
|
|
wantErr: true,
|
|
wantErrCode: errors.InvalidParameter,
|
|
wantErrMsg: "scheduler.(Scheduler).RegisterJob: scheduler.validateJob: missing name: parameter violation: error #100",
|
|
},
|
|
{
|
|
name: "missing-job-description",
|
|
job: testJob{
|
|
name: "name",
|
|
},
|
|
wantErr: true,
|
|
wantErrCode: errors.InvalidParameter,
|
|
wantErrMsg: "scheduler.(Scheduler).RegisterJob: scheduler.validateJob: missing description: parameter violation: error #100",
|
|
},
|
|
{
|
|
name: "valid",
|
|
job: testJob{
|
|
name: "valid",
|
|
description: "description",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
err := sched.RegisterJob(context.Background(), tt.job)
|
|
if tt.wantErr {
|
|
require.Error(err)
|
|
assert.Truef(errors.Match(errors.T(tt.wantErrCode), err), "Unexpected error %s", err)
|
|
assert.Equal(tt.wantErrMsg, err.Error())
|
|
return
|
|
}
|
|
require.NoError(err)
|
|
|
|
j, ok := sched.registeredJobs.Load(tt.job.Name())
|
|
require.True(ok)
|
|
require.NotNil(j)
|
|
assert.Equal(tt.job, j)
|
|
|
|
// Verify job has been persisted
|
|
var dbJob job.Job
|
|
err = rw.LookupWhere(context.Background(), &dbJob, "name = ?", []any{tt.job.Name()})
|
|
require.NoError(err)
|
|
require.NotNil(dbJob)
|
|
assert.Equal(tt.job.Name(), dbJob.Name)
|
|
assert.Equal(tt.job.Description(), dbJob.Description)
|
|
})
|
|
}
|
|
t.Run("multiple-same-job", func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
tj := testJob{
|
|
name: "multiple-same-job",
|
|
description: "description",
|
|
}
|
|
|
|
// Job should not be registered
|
|
_, ok := sched.registeredJobs.Load(tj.name)
|
|
assert.False(ok)
|
|
|
|
err := sched.RegisterJob(context.Background(), tj)
|
|
require.NoError(err)
|
|
|
|
// Job should now be registered
|
|
_, ok = sched.registeredJobs.Load(tj.name)
|
|
assert.True(ok)
|
|
|
|
// Registering the same job should not return an error
|
|
err = sched.RegisterJob(context.Background(), tj)
|
|
require.NoError(err)
|
|
|
|
// Job should still be registered
|
|
_, ok = sched.registeredJobs.Load(tj.name)
|
|
assert.True(ok)
|
|
})
|
|
t.Run("multiple-schedulers-registering-same-job", func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
tj := testJob{
|
|
name: "multiple-schedulers-registering-same-job",
|
|
description: "description",
|
|
}
|
|
err := sched.RegisterJob(context.Background(), tj)
|
|
require.NoError(err)
|
|
_, ok := sched.registeredJobs.Load(tj.name)
|
|
assert.True(ok)
|
|
|
|
sched1 := TestScheduler(t, conn, wrapper)
|
|
// Verify job is not registered on second scheduler
|
|
_, ok = sched1.registeredJobs.Load(tj.name)
|
|
assert.False(ok)
|
|
|
|
// Registering job on second scheduler should not return an error and should be registered
|
|
err = sched1.RegisterJob(context.Background(), tj)
|
|
require.NoError(err)
|
|
_, ok = sched.registeredJobs.Load(tj.name)
|
|
assert.True(ok)
|
|
})
|
|
}
|
|
|
|
func TestScheduler_UpdateJobNextRunInAtLeast(t *testing.T) {
|
|
t.Parallel()
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
rw := db.New(conn)
|
|
wrapper := db.TestWrapper(t)
|
|
iam.TestRepo(t, conn, wrapper)
|
|
|
|
sched := TestScheduler(t, conn, wrapper)
|
|
|
|
t.Run("missing-job-name", func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
|
|
err := sched.UpdateJobNextRunInAtLeast(context.Background(), "", time.Hour)
|
|
require.Error(err)
|
|
assert.Truef(errors.Match(errors.T(errors.InvalidParameter), err), "Unexpected error %s", err)
|
|
assert.Equal("scheduler.(Scheduler).UpdateJobNextRunInAtLeast: missing name: parameter violation: error #100", err.Error())
|
|
})
|
|
t.Run("job-not-found", func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
|
|
// Insert fake job to bypass registration check
|
|
sched.registeredJobs.Store("fake-job-name", nil)
|
|
|
|
err := sched.UpdateJobNextRunInAtLeast(context.Background(), "fake-job-name", time.Hour)
|
|
require.Error(err)
|
|
assert.Truef(errors.Match(errors.T(errors.RecordNotFound), err), "Unexpected error %s", err)
|
|
assert.Equal("scheduler.(Scheduler).UpdateJobNextRunInAtLeast: job.(Repository).UpdateJobNextRunInAtLeast: db.DoTx: job.(Repository).UpdateJobNextRunInAtLeast: job \"fake-job-name\" does not exist: search issue: error #1100", err.Error())
|
|
})
|
|
t.Run("valid", func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
|
|
tj := testJob{
|
|
name: "valid",
|
|
description: "description",
|
|
}
|
|
err := sched.RegisterJob(context.Background(), tj, WithNextRunIn(2*time.Hour))
|
|
require.NoError(err)
|
|
|
|
// get job from repo
|
|
var dbJob job.Job
|
|
err = rw.LookupWhere(context.Background(), &dbJob, "name = ?", []any{tj.name})
|
|
require.NoError(err)
|
|
previousNextRun := dbJob.NextScheduledRun.AsTime()
|
|
|
|
err = sched.UpdateJobNextRunInAtLeast(context.Background(), tj.name, time.Hour)
|
|
require.NoError(err)
|
|
|
|
err = rw.LookupWhere(context.Background(), &dbJob, "name = ?", []any{tj.name})
|
|
require.NoError(err)
|
|
// Verify job run time in repo is at least 1 hour before than the previous run time
|
|
assert.Equal(previousNextRun.Add(-1*time.Hour).Round(time.Minute), dbJob.NextScheduledRun.AsTime().Round(time.Minute))
|
|
})
|
|
|
|
t.Run("next-run-already-sooner", func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
|
|
tj := testJob{
|
|
name: "next-run-already-sooner",
|
|
description: "description",
|
|
}
|
|
err := sched.RegisterJob(context.Background(), tj, WithNextRunIn(2*time.Hour))
|
|
require.NoError(err)
|
|
|
|
// get job from repo
|
|
var dbJob job.Job
|
|
err = rw.LookupWhere(context.Background(), &dbJob, "name = ?", []any{tj.name})
|
|
require.NoError(err)
|
|
previousNextRun := dbJob.NextScheduledRun.AsTime()
|
|
|
|
err = sched.UpdateJobNextRunInAtLeast(context.Background(), tj.name, 4*time.Hour)
|
|
require.NoError(err)
|
|
|
|
err = rw.LookupWhere(context.Background(), &dbJob, "name = ?", []any{tj.name})
|
|
require.NoError(err)
|
|
// Job should not have updated next run time, since its later than the already scheduled time
|
|
assert.Equal(previousNextRun, dbJob.NextScheduledRun.AsTime())
|
|
|
|
err = sched.UpdateJobNextRunInAtLeast(context.Background(), tj.name, time.Hour)
|
|
require.NoError(err)
|
|
|
|
err = rw.LookupWhere(context.Background(), &dbJob, "name = ?", []any{tj.name})
|
|
require.NoError(err)
|
|
// Verify job run time in repo is at least 1 hour before than the previous run time
|
|
assert.Equal(previousNextRun.Add(-1*time.Hour).Round(time.Minute), dbJob.NextScheduledRun.AsTime().Round(time.Minute))
|
|
})
|
|
}
|
|
|
|
func TestScheduler_Start(t *testing.T) {
|
|
t.Parallel()
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
wrapper := db.TestWrapper(t)
|
|
iam.TestRepo(t, conn, wrapper)
|
|
|
|
sched := TestScheduler(t, conn, wrapper)
|
|
|
|
tests := []struct {
|
|
name string
|
|
ctx context.Context
|
|
wg *sync.WaitGroup
|
|
wantErr bool
|
|
wantErrContains string
|
|
}{
|
|
{
|
|
name: "missing-ctx",
|
|
wg: &sync.WaitGroup{},
|
|
wantErr: true,
|
|
wantErrContains: "missing context",
|
|
},
|
|
{
|
|
name: "missing-waitgroup",
|
|
ctx: context.Background(),
|
|
wantErr: true,
|
|
wantErrContains: "missing wait group",
|
|
},
|
|
|
|
{
|
|
name: "valid",
|
|
ctx: context.Background(),
|
|
wg: &sync.WaitGroup{},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
err := sched.Start(tt.ctx, tt.wg)
|
|
if tt.wantErr {
|
|
require.Error(err)
|
|
if tt.wantErrContains != "" {
|
|
assert.Contains(err.Error(), tt.wantErrContains)
|
|
}
|
|
return
|
|
}
|
|
require.NoError(err)
|
|
})
|
|
}
|
|
}
|