-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
deployer_test.go
210 lines (177 loc) · 6.16 KB
/
deployer_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
199
200
201
202
203
204
205
206
207
208
209
210
package deployer
import (
"testing"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
deployapi "github.com/openshift/origin/pkg/deploy/api"
deploytest "github.com/openshift/origin/pkg/deploy/api/test"
deployutil "github.com/openshift/origin/pkg/deploy/util"
)
func TestGetDeploymentContextMissingDeployment(t *testing.T) {
getter := &testReplicationControllerGetter{
getFunc: func(namespace, name string) (*kapi.ReplicationController, error) {
return nil, kerrors.NewNotFound("replicationController", name)
},
listFunc: func(namespace string, selector labels.Selector) (*kapi.ReplicationControllerList, error) {
t.Fatal("unexpected list call")
return nil, nil
},
}
newDeployment, oldDeployments, err := getDeployerContext(getter, kapi.NamespaceDefault, "deployment")
if newDeployment != nil {
t.Fatalf("unexpected newDeployment: %#v", newDeployment)
}
if oldDeployments != nil {
t.Fatalf("unexpected oldDeployments: %#v", oldDeployments)
}
if err == nil {
t.Fatal("expected an error")
}
}
func TestGetDeploymentContextInvalidEncodedConfig(t *testing.T) {
getter := &testReplicationControllerGetter{
getFunc: func(namespace, name string) (*kapi.ReplicationController, error) {
return &kapi.ReplicationController{}, nil
},
listFunc: func(namespace string, selector labels.Selector) (*kapi.ReplicationControllerList, error) {
return &kapi.ReplicationControllerList{}, nil
},
}
newDeployment, oldDeployments, err := getDeployerContext(getter, kapi.NamespaceDefault, "deployment")
if newDeployment != nil {
t.Fatalf("unexpected newDeployment: %#v", newDeployment)
}
if oldDeployments != nil {
t.Fatalf("unexpected oldDeployments: %#v", oldDeployments)
}
if err == nil {
t.Fatal("expected an error")
}
}
func TestGetDeploymentContextNoPriorDeployments(t *testing.T) {
getter := &testReplicationControllerGetter{
getFunc: func(namespace, name string) (*kapi.ReplicationController, error) {
deployment, _ := deployutil.MakeDeployment(deploytest.OkDeploymentConfig(1), kapi.Codec)
return deployment, nil
},
listFunc: func(namespace string, selector labels.Selector) (*kapi.ReplicationControllerList, error) {
return &kapi.ReplicationControllerList{}, nil
},
}
newDeployment, oldDeployments, err := getDeployerContext(getter, kapi.NamespaceDefault, "deployment")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if newDeployment == nil {
t.Fatal("expected deployment")
}
if oldDeployments == nil {
t.Fatal("expected non-nil oldDeployments")
}
if len(oldDeployments) > 0 {
t.Fatalf("unexpected non-empty oldDeployments: %#v", oldDeployments)
}
}
func TestGetDeploymentContextWithPriorDeployments(t *testing.T) {
getter := &testReplicationControllerGetter{
getFunc: func(namespace, name string) (*kapi.ReplicationController, error) {
deployment, _ := deployutil.MakeDeployment(deploytest.OkDeploymentConfig(3), kapi.Codec)
return deployment, nil
},
listFunc: func(namespace string, selector labels.Selector) (*kapi.ReplicationControllerList, error) {
deployment1, _ := deployutil.MakeDeployment(deploytest.OkDeploymentConfig(1), kapi.Codec)
deployment2, _ := deployutil.MakeDeployment(deploytest.OkDeploymentConfig(2), kapi.Codec)
deployment3, _ := deployutil.MakeDeployment(deploytest.OkDeploymentConfig(4), kapi.Codec)
deployment4, _ := deployutil.MakeDeployment(deploytest.OkDeploymentConfig(1), kapi.Codec)
deployment4.Annotations[deployapi.DeploymentConfigAnnotation] = "another-config"
return &kapi.ReplicationControllerList{
Items: []kapi.ReplicationController{
*deployment1,
*deployment2,
*deployment3,
*deployment4,
{},
},
}, nil
},
}
newDeployment, oldDeployments, err := getDeployerContext(getter, kapi.NamespaceDefault, "deployment")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if newDeployment == nil {
t.Fatal("expected deployment")
}
if oldDeployments == nil {
t.Fatal("expected non-nil oldDeployments")
}
expected := []string{"config-1", "config-2"}
for _, e := range expected {
found := false
for _, d := range oldDeployments {
if d.Name == e {
found = true
break
}
}
if !found {
t.Errorf("expected to find old deployment %s", e)
}
}
for _, d := range oldDeployments {
ok := false
for _, e := range expected {
if d.Name == e {
ok = true
break
}
}
if !ok {
t.Errorf("unexpected old deployment %s", d.Name)
}
}
}
func TestGetDeploymentContextInvalidPriorDeployment(t *testing.T) {
getter := &testReplicationControllerGetter{
getFunc: func(namespace, name string) (*kapi.ReplicationController, error) {
deployment, _ := deployutil.MakeDeployment(deploytest.OkDeploymentConfig(1), kapi.Codec)
return deployment, nil
},
listFunc: func(namespace string, selector labels.Selector) (*kapi.ReplicationControllerList, error) {
return &kapi.ReplicationControllerList{
Items: []kapi.ReplicationController{
{
ObjectMeta: kapi.ObjectMeta{
Name: "corrupt-deployment",
Annotations: map[string]string{
deployapi.DeploymentConfigAnnotation: "config",
deployapi.DeploymentVersionAnnotation: "junk",
},
},
},
},
}, nil
},
}
newDeployment, oldDeployments, err := getDeployerContext(getter, kapi.NamespaceDefault, "deployment")
if newDeployment != nil {
t.Fatalf("unexpected newDeployment: %#v", newDeployment)
}
if oldDeployments != nil {
t.Fatalf("unexpected oldDeployments: %#v", oldDeployments)
}
if err == nil {
t.Fatal("expected an error")
}
}
type testReplicationControllerGetter struct {
getFunc func(namespace, name string) (*kapi.ReplicationController, error)
listFunc func(namespace string, selector labels.Selector) (*kapi.ReplicationControllerList, error)
}
func (t *testReplicationControllerGetter) Get(namespace, name string) (*kapi.ReplicationController, error) {
return t.getFunc(namespace, name)
}
func (t *testReplicationControllerGetter) List(namespace string, selector labels.Selector) (*kapi.ReplicationControllerList, error) {
return t.listFunc(namespace, selector)
}