Files
threshold/internal/scheduler/job/additional_verification_test.go
T
hashicorp-copywrite[bot]andJohan Brandhorst-Satzkorn 29da0bcb92 [COMPLIANCE] License changes (#3567)
* 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>
2023-08-11 10:31:53 -07:00

211 lines
6.5 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package job
import (
"context"
"database/sql"
"testing"
"time"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/kms"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestJobWorkflow(t *testing.T) {
t.Parallel()
ctx := context.Background()
assert, require := assert.New(t), require.New(t)
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
wrapper := db.TestWrapper(t)
kms := kms.TestKms(t, conn, wrapper)
iam.TestRepo(t, conn, wrapper)
server := testController(t, conn, wrapper)
repo, err := NewRepository(ctx, rw, rw, kms)
require.NoError(err)
job, err := repo.UpsertJob(ctx, "job1", "description")
require.NoError(err)
require.NotNil(job)
assert.Equal(defaultPluginId, job.PluginId)
runs, err := repo.RunJobs(ctx, server.PrivateId)
require.NoError(err)
assert.Len(runs, 1)
run := runs[0]
assert.NotEmpty(run.PrivateId)
assert.Equal(job.Name, run.JobName)
run, err = repo.UpdateProgress(ctx, run.PrivateId, 100, 110)
require.NoError(err)
assert.Equal(uint32(100), run.CompletedCount)
assert.Equal(uint32(110), run.TotalCount)
// The only available job is already running, a request for work should return nil
newRuns, err := repo.RunJobs(ctx, server.PrivateId)
require.NoError(err)
assert.Nil(newRuns)
run, err = repo.CompleteRun(ctx, run.PrivateId, time.Hour, 0, 0)
require.NoError(err)
assert.Equal(Completed.string(), run.Status)
job, err = repo.LookupJob(ctx, job.Name)
require.NoError(err)
assert.NotNil(job)
// The only available job has a next run in the future, a request for work should return nil
newRuns, err = repo.RunJobs(ctx, server.PrivateId)
require.NoError(err)
assert.Nil(newRuns)
// Update job next run to time in past
job, err = repo.UpdateJobNextRunInAtLeast(ctx, job.Name, 0)
require.NoError(err)
// Now that next scheduled time is in past, a request for work should return a Run
newRuns, err = repo.RunJobs(ctx, server.PrivateId)
require.NoError(err)
require.Len(newRuns, 1)
newRun := newRuns[0]
require.NotEmpty(newRun.PrivateId)
assert.Equal(job.Name, newRun.JobName)
assert.NotEqual(run.PrivateId, newRun.PrivateId)
// The only available job is already running, a request for work should return nil
newRuns, err = repo.RunJobs(ctx, server.PrivateId)
require.NoError(err)
assert.Nil(newRuns)
newRun, err = repo.FailRun(ctx, newRun.PrivateId, 0, 0)
require.NoError(err)
assert.Equal(Failed.string(), newRun.Status)
// Run failed so the job should be available for work immediately
newRuns, err = repo.RunJobs(ctx, server.PrivateId)
require.NoError(err)
assert.Len(newRuns, 1)
}
// TODO (LCR): This test can be dropped once plugin support is plumbed through job repo and scheduler
func TestPlugin(t *testing.T) {
t.Parallel()
t.Run("plugin-unique", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
rows, err := rw.Query(context.Background(), upsertJobQuery, []any{
sql.Named("plugin_id", defaultPluginId),
sql.Named("name", "same-job-name"),
sql.Named("description", "description"),
sql.Named("next_scheduled_run", 0),
})
require.NoError(err)
_ = rows.Close()
rows, err = rw.Query(context.Background(), "select * from job;", nil)
require.NoError(err)
var numRows int
for rows.Next() {
numRows++
}
_ = rows.Close()
require.Equal(1, numRows)
// Calling upsertJob with same name and pluginId should not insert a new job
rows, err = rw.Query(context.Background(), upsertJobQuery, []any{
sql.Named("plugin_id", defaultPluginId),
sql.Named("name", "same-job-name"),
sql.Named("description", "description"),
sql.Named("next_scheduled_run", 0),
})
require.NoError(err)
_ = rows.Close()
// Validate there is still only 1 job in the database
rows, err = rw.Query(context.Background(), "select * from job;", nil)
require.NoError(err)
numRows = 0
for rows.Next() {
numRows++
}
_ = rows.Close()
require.Equal(1, numRows)
// Create test plugin id
testPluginId := "pi_test1234"
numRows, err = rw.Exec(context.Background(), "insert into plugin(public_id, scope_id) values (?, 'global')", []any{testPluginId})
require.NoError(err)
assert.Equal(1, numRows)
// Calling upsertJob with the same name and different pluginId should create a new job
rows, err = rw.Query(context.Background(), upsertJobQuery, []any{
sql.Named("plugin_id", testPluginId),
sql.Named("name", "same-job-name"),
sql.Named("description", "description"),
sql.Named("next_scheduled_run", 0),
})
assert.NoError(err)
_ = rows.Close()
// Validate there are 2 jobs in the database
rows, err = rw.Query(context.Background(), "select * from job;", nil)
require.NoError(err)
numRows = 0
for rows.Next() {
numRows++
}
_ = rows.Close()
require.Equal(2, numRows)
})
t.Run("plugin-id-immutable", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
// Create test plugin id
testPluginId := "pi_test1234"
numRows, err := rw.Exec(context.Background(), "insert into plugin(public_id, scope_id) values (?, 'global')", []any{testPluginId})
assert.NoError(err)
assert.Equal(1, numRows)
newPluginId := "pi_newtest1234"
numUpdated, err := rw.Exec(context.Background(), "update plugin set public_id = ? where public_id = ?", []any{newPluginId, testPluginId})
require.Error(err)
assert.Equal("db.Exec: immutable column: plugin.public_id: integrity violation: error #1003", err.Error())
assert.Equal(0, numUpdated)
})
t.Run("default-plugin-deletion-disallowed", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
numDeleted, err := rw.Exec(context.Background(), "delete from plugin where public_id = ?", []any{defaultPluginId})
require.Error(err)
assert.Equal("db.Exec: deletion of system plugin not allowed: integrity violation: error #1104", err.Error())
assert.Equal(0, numDeleted)
// Create test plugin id
testPluginId := "pi_test1234"
numRows, err := rw.Exec(context.Background(), "insert into plugin(public_id, scope_id) values (?, 'global')", []any{testPluginId})
assert.NoError(err)
assert.Equal(1, numRows)
numDeleted, err = rw.Exec(context.Background(), "delete from plugin where public_id = ?", []any{testPluginId})
assert.NoError(err)
assert.Equal(1, numDeleted)
})
}