-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
120 lines (108 loc) · 2.29 KB
/
main_test.go
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
package main
import (
"os"
"testing"
)
func TestGetCurrentVersionIsUnset(t *testing.T) {
err := os.Unsetenv("TF_MIGRATION_VERSION")
if err != nil {
t.Fatal(err)
}
version, err := getCurrentVersion()
if err != nil {
t.Error(err)
}
if version != 0 {
t.Error(version)
}
}
func TestGetCurrentVersionIsSet(t *testing.T) {
err := os.Setenv("TF_MIGRATION_VERSION", "99")
if err != nil {
t.Fatal(err)
}
version, err := getCurrentVersion()
if err != nil {
t.Error(err)
}
if version != 99 {
t.Error(version)
}
}
func TestGetCurrentVersionIsNotNumber(t *testing.T) {
err := os.Setenv("TF_MIGRATION_VERSION", "boom")
if err != nil {
t.Fatal(err)
}
version, err := getCurrentVersion()
if err.Error() != "failed to parse current version: strconv.Atoi: parsing \"boom\": invalid syntax" {
t.Error(err)
}
if version != -1 {
t.Error(version)
}
}
func TestFindMigrationFileWithNoFile(t *testing.T) {
file, err := findMigrationFile("migrate_test", 1)
if err.Error() != "no migration file found" {
t.Error(err)
}
if file != "" {
t.Error(file)
}
}
func TestFindMigrationFileWithFiles(t *testing.T) {
err := os.Mkdir("migrate_test", 0777)
if err != nil {
t.Fatal(err)
}
os.Create("migrate_test/01_a.sh")
os.Create("migrate_test/2_b.sh")
os.Create("migrate_test/2_c.sh")
os.Create("migrate_test/3_d.sh")
file, err := findMigrationFile("migrate_test", 1)
if err.Error() != "no migration file found" {
t.Error(err)
}
if file != "" {
t.Error(file)
}
file, err = findMigrationFile("migrate_test", 2)
if err.Error() != "2 files found with version 2" {
t.Error(err)
}
if file != "" {
t.Error(file)
}
file, err = findMigrationFile("migrate_test", 3)
if err != nil {
t.Error(err)
}
if file != "migrate_test/3_d.sh" {
t.Error(file)
}
err = os.RemoveAll("migrate_test")
if err != nil {
t.Fatal(err)
}
}
func TestRenameBackendFileMissingFile(t *testing.T) {
err := renameBackendFile()
if err.Error() != "failed to disable backend: rename backend.tf backend.tf.disable: no such file or directory" {
t.Error(err)
}
}
func TestRenameBackendFile(t *testing.T) {
_, err := os.Create("backend.tf")
if err != nil {
t.Fatal(err)
}
err = renameBackendFile()
if err != nil {
t.Error(err)
}
err = os.Remove("backend.tf.disable")
if err != nil {
t.Error(err)
}
}