2021-07-19 21:20:31 +00:00
|
|
|
package mysql
|
2017-03-08 17:17:07 +00:00
|
|
|
|
|
|
|
import (
|
2021-08-24 20:24:52 +00:00
|
|
|
"bytes"
|
2021-09-14 12:11:07 +00:00
|
|
|
"context"
|
2021-08-24 20:24:52 +00:00
|
|
|
"os/exec"
|
2017-03-08 17:17:07 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-08-24 20:24:52 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/config"
|
2021-12-08 22:50:00 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/datastore/mysql/migrations/tables"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2017-03-09 18:40:52 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-03-08 17:17:07 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-07-19 21:20:31 +00:00
|
|
|
func TestMigrationStatus(t *testing.T) {
|
2021-08-24 20:24:52 +00:00
|
|
|
ds := createMySQLDSForMigrationTests(t, t.Name())
|
2021-11-22 17:06:12 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
ds.Close()
|
|
|
|
})
|
2021-07-19 21:20:31 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
status, err := ds.MigrationStatus(context.Background())
|
2021-11-22 17:06:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, fleet.NoMigrationsCompleted, status.StatusCode)
|
|
|
|
assert.Empty(t, status.MissingTable)
|
|
|
|
assert.Empty(t, status.MissingData)
|
2017-03-08 17:17:07 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
require.Nil(t, ds.MigrateTables(context.Background()))
|
2017-03-08 17:17:07 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
status, err = ds.MigrationStatus(context.Background())
|
2021-11-22 17:06:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, fleet.SomeMigrationsCompleted, status.StatusCode)
|
|
|
|
assert.NotEmpty(t, status.MissingData)
|
2017-03-09 18:40:52 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
require.Nil(t, ds.MigrateData(context.Background()))
|
2017-03-09 18:40:52 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
status, err = ds.MigrationStatus(context.Background())
|
2021-11-22 17:06:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, fleet.AllMigrationsCompleted, status.StatusCode)
|
|
|
|
assert.Empty(t, status.MissingTable)
|
2021-12-08 22:50:00 +00:00
|
|
|
assert.Empty(t, status.MissingData)
|
|
|
|
|
|
|
|
// Insert unknown migration.
|
|
|
|
ds.writer.Exec(`INSERT INTO ` + tables.MigrationClient.TableName + ` (version_id, is_applied) VALUES (1638994765, 1)`)
|
|
|
|
status, err = ds.MigrationStatus(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, fleet.UnknownMigrations, status.StatusCode)
|
|
|
|
ds.writer.Exec(`DELETE FROM ` + tables.MigrationClient.TableName + ` WHERE version_id = 1638994765`)
|
|
|
|
|
|
|
|
status, err = ds.MigrationStatus(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, fleet.AllMigrationsCompleted, status.StatusCode)
|
|
|
|
assert.Empty(t, status.MissingTable)
|
2021-11-22 17:06:12 +00:00
|
|
|
assert.Empty(t, status.MissingData)
|
2017-03-08 17:17:07 +00:00
|
|
|
}
|
2021-08-24 20:24:52 +00:00
|
|
|
|
|
|
|
func TestMigrations(t *testing.T) {
|
|
|
|
// Create the database (must use raw MySQL client to do this)
|
|
|
|
ds := createMySQLDSForMigrationTests(t, t.Name())
|
|
|
|
defer ds.Close()
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
require.NoError(t, ds.MigrateTables(context.Background()))
|
2021-08-24 20:24:52 +00:00
|
|
|
|
|
|
|
// Dump schema to dumpfile
|
|
|
|
cmd := exec.Command(
|
|
|
|
"docker-compose", "exec", "-T", "mysql_test",
|
|
|
|
// Command run inside container
|
|
|
|
"mysqldump", "-u"+testUsername, "-p"+testPassword, "TestMigrations", "--compact", "--skip-comments",
|
|
|
|
)
|
|
|
|
var stdoutBuf bytes.Buffer
|
|
|
|
cmd.Stdout = &stdoutBuf
|
|
|
|
require.NoError(t, cmd.Run())
|
|
|
|
|
|
|
|
require.NotEmpty(t, stdoutBuf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func createMySQLDSForMigrationTests(t *testing.T, dbName string) *Datastore {
|
|
|
|
// Create a datastore client in order to run migrations as usual
|
|
|
|
config := config.MysqlConfig{
|
|
|
|
Username: testUsername,
|
|
|
|
Password: testPassword,
|
|
|
|
Address: testAddress,
|
|
|
|
Database: dbName,
|
|
|
|
}
|
2021-09-07 16:48:04 +00:00
|
|
|
ds, err := newDSWithConfig(t, dbName, config)
|
2021-08-24 20:24:52 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return ds
|
|
|
|
}
|