<commit_before>require 'spec_helper'

describe MaestroDev::JenkinsWorker do

  describe 'build()' do
    
    it "should validate fields" do
      @workitem['fields'].delete('host')
      @workitem['fields'].delete('job')
      subject.perform(:build, @workitem)

      subject.error.should include("Missing Fields: host,job")
    end

    it "should build job with jenkins (create job)" do
      # Request for jenkins root, used to get list of projects
      stub_request(:get,  'http://localhost:8080//api/json').to_return(:body => JENKINS_ROOT_WITHOUT_JOB)
      # Request to create Buildaroo job
      stub_request(:post, 'http://localhost:8080//createItem?name=Buildaroo')
      # Request for details about Buildroo project
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/api/json').to_return(:body => BUILDAROO_DETAILS)
      # Request to kick off a build
      stub_request(:post, 'http://localhost:8080//job/Buildaroo/build').to_return(:body => '')
      # Request for status of a build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1//api/json').
        to_return(:status => 404).then.
        to_return(:body => BUILDAROO_STATUS_BUILDING).then.
        to_return(:body => BUILDAROO_STATUS_SUCCESS)
      # Request for log text of build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=0').
        to_return(:body => BUILDAROO_CONSOLE_1, :headers => {'X-Text-Size' => 100, 'X-More-Data' => true})
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=100').
        to_return(:body => BUILDAROO_CONSOLE_2, :headers => {'X-Text-Size' => 300})
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=300').
        to_return(:body => BUILDAROO_CONSOLE_3, :headers => {'X-Text-Size' => 300})
      # Request for test report
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/testReport/api/json').to_return(:status => 404)

      subject.perform(:build, @workitem)

      @workitem['fields']['__error__'].should be_nil
      @workitem['fields']['output'].should be_nil
    end

    it "should build a parameterized job with jenkins (create job)" do
      @workitem['fields']['parameters'] = [ 'param1=value1', 'param2=value2' ]

      # Request for jenkins root, used to get list of projects
      stub_request(:get,  'http://localhost:8080//api/json').to_return(:body => JENKINS_ROOT_WITHOUT_JOB)
      # Request to create Buildaroo job
      stub_request(:post, 'http://localhost:8080//createItem?name=Buildaroo')
      # Request for details about Buildroo project
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/api/json').to_return(:body => BUILDAROO_DETAILS)
      # Request to kick off a build
      stub_request(:post, 'http://localhost:8080//job/Buildaroo/buildWithParameters').
        with(:body => {:param1 => 'value1', :param2 => 'value2'}).
        to_return(:body => '')
      # Request for status of a build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1//api/json').
        to_return(:status => 404).then.
        to_return(:body => BUILDAROO_STATUS_BUILDING).then.
        to_return(:body => BUILDAROO_STATUS_SUCCESS)
      # Request for log text of build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=0').
        to_return(:body => BUILDAROO_CONSOLE_1, :headers => {'X-Text-Size' => 100, 'X-More-Data' => true})
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=100').
        to_return(:body => BUILDAROO_CONSOLE_2, :headers => {'X-Text-Size' => 300})
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=300').
        to_return(:body => BUILDAROO_CONSOLE_3, :headers => {'X-Text-Size' => 300})
      # Request for test report
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/testReport/api/json').to_return(:status => 404)

      subject.perform(:build, @workitem)

      @workitem['fields']['__error__'].should be_nil
      @workitem['fields']['output'].should be_nil
    end

    it "should fail if build details fails to respond" do
      # Request for jenkins root, used to get list of projects
      stub_request(:get,  'http://localhost:8080//api/json').to_return(:body => JENKINS_ROOT_WITHOUT_JOB)
      # Request to create Buildaroo job
      stub_request(:post, 'http://localhost:8080//createItem?name=Buildaroo')
      # Request for details about Buildroo project
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/api/json').to_return(:body => BUILDAROO_DETAILS)
      # Request to kick off a build
      stub_request(:post, 'http://localhost:8080//job/Buildaroo/build').to_return(:body => '')
      # Request for status of a build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1//api/json').
        to_timeout

      subject.perform(:build, @workitem)

      subject.error.should eq("Timed out trying to get build details for Buildaroo build number 1")
    end

    it "should build jobs with user defined axes with jenkins" do
      @workitem['fields']['user_defined_axes'] = ['goal install package']
      @workitem['fields']['label_axes'] = ['linux', 'macos']
      @workitem['fields']['steps'] = ['ls -la /']

      # Request for jenkins root, used to get list of projects
      stub_request(:get,  'http://localhost:8080//api/json').to_return(:body => JENKINS_ROOT_WITHOUT_JOB)
      # Request to create Buildaroo job
      stub_request(:post, 'http://localhost:8080//createItem?name=Buildaroo')
      # Request for details about Buildroo project
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/api/json').to_return(:body => BUILDAROO_DETAILS)
      # Request to kick off a build
      stub_request(:post, 'http://localhost:8080//job/Buildaroo/build').to_return(:body => '')
      # Request for status of a build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1//api/json').
        to_return(:status => 404).then.
        to_return(:body => BUILDAROO_STATUS_BUILDING).then.
        to_return(:body => BUILDAROO_STATUS_SUCCESS)
      # Request for log text of build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=0').
        to_return(:body => BUILDAROO_CONSOLE_1, :headers => {'X-Text-Size' => 100, 'X-More-Data' => true})
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=100').
        to_return(:body => BUILDAROO_CONSOLE_2, :headers => {'X-Text-Size' => 300})
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=300').
        to_return(:body => BUILDAROO_CONSOLE_3, :headers => {'X-Text-Size' => 300})
      # Request for test report
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/testReport/api/json').to_return(:status => 404)

      subject.perform(:build, @workitem)

      @workitem['fields']['__error__'].should be_nil
      @workitem['fields']['output'].should be_nil
    end

    it "should supply error when job fails to start" do
      # Request for jenkins root, used to get list of projects
      stub_request(:get,  'http://localhost:8080//api/json').to_return(:body => JENKINS_ROOT_WITHOUT_JOB)
      # Request to create Buildaroo job
      stub_request(:post, 'http://localhost:8080//createItem?name=Buildaroo')
      # Request for details about Buildroo project
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/api/json').to_return(:body => BUILDAROO_DETAILS)
      # Request to kick off a build
      stub_request(:post, 'http://localhost:8080//job/Buildaroo/build').to_return(:status => 400)

      subject.perform(:build, @workitem)

      subject.error.should eq("Jenkins job failed to start")
    end

    it "should supply error when job fails to be created" do
      # Request for jenkins root, used to get list of projects
      stub_request(:get,  'http://localhost:8080//api/json').to_return(:body => JENKINS_ROOT_WITHOUT_JOB)
      # Request to create Buildaroo job
      stub_request(:post, 'http://localhost:8080//createItem?name=Buildaroo').to_return(:status => 500, :body => 'error')

      subject.perform(:build, @workitem)

      subject.error.should eql("Failed to create job Buildaroo: JenkinsApi::Exceptions::InternelServerErrorException, Error: Internel Server Error. Perhaps the in-memory configuration of Jenkins is different from the disk configuration. Please try to reload the configuration ")
    end

    it "should supply error when job fails" do
      # Request for jenkins root, used to get list of projects
      stub_request(:get,  'http://localhost:8080//api/json').to_return(:body => JENKINS_ROOT_WITHOUT_JOB)
      # Request to create Buildaroo job
      stub_request(:post, 'http://localhost:8080//createItem?name=Buildaroo')
      # Request for details about Buildroo project
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/api/json').to_return(:body => BUILDAROO_DETAILS)
      # Request to kick off a build
      stub_request(:post, 'http://localhost:8080//job/Buildaroo/build').to_return(:body => '')
      # Request for status of a build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1//api/json').
        to_return(:status => 404).then.
        to_return(:body => BUILDAROO_STATUS_BUILDING).then.
        to_return(:body => BUILDAROO_STATUS_FAILED)
      # Request for log text of build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=0').<commit_msg>adjust spec for new api client version<commit_after>require 'spec_helper'

