|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"net/netip" |
| 6 | +"sync" |
| 7 | +"testing" |
| 8 | +"time" |
| 9 | + |
| 10 | +"github.com/stretchr/testify/require" |
| 11 | +"google.golang.org/protobuf/types/known/durationpb" |
| 12 | +"tailscale.com/types/ipproto" |
| 13 | + |
| 14 | +"tailscale.com/types/netlogtype" |
| 15 | + |
| 16 | +"cdr.dev/slog" |
| 17 | +"cdr.dev/slog/sloggers/slogtest" |
| 18 | +"github.com/coder/coder/v2/agent/proto" |
| 19 | +"github.com/coder/coder/v2/testutil" |
| 20 | +) |
| 21 | + |
| 22 | +funcTestStatsReporter(t*testing.T) { |
| 23 | +t.Parallel() |
| 24 | +ctx:=testutil.Context(t,testutil.WaitShort) |
| 25 | +logger:=slogtest.Make(t,nil).Leveled(slog.LevelDebug) |
| 26 | +fSource:=newFakeNetworkStatsSource(ctx,t) |
| 27 | +fCollector:=newFakeCollector(t) |
| 28 | +fDest:=newFakeStatsDest() |
| 29 | +uut:=newStatsReporter(logger,fSource,fCollector) |
| 30 | + |
| 31 | +loopErr:=make(chanerror,1) |
| 32 | +loopCtx,loopCancel:=context.WithCancel(ctx) |
| 33 | +gofunc() { |
| 34 | +err:=uut.reportLoop(loopCtx,fDest) |
| 35 | +loopErr<-err |
| 36 | +}() |
| 37 | + |
| 38 | +// initial request to get duration |
| 39 | +req:=testutil.RequireRecvCtx(ctx,t,fDest.reqs) |
| 40 | +require.NotNil(t,req) |
| 41 | +require.Nil(t,req.Stats) |
| 42 | +interval:=time.Second*34 |
| 43 | +testutil.RequireSendCtx(ctx,t,fDest.resps,&proto.UpdateStatsResponse{ReportInterval:durationpb.New(interval)}) |
| 44 | + |
| 45 | +// call to source to set the callback and interval |
| 46 | +gotInterval:=testutil.RequireRecvCtx(ctx,t,fSource.period) |
| 47 | +require.Equal(t,interval,gotInterval) |
| 48 | + |
| 49 | +// callback returning netstats |
| 50 | +netStats:=map[netlogtype.Connection]netlogtype.Counts{ |
| 51 | +{ |
| 52 | +Proto:ipproto.TCP, |
| 53 | +Src:netip.MustParseAddrPort("192.168.1.33:4887"), |
| 54 | +Dst:netip.MustParseAddrPort("192.168.2.99:9999"), |
| 55 | +}: { |
| 56 | +TxPackets:22, |
| 57 | +TxBytes:23, |
| 58 | +RxPackets:24, |
| 59 | +RxBytes:25, |
| 60 | +}, |
| 61 | +} |
| 62 | +fSource.callback(time.Now(),time.Now(),netStats,nil) |
| 63 | + |
| 64 | +// collector called to complete the stats |
| 65 | +gotNetStats:=testutil.RequireRecvCtx(ctx,t,fCollector.calls) |
| 66 | +require.Equal(t,netStats,gotNetStats) |
| 67 | + |
| 68 | +// while we are collecting the stats, send in two new netStats to simulate |
| 69 | +// what happens if we don't keep up. Only the latest should be kept. |
| 70 | +netStats0:=map[netlogtype.Connection]netlogtype.Counts{ |
| 71 | +{ |
| 72 | +Proto:ipproto.TCP, |
| 73 | +Src:netip.MustParseAddrPort("192.168.1.33:4887"), |
| 74 | +Dst:netip.MustParseAddrPort("192.168.2.99:9999"), |
| 75 | +}: { |
| 76 | +TxPackets:10, |
| 77 | +TxBytes:10, |
| 78 | +RxPackets:10, |
| 79 | +RxBytes:10, |
| 80 | +}, |
| 81 | +} |
| 82 | +fSource.callback(time.Now(),time.Now(),netStats0,nil) |
| 83 | +netStats1:=map[netlogtype.Connection]netlogtype.Counts{ |
| 84 | +{ |
| 85 | +Proto:ipproto.TCP, |
| 86 | +Src:netip.MustParseAddrPort("192.168.1.33:4887"), |
| 87 | +Dst:netip.MustParseAddrPort("192.168.2.99:9999"), |
| 88 | +}: { |
| 89 | +TxPackets:11, |
| 90 | +TxBytes:11, |
| 91 | +RxPackets:11, |
| 92 | +RxBytes:11, |
| 93 | +}, |
| 94 | +} |
| 95 | +fSource.callback(time.Now(),time.Now(),netStats1,nil) |
| 96 | + |
| 97 | +// complete first collection |
| 98 | +stats:=&proto.Stats{SessionCountJetbrains:55} |
| 99 | +testutil.RequireSendCtx(ctx,t,fCollector.stats,stats) |
| 100 | + |
| 101 | +// destination called to report the first stats |
| 102 | +update:=testutil.RequireRecvCtx(ctx,t,fDest.reqs) |
| 103 | +require.NotNil(t,update) |
| 104 | +require.Equal(t,stats,update.Stats) |
| 105 | +testutil.RequireSendCtx(ctx,t,fDest.resps,&proto.UpdateStatsResponse{ReportInterval:durationpb.New(interval)}) |
| 106 | + |
| 107 | +// second update -- only netStats1 is reported |
| 108 | +gotNetStats=testutil.RequireRecvCtx(ctx,t,fCollector.calls) |
| 109 | +require.Equal(t,netStats1,gotNetStats) |
| 110 | +stats=&proto.Stats{SessionCountJetbrains:66} |
| 111 | +testutil.RequireSendCtx(ctx,t,fCollector.stats,stats) |
| 112 | +update=testutil.RequireRecvCtx(ctx,t,fDest.reqs) |
| 113 | +require.NotNil(t,update) |
| 114 | +require.Equal(t,stats,update.Stats) |
| 115 | +testutil.RequireSendCtx(ctx,t,fDest.resps,&proto.UpdateStatsResponse{ReportInterval:durationpb.New(interval)}) |
| 116 | + |
| 117 | +loopCancel() |
| 118 | +err:=testutil.RequireRecvCtx(ctx,t,loopErr) |
| 119 | +require.NoError(t,err) |
| 120 | +} |
| 121 | + |
| 122 | +typefakeNetworkStatsSourcestruct { |
| 123 | +sync.Mutex |
| 124 | +ctx context.Context |
| 125 | +t testing.TB |
| 126 | +callbackfunc(start,end time.Time,virtual,physicalmap[netlogtype.Connection]netlogtype.Counts) |
| 127 | +periodchan time.Duration |
| 128 | +} |
| 129 | + |
| 130 | +func (f*fakeNetworkStatsSource)SetConnStatsCallback(maxPeriod time.Duration,_int,dumpfunc(start time.Time,end time.Time,virtualmap[netlogtype.Connection]netlogtype.Counts,physicalmap[netlogtype.Connection]netlogtype.Counts)) { |
| 131 | +f.Lock() |
| 132 | +deferf.Unlock() |
| 133 | +f.callback=dump |
| 134 | +select { |
| 135 | +case<-f.ctx.Done(): |
| 136 | +f.t.Error("timeout") |
| 137 | +casef.period<-maxPeriod: |
| 138 | +// OK |
| 139 | +} |
| 140 | +} |
| 141 | + |
| 142 | +funcnewFakeNetworkStatsSource(ctx context.Context,t testing.TB)*fakeNetworkStatsSource { |
| 143 | +f:=&fakeNetworkStatsSource{ |
| 144 | +ctx:ctx, |
| 145 | +t:t, |
| 146 | +period:make(chan time.Duration), |
| 147 | +} |
| 148 | +returnf |
| 149 | +} |
| 150 | + |
| 151 | +typefakeCollectorstruct { |
| 152 | +t testing.TB |
| 153 | +callschanmap[netlogtype.Connection]netlogtype.Counts |
| 154 | +statschan*proto.Stats |
| 155 | +} |
| 156 | + |
| 157 | +func (f*fakeCollector)Collect(ctx context.Context,networkStatsmap[netlogtype.Connection]netlogtype.Counts)*proto.Stats { |
| 158 | +select { |
| 159 | +case<-ctx.Done(): |
| 160 | +f.t.Error("timeout on collect") |
| 161 | +returnnil |
| 162 | +casef.calls<-networkStats: |
| 163 | +// ok |
| 164 | +} |
| 165 | +select { |
| 166 | +case<-ctx.Done(): |
| 167 | +f.t.Error("timeout on collect") |
| 168 | +returnnil |
| 169 | +cases:=<-f.stats: |
| 170 | +returns |
| 171 | +} |
| 172 | +} |
| 173 | + |
| 174 | +funcnewFakeCollector(t testing.TB)*fakeCollector { |
| 175 | +return&fakeCollector{ |
| 176 | +t:t, |
| 177 | +calls:make(chanmap[netlogtype.Connection]netlogtype.Counts), |
| 178 | +stats:make(chan*proto.Stats), |
| 179 | +} |
| 180 | +} |
| 181 | + |
| 182 | +typefakeStatsDeststruct { |
| 183 | +reqschan*proto.UpdateStatsRequest |
| 184 | +respschan*proto.UpdateStatsResponse |
| 185 | +} |
| 186 | + |
| 187 | +func (f*fakeStatsDest)UpdateStats(ctx context.Context,req*proto.UpdateStatsRequest) (*proto.UpdateStatsResponse,error) { |
| 188 | +select { |
| 189 | +case<-ctx.Done(): |
| 190 | +returnnil,ctx.Err() |
| 191 | +casef.reqs<-req: |
| 192 | +// OK |
| 193 | +} |
| 194 | +select { |
| 195 | +case<-ctx.Done(): |
| 196 | +returnnil,ctx.Err() |
| 197 | +caseresp:=<-f.resps: |
| 198 | +returnresp,nil |
| 199 | +} |
| 200 | +} |
| 201 | + |
| 202 | +funcnewFakeStatsDest()*fakeStatsDest { |
| 203 | +return&fakeStatsDest{ |
| 204 | +reqs:make(chan*proto.UpdateStatsRequest), |
| 205 | +resps:make(chan*proto.UpdateStatsResponse), |
| 206 | +} |
| 207 | +} |