use strict;
use warnings;

use File::Temp;
use Test::More tests => 18;

use_ok 'Rex';
use_ok 'Rex::Config';

my $ssh_cfg1 = <<EOF;

# Sample SSH config w/o equal signs

Host frontend1
Hostname fe80::1
User bogey
Port 35221

Host web
Hostname 192.168.1.1
User root 

EOF

my $ssh_cfg2 = <<EOF;

Host = frontend2
Hostname = this.is.a.domain.tld
User = 123
Port = 1005

Host = some other hosts
Port = 3306

EOF

my $tempdir = File::Temp::tempdir( CLEANUP => 1 );
ok(open(my $FH1, '>', $tempdir.'/cfg1'),'Opened cfg1');
print $FH1 $ssh_cfg1;
ok(close($FH1),'Closed cfg1');

%Rex::Config::SSH_CONFIG_FOR = ();
Rex::Config::read_ssh_config_file($tempdir.'/cfg1');
my $c = \%Rex::Config::SSH_CONFIG_FOR;

is($c->{'web'}->{'user'},'root');
isnt($c->{'web'}->{'user'},'root ');
is($c->{'web'}->{'hostname'},'192.168.1.1');

is($c->{'frontend1'}->{'user'},'bogey');
is($c->{'frontend1'}->{'hostname'},'fe80::1');
is($c->{'frontend1'}->{'port'},35221);

ok(open(my $FH2, '>', $tempdir.'/cfg2'),'Opened cfg2');
print $FH2 $ssh_cfg2;
ok(close($FH2),'Closed cfg2');

%Rex::Config::SSH_CONFIG_FOR = ();
Rex::Config::read_ssh_config_file($tempdir.'/cfg2');

is($c->{'frontend2'}->{'user'},'123');
is($c->{'frontend2'}->{'hostname'},'this.is.a.domain.tld');
is($c->{'frontend2'}->{'port'},1005);

is($c->{'some'}->{'port'},'3306');
is($c->{'other'}->{'port'},'3306');
is($c->{'hosts'}->{'port'},'3306');

1;