describe MaestroDev::JenkinsWorker do

  describe 'build()' do
    
    it "should validate fields" do
      @workitem['fields'].delete('host')
      @workitem['fields'].delete('job')
      subject.perform(:build, @workitem)

      subject.error.should include("Missing Fields: host,job")
    end

    it "should build job with jenkins (create job)" do
      # Request for jenkins root, used to get list of projects
      stub_request(:get,  'http://localhost:8080//api/json').to_return(:body => JENKINS_ROOT_WITHOUT_JOB)
      # Request to create Buildaroo job
      stub_request(:post, 'http://localhost:8080//createItem?name=Buildaroo')
      # Request for details about Buildroo project
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/api/json').to_return(:body => BUILDAROO_DETAILS)
      # Request to kick off a build
      stub_request(:post, 'http://localhost:8080//job/Buildaroo/build').to_return(:body => '')
      # Request for status of a build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1//api/json').
        to_return(:status => 404).then.
        to_return(:body => BUILDAROO_STATUS_BUILDING).then.
        to_return(:body => BUILDAROO_STATUS_SUCCESS)
      # Request for log text of build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=0').
        to_return(:body => BUILDAROO_CONSOLE_1, :headers => {'X-Text-Size' => 100, 'X-More-Data' => true})
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=100').
        to_return(:body => BUILDAROO_CONSOLE_2, :headers => {'X-Text-Size' => 300})
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=300').
        to_return(:body => BUILDAROO_CONSOLE_3, :headers => {'X-Text-Size' => 300})
      # Request for test report
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/testReport/api/json').to_return(:status => 404)

      subject.perform(:build, @workitem)

      @workitem['fields']['__error__'].should be_nil
      @workitem['fields']['output'].should be_nil
    end

    it "should build a parameterized job with jenkins (create job)" do
      @workitem['fields']['parameters'] = [ 'param1=value1', 'param2=value2' ]

      # Request for jenkins root, used to get list of projects
      stub_request(:get,  'http://localhost:8080//api/json').to_return(:body => JENKINS_ROOT_WITHOUT_JOB)
      # Request to create Buildaroo job
      stub_request(:post, 'http://localhost:8080//createItem?name=Buildaroo')
      # Request for details about Buildroo project
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/api/json').to_return(:body => BUILDAROO_DETAILS)
      # Request to kick off a build
      stub_request(:post, 'http://localhost:8080//job/Buildaroo/buildWithParameters').
        with(:body => {:param1 => 'value1', :param2 => 'value2'}).
        to_return(:body => '')
      # Request for status of a build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1//api/json').
        to_return(:status => 404).then.
        to_return(:body => BUILDAROO_STATUS_BUILDING).then.
        to_return(:body => BUILDAROO_STATUS_SUCCESS)
      # Request for log text of build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=0').
        to_return(:body => BUILDAROO_CONSOLE_1, :headers => {'X-Text-Size' => 100, 'X-More-Data' => true})
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=100').
        to_return(:body => BUILDAROO_CONSOLE_2, :headers => {'X-Text-Size' => 300})
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=300').
        to_return(:body => BUILDAROO_CONSOLE_3, :headers => {'X-Text-Size' => 300})
      # Request for test report
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/testReport/api/json').to_return(:status => 404)

      subject.perform(:build, @workitem)

      @workitem['fields']['__error__'].should be_nil
      @workitem['fields']['output'].should be_nil
    end

    it "should fail if build details fails to respond" do
      # Request for jenkins root, used to get list of projects
      stub_request(:get,  'http://localhost:8080//api/json').to_return(:body => JENKINS_ROOT_WITHOUT_JOB)
      # Request to create Buildaroo job
      stub_request(:post, 'http://localhost:8080//createItem?name=Buildaroo')
      # Request for details about Buildroo project
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/api/json').to_return(:body => BUILDAROO_DETAILS)
      # Request to kick off a build
      stub_request(:post, 'http://localhost:8080//job/Buildaroo/build').to_return(:body => '')
      # Request for status of a build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1//api/json').
        to_timeout

      subject.perform(:build, @workitem)

      subject.error.should eq("Timed out trying to get build details for Buildaroo build number 1")
    end

    it "should build jobs with user defined axes with jenkins" do
      @workitem['fields']['user_defined_axes'] = ['goal install package']
      @workitem['fields']['label_axes'] = ['linux', 'macos']
      @workitem['fields']['steps'] = ['ls -la /']

      # Request for jenkins root, used to get list of projects
      stub_request(:get,  'http://localhost:8080//api/json').to_return(:body => JENKINS_ROOT_WITHOUT_JOB)
      # Request to create Buildaroo job
      stub_request(:post, 'http://localhost:8080//createItem?name=Buildaroo')
      # Request for details about Buildroo project
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/api/json').to_return(:body => BUILDAROO_DETAILS)
      # Request to kick off a build
      stub_request(:post, 'http://localhost:8080//job/Buildaroo/build').to_return(:body => '')
      # Request for status of a build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1//api/json').
        to_return(:status => 404).then.
        to_return(:body => BUILDAROO_STATUS_BUILDING).then.
        to_return(:body => BUILDAROO_STATUS_SUCCESS)
      # Request for log text of build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=0').
        to_return(:body => BUILDAROO_CONSOLE_1, :headers => {'X-Text-Size' => 100, 'X-More-Data' => true})
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=100').
        to_return(:body => BUILDAROO_CONSOLE_2, :headers => {'X-Text-Size' => 300})
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=300').
        to_return(:body => BUILDAROO_CONSOLE_3, :headers => {'X-Text-Size' => 300})
      # Request for test report
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/testReport/api/json').to_return(:status => 404)

      subject.perform(:build, @workitem)

      @workitem['fields']['__error__'].should be_nil
      @workitem['fields']['output'].should be_nil
    end

    it "should supply error when job fails to start" do
      # Request for jenkins root, used to get list of projects
      stub_request(:get,  'http://localhost:8080//api/json').to_return(:body => JENKINS_ROOT_WITHOUT_JOB)
      # Request to create Buildaroo job
      stub_request(:post, 'http://localhost:8080//createItem?name=Buildaroo')
      # Request for details about Buildroo project
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/api/json').to_return(:body => BUILDAROO_DETAILS)
      # Request to kick off a build
      stub_request(:post, 'http://localhost:8080//job/Buildaroo/build').to_return(:status => 400)

      subject.perform(:build, @workitem)

      subject.error.should eq("Jenkins job failed to start")
    end

    it "should supply error when job fails to be created" do
      # Request for jenkins root, used to get list of projects
      stub_request(:get,  'http://localhost:8080//api/json').to_return(:body => JENKINS_ROOT_WITHOUT_JOB)
      # Request to create Buildaroo job
      stub_request(:post, 'http://localhost:8080//createItem?name=Buildaroo').to_return(:status => 500, :body => 'error')

      subject.perform(:build, @workitem)

      subject.error.should eql("Failed to create job Buildaroo: JenkinsApi::Exceptions::InternelServerErrorException, Internel Server Error. Perhaps the in-memory configuration of Jenkins is different from the disk configuration. Please try to reload the configuration ")
    end

    it "should supply error when job fails" do
      # Request for jenkins root, used to get list of projects
      stub_request(:get,  'http://localhost:8080//api/json').to_return(:body => JENKINS_ROOT_WITHOUT_JOB)
      # Request to create Buildaroo job
      stub_request(:post, 'http://localhost:8080//createItem?name=Buildaroo')
      # Request for details about Buildroo project
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/api/json').to_return(:body => BUILDAROO_DETAILS)
      # Request to kick off a build
      stub_request(:post, 'http://localhost:8080//job/Buildaroo/build').to_return(:body => '')
      # Request for status of a build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1//api/json').
        to_return(:status => 404).then.
        to_return(:body => BUILDAROO_STATUS_BUILDING).then.
        to_return(:body => BUILDAROO_STATUS_FAILED)
      # Request for log text of build
      stub_request(:get,  'http://localhost:8080//job/Buildaroo/1/logText/progressiveText?start=0').
{'fraction_non_alphanumeric': 0.14655975168132437, 'fraction_numerical': 0.029798241076047595, 'mean_word_length': 3.6424111431316044, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 141, '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': '5684773', 'n_tokens_mistral': 6517, 'n_tokens_neox': 6114, 'n_words': 1236}