<commit_before>require 'spec_helper'
require_relative '../../../libraries/consul_definition'

describe ConsulCookbook::Resource::ConsulDefinition do
  step_into(:consul_definition)
  let(:chefspec_options) { { platform: 'ubuntu', version: '14.04' } }
  before do
    default_attributes['consul'] = {
      'service_user' => 'consul',
      'service_group' => 'consul',
      'service' => {
        'config_dir' => '/etc/consul/conf.d',
      },
    }
  end

  context 'service definition' do
    recipe do
      consul_definition 'redis' do
        type 'service'
        user 'root'
        parameters(tags: %w(master), address: '127.0.0.1', port: 6379, interval: '10s')
      end
    end

    it { is_expected.to create_directory('/etc/consul/conf.d') }
    it do
      is_expected.to create_file('/etc/consul/conf.d/redis.json')
        .with(user: 'root', group: 'consul', mode: '0640')
        .with(content: JSON.pretty_generate(
          service: {
            tags: ['master'],
            address: '127.0.0.1',
            port: 6379,
            interval: '10s',
            name: 'redis',
          }
        ))
    end
  end

  context 'service definition with explicit name' do
    recipe do
      consul_definition 'redis' do
        type 'service'
        user 'root'
        parameters(name: 'myredis', tags: %w(master), address: '127.0.0.1', port: 6379, interval: '10s')
      end
    end

    it { is_expected.to create_directory('/etc/consul/conf.d') }
    it do
      is_expected.to create_file('/etc/consul/conf.d/redis.json')
        .with(user: 'root', group: 'consul', mode: '0640')
        .with(content: JSON.pretty_generate(
          service: {
            name: 'myredis',
            tags: ['master'],
            address: '127.0.0.1',
            port: 6379,
            interval: '10s',
          }
        ))
    end
  end

  context 'service definition with owner and group from node config' do
    before do
      default_attributes['consul'] = {
        'service_user' => 'root',
        'service_group' => 'root',
        'service' => {
          'config_dir' => '/etc/consul/conf.d',
        },
      }
    end

    recipe do
      consul_definition 'redis' do
        type 'service'
        parameters(name: 'myredis', tags: %w(master), address: '127.0.0.1', port: 6379, interval: '10s')
      end
    end

    it { is_expected.to create_directory('/etc/consul/conf.d') }
    it do
      is_expected.to create_file('/etc/consul/conf.d/redis.json')
        .with(user: 'root', group: 'root', mode: '0640')
        .with(content: JSON.pretty_generate(
          service: {
            name: 'myredis',
            tags: ['master'],
            address: '127.0.0.1',
            port: 6379,
            interval: '10s',
          }
        ))
    end
  end

  context 'check definition' do
    recipe do
      consul_definition 'web-api' do
        type 'check'
        user 'root'
        parameters(http: 'http://localhost:5000/health', ttl: '30s')
      end
    end

    it { is_expected.to create_directory('/etc/consul/conf.d') }
    it do
      is_expected.to create_file('/etc/consul/conf.d/web-api.json')
        .with(user: 'root', group: 'consul', mode: '0640')
        .with(content: JSON.pretty_generate(
          check: {
            http: 'http://localhost:5000/health',
            ttl: '30s',
            name: 'web-api',
          }
        ))
    end
  end
end
<commit_msg>spec: Fix consul definition file mode<commit_after>require 'spec_helper'
require_relative '../../../libraries/consul_definition'

describe ConsulCookbook::Resource::ConsulDefinition do
  step_into(:consul_definition)
  let(:chefspec_options) { { platform: 'ubuntu', version: '14.04' } }
  before do
    default_attributes['consul'] = {
      'service_user' => 'consul',
      'service_group' => 'consul',
      'service' => {
        'config_dir' => '/etc/consul/conf.d',
      },
    }
  end

  context 'service definition' do
    recipe do
      consul_definition 'redis' do
        type 'service'
        user 'root'
        parameters(tags: %w(master), address: '127.0.0.1', port: 6379, interval: '10s')
      end
    end

    it { is_expected.to create_directory('/etc/consul/conf.d') }
    it do
      is_expected.to create_file('/etc/consul/conf.d/redis.json')
        .with(user: 'root', group: 'consul', mode: '0644')
        .with(content: JSON.pretty_generate(
          service: {
            tags: ['master'],
            address: '127.0.0.1',
            port: 6379,
            interval: '10s',
            name: 'redis',
          }
        ))
    end
  end

  context 'service definition with explicit name' do
    recipe do
      consul_definition 'redis' do
        type 'service'
        user 'root'
        parameters(name: 'myredis', tags: %w(master), address: '127.0.0.1', port: 6379, interval: '10s')
      end
    end

    it { is_expected.to create_directory('/etc/consul/conf.d') }
    it do
      is_expected.to create_file('/etc/consul/conf.d/redis.json')
        .with(user: 'root', group: 'consul', mode: '0644')
        .with(content: JSON.pretty_generate(
          service: {
            name: 'myredis',
            tags: ['master'],
            address: '127.0.0.1',
            port: 6379,
            interval: '10s',
          }
        ))
    end
  end

  context 'service definition with owner and group from node config' do
    before do
      default_attributes['consul'] = {
        'service_user' => 'root',
        'service_group' => 'root',
        'service' => {
          'config_dir' => '/etc/consul/conf.d',
        },
      }
    end

    recipe do
      consul_definition 'redis' do
        type 'service'
        parameters(name: 'myredis', tags: %w(master), address: '127.0.0.1', port: 6379, interval: '10s')
      end
    end

    it { is_expected.to create_directory('/etc/consul/conf.d') }
    it do
      is_expected.to create_file('/etc/consul/conf.d/redis.json')
        .with(user: 'root', group: 'root', mode: '0644')
        .with(content: JSON.pretty_generate(
          service: {
            name: 'myredis',
            tags: ['master'],
            address: '127.0.0.1',
            port: 6379,
            interval: '10s',
          }
        ))
    end
  end

  context 'check definition' do
    recipe do
      consul_definition 'web-api' do
        type 'check'
        user 'root'
        parameters(http: 'http://localhost:5000/health', ttl: '30s')
      end
    end

    it { is_expected.to create_directory('/etc/consul/conf.d') }
    it do
      is_expected.to create_file('/etc/consul/conf.d/web-api.json')
        .with(user: 'root', group: 'consul', mode: '0644')
        .with(content: JSON.pretty_generate(
          check: {
            http: 'http://localhost:5000/health',
            ttl: '30s',
            name: 'web-api',
          }
        ))
    end
  end
end

{'fraction_non_alphanumeric': 0.14516129032258066, 'fraction_numerical': 0.03022377215925603, 'mean_word_length': 1.9591573516766982, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 12, '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': '4503182', 'n_tokens_mistral': 2366, 'n_tokens_neox': 2233, 'n_words': 493}