File: test_metrics_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (71 lines) | stat: -rw-r--r-- 2,681 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

RSpec.describe QA::Tools::Ci::TestMetrics do
  include QA::Support::Helpers::StubEnv

  let(:influx_client) { instance_double("InfluxDB2::Client", create_write_api: influx_write_api) }
  let(:influx_write_api) { instance_double("InfluxDB2::WriteApi", write: nil) }
  let(:gcs_client_options) { { force: true, content_type: 'application/json' } }
  let(:gcs_client) { double("Fog::Storage::GoogleJSON::Real", put_object: nil) } # rubocop:disable RSpec/VerifiedDoubles -- Class has `put_object` method but is not getting verified
  let(:logger) { instance_double("Logger", info: true, warn: true) }

  let(:glob) { "metrics_glob/*.json" }
  let(:paths) { ["/metrics_glob/metrics.json"] }
  let(:timestamp) { "2022-11-11 07:54:11 +0000" }
  let(:metrics_json) { metrics_data.to_json }

  let(:metrics_gcs_project_id) { 'metrics-gcs-project' }
  let(:metrics_gcs_creds) { 'metrics-gcs-creds' }
  let(:metrics_gcs_bucket_name) { 'metrics-gcs-bucket' }

  let(:metrics_data) do
    [
      {
        time: timestamp.to_time,
        name: "name",
        tags: {},
        fields: {}
      }
    ]
  end

  before do
    allow(InfluxDB2::Client).to receive(:new) { influx_client }

    allow(Fog::Storage::Google).to receive(:new)
                                     .with(google_project: metrics_gcs_project_id,
                                       google_json_key_string: metrics_gcs_creds)
                                     .and_return(gcs_client)
    allow(Gitlab::QA::TestLogger).to receive(:logger) { logger }
    allow(Dir).to receive(:glob).with(glob) { paths }
    allow(File).to receive(:read).with(paths.first) { metrics_json }

    stub_env('QA_INFLUXDB_URL', "test")
    stub_env('QA_INFLUXDB_TOKEN', "test")
    stub_env('QA_METRICS_GCS_PROJECT_ID', metrics_gcs_project_id)
    stub_env('QA_METRICS_GCS_CREDS', metrics_gcs_creds)
    stub_env('QA_METRICS_GCS_BUCKET_NAME', metrics_gcs_bucket_name)
  end

  context "with metrics files present" do
    it "exports saved metrics to influxdb and GCS", :aggregate_failures do
      described_class.export(glob)

      expect(influx_write_api).to have_received(:write).with(data: metrics_data, bucket: "e2e-test-stats-main")

      expect(gcs_client).to have_received(:put_object).with(metrics_gcs_bucket_name,
        anything, metrics_data.to_json, **gcs_client_options)
    end
  end

  context "without metrics files present" do
    let(:paths) { [] }

    it "exits without error" do
      described_class.export(glob)

      expect(influx_write_api).not_to have_received(:write)
      expect(logger).to have_received(:warn).with("No files matched pattern '#{glob}'")
    end
  end
end