* database: add wt_public_id domain type * static-hosts: add migrations * Rename migrations * migrations: add description column and fix foreign keys * Add protobuf for static host * Add HostCatalog and HostSet * Add Options * Add foreign key constraint * Add package doc * Add copied scanner code * Add NewHostCatalog with tests * Override default table name for static host catalog * WIP: save static host catalog to database * WIP: Print db url for debugging * Add iam test helper function * Make gorm default to null for names and descriptions * Add foreign key constraint from host catalog to scope * Names are not needed for the test org and project * Avoid an ordering dependency in down migrations * Use wt_timestamp * Add check constraint on host address column * Add Host * Add HostSet * Add HostSetMember * Test a HostSetMember can be written to the database * Move the getters and setters for table name to a new file The TableName methods are needed by gorm and the SetTableName methods are for oplog. Moving them to a new file so related functionality is in the same file. * Add repository * Add NewRepository * Close connection before cleanup of database * Add comments to protobuf file and regenerate * Define repository methods for HostCatalog * Assign PublicId when resource is inserted into the database * Wrap a nil parameter error * Add oplog tickets for static host types * WIP: first cut at CreateCatalog * CreateHostCatalog: more tests * Detect unique constraint violation * Use const for PublicId prefixes * Use PostgreSQL error name instead of error code * Add TODO comments * Fix comments * Use PublicId not PublicID * Refactor: move error variables from host/static to db The error variables defined in host/static are generic and can be reused by other packages. * Create a new repository for each test * Validate CreateCatalog returns a new HostCatalog * Do not modify the HostCatalog passed in * Implement UpdateCatalog * Minor cleanup to make folds work * Implement LookupCatalog * Refactor: extract allocCatalog function * Add comment * Refactor: extract newCatalogMetadata function * Make error strings more consistent * Move database specific error check to internal/db package * Change signature of DeleteCatalog and update docs * Implement DeleteCatalog * Make error strings more consistent * Add examples * Update docs * Add package level doc and example for repository * No need for using reflect.DeepEqual * Remove redundant comment * Clarify ErrMultipleRecords results in the transaction being rolled back * Remove redundant error checks * Remove unnecessary check * Return early instead of using nested 'else' statements * Update and Delete must return a row count * catch err on defers (#82) Co-authored-by: Jim Lambert <jimlambrt@Jims-MBP-3.home> * Remove 'if assert' pattern * Use assert package in test helper function * Add test for updating a non existent catalog * Use a single assert when a specific error is expected * Use assert package when it clarifies the check * Catch errors in cleanup functions * Update comments in protobuf file * Update doc for UpdateCatalog to reflect the new behavior * Add common error values for field mask errors * Require a field mask when calling update methods * Add TODO for removing direct call to gorm * Export Public ID prefixes * Add db timestamp guards * Remove unused option Co-authored-by: Jim <jlambert@hashicorp.com> Co-authored-by: Jim Lambert <jimlambrt@Jims-MBP-3.home>
508 lines
9.9 KiB
Go
508 lines
9.9 KiB
Go
package static
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/watchtower/internal/db"
|
|
"github.com/hashicorp/watchtower/internal/host/static/store"
|
|
"github.com/hashicorp/watchtower/internal/iam"
|
|
"github.com/jinzhu/gorm"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestHostCatalog_New(t *testing.T) {
|
|
cleanup, conn, _ := db.TestSetup(t, "postgres")
|
|
defer func() {
|
|
if err := cleanup(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if err := conn.Close(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}()
|
|
|
|
_, prj := iam.TestScopes(t, conn)
|
|
|
|
type args struct {
|
|
scopeId string
|
|
opts []Option
|
|
}
|
|
|
|
var tests = []struct {
|
|
name string
|
|
args args
|
|
want *HostCatalog
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "blank-scopeId",
|
|
args: args{
|
|
scopeId: "",
|
|
},
|
|
want: nil,
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "valid-no-options",
|
|
args: args{
|
|
scopeId: prj.GetPublicId(),
|
|
},
|
|
want: &HostCatalog{
|
|
HostCatalog: &store.HostCatalog{
|
|
ScopeId: prj.GetPublicId(),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "valid-with-name",
|
|
args: args{
|
|
scopeId: prj.GetPublicId(),
|
|
opts: []Option{
|
|
WithName("test-name"),
|
|
},
|
|
},
|
|
want: &HostCatalog{
|
|
HostCatalog: &store.HostCatalog{
|
|
ScopeId: prj.GetPublicId(),
|
|
Name: "test-name",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "valid-with-description",
|
|
args: args{
|
|
scopeId: prj.GetPublicId(),
|
|
opts: []Option{
|
|
WithDescription("test-description"),
|
|
},
|
|
},
|
|
want: &HostCatalog{
|
|
HostCatalog: &store.HostCatalog{
|
|
ScopeId: prj.GetPublicId(),
|
|
Description: "test-description",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
got, err := NewHostCatalog(tt.args.scopeId, tt.args.opts...)
|
|
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()
|
|
assert.NoError(err)
|
|
|
|
tt.want.PublicId = id
|
|
got.PublicId = id
|
|
|
|
w := db.New(conn)
|
|
err2 := w.Create(context.Background(), got)
|
|
assert.NoError(err2)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func testCatalogs(t *testing.T, conn *gorm.DB, count int) []*HostCatalog {
|
|
t.Helper()
|
|
assert := assert.New(t)
|
|
_, prj := iam.TestScopes(t, conn)
|
|
var cats []*HostCatalog
|
|
for i := 0; i < count; i++ {
|
|
cat, err := NewHostCatalog(prj.GetPublicId())
|
|
assert.NoError(err)
|
|
assert.NotNil(cat)
|
|
id, err := newHostCatalogId()
|
|
assert.NoError(err)
|
|
assert.NotEmpty(id)
|
|
cat.PublicId = id
|
|
|
|
w := db.New(conn)
|
|
err2 := w.Create(context.Background(), cat)
|
|
assert.NoError(err2)
|
|
cats = append(cats, cat)
|
|
}
|
|
return cats
|
|
}
|
|
|
|
func testCatalog(t *testing.T, conn *gorm.DB) *HostCatalog {
|
|
t.Helper()
|
|
cats := testCatalogs(t, conn, 1)
|
|
return cats[0]
|
|
}
|
|
|
|
func TestHost_New(t *testing.T) {
|
|
cleanup, conn, _ := db.TestSetup(t, "postgres")
|
|
defer func() {
|
|
if err := cleanup(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if err := conn.Close(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}()
|
|
cat := testCatalog(t, conn)
|
|
|
|
conn.LogMode(false)
|
|
type args struct {
|
|
catalogId string
|
|
address string
|
|
opts []Option
|
|
}
|
|
|
|
var tests = []struct {
|
|
name string
|
|
args args
|
|
want *Host
|
|
wantCreateErr bool
|
|
wantWriteErr bool
|
|
}{
|
|
{
|
|
name: "blank-catalogId",
|
|
args: args{
|
|
catalogId: "",
|
|
address: "127.0.0.1",
|
|
},
|
|
want: nil,
|
|
wantCreateErr: true,
|
|
},
|
|
{
|
|
name: "blank-address",
|
|
args: args{
|
|
catalogId: cat.GetPublicId(),
|
|
address: "",
|
|
},
|
|
want: nil,
|
|
wantCreateErr: true,
|
|
},
|
|
{
|
|
name: "address-to-short",
|
|
args: args{
|
|
catalogId: cat.GetPublicId(),
|
|
address: "1234567",
|
|
},
|
|
want: &Host{
|
|
Host: &store.Host{
|
|
StaticHostCatalogId: cat.GetPublicId(),
|
|
Address: "1234567",
|
|
},
|
|
},
|
|
wantWriteErr: true,
|
|
},
|
|
{
|
|
name: "minimum-address",
|
|
args: args{
|
|
catalogId: cat.GetPublicId(),
|
|
address: "12345678",
|
|
},
|
|
want: &Host{
|
|
Host: &store.Host{
|
|
StaticHostCatalogId: cat.GetPublicId(),
|
|
Address: "12345678",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "valid-no-options",
|
|
args: args{
|
|
catalogId: cat.GetPublicId(),
|
|
address: "127.0.0.1",
|
|
},
|
|
want: &Host{
|
|
Host: &store.Host{
|
|
StaticHostCatalogId: cat.GetPublicId(),
|
|
Address: "127.0.0.1",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "valid-with-name",
|
|
args: args{
|
|
catalogId: cat.GetPublicId(),
|
|
address: "127.0.0.1",
|
|
opts: []Option{
|
|
WithName("test-name"),
|
|
},
|
|
},
|
|
want: &Host{
|
|
Host: &store.Host{
|
|
StaticHostCatalogId: cat.GetPublicId(),
|
|
Address: "127.0.0.1",
|
|
Name: "test-name",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "valid-with-description",
|
|
args: args{
|
|
catalogId: cat.GetPublicId(),
|
|
address: "127.0.0.1",
|
|
opts: []Option{
|
|
WithDescription("test-description"),
|
|
},
|
|
},
|
|
want: &Host{
|
|
Host: &store.Host{
|
|
StaticHostCatalogId: cat.GetPublicId(),
|
|
Address: "127.0.0.1",
|
|
Description: "test-description",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
got, err := NewHost(tt.args.catalogId, tt.args.address, tt.args.opts...)
|
|
if tt.wantCreateErr {
|
|
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 := newHostId()
|
|
assert.NoError(err)
|
|
|
|
tt.want.PublicId = id
|
|
got.PublicId = id
|
|
|
|
w := db.New(conn)
|
|
err2 := w.Create(context.Background(), got)
|
|
if tt.wantWriteErr {
|
|
assert.Error(err2)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHostSet_New(t *testing.T) {
|
|
cleanup, conn, _ := db.TestSetup(t, "postgres")
|
|
defer func() {
|
|
if err := cleanup(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if err := conn.Close(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}()
|
|
cat := testCatalog(t, conn)
|
|
|
|
conn.LogMode(false)
|
|
type args struct {
|
|
catalogId string
|
|
opts []Option
|
|
}
|
|
|
|
var tests = []struct {
|
|
name string
|
|
args args
|
|
want *HostSet
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "blank-catalogId",
|
|
args: args{
|
|
catalogId: "",
|
|
},
|
|
want: nil,
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "valid-no-options",
|
|
args: args{
|
|
catalogId: cat.GetPublicId(),
|
|
},
|
|
want: &HostSet{
|
|
HostSet: &store.HostSet{
|
|
StaticHostCatalogId: cat.GetPublicId(),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "valid-with-name",
|
|
args: args{
|
|
catalogId: cat.GetPublicId(),
|
|
opts: []Option{
|
|
WithName("test-name"),
|
|
},
|
|
},
|
|
want: &HostSet{
|
|
HostSet: &store.HostSet{
|
|
StaticHostCatalogId: cat.GetPublicId(),
|
|
Name: "test-name",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "valid-with-description",
|
|
args: args{
|
|
catalogId: cat.GetPublicId(),
|
|
opts: []Option{
|
|
WithDescription("test-description"),
|
|
},
|
|
},
|
|
want: &HostSet{
|
|
HostSet: &store.HostSet{
|
|
StaticHostCatalogId: cat.GetPublicId(),
|
|
Description: "test-description",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
got, err := NewHostSet(tt.args.catalogId, tt.args.opts...)
|
|
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 := newHostSetId()
|
|
assert.NoError(err)
|
|
|
|
tt.want.PublicId = id
|
|
got.PublicId = id
|
|
|
|
w := db.New(conn)
|
|
err2 := w.Create(context.Background(), got)
|
|
assert.NoError(err2)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func testHosts(t *testing.T, conn *gorm.DB, catalogId string, count int) []*Host {
|
|
t.Helper()
|
|
assert := assert.New(t)
|
|
var hosts []*Host
|
|
|
|
for i := 0; i < count; i++ {
|
|
host, err := NewHost(catalogId, fmt.Sprintf("%s-%d", catalogId, i))
|
|
assert.NoError(err)
|
|
assert.NotNil(host)
|
|
|
|
id, err := newHostCatalogId()
|
|
assert.NoError(err)
|
|
assert.NotEmpty(id)
|
|
host.PublicId = id
|
|
|
|
w := db.New(conn)
|
|
err2 := w.Create(context.Background(), host)
|
|
assert.NoError(err2)
|
|
hosts = append(hosts, host)
|
|
}
|
|
return hosts
|
|
}
|
|
|
|
func testSets(t *testing.T, conn *gorm.DB, catalogId string, count int) []*HostSet {
|
|
t.Helper()
|
|
assert := assert.New(t)
|
|
var sets []*HostSet
|
|
|
|
for i := 0; i < count; i++ {
|
|
set, err := NewHostSet(catalogId)
|
|
assert.NoError(err)
|
|
assert.NotNil(set)
|
|
id, err := newHostSetId()
|
|
assert.NoError(err)
|
|
assert.NotEmpty(id)
|
|
set.PublicId = id
|
|
|
|
w := db.New(conn)
|
|
err2 := w.Create(context.Background(), set)
|
|
assert.NoError(err2)
|
|
sets = append(sets, set)
|
|
}
|
|
return sets
|
|
}
|
|
|
|
func TestHostSetMember_New(t *testing.T) {
|
|
cleanup, conn, _ := db.TestSetup(t, "postgres")
|
|
defer func() {
|
|
if err := cleanup(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if err := conn.Close(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}()
|
|
conn.LogMode(false)
|
|
|
|
cats := testCatalogs(t, conn, 2)
|
|
|
|
blueCat := cats[0]
|
|
blueSets := testSets(t, conn, blueCat.GetPublicId(), 2)
|
|
blueHosts := testHosts(t, conn, blueCat.GetPublicId(), 2)
|
|
|
|
// TODO(mgaffney) 05/2020:
|
|
// these will be needed when the repository code is done
|
|
// greenCat := cats[1]
|
|
// greenSets := testSets(t, conn, greenCat.GetPublicId(), 2)
|
|
// greenHosts := testHosts(t, conn, greenCat.GetPublicId(), 2)
|
|
|
|
var tests = []struct {
|
|
name string
|
|
set *HostSet
|
|
host *Host
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "valid-host-in-set",
|
|
set: blueSets[0],
|
|
host: blueHosts[0],
|
|
},
|
|
// {
|
|
// name: "invalid-diff-catalogs",
|
|
// set: greenSets[0],
|
|
// host: blueHosts[0],
|
|
// wantErr: true,
|
|
// },
|
|
}
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
got, err := NewHostSetMember(tt.set.PublicId, tt.host.PublicId)
|
|
if tt.wantErr {
|
|
assert.Error(err)
|
|
assert.Nil(got)
|
|
} else {
|
|
assert.NoError(err)
|
|
if assert.NotNil(got) {
|
|
w := db.New(conn)
|
|
err2 := w.Create(context.Background(), got)
|
|
assert.NoError(err2)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|