zitadel/internal/eventstore/repository/sql/local_crdb_test.go
Elio Bischof 51a76150ca
fix: revert migration V1.13 (#1466)
* fix: revert migration V1.13

* fix: set project changes correctly in application view model (#1469)

* fix: import user, hide login name suffix (#1474)

* fix: import user, and label policy command side

* feat: Import user and hide loginname suffix (#1464)

* fix: import user

* fix: label policy

* fix: label policy

* fix: label policy

* fix: migrations

* fix: migrations

* fix: migrations

* fix: label policy

* loginSuffix in login ui

* suffix

* fix cursor on disabled user selection

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

(cherry picked from commit 03ddb8fc38)

* feat: Import user and hide loginname suffix (#1464)

* fix: import user

* fix: label policy

* fix: label policy

* fix: label policy

* fix: migrations

* fix: migrations

* fix: migrations

* fix: label policy

* loginSuffix in login ui

* suffix

* fix cursor on disabled user selection

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

(cherry picked from commit 03ddb8fc38)

* feat: Import user and hide loginname suffix (#1464)

* fix: import user

* fix: label policy

* fix: label policy

* fix: label policy

* fix: migrations

* fix: migrations

* fix: migrations

* fix: label policy

* loginSuffix in login ui

* suffix

* fix cursor on disabled user selection

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

(cherry picked from commit 03ddb8fc38)

* fix: label policy events

* loginname placeholder

* fix: tests

* fix: tests

* Update internal/command/iam_policy_label_model.go

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* fix: console eventstore (#1476)

* fix org switch

* user grants

* fix project grants, user grant update

* fix idp removal, prettier buttons, attribute names

* fix: operator reconciling (#1478)

* fix(operator): align backup and restore commands (#1465)

* fix: crd mode broke backup and restore commands

* fix: remove obscure gitops-per-operator flags

(cherry picked from commit 041cacc4af)

* fix: gitops backup and restore need a kubernetes client too (#1475)

(cherry picked from commit 50bc317d27)

Co-authored-by: Elio Bischof <eliobischof@gmail.com>

* feat: features (#1427)

* features

* features

* features

* fix json tags

* add features handler to auth

* mocks for tests

* add setup step

* fixes

* add featurelist to auth api

* grandfather state and typos

* typo

* merge new-eventstore

* fix login policy tests

* label policy in features

* audit log retention

* fix: new es bug fixes (#1477)

* fix: displayname on members

* fix: user grant update

* fix: user grant id

* console grantid

Co-authored-by: Max Peintner <max@caos.ch>

* fix: backend (#1481)

* fix: check ids in proto

* fix sign out

* improve displayed login name after user selection

* fix init user in login

* fix init password in login

Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>

* feat: console feature api (#1480)

* fix: changes (#1482)

* fix: changes

* change listquery to changequery

* fix: changes in auth

Co-authored-by: Max Peintner <max@caos.ch>

* test without transaction

* test without transaction

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com>
Co-authored-by: Max Peintner <max@caos.ch>
Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
2021-03-29 13:38:04 +02:00

153 lines
3.4 KiB
Go

package sql
import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"testing"
"github.com/caos/logging"
"github.com/cockroachdb/cockroach-go/v2/testserver"
)
var (
migrationsPath = os.ExpandEnv("${GOPATH}/src/github.com/caos/zitadel/migrations/cockroach")
testCRDBClient *sql.DB
)
func TestMain(m *testing.M) {
ts, err := testserver.NewTestServer()
if err != nil {
logging.LogWithFields("REPOS-RvjLG", "error", err).Fatal("unable to start db")
}
testCRDBClient, err = sql.Open("postgres", ts.PGURL().String())
if err != nil {
logging.LogWithFields("REPOS-CF6dQ", "error", err).Fatal("unable to connect to db")
}
if err = testCRDBClient.Ping(); err != nil {
logging.LogWithFields("REPOS-CF6dQ", "error", err).Fatal("unable to ping db")
}
defer func() {
testCRDBClient.Close()
ts.Stop()
}()
if err = executeMigrations(); err != nil {
logging.LogWithFields("REPOS-jehDD", "error", err).Fatal("migrations failed")
}
os.Exit(m.Run())
}
func executeMigrations() error {
files, err := migrationFilePaths()
if err != nil {
return err
}
sort.Sort(files)
if err = setPasswordNULL(); err != nil {
return err
}
if err = createFlywayHistory(); err != nil {
return err
}
for _, file := range files {
migrationData, err := ioutil.ReadFile(file)
if err != nil {
return err
}
migration := os.ExpandEnv(string(migrationData))
if _, err = testCRDBClient.Exec(migration); err != nil {
return fmt.Errorf("exec file: %v || err: %w", file, err)
}
}
return nil
}
func setPasswordNULL() error {
passwordNames := []string{
"eventstorepassword",
"managementpassword",
"adminapipassword",
"authpassword",
"notificationpassword",
"authzpassword",
"queriespassword",
}
for _, name := range passwordNames {
if err := os.Setenv(name, "NULL"); err != nil {
return err
}
}
return nil
}
func createFlywayHistory() error {
_, err := testCRDBClient.Exec("CREATE TABLE defaultdb.flyway_schema_history(id TEXT, PRIMARY KEY(id));")
return err
}
func fillUniqueData(unique_type, field string) error {
_, err := testCRDBClient.Exec("INSERT INTO eventstore.unique_constraints (unique_type, unique_field) VALUES ($1, $2)", unique_type, field)
return err
}
type migrationPaths []string
type version struct {
major int
minor int
}
func versionFromPath(s string) version {
v := s[strings.Index(s, "/V")+2 : strings.Index(s, "__")]
splitted := strings.Split(v, ".")
res := version{}
var err error
if len(splitted) >= 1 {
res.major, err = strconv.Atoi(splitted[0])
if err != nil {
panic(err)
}
}
if len(splitted) >= 2 {
res.minor, err = strconv.Atoi(splitted[1])
if err != nil {
panic(err)
}
}
return res
}
func (a migrationPaths) Len() int { return len(a) }
func (a migrationPaths) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a migrationPaths) Less(i, j int) bool {
versionI := versionFromPath(a[i])
versionJ := versionFromPath(a[j])
return versionI.major < versionJ.major ||
(versionI.major == versionJ.major && versionI.minor < versionJ.minor)
}
func migrationFilePaths() (migrationPaths, error) {
files := make(migrationPaths, 0)
err := filepath.Walk(migrationsPath, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() || !strings.HasSuffix(info.Name(), ".sql") {
return err
}
files = append(files, path)
return nil
})
return files, err
}