|
| 1 | +require 'spec_helper_acceptance' |
| 2 | + |
| 3 | +if os[:family] == 'windows' |
| 4 | + os_name = run_shell('systeminfo | findstr /R /C:"OS Name"') |
| 5 | + raise 'Could not retrieve systeminfo for Windows box' if os_name.exit_code != 0 |
| 6 | + os_name = os_name.stdout.split(%r{\s}).include?('2016') ? 'win-2016' : 'win-2019' |
| 7 | + docker_args = 'docker_ee => true' |
| 8 | + docker_network = 'nat' |
| 9 | + volume_location = 'C:\\' |
| 10 | + docker_image = if os_name == 'win-2016' |
| 11 | + 'stefanscherer/nanoserver:sac2016' |
| 12 | + else |
| 13 | + 'stefanscherer/nanoserver:10.0.17763.1040' |
| 14 | + end |
| 15 | +else |
| 16 | + docker_args = if os[:family] == 'redhat' |
| 17 | + "repo_opt => '--enablerepo=localmirror-extras'" |
| 18 | + else |
| 19 | + '' |
| 20 | + end |
| 21 | + docker_network = 'bridge' |
| 22 | + volume_location = '/opt' |
| 23 | + docker_image = 'hello-world:linux' |
| 24 | +end |
| 25 | + |
| 26 | +describe 'docker trigger parammeters change' do |
| 27 | + before(:all) do |
| 28 | + if os[:family] != 'windows' |
| 29 | + install_pp = "class { 'docker': #{docker_args}}" |
| 30 | + apply_manifest(install_pp) |
| 31 | + end |
| 32 | + run_shell("mkdir #{volume_location}/volume_1") |
| 33 | + run_shell("mkdir #{volume_location}/volume_2") |
| 34 | + end |
| 35 | + |
| 36 | + context 'when image is changed' do |
| 37 | + image_changed = if os[:family] == 'windows' |
| 38 | + if os_name == 'win-2016' |
| 39 | + 'stefanscherer/nanoserver:10.0.14393.2551' |
| 40 | + else |
| 41 | + 'stefanscherer/nanoserver:1809' |
| 42 | + end |
| 43 | + else |
| 44 | + 'hello-world:latest' |
| 45 | + end |
| 46 | + let(:pp1) do |
| 47 | + " |
| 48 | + class {'docker': #{docker_args}} |
| 49 | + docker::run {'servercore': image => '#{docker_image}', restart => 'always', net => '#{docker_network}' } |
| 50 | + " |
| 51 | + end |
| 52 | + |
| 53 | + let(:pp2) do |
| 54 | + " |
| 55 | + class {'docker': #{docker_args}} |
| 56 | + docker::run {'servercore': image => '#{image_changed}', restart => 'always', net => '#{docker_network}' } |
| 57 | + " |
| 58 | + end |
| 59 | + |
| 60 | + it 'creates servercore with first image' do |
| 61 | + idempotent_apply(pp1) |
| 62 | + end |
| 63 | + |
| 64 | + it 'detect image change and apply the change' do |
| 65 | + apply_manifest(pp2, catch_failures: true) |
| 66 | + run_shell('docker inspect --format="{{ .Config.Image }}" servercore') do |r| |
| 67 | + expect(r.stdout).to match(%r{#{image_changed}}) |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + context 'when volumes parameter is changed' do |
| 73 | + if os[:family] == 'windows' |
| 74 | + volumes1 = "volumes => ['volume-1:C:\\volume_1']" |
| 75 | + volumes2 = "volumes => ['volume-1:C:\\volume_1', 'volume-2:C:\\volume_2']" |
| 76 | + else |
| 77 | + volumes1 = "volumes => ['volume-1:#{volume_location}/volume_1']" |
| 78 | + volumes2 = "volumes => ['volume-1:#{volume_location}/volume_1', 'volume-2:#{volume_location}/volume_2']" |
| 79 | + end |
| 80 | + |
| 81 | + let(:pp1) do |
| 82 | + " |
| 83 | + class {'docker': #{docker_args}} |
| 84 | + docker::run {'servercore': image => '#{docker_image}', restart => 'always', net => '#{docker_network}', #{volumes1}} |
| 85 | + " |
| 86 | + end |
| 87 | + |
| 88 | + let(:pp2) do |
| 89 | + " |
| 90 | + class {'docker': #{docker_args}} |
| 91 | + docker::run {'servercore': image => '#{docker_image}', restart => 'always', net => '#{docker_network}', #{volumes2}} |
| 92 | + " |
| 93 | + end |
| 94 | + |
| 95 | + it "creates servercore with #{volumes1}" do |
| 96 | + idempotent_apply(pp1) |
| 97 | + end |
| 98 | + |
| 99 | + it "creates servercore with #{volumes2}" do |
| 100 | + apply_manifest(pp2, catch_failures: true) |
| 101 | + run_shell('docker inspect servercore --format="{{ json .Mounts }}"') do |r| |
| 102 | + inspect_result = JSON.parse(r.stdout) |
| 103 | + inspect_result = inspect_result.map { |item| item['Name'] }.sort |
| 104 | + expect(inspect_result).to eq(['volume-1', 'volume-2']) |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + context 'when ports parameter is changed' do |
| 110 | + ports1 = "ports => ['4444']" |
| 111 | + ports2 = "ports => ['4444', '4445']" |
| 112 | + |
| 113 | + let(:pp1) do |
| 114 | + " |
| 115 | + class {'docker': #{docker_args}} |
| 116 | + docker::run {'servercore': image => '#{docker_image}', restart => 'always', net => '#{docker_network}', #{ports1}} |
| 117 | + " |
| 118 | + end |
| 119 | + |
| 120 | + let(:pp2) do |
| 121 | + " |
| 122 | + class {'docker': #{docker_args}} |
| 123 | + docker::run {'servercore': image => '#{docker_image}', restart => 'always', net => '#{docker_network}', #{ports2}} |
| 124 | + " |
| 125 | + end |
| 126 | + |
| 127 | + it 'creates servercore with ports => ["4444"]' do |
| 128 | + idempotent_apply(pp1) |
| 129 | + end |
| 130 | + |
| 131 | + it 'creates servercore with ports => ["4444", "4445"]' do |
| 132 | + apply_manifest(pp2, catch_failures: true) |
| 133 | + run_shell('docker inspect servercore --format="{{ json .HostConfig.PortBindings }}"') do |r| |
| 134 | + inspect_result = JSON.parse(r.stdout) |
| 135 | + inspect_result = inspect_result.keys.map { |item| item.split('/')[0] }.sort |
| 136 | + expect(inspect_result).to eq(['4444', '4445']) |
| 137 | + end |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + after(:all) do |
| 142 | + run_shell("rm -r #{volume_location}/volume_1") |
| 143 | + run_shell("rm -r #{volume_location}/volume_2") |
| 144 | + end |
| 145 | +end |
0 commit comments