File: app_metadata_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 (158 lines) | stat: -rw-r--r-- 4,619 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
# frozen_string_literal: true

require 'spec_helper'

describe Mongo::Server::AppMetadata do
  let(:max_size) { described_class::Truncator::MAX_DOCUMENT_SIZE }

  let(:app_metadata) do
    described_class.new(cluster.options)
  end

  let(:cluster) do
    authorized_client.cluster
  end

  describe '#initialize' do
    context 'when the cluster has an app name option set' do
      let(:client) do
        authorized_client.with(app_name: :app_metadata_test)
      end

      let(:cluster) do
        client.cluster
      end

      it 'sets the app name' do
        expect(app_metadata.client_document[:application][:name]).to eq('app_metadata_test')
      end

      context 'when the app name exceeds the max length of 128' do
        let(:client) do
          authorized_client.with(app_name: "\u3042" * 43)
        end

        let(:cluster) do
          client.cluster
        end

        it 'raises an error' do
          expect { app_metadata.validated_document }
            .to raise_exception(Mongo::Error::InvalidApplicationName)
        end
      end
    end

    context 'when the cluster does not have an app name option set' do
      it 'does not set the app name' do
        expect(app_metadata.client_document[:application]).to be_nil
      end
    end

    context 'when the client document exceeds the max of 512 bytes' do
      shared_examples_for 'a truncated document' do
        it 'is too long before validation' do
          expect(app_metadata.client_document.to_bson.to_s.size).to be > max_size
        end

        it 'is acceptable after validation' do
          app_metadata.validated_document # force validation
          expect(app_metadata.client_document.to_bson.to_s.size).to be <= max_size
        end
      end

      context 'when the os.name length is too long' do
        before do
          allow(app_metadata).to receive(:name).and_return('x' * 500)
        end

        it_behaves_like 'a truncated document'
      end

      context 'when the os.architecture length is too long' do
        before do
          allow(app_metadata).to receive(:architecture).and_return('x' * 500)
        end

        it_behaves_like 'a truncated document'
      end

      context 'when the platform length is too long' do
        before do
          allow(app_metadata).to receive(:platform).and_return('x' * 500)
        end

        it_behaves_like 'a truncated document'
      end
    end

    context 'when run outside of a FaaS environment' do
      context 'when a container is present' do
        local_env 'KUBERNETES_SERVICE_HOST' => 'something'

        it 'includes the :env key in the client document' do
          expect(app_metadata.client_document.key?(:env)).to be true
        end
      end

      context 'when no container is present' do
        it 'excludes the :env key from the client document' do
          expect(app_metadata.client_document.key?(:env)).to be false
        end
      end
    end

    context 'when run inside of a FaaS environment' do
      context 'when the environment is invalid' do
        # invalid, because it is missing the other required fields
        local_env('AWS_EXECUTION_ENV' => 'AWS_Lambda_ruby2.7')

        it 'excludes the :env key from the client document' do
          expect(app_metadata.client_document.key?(:env)).to be false
        end
      end

      context 'when the environment is valid' do
        # valid, because Azure requires only the one field
        local_env('FUNCTIONS_WORKER_RUNTIME' => 'ruby')

        it 'includes the :env key in the client document' do
          expect(app_metadata.client_document.key?(:env)).to be true
          expect(app_metadata.client_document[:env][:name]).to be == 'azure.func'
        end
      end
    end
  end

  describe '#document' do
    let(:document) do
      app_metadata.send(:document)
    end

    context 'when user is given and auth_mech is not given' do
      let(:app_metadata) do
        described_class.new(user: 'foo')
      end

      it 'includes saslSupportedMechs' do
        expect(document[:saslSupportedMechs]).to eq('admin.foo')
      end
    end

    it_behaves_like 'app metadata document'
  end

  describe '#validated_document' do
    it 'raises with too long app name' do
      app_name = 'app' * 500
      expect { described_class.new(app_name: app_name).validated_document }
        .to raise_error(Mongo::Error::InvalidApplicationName)
    end

    it 'does not raise with correct app name' do
      app_name = 'app'
      expect { described_class.new(app_name: app_name).validated_document }
        .not_to raise_error
    end
  end
end