File: enable_spec.rb

package info (click to toggle)
ruby-puppetserver-ca-cli 2.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 696 kB
  • sloc: ruby: 6,970; sh: 4; makefile: 3
file content (92 lines) | stat: -rw-r--r-- 2,995 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
require 'spec_helper'
require 'shared_examples/cli_parsing'
require 'utils/ssl'

require 'puppetserver/ca/action/enable'
require 'puppetserver/ca/logger'

RSpec.describe Puppetserver::Ca::Action::Enable do
  include Utils::SSL

  let(:stdout) { StringIO.new }
  let(:stderr) { StringIO.new }
  let(:logger) { Puppetserver::Ca::Logger.new(:info, stdout, stderr) }
  let(:usage) { /.*Usage:.* puppetserver ca enable.*Display this command-specific help output.*/m }

  subject { Puppetserver::Ca::Action::Enable.new(logger) }

  it 'prints the help output & returns 1 if invalid flags are given' do
    _, exit_code = subject.parse(['--hello'])
    expect(stderr.string).to match(/Error.*--hello/m)
    expect(stderr.string).to match(usage)
    expect(exit_code).to eq(1)
  end

  context 'infracrl' do
    it 'does not print the help output if called correctly' do
      Dir.mktmpdir do |tmpdir|
        with_ca_in tmpdir do |conf, ca_dir|
          inventory = File.join(ca_dir, 'infra_inventory.txt')
          File.open(inventory, 'w') do |f|
            f.puts ''
          end

          exit_code = subject.run({ 'config'   => conf,
                                    'infracrl' => true })
          puts stderr.string
          expect(stderr.string).to be_empty
          expect(exit_code).to eq(0)
        end
      end
    end

    it 'requires an inventory file to be present' do
      Dir.mktmpdir do |tmpdir|
        with_ca_in tmpdir do |conf, ca_dir|
          exit_code = subject.run({ 'config'   => conf,
                                    'infracrl' => true })
          expect(exit_code).to eq(1)
          expect(stderr.string).to match(/Please create/m)
        end
      end
    end

    it 'generates infra CRL' do
      Dir.mktmpdir do |tmpdir|
        with_ca_in tmpdir do |conf, ca_dir|
          inventory = File.join(ca_dir, 'infra_inventory.txt')
          infra_crl = File.join(ca_dir, 'infra_crl.pem')

          File.open(inventory, 'w') do |f|
            f.puts ''
          end
          exit_code = subject.run({ 'config'   => conf,
                                    'infracrl' => true })
          expect(File.exist?(infra_crl)).to be true
        end
      end
    end

    it 'refuses to overwrite existing files' do
      Dir.mktmpdir do |tmpdir|
        with_ca_in tmpdir do |conf, ca_dir|
          inventory = File.join(ca_dir, 'infra_inventory.txt')
          infra_crl = File.join(ca_dir, 'infra_crl.pem')

          File.open(inventory, 'w') do |f|
            f.puts ''
          end
          exit_code = subject.run({ 'config'   => conf,
                                    'infracrl' => true })
          expect(File.exist?(infra_crl)).to be true
          expect(exit_code).to eq(0)

          exit_code2 = subject.run({ 'config'   => conf,
                                     'infracrl' => true })
          expect(exit_code2).to eq(1)
          expect(stderr.string).to match(/Error.*reinitialize/m)
        end
      end
    end
  end
end