-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathdemo_test.go
174 lines (151 loc) · 5.11 KB
/
demo_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
package controllers
import (
goctx "context"
"testing"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
metal3v1alpha1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/metal3-io/baremetal-operator/pkg/provisioner/demo"
)
func newDemoReconciler(initObjs ...runtime.Object) *BareMetalHostReconciler {
c := fakeclient.NewFakeClient(initObjs...)
// Add a default secret that can be used by most hosts.
bmcSecret := newSecret(defaultSecretName, map[string]string{"username": "User", "password": "Pass"})
c.Create(goctx.TODO(), bmcSecret)
return &BareMetalHostReconciler{
Client: c,
Scheme: scheme.Scheme,
ProvisionerFactory: demo.New,
Log: ctrl.Log.WithName("controller").WithName("baremetalhost").WithName("demo").WithName("test"),
}
}
// TestDemoRegistrationError tests that a host with the right name reports
// a registration error
func TestDemoRegistrationError(t *testing.T) {
host := newDefaultNamedHost(demo.RegistrationErrorHost, t)
r := newDemoReconciler(host)
tryReconcile(t, r, host,
func(host *metal3v1alpha1.BareMetalHost, result reconcile.Result) bool {
t.Logf("Status: %q State: %q ErrorMessage: %q",
host.OperationalStatus(),
host.Status.Provisioning.State,
host.Status.ErrorMessage,
)
return host.HasError()
},
)
}
// TestDemoRegistering tests that a host with the right name reports
// that it is being registered
func TestDemoRegistering(t *testing.T) {
host := newDefaultNamedHost(demo.RegisteringHost, t)
r := newDemoReconciler(host)
tryReconcile(t, r, host,
func(host *metal3v1alpha1.BareMetalHost, result reconcile.Result) bool {
t.Logf("Status: %q State: %q ErrorMessage: %q",
host.OperationalStatus(),
host.Status.Provisioning.State,
host.Status.ErrorMessage,
)
return host.Status.Provisioning.State == metal3v1alpha1.StateRegistering
},
)
}
// TestDemoInspecting tests that a host with the right name reports
// that it is being inspected
func TestDemoInspecting(t *testing.T) {
host := newDefaultNamedHost(demo.InspectingHost, t)
r := newDemoReconciler(host)
tryReconcile(t, r, host,
func(host *metal3v1alpha1.BareMetalHost, result reconcile.Result) bool {
t.Logf("Status: %q State: %q ErrorMessage: %q",
host.OperationalStatus(),
host.Status.Provisioning.State,
host.Status.ErrorMessage,
)
return host.Status.Provisioning.State == metal3v1alpha1.StateInspecting
},
)
}
// TestDemoReady tests that a host with the right name reports
// that it is ready to be provisioned
func TestDemoReady(t *testing.T) {
host := newDefaultNamedHost(demo.ReadyHost, t)
r := newDemoReconciler(host)
tryReconcile(t, r, host,
func(host *metal3v1alpha1.BareMetalHost, result reconcile.Result) bool {
t.Logf("Status: %q State: %q ErrorMessage: %q",
host.OperationalStatus(),
host.Status.Provisioning.State,
host.Status.ErrorMessage,
)
return host.Status.Provisioning.State == metal3v1alpha1.StateReady
},
)
}
// TestDemoProvisioning tests that a host with the right name reports
// that it is being provisioned
func TestDemoProvisioning(t *testing.T) {
host := newDefaultNamedHost(demo.ProvisioningHost, t)
host.Spec.Image = &metal3v1alpha1.Image{
URL: "a-url",
Checksum: "a-checksum",
}
host.Spec.Online = true
r := newDemoReconciler(host)
tryReconcile(t, r, host,
func(host *metal3v1alpha1.BareMetalHost, result reconcile.Result) bool {
t.Logf("Status: %q State: %q ErrorMessage: %q",
host.OperationalStatus(),
host.Status.Provisioning.State,
host.Status.ErrorMessage,
)
return host.Status.Provisioning.State == metal3v1alpha1.StateProvisioning
},
)
}
// TestDemoProvisioned tests that a host with the right name reports
// that it has been provisioned
func TestDemoProvisioned(t *testing.T) {
host := newDefaultNamedHost(demo.ProvisionedHost, t)
host.Spec.Image = &metal3v1alpha1.Image{
URL: "a-url",
Checksum: "a-checksum",
}
host.Spec.Online = true
r := newDemoReconciler(host)
tryReconcile(t, r, host,
func(host *metal3v1alpha1.BareMetalHost, result reconcile.Result) bool {
t.Logf("Status: %q State: %q ErrorMessage: %q",
host.OperationalStatus(),
host.Status.Provisioning.State,
host.Status.ErrorMessage,
)
return host.Status.Provisioning.State == metal3v1alpha1.StateProvisioned
},
)
}
// TestDemoValidationError tests that a host with the right name
// reports that it had and error while being provisioned
func TestDemoValidationError(t *testing.T) {
host := newDefaultNamedHost(demo.ValidationErrorHost, t)
host.Spec.Image = &metal3v1alpha1.Image{
URL: "a-url",
Checksum: "a-checksum",
}
host.Spec.Online = true
r := newDemoReconciler(host)
tryReconcile(t, r, host,
func(host *metal3v1alpha1.BareMetalHost, result reconcile.Result) bool {
t.Logf("Status: %q State: %q ErrorMessage: %q",
host.OperationalStatus(),
host.Status.Provisioning.State,
host.Status.ErrorMessage,
)
return host.HasError()
},
)
}