11package elb
22
33import (
4+ "errors"
45 "log/slog"
56 "testing"
67 "time"
@@ -24,6 +25,7 @@ func TestNew(t *testing.T) {
2425 Regions : []ec2Types.Region {
2526 {RegionName : stringPtr ("us-east-1" )},
2627 },
28+ PricingClient : mockClient ,
2729 RegionClients : map [string ]client.Client {
2830 "us-east-1" : mockClient ,
2931 },
@@ -36,6 +38,7 @@ func TestNew(t *testing.T) {
3638 assert .NotNil (t , collector )
3739 assert .Equal (t , config .ScrapeInterval , collector .ScrapeInterval )
3840 assert .Equal (t , config .Regions , collector .regions )
41+ assert .Equal (t , mockClient , collector .pricingClient )
3942 assert .Equal (t , mockClient , collector .awsRegionClientMap ["us-east-1" ])
4043 assert .NotNil (t , collector .pricingMap )
4144}
@@ -130,6 +133,58 @@ func TestCollectRegionLoadBalancers(t *testing.T) {
130133
131134}
132135
136+ func TestFetchRegionPricing (t * testing.T ) {
137+ ctrl := gomock .NewController (t )
138+ mockClient := mock_client .NewMockClient (ctrl )
139+
140+ albProduct := `{"Product":{"Attributes":{"usageType":"USE1-LoadBalancerUsage","operation":"LoadBalancing:Application"}},"Terms":{"OnDemand":{"t1":{"PriceDimensions":{"d1":{"pricePerUnit":{"USD":"0.0225"}}}}}}}`
141+ nlbProduct := `{"Product":{"Attributes":{"usageType":"USE1-LCUUsage","operation":"LoadBalancing:Network"}},"Terms":{"OnDemand":{"t1":{"PriceDimensions":{"d1":{"pricePerUnit":{"USD":"0.006"}}}}}}}`
142+ mockClient .EXPECT ().ListELBPrices (gomock .Any (), "us-east-1" ).Return ([]string {albProduct , nlbProduct }, nil )
143+
144+ pm := NewELBPricingMap (slog .Default ())
145+ pricing , err := pm .FetchRegionPricing (mockClient , t .Context (), "us-east-1" )
146+
147+ assert .NoError (t , err )
148+ assert .Equal (t , 0.0225 , pricing .ALBHourlyRate [LoadBalancerUsage ])
149+ assert .Equal (t , 0.006 , pricing .NLBHourlyRate [LCUUsage ])
150+ }
151+
152+ func TestFetchRegionPricingError (t * testing.T ) {
153+ ctrl := gomock .NewController (t )
154+ mockClient := mock_client .NewMockClient (ctrl )
155+ mockClient .EXPECT ().ListELBPrices (gomock .Any (), "us-east-1" ).Return (nil , errors .New ("api error" ))
156+
157+ pm := NewELBPricingMap (slog .Default ())
158+ pricing , err := pm .FetchRegionPricing (mockClient , t .Context (), "us-east-1" )
159+
160+ assert .Error (t , err )
161+ assert .Nil (t , pricing )
162+ }
163+
164+ func TestRefresh (t * testing.T ) {
165+ ctrl := gomock .NewController (t )
166+ mockClient := mock_client .NewMockClient (ctrl )
167+
168+ albProduct := `{"Product":{"Attributes":{"usageType":"USE1-LoadBalancerUsage","operation":"LoadBalancing:Application"}},"Terms":{"OnDemand":{"t1":{"PriceDimensions":{"d1":{"pricePerUnit":{"USD":"0.0225"}}}}}}}`
169+ mockClient .EXPECT ().ListELBPrices (gomock .Any (), "us-east-1" ).Return ([]string {albProduct }, nil )
170+ mockClient .EXPECT ().ListELBPrices (gomock .Any (), "us-west-2" ).Return ([]string {albProduct }, nil )
171+
172+ pm := NewELBPricingMap (slog .Default ())
173+ regions := []ec2Types.Region {
174+ {RegionName : stringPtr ("us-east-1" )},
175+ {RegionName : stringPtr ("us-west-2" )},
176+ }
177+
178+ err := pm .refresh (t .Context (), mockClient , regions )
179+ assert .NoError (t , err )
180+
181+ for _ , region := range []string {"us-east-1" , "us-west-2" } {
182+ pricing , err := pm .GetRegionPricing (region )
183+ assert .NoError (t , err )
184+ assert .Equal (t , 0.0225 , pricing .ALBHourlyRate [LoadBalancerUsage ])
185+ }
186+ }
187+
133188func stringPtr (s string ) * string {
134189 return & s
135190}
0 commit comments