forked from knative/func
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_migrations_unit_test.go
138 lines (122 loc) · 4.32 KB
/
function_migrations_unit_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
package function
import (
"testing"
"time"
"github.com/coreos/go-semver/semver"
)
// TestMigrated ensures that the .Migrated() method returns whether or not the
// migrations were applied based on its self-reported .SpecVersion member.
func TestMigrated(t *testing.T) {
vNext := semver.New(LastSpecVersion())
vNext.BumpMajor()
tests := []struct {
name string
f Function
migrated bool
}{{
name: "no migration stamp",
f: Function{},
migrated: false, // function with no specVersion stamp should be not migrated.
}, {
name: "explicit small specVersion",
f: Function{SpecVersion: "0.0.1"},
migrated: false,
}, {
name: "latest specVersion",
f: Function{SpecVersion: LastSpecVersion()},
migrated: true,
}, {
name: "future specVersion",
f: Function{SpecVersion: vNext.String()},
migrated: true,
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.f.Migrated() != test.migrated {
t.Errorf("Expected %q.Migrated() to be %t when latest is %q",
test.f.SpecVersion, test.migrated, LastSpecVersion())
}
})
}
}
// TestMigrate ensures that functions have migrations apply the specVersion
// stamp on instantiation indicating migrations have been applied.
func TestMigrate(t *testing.T) {
// Load an old function, as it an earlier version it has registered migrations
// that will need to be applied.
root := "testdata/migrations/v0.19.0"
// Instantiate the function with the antiquated structure, which should cause
// migrations to be applied in order, and result in a function whose version
// compatibility is equivalent to the latest registered migration.
f, err := NewFunction(root)
if err != nil {
t.Fatal(err)
}
if f.SpecVersion != LastSpecVersion() {
t.Fatalf("Function was not migrated to %v on instantiation: specVersion is %v",
LastSpecVersion(), f.SpecVersion)
}
}
// TestMigrateToCreationStamp ensures that the creation timestamp migration
// introduced for functions 0.19.0 and earlier is applied.
func TestMigrateToCreationStamp(t *testing.T) {
// Load a function of version 0.19.0, which should have the migration applied
root := "testdata/migrations/v0.19.0"
now := time.Now()
f, err := NewFunction(root)
if err != nil {
t.Fatal(err)
}
if f.Created.Before(now) {
t.Fatalf("migration not applied: expected timestamp to be now, got %v.", f.Created)
}
}
// TestMigrateToBuilderImages ensures that the migration which migrates
// from "builder" and "builders" to "builderImages" is applied. This results
// in the attributes being removed and no errors on load of the function with
// old schema.
func TestMigrateToBuilderImagesDefault(t *testing.T) {
// Load a function created prior to the adoption of the builder images map
// (was created with 'builder' and 'builders' which does not support different
// builder implementations.
root := "testdata/migrations/v0.23.0"
// Without the migration, instantiating the older function would error
// because its strict unmarshalling would fail parsing the unexpected
// 'builder' and 'builders' members.
_, err := NewFunction(root)
if err != nil {
t.Fatal(err)
}
}
// TestMigrateToBuilderImagesCustom ensures that the migration to builderImages
// correctly carries forward a customized value for 'builder'.
func TestMigrateToBuilderImagesCustom(t *testing.T) {
// An early version of a function which includes a customized value for
// the 'builder'. This should be correctly carried forward to
// the namespaced 'builderImages' map as image for the "pack" builder.
root := "testdata/migrations/v0.23.0-customized"
expected := "example.com/user/custom-builder" // set in testdata func.yaml
f, err := NewFunction(root)
if err != nil {
t.Fatal(f)
}
i, ok := f.BuilderImages["pack"]
if !ok {
t.Fatal("migrated function does not include the pack builder images")
}
if i != expected {
t.Fatalf("migrated function expected builder image '%v', got '%v'", expected, i)
}
}
// TestMigrateToSpecVersion ensures that a func.yaml file with a "version" field
// is migrated to use the field name "specVersion"
func TestMigrateToSpecVersion(t *testing.T) {
root := "testdata/migrations/v0.25.0"
f, err := NewFunction(root)
if err != nil {
t.Fatal(err)
}
if f.SpecVersion != LastSpecVersion() {
t.Fatal("migrated function does not include the Migration field")
}
}