@@ -3,26 +3,114 @@ package control
33import (
44 "context"
55 "testing"
6+ "time"
67
7- "github.com/nats-io/nats.go"
88 "go.wasmcloud.dev/wasmbus"
9+ "go.wasmcloud.dev/wasmbus/wasmbustest"
910)
1011
1112func TestClient (t * testing.T ) {
12- nc , err := nats .Connect (nats .DefaultURL )
13+ nc , teardown := wasmbustest .WithWash (t )
14+ defer teardown (t )
15+
16+ bus := wasmbus .NewNatsBus (nc )
17+ c := NewClient (bus , "default" )
18+
19+ t .Run ("component" , wrapTest (testComponent , c ))
20+ t .Run ("provider" , wrapTest (testProvider , c ))
21+
22+ }
23+
24+ func wrapTest (f func (* testing.T , * Client ), c * Client ) func (* testing.T ) {
25+ return func (t * testing.T ) {
26+ f (t , c )
27+ }
28+ }
29+
30+ func testProvider (t * testing.T , c * Client ) {
31+ req := & ProviderAuctionRequest {
32+ ProviderId : "test-provider" ,
33+ ProviderRef : wasmbustest .ValidProvider ,
34+ Constraints : make (map [string ]string ),
35+ }
36+
37+ resp , err := c .ProviderAuction (context .TODO (), req )
1338 if err != nil {
14- t .Fatal ( err )
39+ t .Fatalf ( "failed to auction: %v" , err )
1540 }
1641
17- bus := wasmbus .NewNatsBus (nc )
18- client := NewClient (bus , "default" )
19- t .Log ("Client created" )
42+ if ! resp .Success {
43+ t .Fatalf ("auction failed: %v" , resp )
44+ }
45+
46+ if resp .Response .HostId == "" {
47+ t .Fatalf ("host id is empty" )
48+ }
49+
50+ reqStart := & ProviderStartRequest {
51+ HostId : resp .Response .HostId ,
52+ ProviderId : req .ProviderId ,
53+ ProviderRef : req .ProviderRef ,
54+ }
2055
21- resp , err := client .ConfigGet (context .Background (), & ConfigGetRequest {
22- Name : "test" ,
23- })
56+ startResp , err := c .ProviderStart (context .TODO (), reqStart )
2457 if err != nil {
25- t .Fatal (err )
58+ t .Fatalf ("failed to start: %v" , err )
59+ }
60+
61+ if ! startResp .Success {
62+ t .Fatalf ("start failed: %v" , startResp )
63+ }
64+ }
65+
66+ func testComponent (t * testing.T , c * Client ) {
67+ // we first need an auction to find the host id
68+
69+ auctionReq := & ComponentAuctionRequest {
70+ ComponentId : "test-component" ,
71+ ComponentRef : wasmbustest .ValidComponent ,
72+ Constraints : make (map [string ]string ),
73+ }
74+
75+ auctionResp , err := c .ComponentAuction (context .TODO (), auctionReq )
76+ if err != nil {
77+ t .Fatalf ("failed to auction: %v" , err )
78+ }
79+
80+ scaleReq := & ScaleComponentRequest {
81+ HostId : auctionResp .Response .HostId ,
82+ ComponentId : auctionReq .ComponentId ,
83+ ComponentRef : auctionReq .ComponentRef ,
84+ Count : 1 ,
85+ }
86+
87+ scaleResp , err := c .ScaleComponent (context .TODO (), scaleReq )
88+ if err != nil {
89+ t .Fatalf ("failed to scale: %v" , err )
90+ }
91+
92+ if ! scaleResp .Success {
93+ t .Fatalf ("scale failed: %v" , scaleResp )
94+ }
95+
96+ // NOTE(lxf): it takes time for the component to be ready
97+ // and the only way to know is to watch for lattice events.
98+ // For now, we'll just sleep for a bit.
99+ <- time .After (1 * time .Second )
100+
101+ updateReq := & UpdateComponentRequest {
102+ HostId : auctionResp .Response .HostId ,
103+ ComponentId : auctionReq .ComponentId ,
104+ NewComponentRef : auctionReq .ComponentRef ,
105+ Annotations : map [string ]string {"test" : "test" },
106+ }
107+
108+ updateResp , err := c .UpdateComponent (context .TODO (), updateReq )
109+ if err != nil {
110+ t .Fatalf ("failed to update: %v" , err )
111+ }
112+
113+ if ! updateResp .Success {
114+ t .Fatalf ("update failed: %v" , updateResp )
26115 }
27- t .Log (resp )
28116}
0 commit comments