File: unpacker_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (75 lines) | stat: -rw-r--r-- 3,260 bytes parent folder | download | duplicates (3)
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
require 'spec_helper'
require 'puppet/util/json'

require 'puppet/module_tool/applications'
require 'puppet/file_system'
require 'puppet_spec/modules'

describe Puppet::ModuleTool::Applications::Unpacker do
  include PuppetSpec::Files

  let(:target)      { tmpdir("unpacker") }
  let(:module_name) { 'myusername-mytarball' }
  let(:filename)    { tmpdir("module") + "/module.tar.gz" }
  let(:working_dir) { tmpdir("working_dir") }

  before :each do
    allow(Puppet.settings).to receive(:[])
    allow(Puppet.settings).to receive(:[]).with(:module_working_dir).and_return(working_dir)
  end

  it "should attempt to untar file to temporary location" do
    untar = double('Tar')
    expect(untar).to receive(:unpack).with(filename, anything, anything) do |src, dest, _|
      FileUtils.mkdir(File.join(dest, 'extractedmodule'))
      File.open(File.join(dest, 'extractedmodule', 'metadata.json'), 'w+') do |file|
        file.puts Puppet::Util::Json.dump('name' => module_name, 'version' => '1.0.0')
      end
      true
    end

    expect(Puppet::ModuleTool::Tar).to receive(:instance).and_return(untar)

    Puppet::ModuleTool::Applications::Unpacker.run(filename, :target_dir => target)
    expect(File).to be_directory(File.join(target, 'mytarball'))
  end

  it "should warn about symlinks", :if => Puppet.features.manages_symlinks? do
    untar = double('Tar')
    expect(untar).to receive(:unpack).with(filename, anything, anything) do |src, dest, _|
      FileUtils.mkdir(File.join(dest, 'extractedmodule'))
      File.open(File.join(dest, 'extractedmodule', 'metadata.json'), 'w+') do |file|
        file.puts Puppet::Util::Json.dump('name' => module_name, 'version' => '1.0.0')
      end
      FileUtils.touch(File.join(dest, 'extractedmodule/tempfile'))
      Puppet::FileSystem.symlink(File.join(dest, 'extractedmodule/tempfile'), File.join(dest, 'extractedmodule/tempfile2'))
      true
    end

    expect(Puppet::ModuleTool::Tar).to receive(:instance).and_return(untar)
    expect(Puppet).to receive(:warning).with(/symlinks/i)

    Puppet::ModuleTool::Applications::Unpacker.run(filename, :target_dir => target)
    expect(File).to be_directory(File.join(target, 'mytarball'))
  end

  it "should warn about symlinks in subdirectories", :if => Puppet.features.manages_symlinks? do
    untar = double('Tar')
    expect(untar).to receive(:unpack).with(filename, anything, anything) do |src, dest, _|
      FileUtils.mkdir(File.join(dest, 'extractedmodule'))
      File.open(File.join(dest, 'extractedmodule', 'metadata.json'), 'w+') do |file|
        file.puts Puppet::Util::Json.dump('name' => module_name, 'version' => '1.0.0')
      end
      FileUtils.mkdir(File.join(dest, 'extractedmodule/manifests'))
      FileUtils.touch(File.join(dest, 'extractedmodule/manifests/tempfile'))
      Puppet::FileSystem.symlink(File.join(dest, 'extractedmodule/manifests/tempfile'), File.join(dest, 'extractedmodule/manifests/tempfile2'))
      true
    end

    expect(Puppet::ModuleTool::Tar).to receive(:instance).and_return(untar)
    expect(Puppet).to receive(:warning).with(/symlinks/i)

    Puppet::ModuleTool::Applications::Unpacker.run(filename, :target_dir => target)
    expect(File).to be_directory(File.join(target, 'mytarball'))
  end
end