control 'default' do
  title 'Checking maxwells default recipe, depends on attributes'

  attribute_file = '/tmp/kitchen/node_attributes.json'
  if file(attribute_file).exist?
    node = json(attribute_file).params

    maxwell_user    = node['default']['maxwell']['user']
    maxwell_group   = node['default']['maxwell']['group']
    maxwell_home    = node['default']['maxwell']['home']
    maxwell_service = node['default']['maxwell']['service_name']

    # maxwell user and home directory
    describe user(maxwell_user) do
      it { should exist }
      its('home') { should eq maxwell_home }
      its('shell') { should eq '/bin/bash' }
    end

    describe directory(maxwell_home) do
      it { should exist }
      it { should be_owned_by maxwell_user }
      it { should be_grouped_into maxwell_group }
    end

    # ./bin directory of maxwell installation
    describe directory("#{maxwell_home}/bin") do
      it { should exist }
      it { should be_owned_by maxwell_user }
      it { should be_grouped_into maxwell_group }
    end

    # Required packages
    packages = %w(
      java-1.8.0-openjdk
      java-1.8.0-openjdk-devel
      unzip
      zip
    )

    packages.each do |package|
      describe package(package) do
        it { should be_installed }
      end
    end

    # If using Kinesis producer, check for python and aws components
    if node['default']['maxwell']['producer'] == 'kinesis'
      describe package('python') do
        it { should be_installed }
        its('version') { should match(/2\.7/) }
      end

      python_packages = %w(
        awscli
      )

      python_packages.each do |pkg|
        describe command('pip list') do
          its('stdout') { should include pkg }
        end
      end

      describe file("#{maxwell_home}/.aws/credentials") do
        it { should exist }
        it { should be_owned_by maxwell_user }
        it { should be_grouped_into maxwell_group }
        its('mode') { should cmp '0640' }
      end

      describe file("#{maxwell_home}/.aws/config") do
        it { should exist }
        it { should be_owned_by maxwell_user }
        it { should be_grouped_into maxwell_group }
        its('mode') { should cmp '0640' }
      end

      describe file("#{maxwell_home}/kinesis-producer-library.properties") do
        it { should exist }
        it { should be_owned_by maxwell_user }
        it { should be_grouped_into maxwell_group }
        its('mode') { should cmp '0644' }
      end

    end

    describe file("#{maxwell_home}/config.properties") do
      it { should exist }
      it { should be_owned_by maxwell_user }
      it { should be_grouped_into maxwell_group }
      its('mode') { should cmp '0640' }
    end

    describe systemd_service(maxwell_service) do
      it { should be_installed }
      it { should be_enabled }
    end

  else
    puts "File #{attribute_file} does not exist, skiping attribute-depending checks"
  end
end
