package
2.22.3+incompatible
Repository: https://github.com/grafana/opensearch-datasource.git
Documentation: pkg.go.dev

# README

Contributing to snapshot tests

Test names

  • File names begin with query_type, e.g. lucene_raw_data_test.go
  • Each file/query type contains two tests:
    • Test_query_type_request: Given query input from the data source frontend in Grafana, validates the request made to OpenSearch
    • Test_query_type_response: Given the response from OpenSearch, validates the snapshot of a data frame generated by the data source at the time of writing the test

File names in testdata

  • Begins with the query type, e.g. lucene_raw_data
  • Then the role of the file in the tests:
    • query_input: from Grafana, the query JSON from the data source frontend to the backend
    • response_from_opensearch
    • expected_result_generated.golden.jsonc - data frame snapshots - when this assertion fails, some non-regression has not passed

To add a request test:

  1. Create a new empty query_input file in testdata with the new query type, e.g. lucene_logs.query_input.json
  2. Access an instance of Grafana with this data source.
  3. Execute a query type for which we are adding a new test.
  4. Use the Network tab to access the payload. Copy only the queries array into the lucene_logs.query_input.json file.
  5. Up one level in snapshot_tests, create a new file lucene_logs_test.go
  6. Copy an existing test Test_query_type_request and find and replace the query type with your query type e.g. replace instances of lucene_raw_data with lucene_logs in order to follow the naming conventions. Make sure any references to file names have been searched and replaced.
  7. Run the test and replace the expected request until it passes.
  8. Make sure it makes sense, you could query in OpenSearch with DevTools.

To add a response test:

  1. To intercept the response from OpenSearch, set debugEnabled = true on the baseClientImpl. We'll use some existing code which reads the response body. There's a method that can be called.
  2. Access an instance of Grafana with this data source and execute a query type for which we are adding a new test. You may want to limit the response by limiting the time range or setting a smaller size.
  3. Set a breakpoint right after the body is read here for Lucene and here for PPL. The tmpBytes JSON is the response from OpenSearch.
  4. Create a new empty response_from_opensearch file in testdata with the new query type, e.g.: lucene_logs.response_from_opensearch.json
  5. Copy the tmpBytes JSON into this file.
  6. Copy the test ending in response and find and replace the query type with your query type e.g. Test_logs_response. Make sure you are referencing the file you created earlier, e.g. lucene_logs.response_from_opensearch.json
  7. Set the update argument of experimental.CheckGoldenJSONResponse() to be true so that it generates a new file.
  8. Run it once (it will fail while generating the new file).
  9. Run it again. It should pass. This is your data frame snapshot.