control 'agent' do
  # Get node attributes
  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
    jenkins_user = node['default']['jenkins']['user']
    jenkins_home_dir = node['default']['jenkins']['jenkins_home_dir']
    docker_run_limit_seconds = node['default']['jenkins']['agent']['docker_run_limit_seconds']
    container_clean_up_log_file = node['default']['jenkins']['agent']['container_clean_up_log_file']

    describe user(jenkins_user) do
      it { should exist }
      its('home') { should eq jenkins_home_dir }
      its('shell') { should eq '/bin/bash' }
    end

    describe directory("#{jenkins_home_dir}/.ssh") do
      it { should exist }
      its('owner') { should eq jenkins_user }
      its('group') { should eq jenkins_user }
      its('mode') { should cmp '0700' }
    end

    describe file("#{jenkins_home_dir}/.ssh/config") do
      it { should exist }
      its('owner') { should eq jenkins_user }
      its('group') { should eq jenkins_user }
      its('mode') { should cmp '0600' }
      its('content') { should include "Host *\n\tStrictHostKeyChecking no" }
    end

    pub_key = 'AAAAB3NzaC1yc2EAAAADAQABAAACAQCiosRmf5zPirdrZ1PGjh5oekGS5V5wlb3/BH4XUq2VkpvCPZn0cKZqPhLNfVSRcyEm9Zbtx5Z+Vquzu/28g13VZx+JfJstEF35/ux0FPa2xjsnxmrkIakrhqe/xLzEJxMsFsWXmAiG0wTOv6dMrebpkvyshde1OkYOfqy6mJgStyxSFnk86Ij6krW6+6hFSbyOoYI4XPH98F0Ng8KAXy2OhqFIGpNMeoz+uRgmPgqVwC+/ZKyZO/xO01MUPBIUFTAD0DUEyXhrRyehGRopCK6jhxaclu6Nmy2jc7QLOWgSIr/Pn9KxwCa64MdOcGx962QMP0mYXUIH158HLejllDWwJMrzSXgKl2MQ96AdzbW72zV6did8ZUPPKLOaBAH5eNiDZzBlqLwWUQPBgHVxam3mCJdAUk/t2zowPsgGllgOSwvcRTX5/PJsWoo4lEOnqz32bcq2vcyB1JEWjFgt9BjzhrvoZvBudWPAeKXRP6HxotUpql5WzZtKuhre8RLhLjXY0az2TbSlCgPrgkr1OtdZfJzK6gel/q9HKt9bj99H3jEdROBkTfNIi5p3fbq4Ot9j70i9LcsT7Uikq5bS0j4spHClJrHkC+xXuwDtaP1oxglZARR/EVyWHTRp1HtRnTmccbyySN8BOMqh+KuhUr1IlnDANiIRFg6eeDa60hK8Bw=='
    describe file("#{jenkins_home_dir}/.ssh/authorized_keys") do
      it { should exist }
      its('content') { should include pub_key }
    end

    describe file("#{jenkins_home_dir}/.ssh/id_rsa") do
      it { should exist }
      its('owner') { should eq jenkins_user }
      its('group') { should eq jenkins_user }
      its('mode') { should cmp '0600' }
    end

    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 'IUS/x86_64' }
          its('stdout') { should include 'IUS-source' }
          its('stdout') { should include 'mysql56-community' }
        end

        # Required packages for Jenkins agents
        packages = %w(
          chromedriver
          chromium
          cifs-utils
          freetype-devel
          gcc
          git
          gnu-free-sans-fonts
          htop
          java-11-openjdk-devel
          libjpeg-turbo
          libjpeg-turbo-devel
          libmspack
          libwebp
          maven
          mysql-community-client
          mysql-community-devel
          nfs-utils
          nmap
          postfix
          postgresql96
          postgresql96-devel
          psmisc
          python34u
          python34u-libs
          python34u-devel
          python34u-pip
          python35u
          python35u-libs
          python35u-devel
          python35u-pip
          python36u
          python36u-libs
          python36u-devel
          redis
          rh-ruby25
          rh-ruby25-ruby-devel
          rh-ruby25-rubygem-bundler
          rh-ruby25-rubygem-mysql2
          rh-ruby25-rubygem-pg
          rh-ruby25-rubygem-rake
          rh-ruby25-rubygems-devel
          samba-common
          sqlite-devel
          sshpass
          strace
          subversion
          xorg-x11-fonts-Type1
          xorg-x11-fonts-75dpi
          xorg-x11-server-Xvfb
          zip
          zlib
          zlib-devel
        )
      end
    when 'debian'
      packages = %w(
        alsa-tools
        ansible
        awscli
        bison
        build-essential
        bzip2
        cron
        ffmpeg
        fontconfig
        fonts-recommended
        gcc
        git
        g++
        htop
        libjpeg62-turbo
        libjpeg62-turbo-dev
        libmysqlclient-dev
        libmysqlclient24
        libreadline8
        libreadline-dev
        libsqlite3-dev
        jq
        make
        maven
        mysql-community-client
        nfs-common
        nmap
        openjdk-17-jdk
        openjdk-17-jre
        openjdk-17-jdk-headless
        openjdk-17-jre-headless
        postfix
        postgresql-client
        postgresql-common
        psmisc
        readline-common
        redis
        redis-tools
        sbt
        sqlite3
        sshpass
        strace
        unzip
        xfonts-75dpi
        xvfb
        zip
        zlib1g
        zlib1g-dev
      )
    end

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

    describe crontab('root') do
      its('commands') { should include '/usr/bin/docker rm -v $(/usr/bin/docker ps -a -q -f status=exited) >/dev/null 2>&1' }
    end

    describe crontab('root').commands('/usr/bin/docker rm -v $(/usr/bin/docker ps -a -q -f status=exited) >/dev/null 2>&1') do
      its('hours') { should cmp '*/2' }
      its('minutes') { should cmp '0' }
    end

    describe crontab('root').commands('/bin/find /var/log/audit -type f -mmin +10 -exec rm -rf {} \; > /dev/null 2>&1') do
      its('hours') { should cmp '*' }
      its('minutes') { should cmp '*/15' }
    end

    describe file('/root/cleanup_containers.sh') do
      it { should exist }
      its('owner') { should eq 'root' }
      its('group') { should eq 'root' }
      its('mode') { should cmp '0755' }
    end

    describe crontab('root').commands("/usr/bin/bash cleanup_containers.sh #{docker_run_limit_seconds} >>#{container_clean_up_log_file} 2>&1") do
      its('hours') { should cmp '*' }
      its('minutes') { should cmp '*/15' }
    end

  else
    puts "File #{node_json} does not exist - skipping attribute-dependent checks"
  end
end
