44 "bytes"
55 "context"
66 "io"
7+ "strings"
78 "testing"
89
910 "github.com/fastly/go-fastly/v13/fastly"
@@ -22,6 +23,7 @@ func TestUsage(t *testing.T) {
2223 api mock.API
2324 wantError string
2425 wantOutput string
26+ wantAbsent string
2527 }{
2628 {
2729 name : "success plain" ,
@@ -59,6 +61,20 @@ func TestUsage(t *testing.T) {
5961 api : mock.API {GetUsageFn : getUsageWithNilEntry },
6062 wantOutput : "Region: europe" ,
6163 },
64+ {
65+ name : "region filter plain" ,
66+ args : args ("stats usage --region=europe" ),
67+ api : mock.API {GetUsageFn : getUsageMultiRegion },
68+ wantOutput : "Region: europe" ,
69+ wantAbsent : "usa" ,
70+ },
71+ {
72+ name : "region filter by-service" ,
73+ args : args ("stats usage --by-service --region=europe" ),
74+ api : mock.API {GetUsageByServiceFn : getUsageByServiceMultiRegion },
75+ wantOutput : "Region: europe" ,
76+ wantAbsent : "usa" ,
77+ },
6278 {
6379 name : "non-success status" ,
6480 args : args ("stats usage" ),
@@ -83,6 +99,9 @@ func TestUsage(t *testing.T) {
8399 err := app .Run (tc .args , nil )
84100 testutil .AssertErrorContains (t , err , tc .wantError )
85101 testutil .AssertStringContains (t , stdout .String (), tc .wantOutput )
102+ if tc .wantAbsent != "" && strings .Contains (stdout .String (), tc .wantAbsent ) {
103+ t .Errorf ("output should not contain %q, got: %s" , tc .wantAbsent , stdout .String ())
104+ }
86105 })
87106 }
88107}
@@ -138,6 +157,46 @@ func getUsageWithNilEntry(_ context.Context, _ *fastly.GetUsageInput) (*fastly.U
138157 }, nil
139158}
140159
160+ func getUsageMultiRegion (_ context.Context , _ * fastly.GetUsageInput ) (* fastly.UsageResponse , error ) {
161+ return & fastly.UsageResponse {
162+ Status : fastly .ToPointer ("success" ),
163+ Data : & fastly.RegionsUsage {
164+ "usa" : & fastly.Usage {
165+ Bandwidth : fastly .ToPointer (uint64 (1000 )),
166+ Requests : fastly .ToPointer (uint64 (500 )),
167+ ComputeRequests : fastly .ToPointer (uint64 (100 )),
168+ },
169+ "europe" : & fastly.Usage {
170+ Bandwidth : fastly .ToPointer (uint64 (2000 )),
171+ Requests : fastly .ToPointer (uint64 (300 )),
172+ ComputeRequests : fastly .ToPointer (uint64 (50 )),
173+ },
174+ },
175+ }, nil
176+ }
177+
178+ func getUsageByServiceMultiRegion (_ context.Context , _ * fastly.GetUsageInput ) (* fastly.UsageByServiceResponse , error ) {
179+ return & fastly.UsageByServiceResponse {
180+ Status : fastly .ToPointer ("success" ),
181+ Data : & fastly.ServicesByRegionsUsage {
182+ "usa" : & fastly.ServicesUsage {
183+ "svc123" : & fastly.Usage {
184+ Bandwidth : fastly .ToPointer (uint64 (1000 )),
185+ Requests : fastly .ToPointer (uint64 (500 )),
186+ ComputeRequests : fastly .ToPointer (uint64 (100 )),
187+ },
188+ },
189+ "europe" : & fastly.ServicesUsage {
190+ "svc456" : & fastly.Usage {
191+ Bandwidth : fastly .ToPointer (uint64 (2000 )),
192+ Requests : fastly .ToPointer (uint64 (300 )),
193+ ComputeRequests : fastly .ToPointer (uint64 (50 )),
194+ },
195+ },
196+ },
197+ }, nil
198+ }
199+
141200func getUsageNonSuccess (_ context.Context , _ * fastly.GetUsageInput ) (* fastly.UsageResponse , error ) {
142201 return & fastly.UsageResponse {
143202 Status : fastly .ToPointer ("error" ),
0 commit comments