@@ -6,31 +6,51 @@ package runtime_test
66
77import (
88 "context"
9+ "net"
910 goruntime "runtime"
1011 "strconv"
1112 "testing"
1213 "time"
1314
15+ "github.com/siderolabs/gen/xtesting/must"
1416 "github.com/stretchr/testify/assert"
1517 "github.com/stretchr/testify/require"
1618 suiterunner "github.com/stretchr/testify/suite"
1719 "go.uber.org/goleak"
1820 "go.uber.org/zap/zaptest"
21+ "google.golang.org/grpc"
22+ "google.golang.org/grpc/credentials/insecure"
1923
24+ "github.com/cosi-project/runtime/api/v1alpha1"
2025 "github.com/cosi-project/runtime/pkg/controller/conformance"
2126 "github.com/cosi-project/runtime/pkg/controller/runtime"
2227 "github.com/cosi-project/runtime/pkg/controller/runtime/options"
2328 "github.com/cosi-project/runtime/pkg/future"
2429 "github.com/cosi-project/runtime/pkg/resource"
30+ "github.com/cosi-project/runtime/pkg/resource/protobuf"
2531 "github.com/cosi-project/runtime/pkg/safe"
2632 "github.com/cosi-project/runtime/pkg/state"
2733 stateconformance "github.com/cosi-project/runtime/pkg/state/conformance"
2834 "github.com/cosi-project/runtime/pkg/state/impl/inmem"
2935 "github.com/cosi-project/runtime/pkg/state/impl/namespaced"
36+ "github.com/cosi-project/runtime/pkg/state/protobuf/client"
37+ "github.com/cosi-project/runtime/pkg/state/protobuf/server"
3038)
3139
40+ func noError (err error ) {
41+ if err != nil {
42+ panic (err )
43+ }
44+ }
45+
46+ func init () {
47+ noError (protobuf .RegisterResource (conformance .IntResourceType , & conformance.IntResource {}))
48+ noError (protobuf .RegisterResource (conformance .StrResourceType , & conformance.StrResource {}))
49+ noError (protobuf .RegisterResource (conformance .SentenceResourceType , & conformance.SentenceResource {}))
50+ }
51+
3252func TestRuntimeConformance (t * testing.T ) {
33- for _ , tt := range []struct {
53+ tests := []struct {
3454 name string
3555 opts []options.Option
3656 metricsReadCacheEnabled bool
@@ -68,25 +88,63 @@ func TestRuntimeConformance(t *testing.T) {
6888 options .WithWarnOnUncachedReads (true ),
6989 },
7090 },
71- } {
91+ }
92+
93+ for _ , tt := range tests {
7294 t .Run (tt .name , func (t * testing.T ) {
7395 t .Cleanup (func () { goleak .VerifyNone (t , goleak .IgnoreCurrent ()) })
7496
75- suite := & conformance.RuntimeSuite {
97+ suiterunner . Run ( t , & conformance.RuntimeSuite {
7698 MetricsReadCacheEnabled : tt .metricsReadCacheEnabled ,
77- }
78- suite .SetupRuntime = func () {
79- suite .State = state .WrapCore (namespaced .NewState (inmem .Build ))
99+ SetupRuntime : func (rs * conformance.RuntimeSuite ) {
100+ rs .State = state .WrapCore (namespaced .NewState (inmem .Build ))
101+ logger := zaptest .NewLogger (rs .T ())
102+ rs .Runtime = must .Value (runtime .NewRuntime (rs .State , logger , tt .opts ... ))(rs .T ())
103+ },
104+ })
105+ })
106+ }
80107
81- var err error
108+ const listenOn = "127.0.0.1:0"
82109
83- logger := zaptest . NewLogger ( t )
110+ t . Log ( "testing networked runtime" )
84111
85- suite .Runtime , err = runtime .NewRuntime (suite .State , logger , tt .opts ... )
86- suite .Require ().NoError (err )
87- }
112+ for _ , tt := range tests {
113+ t .Run (tt .name + "_over-network" , func (t * testing.T ) {
114+ t .Cleanup (func () { goleak .VerifyNone (t , goleak .IgnoreCurrent ()) })
115+
116+ suiterunner .Run (t , & conformance.RuntimeSuite {
117+ MetricsReadCacheEnabled : tt .metricsReadCacheEnabled ,
118+ SetupRuntime : func (rs * conformance.RuntimeSuite ) {
119+ l := must .Value (net .Listen ("tcp" , listenOn ))(rs .T ())
120+
121+ grpcServer := grpc .NewServer ()
122+ inmemState := state .WrapCore (namespaced .NewState (inmem .Build ))
123+ v1alpha1 .RegisterStateServer (grpcServer , server .NewState (inmemState ))
124+
125+ go func () { assert .NoError (rs .T (), grpcServer .Serve (l )) }()
126+
127+ rs .T ().Cleanup (func () { grpcServer .Stop () })
128+
129+ grpcConn := must .Value (grpc .NewClient (
130+ l .Addr ().String (),
131+ grpc .WithTransportCredentials (insecure .NewCredentials ()),
132+ ))(rs .T ())
133+
134+ rs .T ().Cleanup (func () { assert .NoError (rs .T (), grpcConn .Close ()) })
135+
136+ stateClient := v1alpha1 .NewStateClient (grpcConn )
137+ rs .State = state .WrapCore (client .NewAdapter (stateClient ))
138+
139+ must .Value (rs .State .List (rs .Context (), conformance .NewIntResource ("default" , "zero" , 0 ).Metadata ()))(rs .T ())
88140
89- suiterunner .Run (t , suite )
141+ rs .Runtime = must .Value (runtime .NewRuntime (
142+ rs .State ,
143+ zaptest .NewLogger (rs .T ()),
144+ tt .opts ... ,
145+ ))(rs .T ())
146+ },
147+ })
90148 })
91149 }
92150}
0 commit comments