require sprintf('%s/../../path_helper', File.dirname(File.expand_path(__FILE__)))

require 'rouster'
require 'test/unit'

# most of the real passthrough testing is done in test_new.rb, since the only effective difference should be the target of the SSH connection

class TestPassthroughs < Test::Unit::TestCase

  def setup
    @@user_sshkey = sprintf('%s/.ssh/id_rsa', ENV['HOME'])

    unless File.file?(@@user_sshkey)
      File.write(@@user_sshkey, '') # either this or `touch`
      File.chmod(0600, @@user_sshkey)
    end

  end

  def test_functional_local_passthrough

    assert_nothing_raised do
      @local = Rouster.new(
        :name => 'local',
        :passthrough => {
          :type => :local,
        },
        :verbose => 4,
      )
    end

    assert(@local.is_passthrough?(), 'worker is a passthrough')
    assert_equal(false, @local.is_available_via_ssh?(), 'worker is available via SSH')

    # put a file in /tmp/fizz and read it back
    tmpfile = sprintf('/tmp/fizzy.%s.%s', Time.now.to_i, $$)
    content = 'this is some sample text'

    assert_nothing_raised do
      @local.run("echo #{content} >> #{tmpfile}")
    end

    read = @local.run("cat #{tmpfile}").chomp! # using >> automatically includes \n

    assert_equal(content, read, 'worker is able to read and write files on system')

    # TODO better here
    assert_nothing_raised do
      @local.file('/etc/hosts')
      @local.dir('/tmp')
    end

  end

  def test_functional_remote_passthrough

    omit('not running test_good_remote_passthrough, autogenerated a fake ssh key') if File.file?(@@user_sshkey) and File.read(@@user_sshkey).eql?("")

    host = '127.0.0.1'
    `ssh -i #{@@user_sshkey} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null #{host} exit`
    omit("found an ssh key, but it doesn't appear to be valid for this host") unless $?.success?

    assert_nothing_raised do
      @remote = Rouster.new(
        :name => 'remote',
        :passthrough => {
          :type => :remote,
          :host => host,
          :user => ENV['USER'],
          :key  => @@user_sshkey,
        },
        :verbosity => 4,
      )
    end

    assert_equal('remote', @remote.name)
    assert_equal(true, @remote.is_passthrough?())
    assert_equal(false, @remote.uses_sudo?())
    assert_equal(true, @remote.is_available_via_ssh?())

    # TODO better here
    assert_nothing_raised do
      @remote.file('/etc/hosts')
      @remote.dir('/tmp')
    end

  end

  def teardown
    # if the file is empty, we know we created it (or it doesn't matter)..
    File.delete(@@user_sshkey) if File.file?(@@user_sshkey) and File.read(@@user_sshkey).eql?('')
  end

end
