This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathmanifest-versions_test.go
198 lines (186 loc) · 5.13 KB
/
manifest-versions_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
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
// Copyright 2019 Istio Authors
//
// 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 mesh
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
goversion "github.com/hashicorp/go-version"
"gopkg.in/yaml.v2"
"istio.io/operator/pkg/version"
binversion "istio.io/operator/version"
)
func TestGetVersionCompatibleMap(t *testing.T) {
type args struct {
versionsURI string
binVersion *goversion.Version
l *Logger
}
testDataDir = filepath.Join(repoRootDir, "cmd/mesh/testdata/manifest-versions")
testdataVersionsFilePath := filepath.Join(testDataDir, "input", "versions.yaml")
operatorVersionsFilePath := "../../data/versions.yaml"
nonexistentFilePath := "__nonexistent-versions.yaml"
goVerNonexistent, _ := goversion.NewVersion("0.0.999")
goVer133, _ := goversion.NewVersion("1.3.3")
goVer137, _ := goversion.NewVersion("1.3.7")
goVer1331, _ := goversion.NewVersion("1.3.3.1")
goVer1399, _ := goversion.NewVersion("1.3.9.9")
l := NewLogger(true, os.Stdout, os.Stderr)
b, err := ioutil.ReadFile(operatorVersionsFilePath)
if err != nil {
t.Fatal(err)
}
var vs []version.CompatibilityMapping
if err := yaml.Unmarshal(b, &vs); err != nil {
t.Fatal(err)
}
var curCm, ver133Cm, ver137Cm *version.CompatibilityMapping
for i := range vs {
if binversion.OperatorBinaryGoVersion.Equal(vs[i].OperatorVersion) {
curCm = &vs[i]
}
if goVer133.Equal(vs[i].OperatorVersion) {
ver133Cm = &vs[i]
}
if goVer137.Equal(vs[i].OperatorVersion) {
ver137Cm = &vs[i]
}
}
if curCm == nil {
t.Fatalf("OperatorBinaryGoVersion %v cannot be found in %s, "+
"if OperatorBinaryGoVersion is updated to a new version, please also add it "+
"into versions.yaml and generate the built-in vfs data.",
binversion.OperatorBinaryGoVersion, operatorVersionsFilePath)
}
tests := []struct {
name string
args args
want *version.CompatibilityMapping
wantErr error
}{
{
name: "read the current binary version from data versions",
args: args{
versionsURI: operatorVersionsFilePath,
binVersion: binversion.OperatorBinaryGoVersion,
l: l,
},
want: curCm,
wantErr: nil,
},
{
name: "read the current binary version from built-in version map",
args: args{
versionsURI: nonexistentFilePath,
binVersion: binversion.OperatorBinaryGoVersion,
l: l,
},
want: curCm,
wantErr: nil,
},
{
name: "read version 133 from testdata",
args: args{
versionsURI: testdataVersionsFilePath,
binVersion: goVer133,
l: l,
},
want: ver133Cm,
wantErr: nil,
},
{
name: "read nonexistent version from testdata",
args: args{
versionsURI: testdataVersionsFilePath,
binVersion: goVerNonexistent,
l: l,
},
want: nil,
wantErr: fmt.Errorf("this operator version %s was not found in the version map",
goVerNonexistent.String()),
},
{
name: "read nonexistent version in built-in version map",
args: args{
versionsURI: nonexistentFilePath,
binVersion: goVerNonexistent,
l: l,
},
want: nil,
wantErr: fmt.Errorf("this operator version %s was not found in the version map",
goVerNonexistent.String()),
},
{
name: "read previous version if not found from testdata",
args: args{
versionsURI: testdataVersionsFilePath,
binVersion: goVer1331,
l: l,
},
want: ver133Cm,
wantErr: nil,
},
{
name: "read previous version if not found in built-in version map",
args: args{
versionsURI: nonexistentFilePath,
binVersion: goVer1331,
l: l,
},
want: ver133Cm,
wantErr: nil,
},
{
name: "read version matching range only from testdata",
args: args{
versionsURI: testdataVersionsFilePath,
binVersion: goVer1399,
l: l,
},
want: ver137Cm,
wantErr: nil,
},
{
name: "read version matching range only in built-in version map",
args: args{
versionsURI: nonexistentFilePath,
binVersion: goVer1399,
l: l,
},
want: ver137Cm,
wantErr: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, gotErr := getVersionCompatibleMap(tt.args.versionsURI, tt.args.binVersion, tt.args.l)
if fmt.Sprintf("%v", got) != fmt.Sprintf("%v", tt.want) {
t.Errorf("got: %v, want: %v", got, tt.want)
}
if errToString(gotErr) != errToString(tt.wantErr) {
t.Errorf("gotErr: %v, wantErr: %v", gotErr, tt.wantErr)
}
})
}
}
// errToString returns the string representation of err and the empty string if
// err is nil.
func errToString(err error) string {
if err == nil {
return ""
}
return err.Error()
}