github.com/grafana/synthetic-monitoring-agent@v0.64.2/internal/limits/tenant_test.go (about) 1 package limits 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 8 "github.com/grafana/synthetic-monitoring-agent/internal/model" 9 sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring" 10 "github.com/stretchr/testify/require" 11 ) 12 13 var errTestTenantNotFound = errors.New("tenant not found") 14 15 type testTenantProvider struct { 16 tenants map[int64]*sm.Tenant 17 } 18 19 func (tp *testTenantProvider) GetTenant(ctx context.Context, ti *sm.TenantInfo) (*sm.Tenant, error) { 20 if t, ok := tp.tenants[ti.Id]; ok { 21 return t, nil 22 } 23 24 return nil, errTestTenantNotFound 25 } 26 27 var tp = &testTenantProvider{ 28 tenants: map[int64]*sm.Tenant{ 29 // Tenant with specific limits that override defaults 30 1: { 31 Id: 1, 32 Limits: &sm.TenantLimits{ 33 MaxMetricLabels: 30, 34 MaxLogLabels: 25, 35 }, 36 }, 37 // Tenant without specific limits 38 2: { 39 Id: 2, 40 Limits: nil, 41 }, 42 }, 43 } 44 45 func TestMetricLabels(t *testing.T) { 46 t.Parallel() 47 48 testCases := []struct { 49 name string 50 tenantID model.GlobalID 51 expLabels int 52 expErr error 53 }{ 54 { 55 name: "specific tenant limits", 56 tenantID: 1, 57 expLabels: 30, 58 }, 59 { 60 name: "default tenant limits", 61 tenantID: 2, 62 expLabels: maxMetricLabels, 63 }, 64 { 65 name: "expect error", 66 tenantID: 3, 67 expErr: errTestTenantNotFound, 68 }, 69 } 70 71 var ( 72 ctx = context.Background() 73 tl = NewTenantLimits(tp) 74 ) 75 76 for _, tc := range testCases { 77 t.Run(tc.name, func(t *testing.T) { 78 ll, err := tl.MetricLabels(ctx, tc.tenantID) 79 if err != nil { 80 require.Equal(t, tc.expErr, err) 81 } 82 83 require.Equal(t, tc.expLabels, ll) 84 }) 85 } 86 } 87 88 func TestLogLabels(t *testing.T) { 89 t.Parallel() 90 91 testCases := []struct { 92 name string 93 tenantID model.GlobalID 94 expLabels int 95 expErr error 96 }{ 97 { 98 name: "specific tenant limits", 99 tenantID: 1, 100 expLabels: 25, 101 }, 102 { 103 name: "default tenant limits", 104 tenantID: 2, 105 expLabels: maxLogLabels, 106 }, 107 { 108 name: "expect error", 109 tenantID: 3, 110 expErr: errTestTenantNotFound, 111 }, 112 } 113 114 var ( 115 ctx = context.Background() 116 tl = NewTenantLimits(tp) 117 ) 118 119 for _, tc := range testCases { 120 t.Run(tc.name, func(t *testing.T) { 121 ll, err := tl.LogLabels(ctx, tc.tenantID) 122 if err != nil { 123 require.Equal(t, tc.expErr, err) 124 } 125 126 require.Equal(t, tc.expLabels, ll) 127 }) 128 } 129 } 130 131 func TestValidateMetricLabels(t *testing.T) { 132 t.Parallel() 133 134 testCases := []struct { 135 name string 136 tenantID model.GlobalID 137 nLabels int 138 expErr error 139 }{ 140 { 141 name: "below tenant metric labels limit", 142 tenantID: 1, 143 nLabels: 25, 144 }, 145 { 146 name: "equal tenant metric labels limit", 147 tenantID: 1, 148 nLabels: 30, 149 }, 150 { 151 name: "below default metric labels limit", 152 tenantID: 2, 153 nLabels: 15, 154 }, 155 { 156 name: "over tenant metric labels limit", 157 tenantID: 1, 158 nLabels: 35, 159 expErr: ErrTooManyMetricLabels, 160 }, 161 { 162 name: "over tenant metric labels limit", 163 tenantID: 2, 164 nLabels: 23, 165 expErr: ErrTooManyMetricLabels, 166 }, 167 } 168 169 var ( 170 ctx = context.Background() 171 tl = NewTenantLimits(tp) 172 ) 173 174 for _, tc := range testCases { 175 t.Run(tc.name, func(t *testing.T) { 176 err := tl.ValidateMetricLabels(ctx, tc.tenantID, tc.nLabels) 177 require.Equal(t, tc.expErr, err) 178 }) 179 } 180 } 181 182 func TestValidateLogLabels(t *testing.T) { 183 t.Parallel() 184 185 testCases := []struct { 186 name string 187 tenantID model.GlobalID 188 nLabels int 189 expErr error 190 }{ 191 { 192 name: "below tenant log labels limit", 193 tenantID: 1, 194 nLabels: 21, 195 }, 196 { 197 name: "equal tenant log labels limit", 198 tenantID: 1, 199 nLabels: 25, 200 }, 201 { 202 name: "below default log labels limit", 203 tenantID: 2, 204 nLabels: 10, 205 }, 206 { 207 name: "over tenant log labels limit", 208 tenantID: 1, 209 nLabels: 29, 210 expErr: ErrTooManyLogLabels, 211 }, 212 { 213 name: "over tenant log labels limit", 214 tenantID: 2, 215 nLabels: 18, 216 expErr: ErrTooManyLogLabels, 217 }, 218 } 219 220 var ( 221 ctx = context.Background() 222 tl = NewTenantLimits(tp) 223 ) 224 225 for _, tc := range testCases { 226 t.Run(tc.name, func(t *testing.T) { 227 err := tl.ValidateLogLabels(ctx, tc.tenantID, tc.nLabels) 228 require.Equal(t, tc.expErr, err) 229 }) 230 } 231 }