control '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
    # May be we should check if there are tests for included cookbook and add it here??

    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

        deploy_home_dir = '/home/deploy'
        describe user 'deploy' do
          it { should exist }
          its('uid') { should cmp '1001' }
          its('home') { should eq deploy_home_dir }
          its('shell') { should eq '/bin/bash' }
        end

        describe group 'webdev' do
          its('gid') { should cmp '1099' }
        end

        %w(apache deploy).each do |user_name|
          describe user user_name do
            its('groups') { should include 'webdev' }
          end
        end

        pub_key = 'AAAAB3NzaC1yc2EAAAABIwAAAQEAzQ/4Vj3OP6uAiX6cfV5WcBlDQp5jATXMIboUSkEl/FZEbrZFGEsGxhOS5LllHvmH23wVA5rRtmoWxZdcWikiy7GOXzDsWwomBdj5837E55ScV8uL+qXyrN0iI7T8fygqJnqoe87xD742lMWUVujYl7QnQRgwd+LKblzHrvchr8yrl+0rHY1A1oxS1iiQsp+CU/DY+t35YQPFlh3CP1xEuUipcGNkzCZDY/hAI9Prv5Fzvj6RZl+Y+VzgUvoJqKB+rrdv14ZEmvMfNcgM2Tlh33QSQd66Adu2ns22ADXUfsM421nO1IgqZ5dROOblesN8DsrMVCAXM9zL6xddLX47YQ=='
        describe file("#{deploy_home_dir}/.ssh/authorized_keys") do
          it { should exist }
          its('content') { should include pub_key }
        end

        # TODO: ideally we should render template using Chef::Resource::Template and check its content with created one
        # Skipping context checking now
        describe file '/opt/remi/php56/root/etc/php.ini' do
          its('mode') { should cmp '0644' }
          its('owner') { should eq 'root' }
          its('group') { should eq 'root' }
        end

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

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

        describe directory '/var/www/bulkupload/public' do
          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']
        describe file github_ssh_key_location do
          its('owner') { should eq 'deploy' }
          its('mode') { should cmp '0400' }
          its('content') { should eq private_key }
        end

        # Relax ssh host key checks or initial git clone will fail
        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/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
