@@ -17,6 +17,7 @@ import (
1717 "github.com/prometheus/client_golang/prometheus"
1818 "github.com/stretchr/testify/assert"
1919 "github.com/stretchr/testify/mock"
20+ "github.com/stretchr/testify/require"
2021
2122 awsclient "github.com/grafana/cloudcost-exporter/pkg/aws/client"
2223 "github.com/grafana/cloudcost-exporter/pkg/utils"
@@ -155,6 +156,41 @@ func TestDescribe(t *testing.T) {
155156 assert .NoError (t , err )
156157}
157158
159+ func TestCollect_EmitsMetrics (t * testing.T ) {
160+ logger := slog .New (slog .NewTextHandler (os .Stdout , nil ))
161+ mockClient := & MockClient {}
162+
163+ products := []string {
164+ `{"product":{"attributes":{"usageType":"USE1-VpcEndpoint-Hours","regionCode":"us-east-1"}},"terms":{"OnDemand":{"t1":{"priceDimensions":{"d1":{"pricePerUnit":{"USD":"0.01"}}}}}}}` ,
165+ `{"product":{"attributes":{"usageType":"USE1-VpcEndpoint-Service-Hours","regionCode":"us-east-1"}},"terms":{"OnDemand":{"t1":{"priceDimensions":{"d1":{"pricePerUnit":{"USD":"0.01"}}}}}}}` ,
166+ `{"product":{"attributes":{"usageType":"USE1-TransitGateway-Hours","regionCode":"us-east-1"}},"terms":{"OnDemand":{"t1":{"priceDimensions":{"d1":{"pricePerUnit":{"USD":"0.05"}}}}}}}` ,
167+ `{"product":{"attributes":{"usageType":"USE1-PublicIPv4:InUseAddress","regionCode":"us-east-1"}},"terms":{"OnDemand":{"t1":{"priceDimensions":{"d1":{"pricePerUnit":{"USD":"0.005"}}}}}}}` ,
168+ `{"product":{"attributes":{"usageType":"USE1-PublicIPv4:IdleAddress","regionCode":"us-east-1"}},"terms":{"OnDemand":{"t1":{"priceDimensions":{"d1":{"pricePerUnit":{"USD":"0.003"}}}}}}}` ,
169+ }
170+ mockClient .On ("ListVPCServicePrices" , mock .Anything , mock .AnythingOfType ("string" ), mock .Anything ).Return (products , nil )
171+
172+ collector , err := New (t .Context (), & Config {
173+ ScrapeInterval : time .Hour ,
174+ Regions : []ec2Types.Region {{RegionName : utils .StringPtr ("us-east-1" )}},
175+ Client : mockClient ,
176+ AccountID : "123456789012" ,
177+ }, logger )
178+ require .NoError (t , err )
179+
180+ ch := make (chan prometheus.Metric , 10 )
181+ err = collector .Collect (t .Context (), ch )
182+ close (ch )
183+
184+ require .NoError (t , err )
185+
186+ var metrics []prometheus.Metric
187+ for m := range ch {
188+ metrics = append (metrics , m )
189+ }
190+ // One metric per rate type: VPC endpoint, VPC service endpoint, Transit Gateway, EIP in-use, EIP idle
191+ assert .Len (t , metrics , 5 )
192+ }
193+
158194// Test that VPC pricing map methods return errors when no data is available
159195func TestVPCPricingMapErrors (t * testing.T ) {
160196 // Use a logger that discards output to reduce test noise
0 commit comments