@@ -2,21 +2,30 @@ package test
22
33import (
44 "context"
5+ "errors"
56 "fmt"
7+ "net/http"
68 "testing"
79 "time"
810
11+ "github.com/gin-gonic/gin"
12+ "github.com/scroll-tech/da-codec/encoding"
913 "github.com/stretchr/testify/assert"
1014
15+ "scroll-tech/common/types/message"
16+ "scroll-tech/common/version"
17+
1118 "scroll-tech/coordinator/internal/config"
1219 "scroll-tech/coordinator/internal/controller/proxy"
20+ "scroll-tech/coordinator/internal/route"
1321)
1422
1523func testProxyClientCfg () * config.ProxyClient {
1624
1725 return & config.ProxyClient {
18- Secret : "test-secret-key" ,
19- ProxyName : "test-proxy" ,
26+ Secret : "test-secret-key" ,
27+ ProxyName : "test-proxy" ,
28+ ProxyVersion : version .Version ,
2029 }
2130}
2231
@@ -57,13 +66,107 @@ func testProxyClient(t *testing.T) {
5766 // Client should not be nil if login succeeds
5867 // Note: This might be nil if the coordinator is not properly set up for proxy authentication
5968 // but the test validates that the Client method completes without panic
60- t .Logf ("Client toke: %v" , client )
69+ assert .NotNil (t , client )
70+ assert .NotEmpty (t , client .Token ())
71+ t .Logf ("Client token: %s (%v)" , client .Token (), client )
72+ }
73+
74+ var (
75+ proxyConf * config.ProxyConfig
76+ )
77+
78+ func setupProxy (t * testing.T , proxyURL string , coordinatorURL []string ) * http.Server {
79+ var err error
80+ assert .NoError (t , err )
81+
82+ coordinators := make (map [string ]* config.UpStream )
83+ for i , n := range coordinatorURL {
84+ coordinators [fmt .Sprintf ("coordinator_%d" , i )] = testProxyUpStreamCfg (n )
85+ }
86+
87+ tokenTimeout = 60
88+ proxyConf = & config.ProxyConfig {
89+ ProxyName : "test_proxy" ,
90+ ProxyManager : & config.ProxyManager {
91+ Verifier : & config.VerifierConfig {
92+ MinProverVersion : "v4.4.89" ,
93+ Verifiers : []config.AssetConfig {{
94+ AssetsPath : "" ,
95+ ForkName : "euclidV2" ,
96+ }},
97+ },
98+ Client : testProxyClientCfg (),
99+ Auth : & config.Auth {
100+ Secret : "proxy" ,
101+ ChallengeExpireDurationSec : tokenTimeout ,
102+ LoginExpireDurationSec : tokenTimeout ,
103+ },
104+ },
105+ Coordinators : coordinators ,
106+ }
107+
108+ router := gin .New ()
109+ proxy .InitController (proxyConf , nil )
110+ route .ProxyRoute (router , proxyConf , nil )
111+ srv := & http.Server {
112+ Addr : proxyURL ,
113+ Handler : router ,
114+ }
115+ go func () {
116+ runErr := srv .ListenAndServe ()
117+ if runErr != nil && ! errors .Is (runErr , http .ErrServerClosed ) {
118+ assert .NoError (t , runErr )
119+ }
120+ }()
121+ time .Sleep (time .Second * 2 )
122+
123+ return srv
124+ }
125+
126+ func testProxyHandshake (t * testing.T ) {
127+ // Setup proxy http server.
128+ proxyURL := randomURL ()
129+ proxyHttpHandler := setupProxy (t , proxyURL , []string {})
130+ defer func () {
131+ assert .NoError (t , proxyHttpHandler .Shutdown (context .Background ()))
132+ }()
133+
134+ chunkProver := newMockProver (t , "prover_chunk_test" , proxyURL , message .ProofTypeChunk , version .Version )
135+ assert .True (t , chunkProver .healthCheckSuccess (t ))
136+ }
137+
138+ func testProxyGetTask (t * testing.T ) {
139+ // Setup coordinator and http server.
140+ coordinatorURL := randomURL ()
141+ collector , httpHandler := setupCoordinator (t , 3 , coordinatorURL )
142+ defer func () {
143+ collector .Stop ()
144+ assert .NoError (t , httpHandler .Shutdown (context .Background ()))
145+ }()
146+
147+ proxyURL := randomURL ()
148+ proxyHttpHandler := setupProxy (t , proxyURL , []string {coordinatorURL })
149+ defer func () {
150+ assert .NoError (t , proxyHttpHandler .Shutdown (context .Background ()))
151+ }()
152+
153+ err := l2BlockOrm .InsertL2Blocks (context .Background (), []* encoding.Block {block1 , block2 })
154+ assert .NoError (t , err )
155+ dbChunk , err := chunkOrm .InsertChunk (context .Background (), chunk )
156+ assert .NoError (t , err )
157+ err = l2BlockOrm .UpdateChunkHashInRange (context .Background (), 0 , 100 , dbChunk .Hash )
158+ assert .NoError (t , err )
61159
160+ chunkProver := newMockProver (t , "prover_chunk_test" , proxyURL , message .ProofTypeChunk , version .Version )
161+ code , _ := chunkProver .tryGetProverTask (t , message .ProofTypeChunk )
162+ assert .Empty (t , code )
62163}
63164
64165func TestProxyClient (t * testing.T ) {
65166
66167 // Set up the test environment.
67168 setEnv (t )
68- t .Run ("TestProxyHandshake" , testProxyClient )
169+ t .Run ("TestProxyClient" , testProxyClient )
170+ t .Run ("TestProxyHandshake" , testProxyHandshake )
171+ t .Run ("TestProxyGetTask" , testProxyGetTask )
69172}
0 commit comments