Files
threshold/internal/host/static/host_catalog_test.go
T

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

155 lines
3.1 KiB
Go
Raw Normal View History

2020-08-20 13:56:52 -04:00
// Copyright (c) HashiCorp, Inc.
2023-02-02 17:15:17 -08:00
// SPDX-License-Identifier: BUSL-1.1
2020-08-13 14:00:15 -04:00
package static
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2020-08-20 13:56:52 -04:00
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/host/static/store"
"github.com/hashicorp/boundary/internal/iam"
2020-08-13 14:00:15 -04:00
)
func TestHostCatalog_New(t *testing.T) {
ctx := context.Background()
2020-08-13 14:00:15 -04:00
conn, _ := db.TestSetup(t, "postgres")
2020-08-19 16:47:18 -04:00
wrapper := db.TestWrapper(t)
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
2020-08-13 14:00:15 -04:00
type args struct {
2022-07-20 16:24:46 -07:00
projectId string
opts []Option
2020-08-13 14:00:15 -04:00
}
tests := []struct {
2020-08-13 14:00:15 -04:00
name string
args args
want *HostCatalog
wantErr bool
}{
{
2022-07-20 16:24:46 -07:00
name: "blank-projectId",
2020-08-13 14:00:15 -04:00
args: args{
2022-07-20 16:24:46 -07:00
projectId: "",
2020-08-13 14:00:15 -04:00
},
want: nil,
wantErr: true,
},
{
name: "valid-no-options",
args: args{
2022-07-20 16:24:46 -07:00
projectId: prj.GetPublicId(),
2020-08-13 14:00:15 -04:00
},
want: &HostCatalog{
HostCatalog: &store.HostCatalog{
2022-07-20 16:24:46 -07:00
ProjectId: prj.GetPublicId(),
2020-08-13 14:00:15 -04:00
},
},
},
{
name: "valid-with-name",
args: args{
2022-07-20 16:24:46 -07:00
projectId: prj.GetPublicId(),
2020-08-13 14:00:15 -04:00
opts: []Option{
WithName("test-name"),
},
},
want: &HostCatalog{
HostCatalog: &store.HostCatalog{
2022-07-20 16:24:46 -07:00
ProjectId: prj.GetPublicId(),
Name: "test-name",
2020-08-13 14:00:15 -04:00
},
},
},
{
name: "valid-with-description",
args: args{
2022-07-20 16:24:46 -07:00
projectId: prj.GetPublicId(),
2020-08-13 14:00:15 -04:00
opts: []Option{
WithDescription("test-description"),
},
},
want: &HostCatalog{
HostCatalog: &store.HostCatalog{
2022-07-20 16:24:46 -07:00
ProjectId: prj.GetPublicId(),
2020-08-13 14:00:15 -04:00
Description: "test-description",
},
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
got, err := NewHostCatalog(ctx, tt.args.projectId, tt.args.opts...)
2020-08-13 14:00:15 -04:00
if tt.wantErr {
assert.Error(err)
assert.Nil(got)
} else {
assert.NoError(err)
if assert.NotNil(got) {
assert.Emptyf(got.PublicId, "PublicId set")
assert.Equal(tt.want, got)
id, err := newHostCatalogId(ctx)
2020-08-13 14:00:15 -04:00
assert.NoError(err)
tt.want.PublicId = id
got.PublicId = id
w := db.New(conn)
err2 := w.Create(ctx, got)
2020-08-13 14:00:15 -04:00
assert.NoError(err2)
}
}
})
}
}
func testCatalog(t *testing.T, conn *db.DB, scopeId string) *HostCatalog {
2020-08-13 14:00:15 -04:00
t.Helper()
2020-08-20 08:20:26 -07:00
cats := TestCatalogs(t, conn, scopeId, 1)
2020-08-13 14:00:15 -04:00
return cats[0]
}
func TestHostCatalog_SetTableName(t *testing.T) {
defaultTableName := "static_host_catalog"
tests := []struct {
name string
initialName string
setNameTo string
want string
}{
{
name: "new-name",
initialName: "",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
initialName: "initial",
setNameTo: "",
want: defaultTableName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := allocCatalog()
require.Equal(defaultTableName, def.TableName())
s := &HostCatalog{
HostCatalog: &store.HostCatalog{},
tableName: tt.initialName,
}
s.SetTableName(tt.setNameTo)
assert.Equal(tt.want, s.TableName())
})
}
}