# Inspec test for recipe irrigate-php::default

# The Inspec reference, with examples and extensive documentation, can be
# found at http://inspec.io/docs/reference/resources/

control 'default' do
  nodes_dir = '/tmp/kitchen/nodes'
  output = command("ls #{nodes_dir}").stdout.strip
  node_json = "#{nodes_dir}/#{output}"
  if file(node_json).exist?
    node = json(node_json).params
    case node['automatic']['platform_family']
    when 'rhel'
      if node['automatic']['platform_version'].to_s.start_with?('7')
        describe command('yum repolist') do
          its('stdout') { should include 'remi-php56' }
          its('stdout') { should include 'remi-php56-debuginfo' }
          its('stdout') { should include 'remi-safe' }
        end

        packages = %w(
          autoconf
          bzip2-devel
          epel-release
          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
          php56
          php56-php
          php56-php-bcmath
          php56-php-cli
          php56-php-common
          php56-php-devel
          php56-php-gd
          php56-php-gmp
          php56-php-ldap
          php56-php-mbstring
          php56-php-mcrypt
          php56-php-mysqlnd
          php56-php-opcache
          php56-php-pear
          php56-php-pecl-imagick-im6
          php56-php-pecl-imagick-im6-devel
          php56-php-pecl-memcache
          php56-php-pecl-memcached
          php56-php-pecl-redis
          php56-php-pecl-ssh2
          php56-php-pecl-uploadprogress
          php56-php-pecl-yaml
          php56-php-pecl-zip
          php56-php-pgsql
          php56-php-soap
          php56-php-xml
          unixODBC
          unixODBC-devel
        )

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

        require 'erb'
        node['php'] = node['default']['php']
        template1 = File.read('templates/default/remi-php.sh.erb')
        rendered_content1 = ERB.new(template1).result(binding)
        describe file '/etc/profile.d/remi-php.sh' do
          it { should exist }
          its('owner') { should eq 'root' }
          its('group') { should eq 'root' }
          its('mode') { should cmp '0644' }
          its('content') { should eq rendered_content1 }
        end

        template2 = File.read('templates/default/php.ini.erb')
        rendered_content2 = ERB.new(template2).result(binding)
        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' }
          its('content') { should eq rendered_content2 }
        end

        describe file '/usr/local/bin/php' do
          it { should be_symlink }
        end

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