<commit_before># frozen_string_literal: true
require 'spec_helper'

describe 'rabbitmq::default' do
  let(:runner) do
    ChefSpec::ServerRunner.new(REDHAT_OPTS) do |node, _|
      node.override['rabbitmq']['version'] = '3.7.13'
    end
  end
  let(:node) { runner.node }

  let(:chef_run) do
    runner.converge(described_recipe)
  end

  let(:file_cache_path) { Chef::Config[:file_cache_path] }

  include_context 'rabbitmq-stubs'

  it 'creates a directory for mnesiadir' do
    expect(chef_run).to create_directory('/var/lib/rabbitmq/mnesia')
  end

  describe 'rabbitmq-env.conf' do
    let(:file) { chef_run.template('/etc/rabbitmq/rabbitmq-env.conf') }

    it 'creates a template rabbitmq-env.conf with attributes' do
      expect(chef_run).to create_template(file.name).with(
        :user => 'root',
        :group => 'root',
        :source => 'rabbitmq-env.conf.erb',
        :mode => 00644)
    end

    it 'has no erl args by default' do
      [/^SERVER_ADDITIONAL_ERL_ARGS=/,
       /^CTL_ERL_ARGS=/].each do |line|
        expect(chef_run).not_to render_file(file.name).with_content(line)
      end
    end

    it 'has erl args overridden' do
      node.normal['rabbitmq']['server_additional_erl_args'] = 'test123'
      node.normal['rabbitmq']['ctl_erl_args'] = 'test123'
      [/^SERVER_ADDITIONAL_ERL_ARGS='test123'/,
       /^CTL_ERL_ARGS='test123'/].each do |line|
        expect(chef_run).to render_file(file.name).with_content(line)
      end
    end

    it 'has no additional_env_settings default' do
      expect(chef_run).not_to render_file(file.name).with_content(/^# Additional ENV settings/)
    end

    it 'has additional_env_settings' do
      node.normal['rabbitmq']['additional_env_settings'] = [
        'USE_LONGNAME=true',
        'WHATS_ON_THE_TELLY=penguin']
      [/^WHATS_ON_THE_TELLY=penguin/,
       /^# Additional ENV settings/,
       /^USE_LONGNAME=true/].each do |line|
        expect(chef_run).to render_file(file.name).with_content(line)
      end
    end
  end

  it 'should create the config root directory' do
    expect(chef_run).to create_directory('/etc/rabbitmq')
      .with(
        :user => 'root',
        :group => 'root',
        :mode => '755'
      )
  end

  it 'should create the node data directory' do
    expect(chef_run).to create_directory('/var/lib/rabbitmq/mnesia')
      .with(
        :user => 'rabbitmq',
        :group => 'rabbitmq',
        :mode => '775'
      )
  end

  it 'does not enable a rabbitmq service when manage_service is false' do
    node.normal['rabbitmq']['manage_service'] = false
    expect(chef_run).not_to enable_service('rabbitmq-server')
  end

  it 'does not start a rabbitmq service when manage_service is false' do
    node.normal['rabbitmq']['manage_service'] = false
    expect(chef_run).not_to start_service('rabbitmq-server')
  end

  it 'enables a rabbitmq service when manage_service is true' do
    node.normal['rabbitmq']['manage_service'] = true
    expect(chef_run).to enable_service('rabbitmq-server')
  end

  it 'starts a rabbitmq service when manage_service is true' do
    node.normal['rabbitmq']['manage_service'] = true
    expect(chef_run).to start_service('rabbitmq-server')
  end

  it 'should have the use_distro_version set to false' do
    expect(chef_run.node['rabbitmq']['use_distro_version']).to eq(false)
  end

  it 'should install the erlang package' do
    expect(chef_run).to install_package('erlang')
  end

  it 'should create the rabbitmq /etc/default file' do
    expect(chef_run).to create_template("/etc/default/#{chef_run.node['rabbitmq']['service_name']}").with(
      :user => 'root',
      :group => 'root',
      :source => 'default.rabbitmq-server.erb',
      :mode => 00644
    )
  end

  it 'creates a template rabbitmq.config with attributes' do
    expect(chef_run).to create_template('/etc/rabbitmq/rabbitmq.config').with(
      :user => 'root',
      :group => 'root',
      :source => 'rabbitmq.config.erb',
      :mode => 00644)

    if Gem::Version.new(Chef::VERSION.to_s) >= Gem::Version.new('11.14.2')
      expect(chef_run).to create_template('/etc/rabbitmq/rabbitmq.config').with(:sensitive => true)
    else
      expect(chef_run).to create_template('/etc/rabbitmq/rabbitmq.config').with(:sensitive => false)
    end<commit_msg>Backport ChefSpec examples from master<commit_after># frozen_string_literal: true
require 'spec_helper'

describe 'rabbitmq::default' do
  let(:runner) do
    ChefSpec::ServerRunner.new(REDHAT_OPTS) do |node, _|
      node.override['rabbitmq']['version'] = '3.7.13'
    end
  end
  let(:node) { runner.node }

  let(:chef_run) do
    runner.converge(described_recipe)
  end

  let(:file_cache_path) { Chef::Config[:file_cache_path] }

  include_context 'rabbitmq-stubs'

  it 'creates a directory for mnesiadir' do
    expect(chef_run).to create_directory('/var/lib/rabbitmq/mnesia')
  end

  describe 'rabbitmq-env.conf' do
    let(:file) { chef_run.template('/etc/rabbitmq/rabbitmq-env.conf') }

    it 'creates a template rabbitmq-env.conf with attributes' do
      expect(chef_run).to create_template(file.name).with(
        :user => 'root',
        :group => 'root',
        :source => 'rabbitmq-env.conf.erb',
        :mode => 00644)
    end

    it 'has no erl args by default' do
      [/^SERVER_ADDITIONAL_ERL_ARGS=/,
       /^CTL_ERL_ARGS=/].each do |line|
        expect(chef_run).not_to render_file(file.name).with_content(line)
      end
    end

    it 'has erl args overridden' do
      node.normal['rabbitmq']['server_additional_erl_args'] = 'test123'
      node.normal['rabbitmq']['ctl_erl_args'] = 'test123'
      [/^SERVER_ADDITIONAL_ERL_ARGS='test123'/,
       /^CTL_ERL_ARGS='test123'/].each do |line|
        expect(chef_run).to render_file(file.name).with_content(line)
      end
    end

    it 'has no additional_env_settings default' do
      expect(chef_run).not_to render_file(file.name).with_content(/^# Additional ENV settings/)
    end

    it 'has additional_env_settings' do
      node.normal['rabbitmq']['additional_env_settings'] = [
        'USE_LONGNAME=true',
        'WHATS_ON_THE_TELLY=penguin']
      [/^WHATS_ON_THE_TELLY=penguin/,
       /^# Additional ENV settings/,
       /^USE_LONGNAME=true/].each do |line|
        expect(chef_run).to render_file(file.name).with_content(line)
      end
    end
  end

  it 'should create the config root directory' do
    expect(chef_run).to create_directory('/etc/rabbitmq')
      .with(
        :user => 'root',
        :group => 'root',
        :mode => '755'
      )
  end

  it 'should create the node data directory' do
    expect(chef_run).to create_directory('/var/lib/rabbitmq/mnesia')
      .with(
        :user => 'rabbitmq',
        :group => 'rabbitmq',
        :mode => '775'
      )
  end

  it 'does not enable a rabbitmq service when manage_service is false' do
    node.normal['rabbitmq']['manage_service'] = false
    expect(chef_run).not_to enable_service('rabbitmq-server')
  end

  it 'does not start a rabbitmq service when manage_service is false' do
    node.normal['rabbitmq']['manage_service'] = false
    expect(chef_run).not_to start_service('rabbitmq-server')
  end

  it 'enables a rabbitmq service when manage_service is true' do
    node.normal['rabbitmq']['manage_service'] = true
    expect(chef_run).to enable_service('rabbitmq-server')
  end

  it 'starts a rabbitmq service when manage_service is true' do
    node.normal['rabbitmq']['manage_service'] = true
    expect(chef_run).to start_service('rabbitmq-server')
  end

  it 'should have the use_distro_version set to false' do
    expect(chef_run.node['rabbitmq']['use_distro_version']).to eq(false)
  end


  describe "when Erlang is provisioned via Team RabbitMQ's Erlang packages"  do
    let(:runner) do
      ChefSpec::ServerRunner.new(REDHAT_OPTS) do |node, _|
        node.override['rabbitmq']['version'] = '3.7.13'
        node.override['rabbitmq']['erlang']['enabled'] = true
      end
    end

    let(:chef_run) do
      runner.converge(described_recipe, 'rabbitmq::erlang_package')
    end

    it 'should install Erlang' do
      expect(chef_run).to install_package('erlang')
    end
  end

  describe 'when Erlang is provisioned from ESL' do
    let(:runner) do
      ChefSpec::ServerRunner.new(REDHAT_OPTS) do |node, _|
        node.override['rabbitmq']['version'] = '3.7.13'
        node.override['rabbitmq']['erlang']['enabled'] = false
      end
    end

    let(:chef_run) do
      runner.converge(described_recipe, 'rabbitmq::esl_erlang_package')
    end

    it 'should install the ESL Erlang package' do
      expect(chef_run).to install_package('esl-erlang')
    end
  end


  it 'should create the rabbitmq /etc/default file' do
    expect(chef_run).to create_template("/etc/default/#{chef_run.node['rabbitmq']['service_name']}").with(
      :user => 'root',
      :group => 'root',
      :source => 'default.rabbitmq-server.erb',
      :mode => 00644
    )
  end

  it 'creates a template rabbitmq.config with attributes' do
    expect(chef_run).to create_template('/etc/rabbitmq/rabbitmq.config').with(
      :user => 'root',
      :group => 'root',
      :source => 'rabbitmq.config.erb',
      :mode => 00644)

    if Gem::Version.new(Chef::VERSION.to_s) >= Gem::Version.new('11.14.2')
      expect(chef_run).to create_template('/etc/rabbitmq/rabbitmq.config').with(:sensitive => true)
    else
      expect(chef_run).to create_template('/etc/rabbitmq/rabbitmq.config').with(:sensitive => false)
    end
{'fraction_non_alphanumeric': 0.1357270717068548, 'fraction_numerical': 0.009687269664104454, 'mean_word_length': 3.3588802202845343, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6571990', 'n_tokens_mistral': 3398, 'n_tokens_neox': 3154, 'n_words': 657}