|
| 1 | +package blob |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log/slog" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/prometheus/client_golang/prometheus" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + cloudcost_exporter "github.com/grafana/cloudcost-exporter" |
| 16 | +) |
| 17 | + |
| 18 | +var testLogger = slog.New(slog.NewTextHandler(os.Stdout, nil)) |
| 19 | + |
| 20 | +func testCollectSink() chan prometheus.Metric { |
| 21 | + return make(chan prometheus.Metric, 8) |
| 22 | +} |
| 23 | + |
| 24 | +func TestCollector_Describe(t *testing.T) { |
| 25 | + c, err := New(&Config{Logger: testLogger}) |
| 26 | + require.NoError(t, err) |
| 27 | + ch := make(chan *prometheus.Desc, 4) |
| 28 | + require.NoError(t, c.Describe(ch)) |
| 29 | + close(ch) |
| 30 | + var got []string |
| 31 | + for d := range ch { |
| 32 | + got = append(got, d.String()) |
| 33 | + } |
| 34 | + require.Len(t, got, 1) |
| 35 | + joined := strings.Join(got, " ") |
| 36 | + prefix := cloudcost_exporter.MetricPrefix + "_azure_blob_" |
| 37 | + assert.Contains(t, joined, prefix+"storage_by_location_usd_per_gibyte_hour") |
| 38 | +} |
| 39 | + |
| 40 | +func TestCollector_Register(t *testing.T) { |
| 41 | + c, err := New(&Config{Logger: testLogger}) |
| 42 | + require.NoError(t, err) |
| 43 | + // Register does not call registry.MustRegister on cost metrics (AKS pattern). |
| 44 | + require.NoError(t, c.Register(nil)) |
| 45 | +} |
| 46 | + |
| 47 | +func TestCollector_Collect_forwardsStorageGauge(t *testing.T) { |
| 48 | + c, err := New(&Config{Logger: testLogger}) |
| 49 | + require.NoError(t, err) |
| 50 | + ctx := context.Background() |
| 51 | + require.NoError(t, c.Collect(ctx, testCollectSink())) |
| 52 | +} |
| 53 | + |
| 54 | +func TestNew_configPlumbing(t *testing.T) { |
| 55 | + const subUUID = "11111111-1111-1111-1111-111111111111" |
| 56 | + tests := map[string]struct { |
| 57 | + subscriptionID string |
| 58 | + scrapeInterval time.Duration |
| 59 | + wantInterval time.Duration |
| 60 | + }{ |
| 61 | + "zero scrape interval defaults to one hour": { |
| 62 | + subscriptionID: "sub-1", |
| 63 | + scrapeInterval: 0, |
| 64 | + wantInterval: time.Hour, |
| 65 | + }, |
| 66 | + "explicit subscription and interval": { |
| 67 | + subscriptionID: subUUID, |
| 68 | + scrapeInterval: 30 * time.Minute, |
| 69 | + wantInterval: 30 * time.Minute, |
| 70 | + }, |
| 71 | + } |
| 72 | + for name, tt := range tests { |
| 73 | + t.Run(name, func(t *testing.T) { |
| 74 | + c, err := New(&Config{ |
| 75 | + Logger: testLogger, |
| 76 | + SubscriptionId: tt.subscriptionID, |
| 77 | + ScrapeInterval: tt.scrapeInterval, |
| 78 | + }) |
| 79 | + require.NoError(t, err) |
| 80 | + assert.Equal(t, tt.subscriptionID, c.subscriptionID) |
| 81 | + assert.Equal(t, tt.wantInterval, c.scrapeInterval) |
| 82 | + }) |
| 83 | + } |
| 84 | +} |
0 commit comments