
describe "rake nginx:conf" do
  
  behaves_like "rake nginx" 

  before do
    # === nginx won't run unless all dirs exist.
    # Example: /nginx_data/hiya/public/
    nginx.task('setup_app_dirs')
    
    nginx.task('conf')
    
    @conf  = "#{nginx.dir}/conf/nginx.conf"
  end
  
  it 'must write a file to /conf/nginx.conf' do
    @conf.should.be file
  end

  it 'must set owner/group to root/root' do
    @conf.should.be owned_and_in('root')
  end
  
  it 'must set file permissions to world readable' do
    @conf.should have_permissions('-rw-r--r--')
  end

  it 'must include upstreams to thin servers' do
    contents = File.read(@conf)
    %w{ 888 999 }.each { |num|
      contents.should.match %r!server\ +[0-9\.]+:#{num}\d\ ?;!
    }
  end

  it 'must test conf file (nginx -t) before move to nginx conf dir' do
    conf_results = nginx.task('conf')
    file = %~.+\\.nginx\\.conf\\..+~
    
    results = conf_results.split("\n").select { |line|
      line =~ %r!nginx: the configuration file #{file} syntax is ok! ||
        line =~ %r!nginx: configuration file #{file} test is successful! ||
        line =~ %r!sudo mv #{file} #{@conf}!
    }.map { |line| line.strip.split[0,2].join(' ') }
    
    results.should.be == ['nginx: the', 'nginx: configuration', 'sudo mv']
    conf_results.should.not.match %r!test failed!
  end

end # === describe rake nginx:conf
