Files
threshold/internal/target/tcp/target_test.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

416 lines
12 KiB
Go
Raw Normal View History

2020-08-26 13:58:46 -04:00
// Copyright (c) HashiCorp, Inc.
2022-07-20 16:24:46 -07:00
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
2021-09-29 13:13:49 -04:00
package tcp_test
2020-08-26 13:58:46 -04:00
import (
"context"
"testing"
"time"
"github.com/hashicorp/boundary/globals"
2020-08-26 13:58:46 -04:00
"github.com/hashicorp/boundary/internal/db"
dbassert "github.com/hashicorp/boundary/internal/db/assert"
2020-11-05 16:54:03 -05:00
"github.com/hashicorp/boundary/internal/errors"
2020-08-26 13:58:46 -04:00
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/oplog"
2021-09-29 13:13:49 -04:00
"github.com/hashicorp/boundary/internal/target"
"github.com/hashicorp/boundary/internal/target/tcp"
2020-08-26 13:58:46 -04:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)
2021-09-29 13:13:49 -04:00
func TestTarget_Create(t *testing.T) {
2020-08-26 13:58:46 -04:00
t.Parallel()
conn, _ := db.TestSetup(t, "postgres")
wrapper := db.TestWrapper(t)
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
2021-11-04 11:37:32 -04:00
ctx := context.Background()
2020-08-26 13:58:46 -04:00
type args struct {
2022-07-20 16:24:46 -07:00
projectId string
opt []target.Option
2020-08-26 13:58:46 -04:00
}
tests := []struct {
name string
args args
2021-11-04 11:37:32 -04:00
want target.Target
2020-08-26 13:58:46 -04:00
wantErr bool
wantIsErr errors.Code
2020-08-26 13:58:46 -04:00
create bool
wantCreateErr bool
}{
{
2022-07-20 16:24:46 -07:00
name: "empty-projectId",
2020-08-26 13:58:46 -04:00
args: args{},
wantErr: true,
wantIsErr: errors.InvalidParameter,
2020-08-26 13:58:46 -04:00
},
{
2022-07-20 16:24:46 -07:00
name: "valid-proj-id",
2020-08-26 13:58:46 -04:00
args: args{
2022-07-20 16:24:46 -07:00
projectId: prj.PublicId,
opt: []target.Option{target.WithName("valid-proj-id")},
2020-08-26 13:58:46 -04:00
},
2021-11-04 11:37:32 -04:00
want: func() target.Target {
t, _ := target.New(
ctx,
2023-11-02 16:03:47 -04:00
tcp.Subtype,
2021-09-29 13:13:49 -04:00
prj.PublicId,
2022-07-20 16:24:46 -07:00
target.WithName("valid-proj-id"),
2021-09-29 13:13:49 -04:00
target.WithSessionMaxSeconds(uint32((8 * time.Hour).Seconds())),
target.WithSessionConnectionLimit(-1),
2021-09-29 13:13:49 -04:00
)
return t
2020-08-26 13:58:46 -04:00
}(),
create: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
2023-11-02 16:03:47 -04:00
got, err := target.New(ctx, tcp.Subtype, tt.args.projectId, tt.args.opt...)
2020-08-26 13:58:46 -04:00
if tt.wantErr {
require.Error(err)
assert.True(errors.Match(errors.T(tt.wantIsErr), err))
2020-08-26 13:58:46 -04:00
return
}
require.NoError(err)
assert.Equal(tt.want, got)
if tt.create {
id, err := db.NewPublicId(ctx, globals.TcpTargetPrefix)
2020-08-26 13:58:46 -04:00
require.NoError(err)
2024-02-16 08:30:16 -05:00
require.NoError(got.SetPublicId(ctx, id))
2021-11-04 11:37:32 -04:00
err = db.New(conn).Create(ctx, got)
2020-08-26 13:58:46 -04:00
if tt.wantCreateErr {
assert.Error(err)
return
}
2021-09-29 13:13:49 -04:00
assert.NoError(err)
2020-08-26 13:58:46 -04:00
}
})
}
}
2021-09-29 13:13:49 -04:00
func TestTarget_Delete(t *testing.T) {
2020-08-26 13:58:46 -04:00
t.Parallel()
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
wrapper := db.TestWrapper(t)
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
2021-11-04 11:37:32 -04:00
ctx := context.Background()
2020-08-26 13:58:46 -04:00
tests := []struct {
name string
2021-11-04 11:37:32 -04:00
target target.Target
2020-08-26 13:58:46 -04:00
wantRowsDeleted int
wantErr bool
wantErrMsg string
}{
{
name: "valid",
2021-11-04 11:37:32 -04:00
target: tcp.TestTarget(ctx, t, conn, proj.PublicId, tcp.TestTargetName(t, proj.PublicId)),
2020-08-26 13:58:46 -04:00
wantErr: false,
wantRowsDeleted: 1,
},
{
name: "bad-id",
2021-11-04 11:37:32 -04:00
target: func() target.Target {
2023-11-02 16:03:47 -04:00
tar, _ := target.New(ctx, tcp.Subtype, proj.PublicId)
2021-09-29 13:13:49 -04:00
id, err := db.NewPublicId(ctx, globals.TcpTargetPrefix)
2020-08-26 13:58:46 -04:00
require.NoError(t, err)
2024-02-16 08:30:16 -05:00
require.NoError(t, tar.SetPublicId(ctx, id))
2021-11-04 11:37:32 -04:00
tar.SetName(tcp.TestTargetName(t, proj.PublicId))
2021-09-29 13:13:49 -04:00
return tar
2020-08-26 13:58:46 -04:00
}(),
wantErr: false,
wantRowsDeleted: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
deleteTarget := tcp.NewTestTarget(ctx, "")
2024-02-16 08:30:16 -05:00
require.NoError(deleteTarget.SetPublicId(ctx, tt.target.GetPublicId()))
2021-09-29 13:13:49 -04:00
deletedRows, err := rw.Delete(context.Background(), deleteTarget)
2020-08-26 13:58:46 -04:00
if tt.wantErr {
require.Error(err)
return
}
require.NoError(err)
if tt.wantRowsDeleted == 0 {
assert.Equal(tt.wantRowsDeleted, deletedRows)
return
}
assert.Equal(tt.wantRowsDeleted, deletedRows)
foundTarget := tcp.NewTestTarget(ctx, "")
2024-02-16 08:30:16 -05:00
require.NoError(foundTarget.SetPublicId(ctx, tt.target.GetPublicId()))
2021-09-29 13:13:49 -04:00
err = rw.LookupById(context.Background(), foundTarget)
2020-08-26 13:58:46 -04:00
require.Error(err)
assert.True(errors.IsNotFoundError(err))
2020-08-26 13:58:46 -04:00
})
}
}
2021-09-29 13:13:49 -04:00
func TestTarget_Update(t *testing.T) {
2020-08-26 13:58:46 -04:00
t.Parallel()
2021-09-29 13:13:49 -04:00
id := tcp.TestId(t)
2020-08-26 13:58:46 -04:00
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
wrapper := db.TestWrapper(t)
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
type args struct {
name string
description string
fieldMaskPaths []string
nullPaths []string
2022-07-20 16:24:46 -07:00
ProjectId string
2020-08-26 13:58:46 -04:00
}
tests := []struct {
name string
args args
wantRowsUpdate int
wantErr bool
wantErrMsg string
wantDup bool
}{
{
name: "valid",
args: args{
name: "valid" + id,
fieldMaskPaths: []string{"Name"},
2022-07-20 16:24:46 -07:00
ProjectId: proj.PublicId,
2020-08-26 13:58:46 -04:00
},
wantErr: false,
wantRowsUpdate: 1,
},
{
2022-07-20 16:24:46 -07:00
name: "proj-id-not-in-mask",
2020-08-26 13:58:46 -04:00
args: args{
2022-07-20 16:24:46 -07:00
name: "proj-id" + id,
2020-08-26 13:58:46 -04:00
fieldMaskPaths: []string{"Name"},
2022-07-20 16:24:46 -07:00
ProjectId: proj.PublicId,
2020-08-26 13:58:46 -04:00
},
wantErr: false,
wantRowsUpdate: 1,
},
{
2022-07-20 16:24:46 -07:00
name: "empty-project-id",
2020-08-26 13:58:46 -04:00
args: args{
2022-07-20 16:24:46 -07:00
name: "empty-project-id" + id,
2020-08-26 13:58:46 -04:00
fieldMaskPaths: []string{"Name"},
2022-07-20 16:24:46 -07:00
ProjectId: "",
2020-08-26 13:58:46 -04:00
},
wantErr: false,
wantRowsUpdate: 1,
},
{
name: "dup-name",
args: args{
name: "dup-name" + id,
fieldMaskPaths: []string{"Name"},
2022-07-20 16:24:46 -07:00
ProjectId: proj.PublicId,
2020-08-26 13:58:46 -04:00
},
wantErr: true,
wantDup: true,
2022-07-20 16:24:46 -07:00
wantErrMsg: `db.Update: duplicate key value violates unique constraint "target_tcp_project_id_name_uq": unique constraint violation: integrity violation: error #1002`,
2020-08-26 13:58:46 -04:00
},
{
name: "set description null",
args: args{
name: "set description null" + id,
fieldMaskPaths: []string{"Name"},
nullPaths: []string{"Description"},
2022-07-20 16:24:46 -07:00
ProjectId: proj.PublicId,
2020-08-26 13:58:46 -04:00
},
wantErr: false,
wantRowsUpdate: 1,
},
{
name: "set name null",
args: args{
description: "set description null" + id,
fieldMaskPaths: []string{"Description"},
nullPaths: []string{"Name"},
2022-07-20 16:24:46 -07:00
ProjectId: proj.PublicId,
2020-08-26 13:58:46 -04:00
},
wantErr: true,
wantErrMsg: `db.Update: name must not be empty: not null constraint violated: integrity violation: error #1001`,
2020-08-26 13:58:46 -04:00
},
{
name: "set description null",
args: args{
name: "set name null" + id,
fieldMaskPaths: []string{"Name"},
nullPaths: []string{"Description"},
2022-07-20 16:24:46 -07:00
ProjectId: proj.PublicId,
2020-08-26 13:58:46 -04:00
},
wantErr: false,
wantRowsUpdate: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
2021-11-04 11:37:32 -04:00
ctx := context.Background()
2020-08-26 13:58:46 -04:00
if tt.wantDup {
2021-11-04 11:37:32 -04:00
target := tcp.TestTarget(ctx, t, conn, proj.PublicId, tcp.TestTargetName(t, proj.PublicId))
target.SetName(tt.args.name)
_, err := rw.Update(ctx, target, tt.args.fieldMaskPaths, tt.args.nullPaths)
2020-08-26 13:58:46 -04:00
require.NoError(err)
}
2021-09-29 13:13:49 -04:00
id := tcp.TestId(t)
2021-11-04 11:37:32 -04:00
tar := tcp.TestTarget(ctx, t, conn, proj.PublicId, id, target.WithDescription(id))
2020-08-26 13:58:46 -04:00
updateTarget := tcp.NewTestTarget(ctx, tt.args.ProjectId)
2024-02-16 08:30:16 -05:00
require.NoError(updateTarget.SetPublicId(ctx, tar.GetPublicId()))
2021-11-04 11:37:32 -04:00
updateTarget.SetName(tt.args.name)
updateTarget.SetDescription(tt.args.description)
2020-08-26 13:58:46 -04:00
2021-11-04 11:37:32 -04:00
updatedRows, err := rw.Update(ctx, updateTarget, tt.args.fieldMaskPaths, tt.args.nullPaths)
2020-08-26 13:58:46 -04:00
if tt.wantErr {
require.Error(err)
assert.Equal(0, updatedRows)
assert.Equal(tt.wantErrMsg, err.Error())
2021-11-04 11:37:32 -04:00
err = db.TestVerifyOplog(t, rw, tar.GetPublicId(), db.WithOperation(oplog.OpType_OP_TYPE_UPDATE), db.WithCreateNotBefore(10*time.Second))
2020-08-26 13:58:46 -04:00
require.Error(err)
assert.Contains(err.Error(), "record not found")
2020-08-26 13:58:46 -04:00
return
}
require.NoError(err)
assert.Equal(tt.wantRowsUpdate, updatedRows)
2021-11-04 11:37:32 -04:00
assert.NotEqual(tar.GetUpdateTime(), updateTarget.GetUpdateTime())
foundTarget := tcp.NewTestTarget(ctx, tt.args.ProjectId)
2024-02-16 08:30:16 -05:00
require.NoError(foundTarget.SetPublicId(ctx, tar.GetPublicId()))
2021-11-04 11:37:32 -04:00
err = rw.LookupByPublicId(ctx, foundTarget)
2020-08-26 13:58:46 -04:00
require.NoError(err)
2021-11-04 11:37:32 -04:00
assert.True(proto.Equal(updateTarget.(*tcp.Target).Target, foundTarget.(*tcp.Target).Target))
2020-08-26 13:58:46 -04:00
if len(tt.args.nullPaths) != 0 {
underlyingDB, err := conn.SqlDB(ctx)
require.NoError(err)
dbassert := dbassert.New(t, underlyingDB)
2020-08-26 13:58:46 -04:00
for _, f := range tt.args.nullPaths {
2021-11-04 11:37:32 -04:00
ft := foundTarget.(*tcp.Target)
dbassert.IsNull(&ft, f)
2020-08-26 13:58:46 -04:00
}
}
})
}
2022-07-20 16:24:46 -07:00
t.Run("update dup names in diff projects", func(t *testing.T) {
2021-11-04 11:37:32 -04:00
ctx := context.Background()
2020-08-26 13:58:46 -04:00
assert, require := assert.New(t), require.New(t)
2021-09-29 13:13:49 -04:00
id := tcp.TestId(t)
2020-08-26 13:58:46 -04:00
_, proj2 := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
2021-11-04 11:37:32 -04:00
_ = tcp.TestTarget(ctx, t, conn, proj2.PublicId, id, target.WithDescription(id))
projTarget := tcp.TestTarget(ctx, t, conn, proj.PublicId, id)
projTarget.SetName(id)
updatedRows, err := rw.Update(ctx, projTarget, []string{"Name"}, nil)
2020-08-26 13:58:46 -04:00
require.NoError(err)
assert.Equal(1, updatedRows)
2023-11-02 16:03:47 -04:00
foundTarget, _ := target.New(ctx, tcp.Subtype, proj2.PublicId)
2024-02-16 08:30:16 -05:00
require.NoError(foundTarget.SetPublicId(ctx, projTarget.GetPublicId()))
2021-11-04 11:37:32 -04:00
err = rw.LookupByPublicId(ctx, foundTarget)
2020-08-26 13:58:46 -04:00
require.NoError(err)
2021-11-04 11:37:32 -04:00
assert.Equal(id, projTarget.GetName())
2020-08-26 13:58:46 -04:00
})
}
2021-09-29 13:13:49 -04:00
func TestTarget_Clone(t *testing.T) {
2020-08-26 13:58:46 -04:00
t.Parallel()
2021-11-04 11:37:32 -04:00
ctx := context.Background()
2020-08-26 13:58:46 -04:00
conn, _ := db.TestSetup(t, "postgres")
wrapper := db.TestWrapper(t)
t.Run("valid", func(t *testing.T) {
assert := assert.New(t)
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
tar := tcp.TestTarget(ctx, t, conn, proj.PublicId, tcp.TestTargetName(t, proj.PublicId),
target.WithAddress("8.8.8.8"),
)
2021-09-29 13:13:49 -04:00
cp := tar.Clone()
2021-11-04 11:37:32 -04:00
assert.True(proto.Equal(cp.(*tcp.Target).Target, tar.(*tcp.Target).Target))
2020-08-26 13:58:46 -04:00
})
t.Run("not-equal", func(t *testing.T) {
assert := assert.New(t)
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
_, proj2 := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
2021-11-04 11:37:32 -04:00
target1 := tcp.TestTarget(ctx, t, conn, proj.PublicId, tcp.TestTargetName(t, proj.PublicId))
talias := tcp.TestTarget(ctx, t, conn, proj2.PublicId, tcp.TestTargetName(t, proj2.PublicId))
2020-08-26 13:58:46 -04:00
2021-09-29 13:13:49 -04:00
cp := target1.Clone()
assert.True(!proto.Equal(cp.(*tcp.Target).Target, talias.(*tcp.Target).Target))
2020-08-26 13:58:46 -04:00
})
}
2021-09-29 13:13:49 -04:00
func TestTable_SetTableName(t *testing.T) {
2020-08-26 13:58:46 -04:00
t.Parallel()
2021-09-29 13:13:49 -04:00
defaultTableName := tcp.DefaultTableName
2021-11-04 11:37:32 -04:00
ctx := context.Background()
2020-08-26 13:58:46 -04:00
tests := []struct {
name string
setNameTo string
want string
}{
{
name: "new-name",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
2023-11-02 16:03:47 -04:00
def, _ := target.New(ctx, tcp.Subtype, "testScope")
2021-11-04 11:37:32 -04:00
require.Equal(defaultTableName, def.(*tcp.Target).TableName())
2023-11-02 16:03:47 -04:00
ss, _ := target.New(ctx, tcp.Subtype, "testScope")
2021-11-04 11:37:32 -04:00
s := ss.(*tcp.Target)
2020-08-26 13:58:46 -04:00
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}
2021-09-29 13:13:49 -04:00
func TestTarget_oplog(t *testing.T) {
2021-11-04 11:37:32 -04:00
ctx := context.Background()
2021-09-29 13:13:49 -04:00
id := tcp.TestId(t)
2020-08-26 13:58:46 -04:00
tests := []struct {
name string
2021-11-04 11:37:32 -04:00
target target.Target
2020-08-26 13:58:46 -04:00
op oplog.OpType
want oplog.Metadata
}{
{
name: "simple",
2021-11-04 11:37:32 -04:00
target: func() target.Target {
2023-11-02 16:03:47 -04:00
tar, _ := target.New(ctx, tcp.Subtype, id)
2021-11-04 11:37:32 -04:00
if err := tar.SetPublicId(ctx, id); err != nil {
t.Fatalf("failed to set public id: %s", err)
}
return tar
2020-08-26 13:58:46 -04:00
}(),
op: oplog.OpType_OP_TYPE_CREATE,
want: oplog.Metadata{
"resource-public-id": []string{id},
"resource-type": []string{"tcp target"},
"op-type": []string{oplog.OpType_OP_TYPE_CREATE.String()},
2022-07-20 16:24:46 -07:00
"project-id": []string{id},
2020-08-26 13:58:46 -04:00
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
2021-09-29 13:13:49 -04:00
got := tt.target.Oplog(tt.op)
2020-08-26 13:58:46 -04:00
assert.Equal(got, tt.want)
})
}
}