require 'json'

control 'etl_repo' do
  title 'Checking etl_repo recipe'

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

    packages = %w(
      gzip
      unzip
      zip
    )
    packages.each do |package|
      describe package(package) do
        it { should be_installed }
      end
    end

    git_directories = %w(
      /var/www
    )

    git_directories.each do |directory_name|
      describe directory(directory_name) do
        it { should exist }
        its('group') { should eq node['default']['git']['group'] }
        its('owner') { should eq node['default']['git']['owner'] }
        its('mode') { should cmp '0755' }
      end
    end

    # Lifehook for checking git repositories
    repo_url = 'git@github.com:/theorchard/etl.git'
    describe file('/var/www/etl/.git/config') do
      it { should exist }
      its('owner') { should eq 'deploy' }
      its('group') { should eq 'deploy' }
      its('content') { should match(/url = #{repo_url}/) }
    end

    describe file('/var/www/etl/php/configs/aws.yml') do
      it { should exist }
      its('owner') { should eq 'deploy' }
      its('group') { should eq 'deploy' }
      its('mode') { should cmp '0755' }
    end

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