<commit_before>require 'spec_helper'

include Serverspec::Helper::Gentoo

describe commands.check_enabled('httpd') do
  it { should eq "/sbin/rc-update show | grep '^\\s*httpd\\s*|\\s*\\(boot\\|default\\)'" }
end

describe commands.check_file('/etc/passwd') do
  it { should eq 'test -f /etc/passwd' }
end

describe commands.check_directory('/var/log') do
  it { should eq 'test -d /var/log' }
end

describe commands.check_user('root') do
  it { should eq 'id root' }
end

describe commands.check_group('wheel') do
  it { should eq 'getent group | grep -wq wheel' }
end

describe commands.check_installed('httpd') do
  it { should eq '/usr/bin/eix httpd --installed' }
end

describe commands.check_listening(80) do
  it { should eq "netstat -tunl | grep ':80 '" }
end

describe commands.check_running('httpd') do
  it { should eq 'service httpd status' }
end

describe commands.check_running_under_supervisor('httpd') do
  it { should eq 'supervisorctl status httpd' }
end

describe commands.check_process('httpd') do
  it { should eq 'ps aux | grep -w httpd | grep -qv grep' }
end

describe commands.check_file_contain('/etc/passwd', 'root') do
  it { should eq "grep -q 'root' /etc/passwd" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec') do
  it { should eq "sed -n '1,$p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/') do
  it { should eq "sed -n '/^group :test do/,$p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec', nil, '/^end/') do
  it { should eq "sed -n '1,/^end/p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/', '/^end/') do
  it { should eq "sed -n '/^group :test do/,/^end/p' Gemfile | grep -q 'rspec' -" }
end

describe commands.check_mode('/etc/sudoers', 440) do
  it { should eq 'stat -c %a /etc/sudoers | grep \'^440$\'' }
end

describe commands.check_owner('/etc/passwd', 'root') do
  it { should eq 'stat -c %U /etc/passwd | grep \'^root$\'' }
end

describe commands.check_grouped('/etc/passwd', 'wheel') do
  it { should eq 'stat -c %G /etc/passwd | grep \'^wheel$\'' }
end

describe commands.check_cron_entry('root', '* * * * * /usr/local/bin/batch.sh') do
  it { should eq 'crontab -u root -l | grep "\* \* \* \* \* /usr/local/bin/batch.sh"' }
end

describe commands.check_link('/etc/system-release', '/etc/redhat-release') do
  it { should eq 'stat -c %N /etc/system-release | grep /etc/redhat-release' }
end

describe commands.check_installed_by_gem('jekyll') do
  it { should eq 'gem list --local | grep \'^jekyll \'' }
end

describe commands.check_belonging_group('root', 'wheel') do
  it { should eq "id root | awk '{print $3}' | grep wheel" }
end

describe commands.check_iptables_rule('-P INPUT ACCEPT') do
  it { should eq "iptables -S | grep '\\-P INPUT ACCEPT'" }
end

describe commands.check_iptables_rule('-P INPUT ACCEPT', 'mangle', 'INPUT') do
  it { should eq "iptables -t mangle -S INPUT | grep '\\-P INPUT ACCEPT'" }
end

describe commands.get_mode('/dev') do<commit_msg>Fix gentoo/commands_spec.rb to use c.include<commit_after>require 'spec_helper'

describe 'check_enabled', :os => :gentoo do
  subject { commands.check_enabled('httpd') }
  it { should eq "/sbin/rc-update show | grep '^\\s*httpd\\s*|\\s*\\(boot\\|default\\)'" }
end

describe 'check_file', :os => :gentoo do
  subject { commands.check_file('/etc/passwd') }
  it { should eq 'test -f /etc/passwd' }
end

describe 'check_directory', :os => :gentoo do
  subject { commands.check_directory('/var/log') }
  it { should eq 'test -d /var/log' }
end

describe 'check_user', :os => :gentoo do
  subject { commands.check_user('root') }
  it { should eq 'id root' }
end

describe 'check_group', :os => :gentoo do
  subject { commands.check_group('wheel') }
  it { should eq 'getent group | grep -wq wheel' }
end

describe 'check_installed', :os => :gentoo do
  subject { commands.check_installed('httpd') }
  it { should eq '/usr/bin/eix httpd --installed' }
end

describe 'check_listening', :os => :gentoo do
  subject { commands.check_listening(80) }
  it { should eq "netstat -tunl | grep ':80 '" }
end

describe 'check_running', :os => :gentoo do
  subject { commands.check_running('httpd') }
  it { should eq 'service httpd status' }
end

describe 'check_running_under_supervisor', :os => :gentoo do
  subject { commands.check_running_under_supervisor('httpd') }
  it { should eq 'supervisorctl status httpd' }
end

describe 'check_process', :os => :gentoo do
  subject { commands.check_process('httpd') }
  it { should eq 'ps aux | grep -w httpd | grep -qv grep' }
end

describe 'check_file_contain', :os => :gentoo do
  subject { commands.check_file_contain('/etc/passwd', 'root') }
  it { should eq "grep -q 'root' /etc/passwd" }
end

describe 'check_file_contain_within', :os => :gentoo do
  context 'contain a pattern in the file' do
    subject { commands.check_file_contain_within('Gemfile', 'rspec') }
    it { should eq "sed -n '1,$p' Gemfile | grep -q 'rspec' -" }
  end

  context 'contain a pattern after a line in a file' do
    subject { commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/') }
    it { should eq "sed -n '/^group :test do/,$p' Gemfile | grep -q 'rspec' -" }
  end

  context 'contain a pattern before a line in a file' do
    subject {commands.check_file_contain_within('Gemfile', 'rspec', nil, '/^end/') }
    it { should eq "sed -n '1,/^end/p' Gemfile | grep -q 'rspec' -" }
  end

  context 'contain a pattern from within a line and another line in a file' do
    subject { commands.check_file_contain_within('Gemfile', 'rspec', '/^group :test do/', '/^end/') }
    it { should eq "sed -n '/^group :test do/,/^end/p' Gemfile | grep -q 'rspec' -" }
  end
end

describe 'check_mode', :os => :gentoo do
  subject { commands.check_mode('/etc/sudoers', 440) }
  it { should eq 'stat -c %a /etc/sudoers | grep \'^440$\'' }
end

describe 'check_owner', :os => :gentoo do
  subject { commands.check_owner('/etc/passwd', 'root') }
  it { should eq 'stat -c %U /etc/passwd | grep \'^root$\'' }
end

describe 'check_grouped', :os => :gentoo do
  subject { commands.check_grouped('/etc/passwd', 'wheel') }
  it { should eq 'stat -c %G /etc/passwd | grep \'^wheel$\'' }
end

describe 'check_cron_entry', :os => :gentoo do
  subject { commands.check_cron_entry('root', '* * * * * /usr/local/bin/batch.sh') }
  it { should eq 'crontab -u root -l | grep "\* \* \* \* \* /usr/local/bin/batch.sh"' }
end

describe 'check_link', :os => :gentoo do
  subject { commands.check_link('/etc/system-release', '/etc/redhat-release') }
  it { should eq 'stat -c %N /etc/system-release | grep /etc/redhat-release' }
end

describe 'check_installed_by_gem', :os => :gentoo do
  subject { commands.check_installed_by_gem('jekyll') }
  it { should eq 'gem list --local | grep \'^jekyll \'' }
end

describe 'check_belonging_group', :os => :gentoo do
  subject { commands.check_belonging_group('root', 'wheel') }
  it { should eq "id root | awk '{print $3}' | grep wheel" }
end

describe 'check_ipatbles', :os => :gentoo do
  context 'check a rule without a table and a chain' do
    subject { commands.check_iptables_rule('-P INPUT ACCEPT') }
    it { should eq "iptables -S | grep '\\-P INPUT ACCEPT'" }
  end

  context 'chack a rule with a table and a chain' do
    subject { commands.check_iptables_rule('-P INPUT ACCEPT', 'mangle', 'INPUT') }
    it { should eq "iptables -t mangle -S INPUT | grep '\\-P INPUT ACCEPT'" }
  end
end

describe 'check_selinux', :os => :gentoo do
  context 'enforcing' do
    subject { commands.check_selinux('enforcing') }
    it { should eq "/usr/sbin/getenforce | grep -i 'enforcing'" }
  end

  context 'permissive' do
    subject { commands.check_selinux('permissive') }
    it { should eq "/usr/sbin/getenforce | grep -i 'permissive'" }
  end

  context 'disabled' do
    subject { commands.check_selinux('disabled') }
    it { should eq "/usr/sbin/getenforce | grep -i 'disabled'" }
  end
end

describe 'get_mode', :os => :gentoo do
  subject { commands.get_mode('/dev') }
{'fraction_non_alphanumeric': 0.16111247243322716, 'fraction_numerical': 0.0031854937515314874, 'mean_word_length': 3.7157712305026, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2334459', 'n_tokens_mistral': 3056, 'n_tokens_neox': 2904, 'n_words': 859}