File: util_spec.rb

package info (click to toggle)
ruby-docker-api 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 432 kB
  • sloc: ruby: 4,044; sh: 138; makefile: 5
file content (331 lines) | stat: -rw-r--r-- 9,434 bytes parent folder | download | duplicates (2)
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
require 'spec_helper'
require 'tempfile'
require 'fileutils'

SingleCov.covered! uncovered: 71

describe Docker::Util do
  subject { described_class }

  describe '.parse_json' do
    subject { described_class.parse_json(arg) }

    context 'when the argument is nil' do
      let(:arg) { nil }

      it { should be_nil }
    end

    context 'when the argument is empty' do
      let(:arg) { '' }

      it { should be_nil }
    end

    context 'when the argument is \'null\'' do
      let(:arg) { 'null' }

      it { should be_nil }
    end

    context 'when the argument is not valid JSON' do
      let(:arg) { '~~lol not valid json~~' }

      it 'raises an error' do
        expect { subject }.to raise_error Docker::Error::UnexpectedResponseError
      end
    end

    context 'when the argument is valid JSON' do
      let(:arg) { '{"yolo":"swag"}' }

      it 'parses the JSON into a Hash' do
        expect(subject).to eq 'yolo' => 'swag'
      end
    end
  end

  describe '.fix_json' do
    let(:response) { '{"this":"is"}{"not":"json"}' }
    subject { Docker::Util.fix_json(response) }

    it 'fixes the "JSON" response that Docker returns' do
      expect(subject).to eq [
        {
          'this' => 'is'
        },
        {
          'not' => 'json'
        }
      ]
    end
  end

  describe '.create_dir_tar' do
    attr_accessor :tmpdir

    def files_in_tar(tar)
      Gem::Package::TarReader.new(tar) { |content| return content.map(&:full_name).sort }
    end

    # @param base_dir [String] the path to the directory where the structure should be written
    # @param dockerignore_entries [Array<String>] the lines of the desired .dockerignore file
    def structure_context_dir(dockerignore_entries = nil)
      FileUtils.mkdir_p("#{tmpdir}/a_dir/a_subdir")
      [
        '#edge',
        'a_file',
        'a_file2',
        'a_dir/a_file',
        'a_dir/a_subdir/a_file',
      ].each { |f| File.write("#{tmpdir}/#{f}", 'x') }

      File.write("#{tmpdir}/.dockerignore", dockerignore_entries.join("\n")) unless dockerignore_entries.nil?
    end

    def expect_tar_entries(*entries)
      expect(files_in_tar(tar)).to contain_exactly(*entries)
    end

    let(:tar) { subject.create_dir_tar tmpdir }

    around do |example|
      Dir.mktmpdir do |tmpdir|
        self.tmpdir = tmpdir
        example.call
        FileUtils.rm tar
      end
    end

    it 'creates a tarball' do
      tar = subject.create_dir_tar tmpdir
      expect(files_in_tar(tar)).to eq []
    end

    it 'packs regular files' do
      File.write("#{tmpdir}/foo", 'bar')
      expect(files_in_tar(tar)).to eq ['foo']
    end

    it 'packs nested files, but not directory entries' do
      FileUtils.mkdir("#{tmpdir}/foo")
      File.write("#{tmpdir}/foo/bar", 'bar')
      expect(files_in_tar(tar)).to eq ['foo/bar']
    end

    describe '.dockerignore' do
      it 'passes all files when there is no .dockerignore' do
        structure_context_dir
        expect_tar_entries('#edge', 'a_dir/a_file', 'a_dir/a_subdir/a_file', 'a_file', 'a_file2')
      end

      it 'passes all files when there is an empty .dockerignore' do
        structure_context_dir([''])
        expect_tar_entries('#edge', '.dockerignore', 'a_dir/a_file', 'a_dir/a_subdir/a_file', 'a_file', 'a_file2')
      end

      it 'does not interpret comments' do
        structure_context_dir(['#edge'])
        expect_tar_entries('#edge', '.dockerignore', 'a_dir/a_file', 'a_dir/a_subdir/a_file', 'a_file', 'a_file2')
      end

      it 'ignores files' do
        structure_context_dir(['a_file'])
        expect_tar_entries('#edge', '.dockerignore', 'a_dir/a_file', 'a_dir/a_subdir/a_file', 'a_file2')
      end

      it 'ignores files with wildcard' do
        structure_context_dir(['a_file'])
        expect_tar_entries('#edge', '.dockerignore', 'a_dir/a_file', 'a_dir/a_subdir/a_file', 'a_file2')
      end

      it 'ignores files with dir wildcard' do
        structure_context_dir(['**/a_file'])
        expect_tar_entries('#edge', '.dockerignore', 'a_file2')
      end

      it 'ignores files with dir wildcard but handles exceptions' do
        structure_context_dir(['**/a_file', '!a_dir/a_file'])
        expect_tar_entries('#edge', '.dockerignore', 'a_dir/a_file', 'a_file2')
      end

      it 'ignores directories' do
        structure_context_dir(['a_dir'])
        expect_tar_entries('#edge', '.dockerignore', 'a_file', 'a_file2')
      end

      it 'ignores directories with dir wildcard' do
        structure_context_dir(['*/a_subdir'])
        expect_tar_entries('#edge', '.dockerignore', 'a_dir/a_file', 'a_file', 'a_file2')
      end

      it 'ignores directories with dir double wildcard' do
        structure_context_dir(['**/a_subdir'])
        expect_tar_entries('#edge', '.dockerignore', 'a_dir/a_file', 'a_file', 'a_file2')
      end

      it 'ignores directories with dir wildcard' do
        structure_context_dir(['a_dir', '!a_dir/a_subdir'])
        expect_tar_entries('#edge', '.dockerignore', 'a_dir/a_subdir/a_file', 'a_file', 'a_file2')
      end

      it 'ignores files' do
        File.write("#{tmpdir}/foo", 'bar')
        File.write("#{tmpdir}/baz", 'bar')

        File.write("#{tmpdir}/.dockerignore", "foo")

        expect(files_in_tar(tar)).to eq ['.dockerignore', 'baz']
      end

      it 'ignores folders' do
        FileUtils.mkdir("#{tmpdir}/foo")
        File.write("#{tmpdir}/foo/bar", 'bar')

        File.write("#{tmpdir}/.dockerignore", "foo")

        expect(files_in_tar(tar)).to eq ['.dockerignore']
      end

      it 'ignores based on wildcards' do
        File.write("#{tmpdir}/bar", 'bar')
        File.write("#{tmpdir}/baz", 'bar')

        File.write("#{tmpdir}/.dockerignore", "*z")

        expect(files_in_tar(tar)).to eq ['.dockerignore', 'bar']
      end

      it 'ignores comments' do
        File.write("#{tmpdir}/foo", 'bar')
        File.write("#{tmpdir}/baz", 'bar')

        File.write("#{tmpdir}/.dockerignore", "# nothing here\nfoo")

        expect(files_in_tar(tar)).to eq ['.dockerignore', 'baz']
      end

      it 'ignores whitespace' do
        File.write("#{tmpdir}/foo", 'bar')
        File.write("#{tmpdir}/baz", 'bar')

        File.write("#{tmpdir}/.dockerignore", "foo   \n   \n\n")

        expect(files_in_tar(tar)).to eq ['.dockerignore', 'baz']
      end

      it 'ignores multiple patterns' do
        File.write("#{tmpdir}/foo", 'bar')
        File.write("#{tmpdir}/baz", 'bar')
        File.write("#{tmpdir}/zig", 'bar')

        File.write("#{tmpdir}/.dockerignore", "fo*\nba*")

        expect(files_in_tar(tar)).to eq ['.dockerignore', 'zig']
      end
    end
  end

  describe '.build_auth_header' do
    subject { described_class }

    let(:credentials) {
      {
        :username      => 'test',
        :password      => 'password',
        :email         => 'test@example.com',
        :serveraddress => 'https://registry.com/'
      }
    }
    let(:credential_string) { MultiJson.dump(credentials) }
    let(:encoded_creds) { Base64.urlsafe_encode64(credential_string) }
    let(:expected_header) {
      {
        'X-Registry-Auth' => encoded_creds
      }
    }

    context 'given credentials as a Hash' do
      it 'returns an X-Registry-Auth header encoded' do
        expect(subject.build_auth_header(credentials)).to eq(expected_header)
      end
    end

    context 'given credentials as a String' do
      it 'returns an X-Registry-Auth header encoded' do
        expect(
          subject.build_auth_header(credential_string)
        ).to eq(expected_header)
      end
    end

    it 'does not contain newlines' do
      h = subject.build_auth_header(credentials).fetch('X-Registry-Auth')
      expect(h).not_to include("\n")
    end
  end

  describe '.build_config_header' do
    subject { described_class }

    let(:credentials) {
      {
        :username      => 'test',
        :password      => 'password',
        :email         => 'test@example.com',
        :serveraddress => 'https://registry.com/'
      }
    }

    let(:credentials_object) do
      MultiJson.dump(
        :'https://registry.com/' => {
          username: 'test',
          password: 'password',
          email: 'test@example.com'
        }
      )
    end

    let(:encoded_creds) { Base64.urlsafe_encode64(credentials_object) }
    let(:expected_header) {
      {
        'X-Registry-Config' => encoded_creds
      }
    }

    context 'given credentials as a Hash' do
      it 'returns an X-Registry-Config header encoded' do
        expect(subject.build_config_header(credentials)).to eq(expected_header)
      end
    end

    context 'given credentials as a String' do
      it 'returns an X-Registry-Config header encoded' do
        expect(
          subject.build_config_header(MultiJson.dump(credentials))
        ).to eq(expected_header)
      end
    end

    it 'does not contain newlines' do
      h = subject.build_config_header(credentials).fetch('X-Registry-Config')
      expect(h).not_to include("\n")
    end
  end

  describe '.filesystem_permissions' do
    it 'returns the permissions on a file' do
      file = Tempfile.new('test_file')
      file.close
      expected_permissions = 0600
      File.chmod(expected_permissions, file.path)

      actual_permissions = subject.filesystem_permissions(file.path)

      file.unlink
      expect(actual_permissions).to eql(expected_permissions)
    end
  end

end