Files
threshold/internal/target/tcp/repository_test.go
T
Jeff Mitchell a9e372930d Revert 0bf60996c5 (#3990)
Post-merge some issues were discovered with the ways typing is used for
proto reflection. This will be reworked without moving the subtypes
around.
2023-11-02 16:03:47 -04:00

313 lines
7.9 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package tcp_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/hashicorp/boundary/globals"
"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/perms"
"github.com/hashicorp/boundary/internal/target"
"github.com/hashicorp/boundary/internal/target/tcp"
"github.com/hashicorp/boundary/internal/types/action"
"github.com/hashicorp/boundary/internal/types/resource"
"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"
ctx := context.Background()
_, _, err := iamRepo.UpdateScope(ctx, proj, 1, []string{"name"})
require.NoError(t, err)
rw := db.New(conn)
repo, err := target.NewRepository(ctx, rw, rw, testKms)
require.NoError(t, err)
tgt := tcp.TestTarget(ctx, t, conn, proj.PublicId, "target-name")
tests := []struct {
testName string
id string
name string
projectId string
projectName string
wantErr bool
}{
{
testName: "id",
id: tgt.GetPublicId(),
wantErr: false,
},
{
testName: "name only",
name: tgt.GetName(),
wantErr: true,
},
{
testName: "project id only",
projectId: proj.PublicId,
wantErr: true,
},
{
testName: "project name only",
projectName: proj.Name,
wantErr: true,
},
{
testName: "project name and id",
projectId: proj.PublicId,
projectName: proj.Name,
wantErr: true,
},
{
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,
},
{
testName: "name and project id",
name: tgt.GetName(),
projectId: proj.PublicId,
wantErr: false,
},
{
testName: "id and name",
id: tgt.GetPublicId(),
name: tgt.GetName(),
projectId: proj.PublicId,
wantErr: true,
},
{
testName: "id and project name",
id: tgt.GetPublicId(),
projectName: proj.Name,
wantErr: true,
},
{
testName: "id and project id",
id: tgt.GetPublicId(),
projectId: proj.PublicId,
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))
}
if tt.projectId != "" {
opts = append(opts, target.WithProjectId(tt.projectId))
}
if tt.projectName != "" {
opts = append(opts, target.WithProjectName(tt.projectName))
}
got, err := repo.LookupTarget(ctx, id, opts...)
if tt.wantErr {
require.Error(err)
return
}
require.NoError(err)
assert.Equal(tgt.GetPublicId(), got.GetPublicId())
})
}
}
func TestRepository_ListRoles_Multiple_Scopes(t *testing.T) {
t.Parallel()
ctx := context.Background()
conn, _ := db.TestSetup(t, "postgres")
wrapper := db.TestWrapper(t)
testKms := kms.TestKms(t, conn, wrapper)
iamRepo := iam.TestRepo(t, conn, wrapper)
_, proj1 := iam.TestScopes(t, iamRepo)
_, proj2 := iam.TestScopes(t, iamRepo)
db.TestDeleteWhere(t, conn, tcp.NewTestTarget(ctx, ""), "1=1")
const numPerScope = 10
var total int
for i := 0; i < numPerScope; i++ {
tcp.TestTarget(ctx, t, conn, proj1.GetPublicId(), fmt.Sprintf("proj1-%d", i))
total++
tcp.TestTarget(ctx, t, conn, proj2.GetPublicId(), fmt.Sprintf("proj2-%d", i))
total++
}
rw := db.New(conn)
repo, err := target.NewRepository(ctx, rw, rw, testKms,
target.WithPermissions([]perms.Permission{
{
ScopeId: proj1.PublicId,
Resource: resource.Target,
Action: action.List,
All: true,
},
{
ScopeId: proj2.PublicId,
Resource: resource.Target,
Action: action.List,
All: true,
},
}),
)
require.NoError(t, err)
got, err := repo.ListTargets(ctx)
require.NoError(t, err)
assert.Equal(t, total, len(got))
}
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)
ctx := context.Background()
repo, err := target.NewRepository(ctx, rw, rw, testKms)
require.NoError(t, err)
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{
target: tcp.TestTarget(ctx, t, conn, proj.PublicId, "valid"),
},
wantRowsDeleted: 1,
wantErr: false,
},
{
name: "no-public-id",
args: args{
target: func() target.Target {
tar, _ := target.New(ctx, tcp.Subtype, proj.PublicId)
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)
require.NoError(t, err)
tar, _ := target.New(ctx, tcp.Subtype, proj.PublicId)
tar.SetPublicId(ctx, id)
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)
deletedRows, err := repo.DeleteTarget(ctx, tt.args.target.GetPublicId(), tt.args.opt...)
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)
foundGroup, err := repo.LookupTarget(ctx, tt.args.target.GetPublicId())
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)
})
}
}
func TestRepository_ListRoles_Above_Default_Count(t *testing.T) {
t.Parallel()
ctx := context.Background()
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)
numToCreate := db.DefaultLimit + 5
var total int
for i := 0; i < numToCreate; i++ {
tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), fmt.Sprintf("proj1-%d", i), target.WithAddress("1.2.3.4"))
total++
}
require.Equal(t, numToCreate, total)
rw := db.New(conn)
repo, err := target.NewRepository(ctx, rw, rw, testKms,
target.WithPermissions([]perms.Permission{
{
ScopeId: proj.PublicId,
Resource: resource.Target,
Action: action.List,
All: true,
},
}))
require.NoError(t, err)
got, err := repo.ListTargets(ctx, target.WithLimit(-1))
require.NoError(t, err)
assert.Equal(t, total, len(got))
for _, tar := range got {
assert.Equal(t, "1.2.3.4", tar.GetAddress())
}
}