forked from openshift/machine-api-operator
/
testing.go
166 lines (152 loc) · 4.53 KB
/
testing.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
package testing
import (
"fmt"
"time"
mapiv1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/utils/pointer"
)
const (
// Namespace contains the default namespace for machine-api components
Namespace = "openshift-machine-api"
// MachineAnnotationKey contains default machine node annotation
MachineAnnotationKey = "machine.openshift.io/machine"
)
var (
// KnownDate contains date that can be used under tests
KnownDate = metav1.Time{Time: time.Date(1985, 06, 03, 0, 0, 0, 0, time.Local)}
)
// FooBar returns foo:bar map that can be used as default label
func FooBar() map[string]string {
return map[string]string{"foo": "bar"}
}
// NewSelector returns new LabelSelector
func NewSelector(labels map[string]string) *metav1.LabelSelector {
return &metav1.LabelSelector{MatchLabels: labels}
}
// NewSelectorFooBar returns new foo:bar label selector
func NewSelectorFooBar() *metav1.LabelSelector {
return NewSelector(FooBar())
}
func NewExternalRemediationTemplate() *unstructured.Unstructured {
// Create remediation template resource.
infraRemediationTmpl := &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": "InfrastructureRemediationTemplate",
"apiVersion": "infrastructure.machine.openshift.io/v1alpha3",
"spec": map[string]interface{}{
"template": map[string]interface{}{},
},
"metadata": map[string]interface{}{
"namespace": Namespace,
},
},
}
return infraRemediationTmpl
}
func NewExternalRemediationMachine() *unstructured.Unstructured {
return &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": "InfrastructureRemediation",
"apiVersion": "infrastructure.machine.openshift.io/v1alpha3",
"metadata": map[string]interface{}{
"name": "Machine",
"namespace": Namespace,
},
},
}
}
// NewNode returns new node object that can be used for testing
func NewNode(name string, ready bool) *corev1.Node {
nodeReadyStatus := corev1.ConditionTrue
if !ready {
nodeReadyStatus = corev1.ConditionUnknown
}
return &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: metav1.NamespaceNone,
Annotations: map[string]string{
MachineAnnotationKey: fmt.Sprintf("%s/%s", Namespace, "fakeMachine"),
},
Labels: map[string]string{},
UID: uuid.NewUUID(),
},
TypeMeta: metav1.TypeMeta{
Kind: "Node",
},
Status: corev1.NodeStatus{
Conditions: []corev1.NodeCondition{
{
Type: corev1.NodeReady,
Status: nodeReadyStatus,
LastTransitionTime: KnownDate,
},
},
},
}
}
// NewMachine returns new machine object that can be used for testing
func NewMachine(name string, nodeName string) *mapiv1.Machine {
m := &mapiv1.Machine{
TypeMeta: metav1.TypeMeta{Kind: "Machine"},
ObjectMeta: metav1.ObjectMeta{
Annotations: make(map[string]string),
Name: name,
Namespace: Namespace,
Labels: FooBar(),
UID: uuid.NewUUID(),
OwnerReferences: []metav1.OwnerReference{
{
Kind: "MachineSet",
Controller: pointer.BoolPtr(true),
},
},
// the following line is to account for a change in the fake client, see https://github.com/kubernetes-sigs/controller-runtime/pull/1306
ResourceVersion: "999",
},
Spec: mapiv1.MachineSpec{},
}
if nodeName != "" {
m.Status = mapiv1.MachineStatus{
NodeRef: &corev1.ObjectReference{
Name: nodeName,
Namespace: metav1.NamespaceNone,
},
}
}
return m
}
// NewMachineHealthCheck returns new MachineHealthCheck object that can be used for testing
func NewMachineHealthCheck(name string) *mapiv1.MachineHealthCheck {
return &mapiv1.MachineHealthCheck{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: Namespace,
// the following line is to account for a change in the fake client, see https://github.com/kubernetes-sigs/controller-runtime/pull/1306
ResourceVersion: "999",
},
TypeMeta: metav1.TypeMeta{
Kind: "MachineHealthCheck",
},
Spec: mapiv1.MachineHealthCheckSpec{
Selector: *NewSelectorFooBar(),
UnhealthyConditions: []mapiv1.UnhealthyCondition{
{
Type: "Ready",
Status: "Unknown",
Timeout: metav1.Duration{Duration: 300 * time.Second},
},
{
Type: "Ready",
Status: "False",
Timeout: metav1.Duration{Duration: 300 * time.Second},
},
},
},
Status: mapiv1.MachineHealthCheckStatus{},
}
}