File: migrator_simple_test.go

package info (click to toggle)
golang-github-mendersoftware-go-lib-micro 0.0~git20211108.4e20429%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, experimental, forky, sid, trixie
  • size: 656 kB
  • sloc: makefile: 5
file content (234 lines) | stat: -rw-r--r-- 6,354 bytes parent folder | download
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2019 Northern.tech AS
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
package migrate_test

import (
	"context"
	"errors"
	"sort"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"

	. "github.com/mendersoftware/go-lib-micro/mongo/migrate"
	"github.com/mendersoftware/go-lib-micro/mongo/migrate/mocks"
	"go.mongodb.org/mongo-driver/bson"
)

func TestSimpleMigratorApply(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping TestDummyMigratorApply in short mode.")
	}

	makeMigration := func(v Version, from Version, err error) Migration {
		m := &mocks.Migration{}
		m.On("Up", from).Return(err)
		m.On("Version").Return(v)
		return m
	}

	testCases := map[string]struct {
		Automigrate     bool
		InputMigrations []MigrationEntry
		InputVersion    Version

		Migrators []Migration

		OutputVersion Version
		OutputError   error
	}{
		"ok - empty state": {
			Automigrate:     true,
			InputMigrations: nil,
			InputVersion:    MakeVersion(1, 0, 0),

			OutputVersion: MakeVersion(1, 0, 0),
		},

		"ok - already has version": {
			Automigrate: true,
			InputMigrations: []MigrationEntry{
				{
					Version:   MakeVersion(1, 0, 0),
					Timestamp: time.Now(),
				},
			},
			InputVersion:  MakeVersion(1, 0, 0),
			OutputVersion: MakeVersion(1, 0, 0),
		},
		"ok - already has version, no automigrate": {
			Automigrate: false,
			InputMigrations: []MigrationEntry{
				{
					Version:   MakeVersion(1, 0, 0),
					Timestamp: time.Now(),
				},
			},
			InputVersion:  MakeVersion(1, 0, 0),
			OutputVersion: MakeVersion(1, 0, 0),
		},
		"ok - add default target version": {
			Automigrate: true,
			InputMigrations: []MigrationEntry{
				{
					Version:   MakeVersion(1, 0, 0),
					Timestamp: time.Now(),
				},
			},
			InputVersion:  MakeVersion(1, 1, 0),
			OutputVersion: MakeVersion(1, 1, 0),
		},
		"ok - add default target version, no automigrate": {
			Automigrate: false,
			InputMigrations: []MigrationEntry{
				{
					Version:   MakeVersion(1, 0, 0),
					Timestamp: time.Now(),
				},
			},
			InputVersion:  MakeVersion(1, 1, 0),
			OutputVersion: MakeVersion(1, 0, 0),
			OutputError:   errors.New("db needs migration: test has version 1.0.0, needs version 1.1.0"),
		},
		"ok - ran migrations": {
			Automigrate: true,
			InputMigrations: []MigrationEntry{
				{
					Version:   MakeVersion(1, 0, 0),
					Timestamp: time.Now(),
				},
				{
					Version:   MakeVersion(1, 0, 1),
					Timestamp: time.Now(),
				},
			},
			InputVersion:  MakeVersion(1, 1, 0),
			OutputVersion: MakeVersion(1, 1, 0),

			Migrators: []Migration{
				makeMigration(MakeVersion(1, 0, 1), MakeVersion(1, 0, 0), nil),
				makeMigration(MakeVersion(1, 1, 0), MakeVersion(1, 0, 1), nil),
			},
		},
		"ok - ran migrations, no automigrate": {
			Automigrate: false,
			InputMigrations: []MigrationEntry{
				{
					Version:   MakeVersion(1, 0, 0),
					Timestamp: time.Now(),
				},
				{
					Version:   MakeVersion(1, 0, 1),
					Timestamp: time.Now(),
				},
			},
			InputVersion:  MakeVersion(1, 1, 0),
			OutputVersion: MakeVersion(1, 0, 1),

			Migrators: []Migration{
				makeMigration(MakeVersion(1, 0, 1), MakeVersion(1, 0, 0), nil),
				makeMigration(MakeVersion(1, 1, 0), MakeVersion(1, 0, 1), nil),
			},
			OutputError: errors.New("db needs migration: test has version 1.0.1, needs version 1.1.0"),
		},
		"ok - migration to lower": {
			Automigrate:     true,
			InputMigrations: nil,
			InputVersion:    MakeVersion(0, 1, 0),
			OutputVersion:   MakeVersion(0, 1, 0),

			Migrators: []Migration{
				makeMigration(MakeVersion(1, 0, 1), MakeVersion(0, 0, 0), nil),
				makeMigration(MakeVersion(1, 1, 0), MakeVersion(1, 0, 1), nil),
			},
		},
		"err - failed migration": {
			Automigrate: true,
			InputMigrations: []MigrationEntry{
				{
					Version:   MakeVersion(1, 0, 0),
					Timestamp: time.Now(),
				},
			},
			InputVersion: MakeVersion(1, 1, 0),
			// migration 1.0.3 fails, thus the output should remain at 1.0.2
			OutputVersion: MakeVersion(1, 0, 2),

			Migrators: []Migration{
				makeMigration(MakeVersion(1, 0, 1), MakeVersion(1, 0, 0), nil),
				makeMigration(MakeVersion(1, 0, 3), MakeVersion(1, 0, 2), errors.New("failed")),
				makeMigration(MakeVersion(1, 0, 2), MakeVersion(1, 0, 1), nil),
			},

			OutputError: errors.New("failed to apply migration from 1.0.2 to 1.0.3: failed"),
		},
	}

	for name := range testCases {
		tc := testCases[name]
		t.Run(name, func(t *testing.T) {

			//setup
			db.Wipe()
			client := db.Client()
			for i := range tc.InputMigrations {
				_, err := client.Database("test").
					Collection(DbMigrationsColl).
					InsertOne(db.CTX(), tc.InputMigrations[i])
				assert.NoError(t, err)
			}

			//test
			m := &SimpleMigrator{Client: client, Db: "test", Automigrate: tc.Automigrate}
			err := m.Apply(context.Background(), tc.InputVersion, tc.Migrators)
			if tc.OutputError != nil {
				assert.Error(t, err)
				assert.EqualError(t, err, tc.OutputError.Error())
			} else {
				assert.NoError(t, err)
			}

			//verify
			var out []MigrationEntry
			cursor, _ := client.Database("test").
				Collection(DbMigrationsColl).
				Find(db.CTX(), bson.M{})

			count := 0
			for cursor.Next(db.CTX()) {
				var res MigrationEntry
				count++
				elem := &bson.D{}
				err = cursor.Decode(elem)
				bsonBytes, _ := bson.Marshal(elem)
				bson.Unmarshal(bsonBytes, &res)
				out = append(out, res)
			}

			// sort applied migrations
			sort.Slice(out, func(i int, j int) bool {
				return VersionIsLess(out[i].Version, out[j].Version)
			})
			// applied migration should be last
			assert.Equal(t, tc.OutputVersion, out[len(out)-1].Version)
		})
	}
}

func TestErrNeedsMigration(t *testing.T) {
	err := errors.New("db needs migration: mydbname has version 1.0.0, needs version 1.1.0")

	assert.True(t, IsErrNeedsMigration(err))
}