File: environment_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (344 lines) | stat: -rw-r--r-- 9,588 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'
require 'fileutils'

MOCKED_DOCKERENV_PATH = File.expand_path(File.join(Dir.pwd, '.dockerenv-mocked'))

module ContainerChecking
  def mock_dockerenv_path
    before do
      allow_any_instance_of(Mongo::Server::AppMetadata::Environment)
        .to receive(:dockerenv_path)
        .and_return(MOCKED_DOCKERENV_PATH)
    end
  end

  def with_docker
    mock_dockerenv_path

    around do |example|
      File.write(MOCKED_DOCKERENV_PATH, 'placeholder')
      example.run
    ensure
      File.delete(MOCKED_DOCKERENV_PATH)
    end
  end

  def without_docker
    mock_dockerenv_path

    around do |example|
      FileUtils.rm_f(MOCKED_DOCKERENV_PATH)
      example.run
    end
  end

  def with_kubernetes
    local_env 'KUBERNETES_SERVICE_HOST' => 'kubernetes.default.svc.cluster.local'
  end

  def without_kubernetes
    local_env 'KUBERNETES_SERVICE_HOST' => nil
  end
end

describe Mongo::Server::AppMetadata::Environment do
  extend ContainerChecking

  let(:env) { described_class.new }

  shared_examples_for 'running in a FaaS environment' do
    it 'reports that a FaaS environment is detected' do
      expect(env.faas?).to be true
    end
  end

  shared_examples_for 'running outside a FaaS environment' do
    it 'reports that no FaaS environment is detected' do
      expect(env.faas?).to be false
    end
  end

  shared_examples_for 'not running in a Docker container' do
    it 'does not detect Docker' do
      expect(env.container || {}).not_to include :runtime
    end
  end

  shared_examples_for 'not running under Kubernetes' do
    it 'does not detect Kubernetes' do
      expect(env.container || {}).not_to include :orchestrator
    end
  end

  shared_examples_for 'running under Kubernetes' do
    it 'detects that Kubernetes is present' do
      expect(env.container[:orchestrator]).to be == 'kubernetes'
    end
  end

  shared_examples_for 'running in a Docker container' do
    it 'detects that Docker is present' do
      expect(env.container[:runtime]).to be == 'docker'
    end
  end

  shared_examples_for 'running under Kerbenetes' do
    it 'detects that kubernetes is present' do
      expect(env.container['orchestrator']).to be == 'kubernetes'
    end
  end

  context 'when run outside of a FaaS environment' do
    it_behaves_like 'running outside a FaaS environment'
  end

  context 'when run in a FaaS environment' do
    context 'when environment is invalid due to type mismatch' do
      local_env(
        'AWS_EXECUTION_ENV' => 'AWS_Lambda_ruby2.7',
        'AWS_REGION' => 'us-east-2',
        'AWS_LAMBDA_FUNCTION_MEMORY_SIZE' => 'big'
      )

      it_behaves_like 'running outside a FaaS environment'

      it 'fails due to type mismatch' do
        expect(env.error).to match(/AWS_LAMBDA_FUNCTION_MEMORY_SIZE must be integer/)
      end
    end

    context 'when environment is invalid due to long string' do
      local_env(
        'AWS_EXECUTION_ENV' => 'AWS_Lambda_ruby2.7',
        'AWS_REGION' => 'a' * 512,
        'AWS_LAMBDA_FUNCTION_MEMORY_SIZE' => '1024'
      )

      it_behaves_like 'running outside a FaaS environment'

      it 'fails due to long string' do
        expect(env.error).to match(/too long/)
      end
    end

    context 'when environment is invalid due to multiple providers' do
      local_env(
        'AWS_EXECUTION_ENV' => 'AWS_Lambda_ruby2.7',
        'AWS_REGION' => 'us-east-2',
        'AWS_LAMBDA_FUNCTION_MEMORY_SIZE' => '1024',
        'FUNCTIONS_WORKER_RUNTIME' => 'ruby'
      )

      it_behaves_like 'running outside a FaaS environment'

      it 'fails due to multiple providers' do
        expect(env.error).to match(/too many environments/)
      end
    end

    context 'when VERCEL and AWS are both given' do
      local_env(
        'AWS_EXECUTION_ENV' => 'AWS_Lambda_ruby2.7',
        'AWS_REGION' => 'us-east-2',
        'AWS_LAMBDA_FUNCTION_MEMORY_SIZE' => '1024',
        'VERCEL' => '1',
        'VERCEL_REGION' => 'cdg1'
      )

      it_behaves_like 'running in a FaaS environment'

      it 'prefers vercel' do
        expect(env.aws?).to be false
        expect(env.vercel?).to be true
        expect(env.fields[:region]).to be == 'cdg1'
      end
    end

    context 'when environment is invalid due to missing variable' do
      local_env(
        'AWS_EXECUTION_ENV' => 'AWS_Lambda_ruby2.7',
        'AWS_LAMBDA_FUNCTION_MEMORY_SIZE' => '1024'
      )

      it_behaves_like 'running outside a FaaS environment'

      it 'fails due to missing variable' do
        expect(env.error).to match(/missing environment variable/)
      end
    end

    context 'when FaaS environment is AWS' do
      shared_examples_for 'running in an AWS environment' do
        context 'when environment is valid' do
          local_env(
            'AWS_REGION' => 'us-east-2',
            'AWS_LAMBDA_FUNCTION_MEMORY_SIZE' => '1024'
          )

          it_behaves_like 'running in a FaaS environment'

          it 'recognizes AWS' do
            expect(env.name).to be == 'aws.lambda'
            expect(env.fields[:region]).to be == 'us-east-2'
            expect(env.fields[:memory_mb]).to be == 1024
          end
        end
      end

      # per DRIVERS-2623, AWS_EXECUTION_ENV must be prefixed
      # with 'AWS_Lambda_'.
      context 'when AWS_EXECUTION_ENV is invalid' do
        local_env(
          'AWS_EXECUTION_ENV' => 'EC2',
          'AWS_REGION' => 'us-east-2',
          'AWS_LAMBDA_FUNCTION_MEMORY_SIZE' => '1024'
        )

        it_behaves_like 'running outside a FaaS environment'
      end

      context 'when AWS_EXECUTION_ENV is detected' do
        local_env('AWS_EXECUTION_ENV' => 'AWS_Lambda_ruby2.7')
        it_behaves_like 'running in an AWS environment'
      end

      context 'when AWS_LAMBDA_RUNTIME_API is detected' do
        local_env('AWS_LAMBDA_RUNTIME_API' => 'lambda.aws.amazon.com/api')
        it_behaves_like 'running in an AWS environment'
      end
    end

    context 'when FaaS environment is Azure' do
      local_env('FUNCTIONS_WORKER_RUNTIME' => 'ruby')

      it_behaves_like 'running in a FaaS environment'

      it 'recognizes Azure' do
        expect(env.name).to be == 'azure.func'
      end
    end

    context 'when FaaS environment is GCP' do
      local_env(
        'FUNCTION_MEMORY_MB' => '1024',
        'FUNCTION_TIMEOUT_SEC' => '60',
        'FUNCTION_REGION' => 'us-central1'
      )

      shared_examples_for 'running in a GCP environment' do
        it_behaves_like 'running in a FaaS environment'

        it 'recognizes GCP' do
          expect(env.name).to be == 'gcp.func'
          expect(env.fields[:region]).to be == 'us-central1'
          expect(env.fields[:memory_mb]).to be == 1024
          expect(env.fields[:timeout_sec]).to be == 60
        end
      end

      context 'when K_SERVICE is present' do
        local_env('K_SERVICE' => 'servicename')
        it_behaves_like 'running in a GCP environment'
      end

      context 'when FUNCTION_NAME is present' do
        local_env('FUNCTION_NAME' => 'functionName')
        it_behaves_like 'running in a GCP environment'
      end
    end

    context 'when FaaS environment is Vercel' do
      local_env(
        'VERCEL' => '1',
        'VERCEL_REGION' => 'cdg1'
      )

      it_behaves_like 'running in a FaaS environment'

      it 'recognizes Vercel' do
        expect(env.name).to be == 'vercel'
        expect(env.fields[:region]).to be == 'cdg1'
      end
    end

    context 'when converting environment to a hash' do
      local_env(
        'K_SERVICE' => 'servicename',
        'FUNCTION_MEMORY_MB' => '1024',
        'FUNCTION_TIMEOUT_SEC' => '60',
        'FUNCTION_REGION' => 'us-central1'
      )

      it 'includes name and all fields' do
        expect(env.to_h).to be == {
          name: 'gcp.func', memory_mb: 1024,
          timeout_sec: 60, region: 'us-central1',
        }
      end

      context 'when a container is present' do
        with_kubernetes
        with_docker

        it 'includes a container key' do
          expect(env.to_h[:container]).to be == {
            runtime: 'docker',
            orchestrator: 'kubernetes'
          }
        end
      end

      context 'when no container is present' do
        without_kubernetes
        without_docker

        it 'does not include a container key' do
          expect(env.to_h).not_to include(:container)
        end
      end
    end
  end

  # have a specific test for this, since the tests that check
  # for Docker use a mocked value for the .dockerenv path.
  it 'should look for dockerenv in root directory' do
    expect(described_class::DOCKERENV_PATH).to be == '/.dockerenv'
  end

  context 'when no container is present' do
    without_kubernetes
    without_docker

    it_behaves_like 'not running in a Docker container'
    it_behaves_like 'not running under Kubernetes'
  end

  context 'when container is present' do
    context 'when kubernetes is present' do
      without_docker
      with_kubernetes

      it_behaves_like 'not running in a Docker container'
      it_behaves_like 'running under Kubernetes'
    end

    context 'when docker is present' do
      with_docker
      without_kubernetes

      it_behaves_like 'running in a Docker container'
      it_behaves_like 'not running under Kubernetes'
    end

    context 'when both kubernetes and docker are present' do
      with_docker
      with_kubernetes

      it_behaves_like 'running in a Docker container'
      it_behaves_like 'running under Kubernetes'
    end
  end
end