88 "testing"
99 "time"
1010
11+ "github.com/alicebob/miniredis/v2"
1112 "github.com/google/uuid"
1213 "github.com/redis/go-redis/v9"
1314
@@ -17,6 +18,7 @@ import (
1718)
1819
1920func TestAgentAuthRecycledVMTriggersDeleteOnce (t * testing.T ) {
21+ rdb := newTestRedis (t )
2022 vmClient := & vmDeleterStub {ch : make (chan struct {}, 1 )}
2123 handler := & InternalHostHandler {
2224 logger : slog .New (slog .NewTextHandler (io .Discard , nil )),
@@ -32,7 +34,7 @@ func TestAgentAuthRecycledVMTriggersDeleteOnce(t *testing.T) {
3234 },
3335 },
3436 vmDeleter : vmClient ,
35- limiter : & setNXLimiterStub { result : true } ,
37+ limiter : rdb ,
3638 skipSoftDelete : func (ctx context.Context ) context.Context { return ctx },
3739 }
3840
@@ -50,6 +52,10 @@ func TestAgentAuthRecycledVMTriggersDeleteOnce(t *testing.T) {
5052}
5153
5254func TestAgentAuthRecycledVMLimitedSkipsDelete (t * testing.T ) {
55+ rdb := newTestRedis (t )
56+ if ok , err := rdb .SetNX (context .Background (), "vm:recycle:retry:agent_2" , "1" , time .Minute ).Result (); err != nil || ! ok {
57+ t .Fatalf ("seed redis limiter failed, ok=%v err=%v" , ok , err )
58+ }
5359 vmClient := & vmDeleterStub {ch : make (chan struct {}, 1 )}
5460 handler := & InternalHostHandler {
5561 logger : slog .New (slog .NewTextHandler (io .Discard , nil )),
@@ -65,7 +71,7 @@ func TestAgentAuthRecycledVMLimitedSkipsDelete(t *testing.T) {
6571 },
6672 },
6773 vmDeleter : vmClient ,
68- limiter : & setNXLimiterStub { result : false } ,
74+ limiter : rdb ,
6975 skipSoftDelete : func (ctx context.Context ) context.Context { return ctx },
7076 }
7177
@@ -79,6 +85,7 @@ func TestAgentAuthRecycledVMLimitedSkipsDelete(t *testing.T) {
7985}
8086
8187func TestAgentAuthSoftDeletedRecycledVMStillTriggersDelete (t * testing.T ) {
88+ rdb := newTestRedis (t )
8289 vmClient := & vmDeleterStub {ch : make (chan struct {}, 1 )}
8390 skipCalled := false
8491 type testSkipMarkerKey struct {}
@@ -101,7 +108,7 @@ func TestAgentAuthSoftDeletedRecycledVMStillTriggersDelete(t *testing.T) {
101108 getAgentToken : func (context.Context , string ) (string , error ) { return "" , redis .Nil },
102109 repo : repo ,
103110 vmDeleter : vmClient ,
104- limiter : & setNXLimiterStub { result : true } ,
111+ limiter : rdb ,
105112 skipSoftDelete : func (ctx context.Context ) context.Context {
106113 skipCalled = true
107114 return context .WithValue (ctx , markerKey , markerValue )
@@ -204,19 +211,6 @@ func (s *internalHostRepoStub) GetGitCredentialByTask(context.Context, string) (
204211 return nil , errors .New ("task not found" )
205212}
206213
207- type setNXLimiterStub struct {
208- result bool
209- err error
210- keys []string
211- ttl time.Duration
212- }
213-
214- func (s * setNXLimiterStub ) SetNX (_ context.Context , key string , _ interface {}, ttl time.Duration ) * redis.BoolCmd {
215- s .keys = append (s .keys , key )
216- s .ttl = ttl
217- return redis .NewBoolResult (s .result , s .err )
218- }
219-
220214type vmDeleterStub struct {
221215 reqs []* taskflow.DeleteVirtualMachineReq
222216 err error
@@ -235,6 +229,46 @@ func (s *vmDeleterStub) Delete(_ context.Context, req *taskflow.DeleteVirtualMac
235229 return s .err
236230}
237231
232+ func (s * vmDeleterStub ) Create (context.Context , * taskflow.CreateVirtualMachineReq ) (* taskflow.VirtualMachine , error ) {
233+ return nil , errors .New ("not implemented" )
234+ }
235+
236+ func (s * vmDeleterStub ) Hibernate (context.Context , * taskflow.HibernateVirtualMachineReq ) error {
237+ return errors .New ("not implemented" )
238+ }
239+
240+ func (s * vmDeleterStub ) Resume (context.Context , * taskflow.ResumeVirtualMachineReq ) error {
241+ return errors .New ("not implemented" )
242+ }
243+
244+ func (s * vmDeleterStub ) List (context.Context , string ) ([]* taskflow.VirtualMachine , error ) {
245+ return nil , errors .New ("not implemented" )
246+ }
247+
248+ func (s * vmDeleterStub ) Info (context.Context , taskflow.VirtualMachineInfoReq ) (* taskflow.VirtualMachine , error ) {
249+ return nil , errors .New ("not implemented" )
250+ }
251+
252+ func (s * vmDeleterStub ) Terminal (context.Context , * taskflow.TerminalReq ) (taskflow.Sheller , error ) {
253+ return nil , errors .New ("not implemented" )
254+ }
255+
256+ func (s * vmDeleterStub ) Reports (context.Context , taskflow.ReportSubscribeReq ) (taskflow.Reporter , error ) {
257+ return nil , errors .New ("not implemented" )
258+ }
259+
260+ func (s * vmDeleterStub ) TerminalList (context.Context , string ) ([]* taskflow.Terminal , error ) {
261+ return nil , errors .New ("not implemented" )
262+ }
263+
264+ func (s * vmDeleterStub ) CloseTerminal (context.Context , * taskflow.CloseTerminalReq ) error {
265+ return errors .New ("not implemented" )
266+ }
267+
268+ func (s * vmDeleterStub ) IsOnline (context.Context , * taskflow.IsOnlineReq [string ]) (* taskflow.IsOnlineResp , error ) {
269+ return nil , errors .New ("not implemented" )
270+ }
271+
238272func (s * vmDeleterStub ) waitReqs (t * testing.T , timeout time.Duration ) []* taskflow.DeleteVirtualMachineReq {
239273 t .Helper ()
240274 select {
@@ -254,3 +288,19 @@ func (s *vmDeleterStub) hasReqWithin(timeout time.Duration) bool {
254288 return false
255289 }
256290}
291+
292+ func newTestRedis (t * testing.T ) * redis.Client {
293+ t .Helper ()
294+
295+ mr , err := miniredis .Run ()
296+ if err != nil {
297+ t .Fatalf ("miniredis.Run() error = %v" , err )
298+ }
299+ t .Cleanup (mr .Close )
300+
301+ rdb := redis .NewClient (& redis.Options {Addr : mr .Addr ()})
302+ t .Cleanup (func () {
303+ _ = rdb .Close ()
304+ })
305+ return rdb
306+ }
0 commit comments