<commit_before>
class TestRemoteExec < ActiveSupport::TestCase

  def setup
    WebMock.reset!
  end

  def test_create_spec
      c = CommandSpec.new(:repo => "support", :bundle => "foobar")
      assert_equal c, RemoteExec.create_spec(c)

      c = CommandSpec.new(:repo => "support", :bundle => "foobar")
      s = RemoteExec.create_spec(c.to_json)
      assert_equal c.repo, s.repo
      assert_equal c.bundle, s.bundle

      repo = Repo.new(:name => "vendor")
      cmd = Command.new(:bundle => "foobar", :command => "baz", :repo => repo)
      cs = RemoteExec.create_spec(cmd)

      assert_not_equal cs, cmd
      assert_equal "baz", cs.command
      assert_equal "foobar", cs.bundle

  end

  def test_exec
    repo  = Repo.new(:name => "vendor")
    agent = Agent.new(:ip => "2.2.2.2", :port => 18000)
    cmd   = Command.new(:bundle => "foobar", :command => "baz", :repo => repo)

    stub = stub_request(:post, "http://2.2.2.2:18000/").with { |req|
      j = JSON.parse(req.body)
      jp = j["params"]
      j["operation"] == "exec" and jp["repo"] == "vendor" and jp["bundle"] == "foobar" and jp["command"] == "baz"
    }.to_return(:status => 200, :body => JsonResponse.new("success").to_json)

    ret = RemoteExec.exec(agent, cmd)

    assert_requested(stub)
    assert_equal "success", ret.status
  end

  def test_exec_with_provision

    BundleRepository.path = "#{Rails.root}/test"
    repo  = Repo.new(:name => "support")
    agent = Agent.new(:ip => "2.2.2.2", :port => 18000)
    cmd   = Command.new(:bundle => "test_bundle", :command => "echo", :repo => repo)

    url = "http://2.2.2.2:18000/"
    res = []
    res << JsonResponse.bundle_not_found(cmd).to_json
    res << JsonResponse.new("success", "", {:status => 0, :stdout => "frobnicator echoed"}).to_json
    stub = stub_request(:post, url).with { |req|
      j = JSON.parse(req.body)
      jp = j["params"]
      j["operation"] == "exec" and jp["repo"] == "support" and jp["bundle"] == "test_bundle" and jp["command"] == "echo"
    }.to_return { { :status => 200, :body => res.shift } }

    stub2 = stub_request(:post, url).with { |req|
      req.body =~ %r{system/provisioning} and req.body =~ /get_bundle.rb/
    }.to_return(:status => 200, :body => JsonResponse.new("success", "", {}).to_json).times(3)

    ret = RemoteExec.exec(agent, cmd)
<commit_msg>fixed test<commit_after>
class TestRemoteExec < ActiveSupport::TestCase

  def setup
    WebMock.reset!
  end

  def test_create_spec
      c = CommandSpec.new(:repo => "support", :bundle => "foobar")
      assert_equal c, RemoteExec.create_spec(c)

      c = CommandSpec.new(:repo => "support", :bundle => "foobar")
      s = RemoteExec.create_spec(c.to_json)
      assert_equal c.repo, s.repo
      assert_equal c.bundle, s.bundle

      repo = Repo.new(:name => "vendor")
      cmd = Command.new(:bundle => "foobar", :command => "baz", :repo => repo)
      cs = RemoteExec.create_spec(cmd)

      assert_not_equal cs, cmd
      assert_equal "baz", cs.command
      assert_equal "foobar", cs.bundle

  end

  def test_exec
    repo  = Repo.new(:name => "vendor")
    agent = Agent.new(:ip => "2.2.2.2", :port => 18000)
    cmd   = Command.new(:bundle => "foobar", :command => "baz", :repo => repo)

    stub = stub_request(:post, "http://2.2.2.2:18000/").with { |req|
      j = JSON.parse(req.body)
      jp = j["params"]
      j["operation"] == "exec" and jp["repo"] == "vendor" and jp["bundle"] == "foobar" and jp["command"] == "baz"
    }.to_return(:status => 200, :body => JsonResponse.new("success", "", {:status => 0, :stdout => "frobnicator echoed"}).to_json)

    ret = RemoteExec.exec(agent, cmd)

    assert_requested(stub)
    assert ret.success?
  end

  def test_exec_with_provision

    BundleRepository.path = "#{Rails.root}/test"
    repo  = Repo.new(:name => "support")
    agent = Agent.new(:ip => "2.2.2.2", :port => 18000)
    cmd   = Command.new(:bundle => "test_bundle", :command => "echo", :repo => repo)

    url = "http://2.2.2.2:18000/"
    res = []
    res << JsonResponse.bundle_not_found(cmd).to_json
    res << JsonResponse.new("success", "", {:status => 0, :stdout => "frobnicator echoed"}).to_json
    stub = stub_request(:post, url).with { |req|
      j = JSON.parse(req.body)
      jp = j["params"]
      j["operation"] == "exec" and jp["repo"] == "support" and jp["bundle"] == "test_bundle" and jp["command"] == "echo"
    }.to_return { { :status => 200, :body => res.shift } }

    stub2 = stub_request(:post, url).with { |req|
      req.body =~ %r{system/provisioning} and req.body =~ /get_bundle.rb/
    }.to_return(:status => 200, :body => JsonResponse.new("success", "", {}).to_json).times(3)

    ret = RemoteExec.exec(agent, cmd)

{'fraction_non_alphanumeric': 0.1899002758328029, 'fraction_numerical': 0.020581370676851264, 'mean_word_length': 3.2051739518287246, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, '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': '7311485', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1639, 'n_words': 351}