<commit_before>require 'spec_helper'

describe Nagira do 
  
  set :environment, :test       #  This is potentially desctructive test, run only in  test mode
  # exit unless ENV['RACK_ENV'] == 'test'
  include Rack::Test::Methods
  def app 
    @app ||= Nagira
  end
  
  let (:content_type) {  {'Content-Type' => 'application/json'} }
  let (:nagios_cmd )  { $nagios[:commands].path }
  before :all do
    get "/_status/_list.json"

    @host = JSON.parse(last_response.body).first
    get "/_status/#{@host}/_services.json"

    @good_data = {
      "service_description" => "Host Service", 
      "return_code" => 0, 
      "plugin_output" => "Plugin said: Bla" 
    }

  end

  before :each do 
    %x{ cat /dev/null > #{nagios_cmd}}
  end

  context "updates /_status/:host/_services" do
    
    let (:url) { "/_status/#{@host}/_services" }
    context "valid host" do
      
      it "update with valid data" do 
        put url, @good_data.to_json, content_type
        last_response.should be_ok
      end
      
      it "writes to nagios.cmd file" do 
        put url, @good_data.to_json, content_type
        File.read(nagios_cmd).should =~ /^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;#{@host}/
      end
      
      it 'Fails with missing data' do 
        @good_data.keys.each do |key|
          data = @good_data
          data.delete key
          put url, data.to_json, content_type
          last_response.status.should be 400
        end
      end

      it "ignores 404 for some services" do 
        pending
      end

    end                       # valid host

    context 'host does not exist' do<commit_msg>nagios.cmd file exist check before and after test.<commit_after>require 'spec_helper'

describe Nagira do 
  
  set :environment, :test       #  This is potentially desctructive test, run only in  test mode
  # exit unless ENV['RACK_ENV'] == 'test'
  include Rack::Test::Methods
  def app 
    @app ||= Nagira
  end
  
  let (:content_type) {  {'Content-Type' => 'application/json'} }
  let (:nagios_cmd )  { $nagios[:commands].path }
  before :all do
    get "/_status/_list.json"

    @host = JSON.parse(last_response.body).first
    get "/_status/#{@host}/_services.json"

    @good_data = {
      "service_description" => "Host Service", 
      "return_code" => 0, 
      "plugin_output" => "Plugin said: Bla" 
    }

  end

  before :each do 
    File.delete nagios_cmd rescue nil
  end

  context "updates /_status/:host/_services" do
    
    let (:url) { "/_status/#{@host}/_services" }
    context "valid host" do
      
      it "update with valid data" do 
        put url, @good_data.to_json, content_type
        last_response.should be_ok
      end
      
      it "writes to nagios.cmd file" do
        File.should_not exist(nagios_cmd)
        put url, @good_data.to_json, content_type
        File.should exist(nagios_cmd)
        File.read(nagios_cmd).should =~ /^\[\d+\] PROCESS_SERVICE_CHECK_RESULT;#{@host}/
      end
      
      it 'Fails with missing data' do 
        @good_data.keys.each do |key|
          data = @good_data
          data.delete key
          put url, data.to_json, content_type
          last_response.status.should be 400
        end
      end

      it "ignores 404 for some services" do 
        pending
      end

    end                       # valid host

    context 'host does not exist' do
{'fraction_non_alphanumeric': 0.10916641946010086, 'fraction_numerical': 0.004153070305547315, 'mean_word_length': 2.1841359773371103, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '1967959', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1091, 'n_words': 294}