auth_token_test.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { sleep, check, group } from 'k6';
  2. import { createClient, createBasicAuthClient } from './modules/client.js';
  3. import { createTestOrgIfNotExists, createTestdataDatasourceIfNotExists } from './modules/util.js';
  4. export let options = {
  5. noCookiesReset: true
  6. };
  7. let endpoint = __ENV.URL || 'http://localhost:3000';
  8. const client = createClient(endpoint);
  9. export const setup = () => {
  10. const basicAuthClient = createBasicAuthClient(endpoint, 'admin', 'admin');
  11. const orgId = createTestOrgIfNotExists(basicAuthClient);
  12. const datasourceId = createTestdataDatasourceIfNotExists(basicAuthClient);
  13. client.withOrgId(orgId);
  14. return {
  15. orgId: orgId,
  16. datasourceId: datasourceId,
  17. };
  18. }
  19. export default (data) => {
  20. group("user auth token test", () => {
  21. if (__ITER === 0) {
  22. group("user authenticates thru ui with username and password", () => {
  23. let res = client.ui.login('admin', 'admin');
  24. check(res, {
  25. 'response status is 200': (r) => r.status === 200,
  26. 'response has cookie \'grafana_session\' with 32 characters': (r) => r.cookies.grafana_session[0].value.length === 32,
  27. });
  28. });
  29. }
  30. if (__ITER !== 0) {
  31. group("batch tsdb requests", () => {
  32. const batchCount = 20;
  33. const requests = [];
  34. const payload = {
  35. from: '1547765247624',
  36. to: '1547768847624',
  37. queries: [{
  38. refId: 'A',
  39. scenarioId: 'random_walk',
  40. intervalMs: 10000,
  41. maxDataPoints: 433,
  42. datasourceId: data.datasourceId,
  43. }]
  44. };
  45. requests.push({ method: 'GET', url: '/api/annotations?dashboardId=2074&from=1548078832772&to=1548082432772' });
  46. for (let n = 0; n < batchCount; n++) {
  47. requests.push({ method: 'POST', url: '/api/tsdb/query', body: payload });
  48. }
  49. let responses = client.batch(requests);
  50. for (let n = 0; n < batchCount; n++) {
  51. check(responses[n], {
  52. 'response status is 200': (r) => r.status === 200,
  53. });
  54. }
  55. });
  56. }
  57. });
  58. sleep(5)
  59. }
  60. export const teardown = (data) => {}