github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/migrations/migrations_test.go (about) 1 // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package migrations 5 6 import ( 7 "flag" 8 "os" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 13 "github.com/mattermost/mattermost-server/mlog" 14 "github.com/mattermost/mattermost-server/model" 15 "github.com/mattermost/mattermost-server/store/storetest" 16 "github.com/mattermost/mattermost-server/utils" 17 ) 18 19 func TestMain(m *testing.M) { 20 flag.Parse() 21 22 // Setup a global logger to catch tests logging outside of app context 23 // The global logger will be stomped by apps initalizing but that's fine for testing. Ideally this won't happen. 24 mlog.InitGlobalLogger(mlog.NewLogger(&mlog.LoggerConfiguration{ 25 EnableConsole: true, 26 ConsoleJson: true, 27 ConsoleLevel: "error", 28 EnableFile: false, 29 })) 30 31 utils.TranslationsPreInit() 32 33 // In the case where a dev just wants to run a single test, it's faster to just use the default 34 // store. 35 if filter := flag.Lookup("test.run").Value.String(); filter != "" && filter != "." { 36 mlog.Info("-test.run used, not creating temporary containers") 37 os.Exit(m.Run()) 38 } 39 40 status := 0 41 42 container, settings, err := storetest.NewMySQLContainer() 43 if err != nil { 44 panic(err) 45 } 46 47 UseTestStore(container, settings) 48 49 defer func() { 50 StopTestStore() 51 os.Exit(status) 52 }() 53 54 status = m.Run() 55 } 56 57 func TestGetMigrationState(t *testing.T) { 58 th := Setup() 59 defer th.TearDown() 60 61 migrationKey := model.NewId() 62 63 th.DeleteAllJobsByTypeAndMigrationKey(model.JOB_TYPE_MIGRATIONS, migrationKey) 64 65 // Test with no job yet. 66 state, job, err := GetMigrationState(migrationKey, th.App.Srv.Store) 67 assert.Nil(t, err) 68 assert.Nil(t, job) 69 assert.Equal(t, "unscheduled", state) 70 71 // Test with the system table showing the migration as done. 72 system := model.System{ 73 Name: migrationKey, 74 Value: "true", 75 } 76 res1 := <-th.App.Srv.Store.System().Save(&system) 77 assert.Nil(t, res1.Err) 78 79 state, job, err = GetMigrationState(migrationKey, th.App.Srv.Store) 80 assert.Nil(t, err) 81 assert.Nil(t, job) 82 assert.Equal(t, "completed", state) 83 84 res2 := <-th.App.Srv.Store.System().PermanentDeleteByName(migrationKey) 85 assert.Nil(t, res2.Err) 86 87 // Test with a job scheduled in "pending" state. 88 j1 := &model.Job{ 89 Id: model.NewId(), 90 CreateAt: model.GetMillis(), 91 Data: map[string]string{ 92 JOB_DATA_KEY_MIGRATION: migrationKey, 93 }, 94 Status: model.JOB_STATUS_PENDING, 95 Type: model.JOB_TYPE_MIGRATIONS, 96 } 97 98 j1 = (<-th.App.Srv.Store.Job().Save(j1)).Data.(*model.Job) 99 100 state, job, err = GetMigrationState(migrationKey, th.App.Srv.Store) 101 assert.Nil(t, err) 102 assert.Equal(t, j1.Id, job.Id) 103 assert.Equal(t, "in_progress", state) 104 105 // Test with a job scheduled in "in progress" state. 106 j2 := &model.Job{ 107 Id: model.NewId(), 108 CreateAt: j1.CreateAt + 1, 109 Data: map[string]string{ 110 JOB_DATA_KEY_MIGRATION: migrationKey, 111 }, 112 Status: model.JOB_STATUS_IN_PROGRESS, 113 Type: model.JOB_TYPE_MIGRATIONS, 114 } 115 116 j2 = (<-th.App.Srv.Store.Job().Save(j2)).Data.(*model.Job) 117 118 state, job, err = GetMigrationState(migrationKey, th.App.Srv.Store) 119 assert.Nil(t, err) 120 assert.Equal(t, j2.Id, job.Id) 121 assert.Equal(t, "in_progress", state) 122 123 // Test with a job scheduled in "error" state. 124 j3 := &model.Job{ 125 Id: model.NewId(), 126 CreateAt: j2.CreateAt + 1, 127 Data: map[string]string{ 128 JOB_DATA_KEY_MIGRATION: migrationKey, 129 }, 130 Status: model.JOB_STATUS_ERROR, 131 Type: model.JOB_TYPE_MIGRATIONS, 132 } 133 134 j3 = (<-th.App.Srv.Store.Job().Save(j3)).Data.(*model.Job) 135 136 state, job, err = GetMigrationState(migrationKey, th.App.Srv.Store) 137 assert.Nil(t, err) 138 assert.Equal(t, j3.Id, job.Id) 139 assert.Equal(t, "unscheduled", state) 140 }