github.com/grafana/synthetic-monitoring-agent@v0.64.2/internal/cals/cals_test.go (about) 1 package cals 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" 9 "github.com/stretchr/testify/require" 10 ) 11 12 type mockTenantProvider struct { 13 tenant *sm.Tenant 14 err error 15 } 16 17 func (m mockTenantProvider) GetTenant(_ context.Context, _ *sm.TenantInfo) (*sm.Tenant, error) { 18 return m.tenant, m.err 19 } 20 21 func TestTenantCostAttributionLabels_GetCostAttributionLabels(t *testing.T) { 22 testcases := map[string]struct { 23 tenantProvider *mockTenantProvider 24 expectedCostAttributionLabels []string 25 expectError bool 26 }{ 27 "cals should match up": { 28 tenantProvider: &mockTenantProvider{ 29 tenant: &sm.Tenant{ 30 CostAttributionLabels: []string{"this", "is", "a", "test"}, 31 }, 32 }, 33 expectedCostAttributionLabels: []string{"this", "is", "a", "test"}, 34 expectError: false, 35 }, 36 "Handle returning an error": { 37 tenantProvider: &mockTenantProvider{ 38 tenant: &sm.Tenant{}, 39 err: fmt.Errorf("error getting tenant info"), 40 }, 41 expectedCostAttributionLabels: []string{}, 42 expectError: true, 43 }, 44 } 45 for name, testcase := range testcases { 46 t.Run(name, func(t *testing.T) { 47 tcal := NewCostAttributionLabels(testcase.tenantProvider) 48 49 cals, err := tcal.CostAttributionLabels(context.Background(), 1) 50 if testcase.expectError { 51 require.ErrorIs(t, err, ErrTenantProvider) 52 } 53 54 require.ElementsMatch(t, cals, testcase.expectedCostAttributionLabels) 55 }) 56 } 57 }