* Remove the server_id from session table, change it to worker_id on session_connection This is step 1 in updating the worker table. The worker is becoming a public resource so it must have a public id. We drop the server_id column on the session_connection table and add the worker_id column so we can use the wt_public_key type. Since db migrations are applied while the controller is offline, it should be safe to assume all session connections are not in a running state, thus allowing us to null out all the worker ids in the session_connection table. For now we don't have very good enforcement of the wt_public_id contraints, since the id is currently defined in the worker config, but this will soon change and the worker's id will be generated by the controller. * Updating server_id references to controller_id. * Adding worker tag db writing test. * Move the server table back to the top. * Update comment for the worker object. * Update comment for the worker table. * Update the newWorkerId call to take a context and fix its error. * Correct comment for the wt_network_address domain.
186 lines
3.4 KiB
Go
186 lines
3.4 KiB
Go
package servers
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/hashicorp/boundary/internal/db"
|
|
"github.com/hashicorp/boundary/internal/iam"
|
|
"github.com/hashicorp/boundary/internal/servers/store"
|
|
"github.com/hashicorp/boundary/internal/types/scope"
|
|
"github.com/stretchr/testify/assert"
|
|
"google.golang.org/protobuf/testing/protocmp"
|
|
)
|
|
|
|
func TestWorker_New(t *testing.T) {
|
|
t.Parallel()
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
wrapper := db.TestWrapper(t)
|
|
rw := db.New(conn)
|
|
|
|
org, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
|
|
|
type args struct {
|
|
scopeId string
|
|
opts []Option
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want *Worker
|
|
wantCreateErr bool
|
|
}{
|
|
{
|
|
name: "missing-scope-id",
|
|
args: args{},
|
|
want: &Worker{
|
|
Worker: &store.Worker{},
|
|
},
|
|
wantCreateErr: true,
|
|
},
|
|
{
|
|
name: "global-scope-id",
|
|
args: args{
|
|
scopeId: scope.Global.String(),
|
|
},
|
|
want: &Worker{
|
|
Worker: &store.Worker{
|
|
ScopeId: scope.Global.String(),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "org-scope-id",
|
|
args: args{
|
|
scopeId: org.GetPublicId(),
|
|
},
|
|
want: &Worker{
|
|
Worker: &store.Worker{
|
|
ScopeId: org.GetPublicId(),
|
|
},
|
|
},
|
|
wantCreateErr: true,
|
|
},
|
|
{
|
|
name: "project-scope-id",
|
|
args: args{
|
|
scopeId: prj.GetPublicId(),
|
|
},
|
|
want: &Worker{
|
|
Worker: &store.Worker{
|
|
ScopeId: prj.GetPublicId(),
|
|
},
|
|
},
|
|
wantCreateErr: true,
|
|
},
|
|
{
|
|
name: "with public id",
|
|
args: args{
|
|
scopeId: scope.Global.String(),
|
|
opts: []Option{
|
|
WithPublicId("w_test_public_id"),
|
|
},
|
|
},
|
|
want: &Worker{
|
|
Worker: &store.Worker{
|
|
PublicId: "w_test_public_id",
|
|
ScopeId: scope.Global.String(),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "with name",
|
|
args: args{
|
|
scopeId: scope.Global.String(),
|
|
opts: []Option{
|
|
WithName("foo"),
|
|
},
|
|
},
|
|
want: &Worker{
|
|
Worker: &store.Worker{
|
|
ScopeId: scope.Global.String(),
|
|
Name: "foo",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "with description",
|
|
args: args{
|
|
scopeId: scope.Global.String(),
|
|
opts: []Option{
|
|
WithDescription("foo"),
|
|
},
|
|
},
|
|
want: &Worker{
|
|
Worker: &store.Worker{
|
|
ScopeId: scope.Global.String(),
|
|
Description: "foo",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "with address",
|
|
args: args{
|
|
scopeId: scope.Global.String(),
|
|
opts: []Option{
|
|
WithAddress("foo"),
|
|
},
|
|
},
|
|
want: &Worker{
|
|
Worker: &store.Worker{
|
|
ScopeId: scope.Global.String(),
|
|
Address: "foo",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "with Tags",
|
|
args: args{
|
|
scopeId: scope.Global.String(),
|
|
opts: []Option{
|
|
WithWorkerTags(&Tag{
|
|
Key: "key",
|
|
Value: "val",
|
|
}),
|
|
},
|
|
},
|
|
want: &Worker{
|
|
Worker: &store.Worker{
|
|
ScopeId: scope.Global.String(),
|
|
},
|
|
Tags: []*Tag{
|
|
{
|
|
Key: "key",
|
|
Value: "val",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
got := NewWorker(tt.args.scopeId, tt.args.opts...)
|
|
assert.Empty(cmp.Diff(tt.want, got.clone(), protocmp.Transform()))
|
|
|
|
id, err := newWorkerId(context.Background())
|
|
assert.NoError(err)
|
|
|
|
tt.want.PublicId = id
|
|
got.PublicId = id
|
|
|
|
err2 := rw.Create(context.Background(), got)
|
|
if tt.wantCreateErr {
|
|
assert.Error(err2)
|
|
} else {
|
|
assert.NoError(err2)
|
|
assert.Equal(uint32(1), got.Version)
|
|
}
|
|
})
|
|
}
|
|
}
|