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

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

223 lines
5.6 KiB
Go
Raw Normal View History

2021-09-29 13:13:49 -04:00
// Copyright (c) HashiCorp, Inc.
// 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
import (
"context"
"testing"
"time"
"github.com/hashicorp/boundary/globals"
2021-09-29 13:13:49 -04:00
"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/oplog"
"github.com/hashicorp/boundary/internal/target"
"github.com/hashicorp/boundary/internal/target/tcp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRepository_LookupTarget(t *testing.T) {
t.Parallel()
conn, _ := db.TestSetup(t, "postgres")
wrapper := db.TestWrapper(t)
testKms := kms.TestKms(t, conn, wrapper)
iamRepo := iam.TestRepo(t, conn, wrapper)
_, proj := iam.TestScopes(t, iamRepo)
proj.Name = "project-name"
2021-11-04 11:37:32 -04:00
ctx := context.Background()
_, _, err := iamRepo.UpdateScope(ctx, proj, 1, []string{"name"})
2021-09-29 13:13:49 -04:00
require.NoError(t, err)
rw := db.New(conn)
repo, err := target.NewRepository(ctx, rw, rw, testKms)
2021-09-29 13:13:49 -04:00
require.NoError(t, err)
2021-11-04 11:37:32 -04:00
tgt := tcp.TestTarget(ctx, t, conn, proj.PublicId, "target-name")
2021-09-29 13:13:49 -04:00
tests := []struct {
2022-07-20 16:24:46 -07:00
testName string
id string
name string
projectId string
projectName string
wantErr bool
2021-09-29 13:13:49 -04:00
}{
{
testName: "id",
2021-11-04 11:37:32 -04:00
id: tgt.GetPublicId(),
2021-09-29 13:13:49 -04:00
wantErr: false,
},
{
testName: "name only",
2021-11-04 11:37:32 -04:00
name: tgt.GetName(),
2021-09-29 13:13:49 -04:00
wantErr: true,
},
{
2022-07-20 16:24:46 -07:00
testName: "project id only",
projectId: proj.PublicId,
wantErr: true,
2021-09-29 13:13:49 -04:00
},
{
2022-07-20 16:24:46 -07:00
testName: "project name only",
projectName: proj.Name,
wantErr: true,
2021-09-29 13:13:49 -04:00
},
{
2022-07-20 16:24:46 -07:00
testName: "project name and id",
projectId: proj.PublicId,
projectName: proj.Name,
wantErr: true,
2021-09-29 13:13:49 -04:00
},
{
2022-07-20 16:24:46 -07:00
testName: "everything",
name: tgt.GetName(),
projectId: proj.PublicId,
projectName: proj.Name,
wantErr: true,
},
{
testName: "name and project name",
name: tgt.GetName(),
projectName: proj.Name,
wantErr: false,
2021-09-29 13:13:49 -04:00
},
{
2022-07-20 16:24:46 -07:00
testName: "name and project id",
2021-11-04 11:37:32 -04:00
name: tgt.GetName(),
2022-07-20 16:24:46 -07:00
projectId: proj.PublicId,
2021-09-29 13:13:49 -04:00
wantErr: false,
},
{
2022-07-20 16:24:46 -07:00
testName: "id and name",
id: tgt.GetPublicId(),
name: tgt.GetName(),
projectId: proj.PublicId,
wantErr: true,
2021-09-29 13:13:49 -04:00
},
{
2022-07-20 16:24:46 -07:00
testName: "id and project name",
id: tgt.GetPublicId(),
projectName: proj.Name,
wantErr: true,
2021-09-29 13:13:49 -04:00
},
{
2022-07-20 16:24:46 -07:00
testName: "id and project id",
2021-11-04 11:37:32 -04:00
id: tgt.GetPublicId(),
2022-07-20 16:24:46 -07:00
projectId: proj.PublicId,
2021-09-29 13:13:49 -04:00
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
id := tt.id
if tt.name != "" && tt.id == "" {
id = tt.name
}
var opts []target.Option
if tt.name != "" {
opts = append(opts, target.WithName(tt.name))
}
2022-07-20 16:24:46 -07:00
if tt.projectId != "" {
opts = append(opts, target.WithProjectId(tt.projectId))
2021-09-29 13:13:49 -04:00
}
2022-07-20 16:24:46 -07:00
if tt.projectName != "" {
opts = append(opts, target.WithProjectName(tt.projectName))
2021-09-29 13:13:49 -04:00
}
2023-03-30 15:19:30 -04:00
got, err := repo.LookupTarget(ctx, id, opts...)
2021-09-29 13:13:49 -04:00
if tt.wantErr {
require.Error(err)
return
}
require.NoError(err)
2021-11-04 11:37:32 -04:00
assert.Equal(tgt.GetPublicId(), got.GetPublicId())
2021-09-29 13:13:49 -04:00
})
}
}
func TestRepository_DeleteTarget(t *testing.T) {
t.Parallel()
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
wrapper := db.TestWrapper(t)
testKms := kms.TestKms(t, conn, wrapper)
iamRepo := iam.TestRepo(t, conn, wrapper)
_, proj := iam.TestScopes(t, iamRepo)
2021-11-04 11:37:32 -04:00
ctx := context.Background()
repo, err := target.NewRepository(ctx, rw, rw, testKms)
require.NoError(t, err)
2021-09-29 13:13:49 -04:00
type args struct {
target target.Target
opt []target.Option
}
tests := []struct {
name string
args args
wantRowsDeleted int
wantErr bool
wantErrMsg string
}{
{
name: "valid",
args: args{
2021-11-04 11:37:32 -04:00
target: tcp.TestTarget(ctx, t, conn, proj.PublicId, "valid"),
2021-09-29 13:13:49 -04:00
},
wantRowsDeleted: 1,
wantErr: false,
},
{
name: "no-public-id",
args: args{
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
return tar
}(),
},
wantRowsDeleted: 0,
wantErr: true,
wantErrMsg: "target.(Repository).DeleteTarget: missing public id: parameter violation: error #100",
},
{
name: "not-found",
args: args{
target: func() target.Target {
id, err := db.NewPublicId(ctx, globals.TcpTargetPrefix)
2021-09-29 13:13:49 -04:00
require.NoError(t, err)
2023-11-02 16:03:47 -04:00
tar, _ := target.New(ctx, tcp.Subtype, proj.PublicId)
2021-11-04 11:37:32 -04:00
tar.SetPublicId(ctx, id)
2021-09-29 13:13:49 -04:00
return tar
}(),
},
wantRowsDeleted: 0,
wantErr: true,
wantErrMsg: "db.LookupById: record not found, search issue: error #1100",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
2021-11-04 11:37:32 -04:00
deletedRows, err := repo.DeleteTarget(ctx, tt.args.target.GetPublicId(), tt.args.opt...)
2021-09-29 13:13:49 -04:00
if tt.wantErr {
assert.Error(err)
assert.Equal(0, deletedRows)
assert.Contains(err.Error(), tt.wantErrMsg)
err = db.TestVerifyOplog(t, rw, tt.args.target.GetPublicId(), db.WithOperation(oplog.OpType_OP_TYPE_DELETE), db.WithCreateNotBefore(10*time.Second))
assert.Error(err)
assert.True(errors.IsNotFoundError(err))
return
}
assert.NoError(err)
assert.Equal(tt.wantRowsDeleted, deletedRows)
2023-03-30 15:19:30 -04:00
foundGroup, err := repo.LookupTarget(ctx, tt.args.target.GetPublicId())
2021-09-29 13:13:49 -04:00
assert.NoError(err)
assert.Nil(foundGroup)
err = db.TestVerifyOplog(t, rw, tt.args.target.GetPublicId(), db.WithOperation(oplog.OpType_OP_TYPE_DELETE), db.WithCreateNotBefore(10*time.Second))
assert.NoError(err)
})
}
}