summaryrefslogtreecommitdiff
path: root/spec/controllers/chaos_controller_spec.rb
blob: 26ae4a6b693a9a79926217498bde4607995022fe (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ChaosController do
  describe '#leakmem' do
    it 'calls synchronously' do
      expect(Gitlab::Chaos).to receive(:leak_mem).with(100, 30.seconds)

      get :leakmem

      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'call synchronously with params' do
      expect(Gitlab::Chaos).to receive(:leak_mem).with(1, 2.seconds)

      get :leakmem, params: { memory_mb: 1, duration_s: 2 }

      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'calls asynchronously' do
      expect(Chaos::LeakMemWorker).to receive(:perform_async).with(100, 30.seconds)

      get :leakmem, params: { async: 1 }

      expect(response).to have_gitlab_http_status(:ok)
    end
  end

  describe '#cpu_spin' do
    it 'calls synchronously' do
      expect(Gitlab::Chaos).to receive(:cpu_spin).with(30.seconds)

      get :cpu_spin

      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'calls synchronously with params' do
      expect(Gitlab::Chaos).to receive(:cpu_spin).with(3.seconds)

      get :cpu_spin, params: { duration_s: 3 }

      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'calls asynchronously' do
      expect(Chaos::CpuSpinWorker).to receive(:perform_async).with(30.seconds)

      get :cpu_spin, params: { async: 1 }

      expect(response).to have_gitlab_http_status(:ok)
    end
  end

  describe '#db_spin' do
    it 'calls synchronously' do
      expect(Gitlab::Chaos).to receive(:db_spin).with(30.seconds, 1.second)

      get :db_spin

      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'calls synchronously with params' do
      expect(Gitlab::Chaos).to receive(:db_spin).with(4.seconds, 5.seconds)

      get :db_spin, params: { duration_s: 4, interval_s: 5 }

      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'calls asynchronously' do
      expect(Chaos::DbSpinWorker).to receive(:perform_async).with(30.seconds, 1.second)

      get :db_spin, params: { async: 1 }

      expect(response).to have_gitlab_http_status(:ok)
    end
  end

  describe '#sleep' do
    it 'calls synchronously' do
      expect(Gitlab::Chaos).to receive(:sleep).with(30.seconds)

      get :sleep

      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'calls synchronously with params' do
      expect(Gitlab::Chaos).to receive(:sleep).with(5.seconds)

      get :sleep, params: { duration_s: 5 }

      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'calls asynchronously' do
      expect(Chaos::SleepWorker).to receive(:perform_async).with(30.seconds)

      get :sleep, params: { async: 1 }

      expect(response).to have_gitlab_http_status(:ok)
    end
  end

  describe '#kill' do
    it 'calls synchronously' do
      expect(Gitlab::Chaos).to receive(:kill).with('KILL')

      get :kill

      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'calls asynchronously' do
      expect(Chaos::KillWorker).to receive(:perform_async).with('KILL')

      get :kill, params: { async: 1 }

      expect(response).to have_gitlab_http_status(:ok)
    end
  end

  describe '#quit' do
    it 'calls synchronously' do
      expect(Gitlab::Chaos).to receive(:kill).with('QUIT')

      get :quit

      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'calls asynchronously' do
      expect(Chaos::KillWorker).to receive(:perform_async).with('QUIT')

      get :quit, params: { async: 1 }

      expect(response).to have_gitlab_http_status(:ok)
    end
  end

  describe '#gc' do
    let(:gc_stat) { GC.stat.stringify_keys }

    it 'runs a full GC on the current web worker' do
      expect(Prometheus::PidProvider).to receive(:worker_id).and_return('worker-0')
      expect(Gitlab::Chaos).to receive(:run_gc).and_return(gc_stat)

      post :gc

      expect(response).to have_gitlab_http_status(:ok)
      expect(response_json['worker_id']).to eq('worker-0')
      expect(response_json['gc_stat']).to eq(gc_stat)
    end
  end

  def response_json
    Gitlab::Json.parse(response.body)
  end
end