-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_test.go
120 lines (114 loc) · 2.44 KB
/
update_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 workutils
import (
"testing"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/utils/pointer"
workapiv1 "open-cluster-management.io/api/work/v1"
)
func TestUpdate(t *testing.T) {
deploymentStr := `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: nginx
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
env:
- name: FOO
value: "BAR"
ports:
- containerPort: 80
`
raw, err := stringToRawExtension(deploymentStr)
require.Nil(t, err)
work := workapiv1.ManifestWork{
Spec: workapiv1.ManifestWorkSpec{
Workload: workapiv1.ManifestsTemplate{
Manifests: []workapiv1.Manifest{
{
RawExtension: raw,
},
},
},
},
}
deployment := &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1",
Kind: "Deployment",
},
ObjectMeta: metav1.ObjectMeta{
Name: "nginx-deployment",
Namespace: "nginx",
Labels: map[string]string{
"app": "nginx",
},
},
Spec: appsv1.DeploymentSpec{
Replicas: pointer.Int32(3),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "nginx",
},
},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": "nginx",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "nginx",
Image: "nginx:1.14.2",
Env: []v1.EnvVar{
{
Name: "FOO",
Value: "UPDATED",
},
},
Ports: []v1.ContainerPort{
{
ContainerPort: 80,
},
},
},
},
},
},
},
}
client := NewWorkUtilsClient(scheme.Scheme)
updatedWork, err := client.Update(work, deployment)
require.Nil(t, err)
require.Equal(t, len(updatedWork.Spec.Workload.Manifests), 1)
obj, err := client.Get(updatedWork, Resource{
Group: "apps",
Version: "v1",
Kind: "Deployment",
Name: "nginx-deployment",
Namespace: "nginx",
})
require.Nil(t, err)
updatedDeployment := obj.(*appsv1.Deployment)
require.Equal(t, "UPDATED", updatedDeployment.Spec.Template.Spec.Containers[0].Env[0].Value)
}