|
| 1 | +# spec/acceptance/deferred_spec.rb |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require 'spec_helper_acceptance' |
| 5 | + |
| 6 | +def read_fixture(name) |
| 7 | + File.read(File.join(__dir__, '..', 'fixtures', 'manifests', name)) |
| 8 | +end |
| 9 | + |
| 10 | +def read_win_file_if_exists(path) |
| 11 | + # Use a script block with literals; avoid $variables to prevent transport/quoting expansion |
| 12 | + # Also keep exit 0 regardless of existence so run_shell doesn't raise. |
| 13 | + ps = %{& { if (Test-Path -LiteralPath '#{path}') { Get-Content -Raw -LiteralPath '#{path}' } else { '<<<FILE_NOT_FOUND>>>' } } } |
| 14 | + r = run_shell(%(powershell.exe -NoProfile -NonInteractive -Command "#{ps}")) |
| 15 | + body = (r.stdout || '').to_s |
| 16 | + exists = !body.include?('<<<FILE_NOT_FOUND>>>') |
| 17 | + { exists: exists, content: exists ? body : '' } |
| 18 | +end |
| 19 | + |
| 20 | +describe 'deferred values with dsc_lite' do |
| 21 | + let(:control_manifest) { read_fixture('01_file_deferred.pp') } |
| 22 | + let(:dsc_control_manifest_epp) { read_fixture('01b_file_deferred_with_epp.pp') } |
| 23 | + let(:dsc_deferred_direct) { read_fixture('02_dsc_deferred_direct.pp') } |
| 24 | + let(:dsc_deferred_inline) { read_fixture('02b_dsc_deferred_inline.pp') } # ← NEW |
| 25 | + let(:dsc_deferred_epp_inline) { read_fixture('02c_dsc_deferred_epp_inline.pp') } # ← NEW |
| 26 | + let(:dsc_deferred_stringified) { read_fixture('03a_dsc_deferred_stringified.pp') } |
| 27 | + let(:dsc_deferred_bad_unwrap) { read_fixture('03b_dsc_deferred_bad_unwrap.pp') } |
| 28 | + |
| 29 | + it 'control (01): native file + Deferred resolves to hello-file' do |
| 30 | + result = idempotent_apply(control_manifest) |
| 31 | + expect(result.exit_code).to eq(0) |
| 32 | + out = read_win_file_if_exists('C:/Temp/deferred_ok.txt') |
| 33 | + expect(out[:exists]).to be(true) |
| 34 | + expect(out[:content].strip).to eq('hello-file') |
| 35 | + end |
| 36 | + |
| 37 | + it 'control (01b): native file + Deferred resolves to hello-file (EPP)' do |
| 38 | + result = idempotent_apply_debug(dsc_control_manifest_epp) |
| 39 | + expect(result.exit_code).to eq(0) |
| 40 | + out = read_win_file_if_exists('C:/Temp/deferred_ok.txt') |
| 41 | + expect(out[:exists]).to be(true) |
| 42 | + expect(out[:content].strip).to eq('hello-file') |
| 43 | + end |
| 44 | + |
| 45 | + it '02: passing Deferred via variable to DSC resolves to hello-dsc (otherwise flag bug)' do |
| 46 | + apply = apply_manifest(dsc_deferred_direct) |
| 47 | + out = read_win_file_if_exists('C:/Temp/from_dsc.txt') |
| 48 | + content = out[:content].strip |
| 49 | + if out[:exists] && content == 'hello-dsc' |
| 50 | + expect(true).to be(true) |
| 51 | + elsif out[:exists] && content =~ %r{Deferred\s*\(|Puppet::Pops::Types::Deferred}i |
| 52 | + raise "BUG: 02 wrote stringified Deferred: #{content.inspect}\nApply:\n#{apply.stdout}#{apply.stderr}" |
| 53 | + else |
| 54 | + raise "Unexpected 02 outcome. Exists=#{out[:exists]} Content=#{content.inspect}\nApply:\n#{apply.stdout}#{apply.stderr}" |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + # NEW 02b: inline Deferred on the DSC property (no variable intermediary) |
| 59 | + it '02b: passing Deferred inline to DSC resolves to hello-dsc-inline (otherwise flag bug)' do |
| 60 | + apply = apply_manifest(dsc_deferred_inline) |
| 61 | + out = read_win_file_if_exists('C:/Temp/from_dsc_inline.txt') |
| 62 | + content = out[:content].strip |
| 63 | + if out[:exists] && content == 'hello-dsc-inline' |
| 64 | + expect(true).to be(true) |
| 65 | + elsif out[:exists] && content =~ %r{Deferred\s*\(|Puppet::Pops::Types::Deferred}i |
| 66 | + raise "BUG: 02b wrote stringified Deferred: #{content.inspect}\nApply:\n#{apply.stdout}#{apply.stderr}" |
| 67 | + else |
| 68 | + raise "Unexpected 02b outcome. Exists=#{out[:exists]} Content=#{content.inspect}\nApply:\n#{apply.stdout}#{apply.stderr}" |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + # NEW 02c: inline Deferred on the DSC property (no variable intermediary) |
| 73 | + it '02c: passing a Deferred inline while calling an epp' do |
| 74 | + apply = apply_manifest_debug(dsc_deferred_epp_inline) |
| 75 | + out = read_win_file_if_exists('C:/Temp/from_dsc_inline.txt') |
| 76 | + content = out[:content].strip |
| 77 | + if out[:exists] && content == 'hello-dsc-epp' |
| 78 | + expect(true).to be(true) |
| 79 | + elsif out[:exists] && content =~ %r{Deferred\s*\(|Puppet::Pops::Types::Deferred}i |
| 80 | + raise "BUG: 02c wrote stringified Deferred: #{content.inspect}\nApply:\n#{apply.stdout}#{apply.stderr}" |
| 81 | + else |
| 82 | + raise "Unexpected 02c outcome. Exists=#{out[:exists]} Content=#{content.inspect}\nApply:\n#{apply.stdout}#{apply.stderr}" |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + it '03a: stringifying a Deferred writes the function form (reproduces customer report)' do |
| 87 | + apply_manifest(dsc_deferred_stringified) |
| 88 | + out = read_win_file_if_exists('C:/Temp/from_dsc_var_string.txt') |
| 89 | + expect(out[:exists]).to be(true) |
| 90 | + expect(out[:content]).to match(%r{Deferred\s*\(|Puppet::Pops::Types::Deferred}i) |
| 91 | + expect(out[:content]).not_to match(%r{\bhello-var\b}) |
| 92 | + end |
| 93 | + |
| 94 | + it '03b: unwrap on a non‑Sensitive is a no‑op; also writes the function form' do |
| 95 | + apply_manifest(dsc_deferred_bad_unwrap) |
| 96 | + out = read_win_file_if_exists('C:/Temp/from_dsc_var_bad_unwrap.txt') |
| 97 | + out = read_win_file_if_exists('C:/Temp/from_dsc_var.txt') unless out[:exists] |
| 98 | + expect(out[:exists]).to be(true) |
| 99 | + expect(out[:content]).to match(%r{Deferred\s*\(|Puppet::Pops::Types::Deferred}i) |
| 100 | + expect(out[:content]).not_to match(%r{\bhello-var\b}) |
| 101 | + end |
| 102 | +end |
0 commit comments