control 'qa_bulkupload' do
  attribute_file = '/tmp/kitchen/node_attributes.json'
  if file(attribute_file).exist?
    node = json(attribute_file).params
    # TODO: we skip included recipes for now

    case node['automatic']['platform_family']
    when 'rhel'
      if node['automatic']['platform_version'].to_s.start_with?('7')

        packages = %w(
          autoconf
          bzip2-devel
          freetype-devel
          gcc-c++
          glibc
          gmp-devel
          httpd-devel
          ImageMagick
          ImageMagick-devel
          libcurl-devel
          libdb4-devel
          libevent
          libjpeg-turbo
          libjpeg-turbo-devel
          libmcrypt
          libmcrypt-devel
          libmemcached
          libpng-devel
          libtiff
          libtiff-devel
          libxml2-devel
          make
          memcached
          mhash
          mhash-devel
          mod_ssl
          openssl-devel
          zip
        )
        packages.each do |name|
          describe package name do
            it { should be_installed }
          end
        end

        describe file '/opt/remi/php56/root/etc/php.ini' do
          it { should exist }
          its('owner') { should eq 'root' }
          its('group') { should eq 'root' }
          its('mode') { should cmp '0644' }
        end

        describe file '/opt/remi/php56/root/etc/php.d/10-opcache.ini' do
          it { should exist }
          its('owner') { should eq 'root' }
          its('group') { should eq 'root' }
          its('mode') { should cmp '0644' }
        end

        describe file '/opt/remi/php56/root/etc/php.d/50-memcached.ini' do
          it { should exist }
          its('owner') { should eq 'root' }
          its('group') { should eq 'root' }
          its('mode') { should cmp '0644' }
        end

        describe directory '/var/www/html_qa/bulkupload/public' do
          it { should exist }
          its('owner') { should eq 'deploy' }
          its('group') { should eq 'webdev' }
          its('mode') { should cmp '0755' }
        end

        deploy_home_dir = '/home/deploy'
        github_ssh_key_location = "#{deploy_home_dir}/.ssh/id_rsa"
        data_bag_path = '../../chef_repo/data_bags/secrets/qa_github_ssh_key.json'
        github_ssh_key = JSON.parse(File.read(data_bag_path)).to_hash
        private_key = github_ssh_key['private_key'].chomp
        describe file github_ssh_key_location do
          its('owner') { should eq 'deploy' }
          its('mode') { should cmp '0400' }
          its('content') { should eq private_key }
        end

        key_config = "Host *\n\tStrictHostKeyChecking no\n"
        describe file "#{deploy_home_dir}/.ssh/config" do
          its('owner') { should eq 'deploy' }
          its('mode') { should cmp '0600' }
          its('content') { should eq key_config }
        end

        cookbook_file = File.read('files/default/github.pub')
        describe file "#{deploy_home_dir}/.ssh/id_rsa.pub" do
          its('mode') { should cmp '0644' }
          its('content') { should eq cookbook_file }
        end

        repo_url = 'git@github.com:/theorchard/bulkupload.git'
        describe file('/var/www/html_qa/bulkupload/public/.git/config') do
          it { should exist }
          its('owner') { should eq 'deploy' }
          its('group') { should eq 'webdev' }
          its('content') { should match(/url = #{repo_url}/) }
        end

        apache_dir = node['default']['apache']['dir']
        conf_path = "#{apache_dir}/conf-available"
        # Check only if such file exists
        describe file "#{conf_path}/bulkupload.conf" do
          it { should exist }
        end

        # Ensure the default vhost is disabled
        %w(default default.conf 000-default 000-default.conf).each do |site|
          describe file "#{apache_dir}/sites-enabled/#{site}" do
            it { should_not exist }
          end
        end

        # Vhost attributes are contained in roles
        node['normal']['webapps'].each do |vhost, vhost_info|
          describe file "#{apache_dir}/sites-available/#{vhost}.conf" do
            its('content') { should include "<VirtualHost *:#{vhost_info['port']}>" }
            its('content') { should include "ServerAdmin #{vhost_info['server_admin']}" }
            its('content') { should include "ServerName #{vhost_info['server_name']}" }
            its('content') { should include "DocumentRoot #{vhost_info['docroot']}" }
            # TODO: noto all params are checked, bette to use template rendering
          end
        end
      end
    else
      puts 'Unsupported platform'
    end

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