-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgreetings_test.go
97 lines (90 loc) · 1.94 KB
/
greetings_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
package greetings_test
import (
"context"
"testing"
"time"
"github.com/verifa/horizon/examples/greetings"
"github.com/verifa/horizon/pkg/hz"
"github.com/verifa/horizon/pkg/hztest"
"github.com/verifa/horizon/pkg/server"
)
func TestGreeting(t *testing.T) {
ctx := context.Background()
// Create a test server which includes the core of Horizon.
ts := server.Test(t, ctx)
client := hz.NewClient(
ts.Conn,
hz.WithClientInternal(true),
hz.WithClientManager("ctlr-greeting"),
)
greetClient := hz.ObjectClient[greetings.Greeting]{Client: client}
//
// Setup greetings controller with validator and reconciler.
//
validr := greetings.GreetingValidator{}
recon := greetings.GreetingReconciler{
GreetingClient: greetClient,
}
ctlr, err := hz.StartController(
ctx,
ts.Conn,
hz.WithControllerFor(greetings.Greeting{}),
hz.WithControllerValidator(&validr),
hz.WithControllerReconciler(&recon),
)
if err != nil {
t.Fatal("starting greeting controller: ", err)
}
defer ctlr.Stop()
//
// Setup greetings actor.
//
action := greetings.GreetingsHelloAction{}
actor, err := hz.StartActor(
ctx,
ts.Conn,
hz.WithActorActioner(action),
)
if err != nil {
t.Fatal("starting greeting actor: ", err)
}
defer actor.Stop()
//
// Apply a greetings object.
//
greeting := greetings.Greeting{
ObjectMeta: hz.ObjectMeta{
Namespace: "test",
Name: "Pekka",
},
Spec: &greetings.GreetingSpec{
Name: "Pekka",
},
}
_, err = greetClient.Apply(ctx, greeting)
if err != nil {
t.Fatal("applying greeting: ", err)
}
//
// Verify that the controller reconciles the object.
//
// Watch until the greeting is ready.
// If the timeout is reached, the test fails.
//
hztest.WatchWaitUntil(
t,
ctx,
ts.Conn,
time.Second*5,
greeting,
func(greeting greetings.Greeting) bool {
if greeting.Status == nil {
return false
}
if greeting.Status.Ready == true {
return true
}
return false
},
)
}