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

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

239 lines
6.6 KiB
Go
Raw Normal View History

2020-08-26 13:58:46 -04:00
// Copyright (c) HashiCorp, Inc.
2023-02-02 17:15:17 -08:00
// SPDX-License-Identifier: BUSL-1.1
2021-09-29 13:13:49 -04:00
package tcp_test
2020-08-26 13:58:46 -04:00
import (
"context"
"testing"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/db/timestamp"
"github.com/hashicorp/boundary/internal/host/static"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/kms"
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"
"google.golang.org/protobuf/types/known/timestamppb"
)
func TestTarget_ImmutableFields(t *testing.T) {
t.Parallel()
conn, _ := db.TestSetup(t, "postgres")
wrapper := db.TestWrapper(t)
rw := db.New(conn)
ts := timestamp.Timestamp{Timestamp: &timestamppb.Timestamp{Seconds: 0, Nanos: 0}}
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
2021-11-04 11:37:32 -04:00
ctx := context.Background()
new := tcp.TestTarget(ctx, t, conn, proj.PublicId, tcp.TestId(t))
2020-08-26 13:58:46 -04:00
tests := []struct {
2020-08-26 13:58:46 -04:00
name string
2021-09-29 13:13:49 -04:00
update *tcp.Target
2020-08-26 13:58:46 -04:00
fieldMask []string
}{
{
name: "public_id",
2021-09-29 13:13:49 -04:00
update: func() *tcp.Target {
target := new.Clone().(*tcp.Target)
2020-08-26 13:58:46 -04:00
target.PublicId = "p_thisIsNotAValidId"
return target
}(),
fieldMask: []string{"PublicId"},
},
{
name: "create time",
2021-09-29 13:13:49 -04:00
update: func() *tcp.Target {
target := new.Clone().(*tcp.Target)
2020-08-26 13:58:46 -04:00
target.CreateTime = &ts
return target
}(),
fieldMask: []string{"CreateTime"},
},
{
2022-07-20 16:24:46 -07:00
name: "project_id",
2021-09-29 13:13:49 -04:00
update: func() *tcp.Target {
target := new.Clone().(*tcp.Target)
2022-07-20 16:24:46 -07:00
target.ProjectId = "o_thisIsNotAValidId"
2020-08-26 13:58:46 -04:00
return target
}(),
2022-07-20 16:24:46 -07:00
fieldMask: []string{"ProjectId"},
2020-08-26 13:58:46 -04:00
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
orig := new.Clone()
2021-09-29 13:13:49 -04:00
orig.(*tcp.Target).SetTableName("target")
2020-08-26 13:58:46 -04:00
err := rw.LookupById(context.Background(), orig)
require.NoError(err)
tt.update.SetTableName("target")
rowsUpdated, err := rw.Update(context.Background(), tt.update, tt.fieldMask, nil, db.WithSkipVetForWrite(true))
2020-08-26 13:58:46 -04:00
require.Error(err)
assert.Equal(0, rowsUpdated)
after := new.Clone()
2021-09-29 13:13:49 -04:00
after.(*tcp.Target).SetTableName("target")
2020-08-26 13:58:46 -04:00
err = rw.LookupById(context.Background(), after)
require.NoError(err)
2021-09-29 13:13:49 -04:00
assert.True(proto.Equal(orig.(*tcp.Target), after.(*tcp.Target)))
2020-08-26 13:58:46 -04:00
})
}
}
func TestTcpTarget_ImmutableFields(t *testing.T) {
t.Parallel()
conn, _ := db.TestSetup(t, "postgres")
wrapper := db.TestWrapper(t)
rw := db.New(conn)
ts := timestamp.Timestamp{Timestamp: &timestamppb.Timestamp{Seconds: 0, Nanos: 0}}
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
2021-11-04 11:37:32 -04:00
ctx := context.Background()
new := tcp.TestTarget(ctx, t, conn, proj.PublicId, tcp.TestId(t))
2020-08-26 13:58:46 -04:00
tests := []struct {
2020-08-26 13:58:46 -04:00
name string
2021-09-29 13:13:49 -04:00
update *tcp.Target
2020-08-26 13:58:46 -04:00
fieldMask []string
}{
{
name: "public_id",
2021-09-29 13:13:49 -04:00
update: func() *tcp.Target {
target := new.Clone().(*tcp.Target)
2020-08-26 13:58:46 -04:00
target.PublicId = "p_thisIsNotAValidId"
return target
}(),
fieldMask: []string{"PublicId"},
},
{
name: "create time",
2021-09-29 13:13:49 -04:00
update: func() *tcp.Target {
target := new.Clone().(*tcp.Target)
2020-08-26 13:58:46 -04:00
target.CreateTime = &ts
return target
}(),
fieldMask: []string{"CreateTime"},
},
{
2022-07-20 16:24:46 -07:00
name: "project_id",
2021-09-29 13:13:49 -04:00
update: func() *tcp.Target {
target := new.Clone().(*tcp.Target)
2022-07-20 16:24:46 -07:00
target.ProjectId = "o_thisIsNotAValidId"
2020-08-26 13:58:46 -04:00
return target
}(),
2022-07-20 16:24:46 -07:00
fieldMask: []string{"ProjectId"},
2020-08-26 13:58:46 -04:00
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
orig := new.Clone()
err := rw.LookupById(context.Background(), orig)
require.NoError(err)
rowsUpdated, err := rw.Update(context.Background(), tt.update, tt.fieldMask, nil, db.WithSkipVetForWrite(true))
2020-08-26 13:58:46 -04:00
require.Error(err)
assert.Equal(0, rowsUpdated)
after := new.Clone()
err = rw.LookupById(context.Background(), after)
require.NoError(err)
2021-09-29 13:13:49 -04:00
assert.True(proto.Equal(orig.(*tcp.Target), after.(*tcp.Target)))
2020-08-26 13:58:46 -04:00
})
}
}
func TestTargetHostSet_ImmutableFields(t *testing.T) {
t.Parallel()
conn, _ := db.TestSetup(t, "postgres")
wrapper := db.TestWrapper(t)
testKms := kms.TestKms(t, conn, wrapper)
ctx := context.Background()
2020-08-26 13:58:46 -04:00
rw := db.New(conn)
repo, err := target.NewRepository(ctx, rw, rw, testKms)
2020-08-26 13:58:46 -04:00
require.NoError(t, err)
ts := timestamp.Timestamp{Timestamp: &timestamppb.Timestamp{Seconds: 0, Nanos: 0}}
_, proj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
2021-11-04 11:37:32 -04:00
projTarget := tcp.TestTarget(ctx, t, conn, proj.PublicId, tcp.TestId(t))
2020-08-26 13:58:46 -04:00
testCats := static.TestCatalogs(t, conn, proj.PublicId, 1)
hsets := static.TestSets(t, conn, testCats[0].GetPublicId(), 2)
require.Equal(t, 2, len(hsets))
2021-11-04 11:37:32 -04:00
updateTarget := tcp.TestTarget(ctx, t, conn, proj.PublicId, tcp.TestId(t))
2020-08-26 13:58:46 -04:00
updateHset := hsets[1]
2023-03-30 15:19:30 -04:00
gotTarget, err := repo.AddTargetHostSources(ctx, projTarget.GetPublicId(), 1, []string{hsets[0].PublicId})
gotHostSources := gotTarget.GetHostSources()
2020-08-26 13:58:46 -04:00
require.NoError(t, err)
require.Equal(t, 1, len(gotHostSources))
new, err := target.NewTargetHostSet(ctx, projTarget.GetPublicId(), gotHostSources[0].Id())
2020-08-26 13:58:46 -04:00
require.NoError(t, err)
tests := []struct {
2020-08-26 13:58:46 -04:00
name string
2021-09-29 13:13:49 -04:00
update *target.TargetHostSet
2020-08-26 13:58:46 -04:00
fieldMask []string
}{
{
name: "target_id",
2021-09-29 13:13:49 -04:00
update: func() *target.TargetHostSet {
target := new.Clone().(*target.TargetHostSet)
2021-11-04 11:37:32 -04:00
target.TargetId = updateTarget.GetPublicId()
2020-08-26 13:58:46 -04:00
return target
}(),
fieldMask: []string{"TargetId"},
},
{
name: "create time",
2021-09-29 13:13:49 -04:00
update: func() *target.TargetHostSet {
target := new.Clone().(*target.TargetHostSet)
2020-08-26 13:58:46 -04:00
target.CreateTime = &ts
return target
}(),
fieldMask: []string{"CreateTime"},
},
{
name: "host_set_id",
2021-09-29 13:13:49 -04:00
update: func() *target.TargetHostSet {
target := new.Clone().(*target.TargetHostSet)
2020-08-26 13:58:46 -04:00
target.HostSetId = updateHset.PublicId
return target
}(),
fieldMask: []string{"HostSetId"},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
orig := new.Clone()
2022-10-25 18:29:38 -05:00
err := rw.LookupWhere(context.Background(), orig, "target_id = ? and host_set_id = ?", []any{new.TargetId, new.HostSetId})
2020-08-26 13:58:46 -04:00
require.NoError(err)
rowsUpdated, err := rw.Update(context.Background(), tt.update, tt.fieldMask, nil, db.WithSkipVetForWrite(true))
2020-08-26 13:58:46 -04:00
require.Error(err)
assert.Equal(0, rowsUpdated)
after := new.Clone()
2022-10-25 18:29:38 -05:00
err = rw.LookupWhere(context.Background(), after, "target_id = ? and host_set_id = ?", []any{new.TargetId, new.HostSetId})
2020-08-26 13:58:46 -04:00
require.NoError(err)
2021-09-29 13:13:49 -04:00
assert.True(proto.Equal(orig.(*target.TargetHostSet), after.(*target.TargetHostSet)))
2020-08-26 13:58:46 -04:00
})
}
}