|
| 1 | +import { config } from '@grafana/runtime'; |
1 | 2 | import { BASIC_CHECK_ALERTS } from 'test/fixtures/checkAlerts'; |
2 | 3 | import { BASIC_HTTP_CHECK } from 'test/fixtures/checks'; |
3 | | -import { SM_DATASOURCE } from 'test/fixtures/datasources'; |
| 4 | +import { LOGS_DATASOURCE, METRICS_DATASOURCE, SM_DATASOURCE } from 'test/fixtures/datasources'; |
4 | 5 | import { PRIVATE_PROBE } from 'test/fixtures/probes'; |
5 | 6 | import { apiRoute, ApiRoutes, getServerRequests } from 'test/handlers'; |
6 | 7 | import { server } from 'test/server'; |
@@ -144,4 +145,115 @@ describe('SMDataSource', () => { |
144 | 145 | expect(request.headers.get('X-Client-ID')).toEqual(process.env.SM_PLUGIN_ID); |
145 | 146 | expect(request.headers.get('X-Client-Version')).toEqual(process.env.SM_PLUGIN_VERSION); |
146 | 147 | }); |
| 148 | + |
| 149 | + describe('getLogsDS', () => { |
| 150 | + beforeEach(() => { |
| 151 | + config.datasources = {}; |
| 152 | + }); |
| 153 | + |
| 154 | + it('should return the configured datasource when it exists', () => { |
| 155 | + config.datasources = { |
| 156 | + [LOGS_DATASOURCE.name]: LOGS_DATASOURCE, |
| 157 | + }; |
| 158 | + |
| 159 | + const smDataSource = new SMDataSource(SM_DATASOURCE); |
| 160 | + const result = smDataSource.getLogsDS(); |
| 161 | + |
| 162 | + expect(result).toEqual(LOGS_DATASOURCE); |
| 163 | + expect(result?.uid).toEqual('P4DC6B4C9A7FFCC6C'); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should fall back to default grafanacloud-logs UID when configured datasource does not exist', () => { |
| 167 | + const defaultLogsDS = { ...LOGS_DATASOURCE, uid: 'grafanacloud-logs' }; |
| 168 | + config.datasources = { |
| 169 | + [defaultLogsDS.name]: defaultLogsDS, |
| 170 | + }; |
| 171 | + |
| 172 | + const smDataSourceWithMissingConfig = new SMDataSource({ |
| 173 | + ...SM_DATASOURCE, |
| 174 | + jsonData: { |
| 175 | + ...SM_DATASOURCE.jsonData, |
| 176 | + logs: { |
| 177 | + ...SM_DATASOURCE.jsonData.logs, |
| 178 | + uid: 'non-existent-uid', |
| 179 | + }, |
| 180 | + }, |
| 181 | + }); |
| 182 | + |
| 183 | + const result = smDataSourceWithMissingConfig.getLogsDS(); |
| 184 | + |
| 185 | + expect(result).toEqual(defaultLogsDS); |
| 186 | + expect(result?.uid).toEqual('grafanacloud-logs'); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should respect custom datasource configuration', () => { |
| 190 | + const lbacLogsDS = { |
| 191 | + ...LOGS_DATASOURCE, |
| 192 | + uid: 'grafanacloud-logs-lbac', |
| 193 | + name: 'grafanacloud-ckbedwellksix-logs-lbac', |
| 194 | + }; |
| 195 | + |
| 196 | + config.datasources = { |
| 197 | + [lbacLogsDS.name]: lbacLogsDS, |
| 198 | + }; |
| 199 | + |
| 200 | + const smDataSourceWithLBAC = new SMDataSource({ |
| 201 | + ...SM_DATASOURCE, |
| 202 | + jsonData: { |
| 203 | + ...SM_DATASOURCE.jsonData, |
| 204 | + logs: { |
| 205 | + ...SM_DATASOURCE.jsonData.logs, |
| 206 | + uid: 'grafanacloud-logs-lbac', |
| 207 | + grafanaName: 'grafanacloud-ckbedwellksix-logs-lbac', |
| 208 | + }, |
| 209 | + }, |
| 210 | + }); |
| 211 | + |
| 212 | + const result = smDataSourceWithLBAC.getLogsDS(); |
| 213 | + |
| 214 | + expect(result).toEqual(lbacLogsDS); |
| 215 | + expect(result?.uid).toEqual('grafanacloud-logs-lbac'); |
| 216 | + }); |
| 217 | + }); |
| 218 | + |
| 219 | + describe('getMetricsDS', () => { |
| 220 | + beforeEach(() => { |
| 221 | + config.datasources = {}; |
| 222 | + }); |
| 223 | + |
| 224 | + it('should return the configured datasource when it exists', () => { |
| 225 | + config.datasources = { |
| 226 | + [METRICS_DATASOURCE.name]: METRICS_DATASOURCE, |
| 227 | + }; |
| 228 | + |
| 229 | + const smDataSource = new SMDataSource(SM_DATASOURCE); |
| 230 | + const result = smDataSource.getMetricsDS(); |
| 231 | + |
| 232 | + expect(result).toEqual(METRICS_DATASOURCE); |
| 233 | + expect(result?.uid).toEqual('P4DCEA413A673ADCC'); |
| 234 | + }); |
| 235 | + |
| 236 | + it('should fall back to default grafanacloud-metrics UID when configured datasource does not exist', () => { |
| 237 | + const defaultMetricsDS = { ...METRICS_DATASOURCE, uid: 'grafanacloud-metrics' }; |
| 238 | + config.datasources = { |
| 239 | + [defaultMetricsDS.name]: defaultMetricsDS, |
| 240 | + }; |
| 241 | + |
| 242 | + const smDataSourceWithMissingConfig = new SMDataSource({ |
| 243 | + ...SM_DATASOURCE, |
| 244 | + jsonData: { |
| 245 | + ...SM_DATASOURCE.jsonData, |
| 246 | + metrics: { |
| 247 | + ...SM_DATASOURCE.jsonData.metrics, |
| 248 | + uid: 'non-existent-uid', |
| 249 | + }, |
| 250 | + }, |
| 251 | + }); |
| 252 | + |
| 253 | + const result = smDataSourceWithMissingConfig.getMetricsDS(); |
| 254 | + |
| 255 | + expect(result).toEqual(defaultMetricsDS); |
| 256 | + expect(result?.uid).toEqual('grafanacloud-metrics'); |
| 257 | + }); |
| 258 | + }); |
147 | 259 | }); |
0 commit comments