summaryrefslogtreecommitdiffstats
path: root/migrations/migrations_test.go
blob: 30831943064920b50a107033ea7d20149617ca4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package migrations

import (
	"flag"
	"os"
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/mattermost/mattermost-server/mlog"
	"github.com/mattermost/mattermost-server/model"
	"github.com/mattermost/mattermost-server/store/storetest"
	"github.com/mattermost/mattermost-server/utils"
)

func TestMain(m *testing.M) {
	flag.Parse()

	// Setup a global logger to catch tests logging outside of app context
	// The global logger will be stomped by apps initalizing but that's fine for testing. Ideally this won't happen.
	mlog.InitGlobalLogger(mlog.NewLogger(&mlog.LoggerConfiguration{
		EnableConsole: true,
		ConsoleJson:   true,
		ConsoleLevel:  "error",
		EnableFile:    false,
	}))

	utils.TranslationsPreInit()

	// In the case where a dev just wants to run a single test, it's faster to just use the default
	// store.
	if filter := flag.Lookup("test.run").Value.String(); filter != "" && filter != "." {
		mlog.Info("-test.run used, not creating temporary containers")
		os.Exit(m.Run())
	}

	status := 0

	container, settings, err := storetest.NewMySQLContainer()
	if err != nil {
		panic(err)
	}

	UseTestStore(container, settings)

	defer func() {
		StopTestStore()
		os.Exit(status)
	}()

	status = m.Run()
}

func TestGetMigrationState(t *testing.T) {
	th := Setup()
	defer th.TearDown()

	migrationKey := model.NewId()

	th.DeleteAllJobsByTypeAndMigrationKey(model.JOB_TYPE_MIGRATIONS, migrationKey)

	// Test with no job yet.
	state, job, err := GetMigrationState(migrationKey, th.App.Srv.Store)
	assert.Nil(t, err)
	assert.Nil(t, job)
	assert.Equal(t, "unscheduled", state)

	// Test with the system table showing the migration as done.
	system := model.System{
		Name:  migrationKey,
		Value: "true",
	}
	res1 := <-th.App.Srv.Store.System().Save(&system)
	assert.Nil(t, res1.Err)

	state, job, err = GetMigrationState(migrationKey, th.App.Srv.Store)
	assert.Nil(t, err)
	assert.Nil(t, job)
	assert.Equal(t, "completed", state)

	res2 := <-th.App.Srv.Store.System().PermanentDeleteByName(migrationKey)
	assert.Nil(t, res2.Err)

	// Test with a job scheduled in "pending" state.
	j1 := &model.Job{
		Id:       model.NewId(),
		CreateAt: model.GetMillis(),
		Data: map[string]string{
			JOB_DATA_KEY_MIGRATION: migrationKey,
		},
		Status: model.JOB_STATUS_PENDING,
		Type:   model.JOB_TYPE_MIGRATIONS,
	}

	j1 = (<-th.App.Srv.Store.Job().Save(j1)).Data.(*model.Job)

	state, job, err = GetMigrationState(migrationKey, th.App.Srv.Store)
	assert.Nil(t, err)
	assert.Equal(t, j1.Id, job.Id)
	assert.Equal(t, "in_progress", state)

	// Test with a job scheduled in "in progress" state.
	j2 := &model.Job{
		Id:       model.NewId(),
		CreateAt: j1.CreateAt + 1,
		Data: map[string]string{
			JOB_DATA_KEY_MIGRATION: migrationKey,
		},
		Status: model.JOB_STATUS_IN_PROGRESS,
		Type:   model.JOB_TYPE_MIGRATIONS,
	}

	j2 = (<-th.App.Srv.Store.Job().Save(j2)).Data.(*model.Job)

	state, job, err = GetMigrationState(migrationKey, th.App.Srv.Store)
	assert.Nil(t, err)
	assert.Equal(t, j2.Id, job.Id)
	assert.Equal(t, "in_progress", state)

	// Test with a job scheduled in "error" state.
	j3 := &model.Job{
		Id:       model.NewId(),
		CreateAt: j2.CreateAt + 1,
		Data: map[string]string{
			JOB_DATA_KEY_MIGRATION: migrationKey,
		},
		Status: model.JOB_STATUS_ERROR,
		Type:   model.JOB_TYPE_MIGRATIONS,
	}

	j3 = (<-th.App.Srv.Store.Job().Save(j3)).Data.(*model.Job)

	state, job, err = GetMigrationState(migrationKey, th.App.Srv.Store)
	assert.Nil(t, err)
	assert.Equal(t, j3.Id, job.Id)
	assert.Equal(t, "unscheduled", state)
}