2021-09-10 09:31:27 -04:00
|
|
|
package oss_test
|
2021-04-08 20:54:32 -04:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/boundary/internal/auth/password"
|
|
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
2021-09-30 09:17:27 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/db/common"
|
2021-04-08 20:54:32 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/db/schema"
|
|
|
|
|
"github.com/hashicorp/boundary/internal/iam"
|
2021-07-30 08:49:56 -04:00
|
|
|
"github.com/hashicorp/boundary/testing/dbtest"
|
2021-09-30 08:47:34 -04:00
|
|
|
|
2021-04-08 20:54:32 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2021-09-10 09:31:27 -04:00
|
|
|
// Tests migration:
|
|
|
|
|
// migrations/oss/2/07_iam.up.sql
|
2021-04-08 20:54:32 -04:00
|
|
|
func Test_PrimaryAuthMethodChanges(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2021-04-14 11:47:21 -04:00
|
|
|
const priorMigration = 2006
|
|
|
|
|
const primaryAuthMethodMigration = 2007
|
2021-04-08 20:54:32 -04:00
|
|
|
t.Run("migrate-store", func(t *testing.T) {
|
|
|
|
|
// this is a sequential test which relies on:
|
|
|
|
|
// 1) initializing the db using a migration up to the "priorMigration"
|
|
|
|
|
//
|
|
|
|
|
// 2) seeding the database with some test scopes and auth methods
|
|
|
|
|
//
|
|
|
|
|
// 3) running the migrations that sets the primary auth methods for
|
|
|
|
|
// existing scopes which only have one auth method.
|
|
|
|
|
//
|
|
|
|
|
// 4) asserting some bits about the state of the db.
|
|
|
|
|
assert, require := assert.New(t), require.New(t)
|
2021-07-30 08:49:56 -04:00
|
|
|
dialect := dbtest.Postgres
|
2021-04-08 20:54:32 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
2021-07-30 08:49:56 -04:00
|
|
|
c, u, _, err := dbtest.StartUsingTemplate(dialect, dbtest.WithTemplate(dbtest.Template1))
|
2021-04-08 20:54:32 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
require.NoError(c())
|
|
|
|
|
})
|
2021-09-30 09:17:27 -04:00
|
|
|
d, err := common.SqlOpen(dialect, u)
|
2021-04-08 20:54:32 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
|
|
// migration to the prior migration (before the one we want to test)
|
2021-09-10 09:31:27 -04:00
|
|
|
m, err := schema.NewManager(ctx, schema.Dialect(dialect), d, schema.WithEditions(
|
|
|
|
|
schema.TestCreatePartialEditions(schema.Dialect(dialect), schema.PartialEditions{"oss": priorMigration}),
|
|
|
|
|
))
|
2021-04-08 20:54:32 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
2021-09-10 09:31:27 -04:00
|
|
|
assert.NoError(m.ApplyMigrations(ctx))
|
2021-04-08 20:54:32 -04:00
|
|
|
state, err := m.CurrentState(ctx)
|
|
|
|
|
require.NoError(err)
|
2021-09-10 09:31:27 -04:00
|
|
|
want := &schema.State{
|
|
|
|
|
Initialized: true,
|
|
|
|
|
Editions: []schema.EditionState{
|
|
|
|
|
{
|
|
|
|
|
Name: "oss",
|
|
|
|
|
BinarySchemaVersion: priorMigration,
|
|
|
|
|
DatabaseSchemaVersion: priorMigration,
|
|
|
|
|
DatabaseSchemaState: schema.Equal,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
assert.Equal(want, state)
|
2021-04-08 20:54:32 -04:00
|
|
|
|
|
|
|
|
// okay, now we can seed the database with test data
|
2021-09-30 08:47:34 -04:00
|
|
|
dbType, err := db.StringToDbType(dialect)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
conn, err := db.Open(dbType, u)
|
2021-04-08 20:54:32 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
rootWrapper := db.TestWrapper(t)
|
|
|
|
|
iamRepo := iam.TestRepo(t, conn, rootWrapper)
|
|
|
|
|
org, _ := iam.TestScopes(t, iamRepo)
|
|
|
|
|
_ = password.TestAuthMethods(t, conn, org.PublicId, 3)
|
|
|
|
|
org2, _ := iam.TestScopes(t, iamRepo)
|
|
|
|
|
_ = password.TestAuthMethods(t, conn, org2.PublicId, 2)
|
|
|
|
|
|
|
|
|
|
org3, _ := iam.TestScopes(t, iamRepo)
|
|
|
|
|
org3AuthMethods := password.TestAuthMethods(t, conn, org3.PublicId, 1)
|
|
|
|
|
org4, _ := iam.TestScopes(t, iamRepo)
|
|
|
|
|
org4AuthMethods := password.TestAuthMethods(t, conn, org4.PublicId, 1)
|
|
|
|
|
|
|
|
|
|
// now we're ready for the migration we want to test.
|
2021-09-10 09:31:27 -04:00
|
|
|
m, err = schema.NewManager(ctx, schema.Dialect(dialect), d, schema.WithEditions(
|
|
|
|
|
schema.TestCreatePartialEditions(schema.Dialect(dialect), schema.PartialEditions{"oss": primaryAuthMethodMigration}),
|
|
|
|
|
))
|
2021-04-08 20:54:32 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
2021-09-10 09:31:27 -04:00
|
|
|
assert.NoError(m.ApplyMigrations(ctx))
|
2021-04-08 20:54:32 -04:00
|
|
|
state, err = m.CurrentState(ctx)
|
|
|
|
|
require.NoError(err)
|
2021-09-10 09:31:27 -04:00
|
|
|
want = &schema.State{
|
|
|
|
|
Initialized: true,
|
|
|
|
|
Editions: []schema.EditionState{
|
|
|
|
|
{
|
|
|
|
|
Name: "oss",
|
|
|
|
|
BinarySchemaVersion: primaryAuthMethodMigration,
|
|
|
|
|
DatabaseSchemaVersion: primaryAuthMethodMigration,
|
|
|
|
|
DatabaseSchemaState: schema.Equal,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
assert.Equal(want, state)
|
2021-04-08 20:54:32 -04:00
|
|
|
|
2021-09-10 09:31:27 -04:00
|
|
|
entries, err := m.GetMigrationLog(ctx)
|
2021-04-08 20:54:32 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
assert.Equalf(2, len(entries), "expected 2 scopes without a primary auth method and got: %d", len(entries))
|
|
|
|
|
|
|
|
|
|
scope1, err := iamRepo.LookupScope(ctx, org.PublicId)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
assert.Empty(scope1.PrimaryAuthMethodId)
|
|
|
|
|
scope2, err := iamRepo.LookupScope(ctx, org2.PublicId)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
assert.Empty(scope2.PrimaryAuthMethodId)
|
|
|
|
|
|
|
|
|
|
scope3, err := iamRepo.LookupScope(ctx, org3.PublicId)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
assert.Equal(org3AuthMethods[0].PublicId, scope3.PrimaryAuthMethodId)
|
|
|
|
|
|
|
|
|
|
scope4, err := iamRepo.LookupScope(ctx, org4.PublicId)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
assert.Equal(org4AuthMethods[0].PublicId, scope4.PrimaryAuthMethodId)
|
|
|
|
|
})
|
|
|
|
|
}
|