forked from emicklei/gmig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration_test.go
146 lines (132 loc) · 3.4 KB
/
migration_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
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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
)
var one = `
do:
- going up
# comment for down
undo:
- going down
`
func TestParseMigration(t *testing.T) {
var m Migration
if err := yaml.Unmarshal([]byte(one), &m); err != nil {
t.Error(err)
}
if got, want := m.DoSection[0], "going up"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
if got, want := m.UndoSection[0], "going down"; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
// gcloud config list --format json
func readConfig() Config {
type gcloudconfig struct {
Compute struct {
Region, Zone string
}
Core struct {
Project string
}
}
var gc gcloudconfig
cmd := exec.Command("gcloud", "config", "list", "--format", "json")
out, _ := runCommand(cmd)
json.Unmarshal(out, &gc)
return Config{
Project: gc.Core.Project,
Region: gc.Compute.Region,
Zone: gc.Compute.Zone,
}
}
func TestSetupShellScriptNotVerbose(t *testing.T) {
want := `#!/bin/bash
# temporary gmig execution script
set -e -v`
if got := setupShellScript(false); got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
func TestSetupShellScriptVerbose(t *testing.T) {
want := `#!/bin/bash
# temporary gmig execution script
set -e -x`
if got := setupShellScript(true); got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
func TestNewFilenameWithIndex(t *testing.T) {
wd, _ := os.Getwd()
dir, err := ioutil.TempDir("", "testing")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
// change and restore finally
if err := os.Chdir(dir); err != nil {
return
}
defer os.Chdir(wd)
desc := "first migration"
want := "010_first_migration.yaml"
if got := NewFilenameWithIndex(desc); got != want {
t.Errorf("NewFilenameWithIndex(%s) = %v, want %v", desc, got, want)
}
tmpfn := filepath.Join(dir, "20181026t183700_starts_with_timestamp.yaml")
if err := ioutil.WriteFile(tmpfn, []byte(""), 0444); err != nil {
log.Fatal(err)
}
desc = "first after timestamp"
want = "300_first_after_timestamp.yaml"
if got := NewFilenameWithIndex(desc); got != want {
t.Errorf("NewFilenameWithIndex(%s) = %v, want %v", desc, got, want)
}
tmpfn = filepath.Join(dir, "400_starts_with_high_index.yaml")
if err := ioutil.WriteFile(tmpfn, []byte(""), 0444); err != nil {
log.Fatal(err)
}
desc = "first after high index"
want = "405_first_after_high_index.yaml"
if got := NewFilenameWithIndex(desc); got != want {
t.Errorf("NewFilenameWithIndex(%s) = %v, want %v", desc, got, want)
}
tmpfn = filepath.Join(dir, "unexpected_yaml_in_directory.yaml")
if err := ioutil.WriteFile(tmpfn, []byte(""), 0444); err != nil {
log.Fatal(err)
}
desc = "potentially unexpected naming"
want = "010_potentially_unexpected_naming.yaml"
if got := NewFilenameWithIndex(desc); got != want {
t.Errorf("NewFilenameWithIndex(%s) = %v, want %v", desc, got, want)
}
}
func TestEvaluateCondition(t *testing.T) {
envs := []string{"ZONE=A", "PROJECT=B"}
ok, err := evaluateCondition(`PROJECT == "B"`, envs)
if err != nil {
log.Fatal(err)
}
if got, want := ok, true; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
func TestMigrationContextWithMigrationsOverride(t *testing.T) {
t.Skip()
c := new(cli.Context)
c.Set("migrations", "/tmp/here")
ctx, err := getMigrationContext(c)
if err != nil {
t.Fatal(err)
}
t.Log(ctx)
}