ec2ssh 2.0.8 → 3.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,41 @@
1
+ require 'ec2ssh/command/init'
2
+
3
+ describe Ec2ssh::Command::Init do
4
+ describe '#run' do
5
+ let(:command) do
6
+ described_class.new(cli).tap do |cmd|
7
+ allow(cmd).to receive(:ssh_config_path).and_return('/path/to/ssh/config')
8
+ allow(cmd).to receive(:dotfile_path).and_return('/path/to/dotfile')
9
+ end
10
+ end
11
+ let(:cli) do
12
+ double(:cli, red: nil, yellow: nil, green: nil)
13
+ end
14
+ let(:ssh_config) do
15
+ double(:ssh_config, mark_exist?: nil, append_mark!: nil)
16
+ end
17
+
18
+ before do
19
+ expect(ssh_config).to receive(:mark_exist?).and_return(mark_exist)
20
+ allow(command).to receive(:ssh_config).and_return(ssh_config)
21
+ end
22
+
23
+ context 'when the marker already exists' do
24
+ let(:mark_exist) { true }
25
+
26
+ it do
27
+ expect { command.run }.to raise_error(Ec2ssh::MarkAlreadyExists)
28
+ expect(ssh_config).to_not have_received(:append_mark!)
29
+ end
30
+ end
31
+
32
+ context 'when the marker does not exists' do
33
+ let(:mark_exist) { false }
34
+
35
+ it do
36
+ expect { command.run }.not_to raise_error
37
+ expect(ssh_config).to have_received(:append_mark!).once
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+ require 'ec2ssh/command/migrate'
3
+
4
+ describe Ec2ssh::Command::Migrate do
5
+ describe '#run' do
6
+ let(:cli) do
7
+ double(:cli,
8
+ options: options, yes?: nil,
9
+ red: nil, yellow: nil, green: nil)
10
+ end
11
+ let(:command) do
12
+ described_class.new cli
13
+ end
14
+ let(:options) do
15
+ double(:options, dotfile: '/dotfile')
16
+ end
17
+
18
+ before do
19
+ File.open('/dotfile', 'w') {|f| f.write dotfile_str }
20
+ end
21
+
22
+ around do |example|
23
+ silence_stdout { example.run }
24
+ end
25
+
26
+ context 'version 2' do
27
+ let(:dotfile_str) { <<-END }
28
+ ---
29
+ path: /path/to/ssh/config
30
+ aws_keys:
31
+ key1:
32
+ access_key_id: ACCESS_KEY1
33
+ secret_access_key: SECRET1
34
+ END
35
+
36
+ context 'yes' do
37
+ before do
38
+ expect(cli).to receive(:yes?).and_return(true)
39
+ Timecop.freeze(Time.utc(2014, 1, 1)) do
40
+ command.run
41
+ end
42
+ end
43
+
44
+ it do
45
+ expect(File.read('/dotfile')).to eq <<-END
46
+ path '/path/to/ssh/config'
47
+ aws_keys(
48
+ key1: { access_key_id: 'ACCESS_KEY1', secret_access_key: 'SECRET1' }
49
+ )
50
+
51
+ # You can use methods of AWS::EC2::Instance.
52
+ # See http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/Instance.html
53
+ host_line <<EOS
54
+ Host <%= tags['Name'] %>.<%= availability_zone %>
55
+ HostName <%= dns_name || private_ip_address %>
56
+ EOS
57
+
58
+ # ---
59
+ # path: /path/to/ssh/config
60
+ # aws_keys:
61
+ # key1:
62
+ # access_key_id: ACCESS_KEY1
63
+ # secret_access_key: SECRET1
64
+ END
65
+ end
66
+
67
+ it do
68
+ expect(File.read('/dotfile.20140101000000')).to eq(dotfile_str)
69
+ end
70
+ end
71
+
72
+ context 'no' do
73
+ before do
74
+ expect(cli).to receive(:yes?).and_return(false)
75
+ command.run
76
+ end
77
+
78
+ it do
79
+ expect(File.read('/dotfile')).to eq(dotfile_str)
80
+ end
81
+ end
82
+ end
83
+
84
+ context 'version 3' do
85
+ let(:dotfile_str) { <<-END }
86
+ path '/path/to/ssh/config'
87
+ aws_keys(
88
+ key1: { access_key_id: 'ACCESS_KEY1', secret_access_key: 'SECRET1' }
89
+ )
90
+
91
+ # You can use methods of AWS::EC2::Instance.
92
+ # See http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/Instance.html
93
+ host_line <<EOS
94
+ Host <%= tags['Name'] %>.<%= availability_zone %>
95
+ HostName <%= dns_name || private_ip_address %>
96
+ EOS
97
+ END
98
+
99
+ before do
100
+ command.run
101
+ end
102
+
103
+ it do
104
+ expect(File.read('/dotfile')).to eq(dotfile_str)
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+ require 'ec2ssh/command/remove'
3
+
4
+ describe Ec2ssh::Command::Remove do
5
+ describe '#run' do
6
+ let(:command) do
7
+ described_class.new(cli).tap do |cmd|
8
+ allow(cmd).to receive(:options).and_return(options)
9
+ end
10
+ end
11
+ let(:options) do
12
+ double(:options, path: '/ssh_config', dotfile: '/dotfile', aws_key: 'default')
13
+ end
14
+ let(:cli) do
15
+ double(:cli, options: options, red: nil, yellow: nil, green: nil)
16
+ end
17
+
18
+ let(:dotfile_str) { <<-END }
19
+ path '/dotfile'
20
+ aws_keys(
21
+ default: { access_key_id: 'ACCESS_KEY1', secret_access_key: 'SECRET1' }
22
+ )
23
+ host_line <<EOS
24
+ Host <%= tags['Name'] %>
25
+ HostName <%= private_ip_address %>
26
+ EOS
27
+ END
28
+
29
+ before do
30
+ File.open('/ssh_config', 'w') {|f| f.write ssh_config_str }
31
+ File.open('/dotfile', 'w') {|f| f.write dotfile_str }
32
+ end
33
+
34
+ context 'with unmarked ssh_config' do
35
+ let(:ssh_config_str) { '' }
36
+
37
+ it do
38
+ expect { command.run }.to raise_error(Ec2ssh::MarkNotFound)
39
+ end
40
+ end
41
+
42
+ context 'with marked ssh_config' do
43
+ let(:ssh_config_str) { <<-END }
44
+ # before lines...
45
+
46
+ ### EC2SSH BEGIN ###
47
+ ### EC2SSH END ###
48
+
49
+ # after lines...
50
+ END
51
+
52
+ before do
53
+ Timecop.freeze(Time.utc(2014,1,1)) do
54
+ command.run
55
+ end
56
+ end
57
+
58
+ it do
59
+ expect(File.read('/ssh_config')).to eq(<<-END)
60
+ # before lines...
61
+
62
+
63
+ # after lines...
64
+ END
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+ require 'ec2ssh/command/update'
3
+ require 'ec2ssh/exceptions'
4
+
5
+ describe Ec2ssh::Command::Update do
6
+ describe '#run' do
7
+ let(:command) do
8
+ described_class.new(cli).tap do |cmd|
9
+ allow(cmd).to receive(:options).and_return(options)
10
+ allow(cmd.builder.ec2s).to receive(:instances) { instances }
11
+ end
12
+ end
13
+ let(:options) do
14
+ double(:options, path: '/ssh_config', dotfile: '/dotfile', aws_key: 'default')
15
+ end
16
+ let(:cli) do
17
+ double(:cli, options: options, red: nil, yellow: nil, green: nil)
18
+ end
19
+ let(:instances) do
20
+ [
21
+ double('instance', tags: {'Name' => 'srv1'}, private_ip_address: '10.0.0.1'),
22
+ double('instance', tags: {'Name' => 'srv2'}, private_ip_address: '10.0.0.2')
23
+ ]
24
+ end
25
+
26
+ before do
27
+ File.open('/ssh_config', 'w') {|f| f.write ssh_config_str }
28
+ File.open('/dotfile', 'w') {|f| f.write dotfile_str }
29
+ end
30
+
31
+ context 'with unmarked ssh_config' do
32
+ let(:ssh_config_str) { '' }
33
+ let(:dotfile_str) { <<-END }
34
+ path '/dotfile'
35
+ aws_keys(
36
+ default: { access_key_id: 'ACCESS_KEY1', secret_access_key: 'SECRET1' }
37
+ )
38
+ host_line <<EOS
39
+ Host <%= tags['Name'] %>
40
+ HostName <%= private_ip_address %>
41
+ EOS
42
+ END
43
+
44
+ it do
45
+ expect { command.run }.to raise_error(Ec2ssh::MarkNotFound)
46
+ end
47
+ end
48
+
49
+ context 'with marked ssh_config' do
50
+ let(:ssh_config_str) { <<-END }
51
+ # before lines...
52
+
53
+ ### EC2SSH BEGIN ###
54
+ ### EC2SSH END ###
55
+
56
+ # after lines...
57
+ END
58
+
59
+ let(:dotfile_str) { <<-END }
60
+ path '/dotfile'
61
+ aws_keys(
62
+ default: { access_key_id: 'ACCESS_KEY1', secret_access_key: 'SECRET1' }
63
+ )
64
+ host_line <<EOS
65
+ Host <%= tags['Name'] %>
66
+ HostName <%= private_ip_address %>
67
+ EOS
68
+ END
69
+
70
+ before do
71
+ Timecop.freeze(Time.utc(2014,1,1)) do
72
+ command.run
73
+ end
74
+ end
75
+
76
+ it do
77
+ expect(File.read('/ssh_config')).to eq(<<-END)
78
+ # before lines...
79
+
80
+ ### EC2SSH BEGIN ###
81
+ # Generated by ec2ssh http://github.com/mirakui/ec2ssh
82
+ # DO NOT edit this block!
83
+ # Updated 2014-01-01T00:00:00Z
84
+ # section: default
85
+ Host srv1
86
+ HostName 10.0.0.1
87
+ Host srv2
88
+ HostName 10.0.0.2
89
+
90
+ ### EC2SSH END ###
91
+
92
+ # after lines...
93
+ END
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ require 'ec2ssh/dsl'
3
+
4
+ describe Ec2ssh::Dsl do
5
+ shared_examples 'a filled dsl container' do
6
+ end
7
+
8
+ let(:dsl_str) do
9
+ <<-END
10
+ aws_keys(
11
+ key1: { access_key_id: 'ACCESS_KEY1', secret_access_key: 'SECRET1' },
12
+ key2: { access_key_id: 'ACCESS_KEY2', secret_access_key: 'SECRET2' }
13
+ )
14
+ regions 'ap-northeast-1', 'us-east-1'
15
+ host_line 'host lines'
16
+ reject {|instance| instance }
17
+ path 'path'
18
+ END
19
+ end
20
+
21
+ subject(:result) { Ec2ssh::Dsl::Parser.parse dsl_str }
22
+
23
+ its(:aws_keys) do
24
+ should == {
25
+ key1: { access_key_id: 'ACCESS_KEY1', secret_access_key: 'SECRET1' },
26
+ key2: { access_key_id: 'ACCESS_KEY2', secret_access_key: 'SECRET2' }
27
+ }
28
+ end
29
+ its(:regions) { should == ['ap-northeast-1', 'us-east-1'] }
30
+ its(:host_line) { should == 'host lines' }
31
+ it { expect(result.reject.call(123)).to eq(123) }
32
+ its(:path) { should == 'path' }
33
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+ require 'ec2ssh/migrator'
3
+
4
+ describe Ec2ssh::Migrator do
5
+ subject(:migrator) { described_class.new '/dotfile' }
6
+
7
+ before do
8
+ File.open('/dotfile', 'w') {|f| f.write dotfile_str }
9
+ end
10
+
11
+ context 'from version 2' do
12
+ let(:dotfile_str) { <<-END }
13
+ ---
14
+ path: /path/to/ssh/config
15
+ aws_keys:
16
+ key1:
17
+ access_key_id: ACCESS_KEY1
18
+ secret_access_key: SECRET1
19
+ key2:
20
+ access_key_id: ACCESS_KEY2
21
+ secret_access_key: SECRET2
22
+ regions:
23
+ - ap-northeast-1
24
+ - us-east-1
25
+ END
26
+
27
+ let(:new_dotfile_str) { <<-END }
28
+ path '/path/to/ssh/config'
29
+ aws_keys(
30
+ key1: { access_key_id: 'ACCESS_KEY1', secret_access_key: 'SECRET1' },
31
+ key2: { access_key_id: 'ACCESS_KEY2', secret_access_key: 'SECRET2' }
32
+ )
33
+ regions 'ap-northeast-1', 'us-east-1'
34
+
35
+ # You can use methods of AWS::EC2::Instance.
36
+ # See http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/EC2/Instance.html
37
+ host_line <<EOS
38
+ Host <%= tags['Name'] %>.<%= availability_zone %>
39
+ HostName <%= dns_name || private_ip_address %>
40
+ EOS
41
+
42
+ # ---
43
+ # path: /path/to/ssh/config
44
+ # aws_keys:
45
+ # key1:
46
+ # access_key_id: ACCESS_KEY1
47
+ # secret_access_key: SECRET1
48
+ # key2:
49
+ # access_key_id: ACCESS_KEY2
50
+ # secret_access_key: SECRET2
51
+ # regions:
52
+ # - ap-northeast-1
53
+ # - us-east-1
54
+ END
55
+
56
+ it { expect(migrator.check_version).to eq('2') }
57
+ it { expect(migrator.migrate_from_2).to eq(new_dotfile_str) }
58
+ end
59
+ end
@@ -2,13 +2,45 @@ require 'spec_helper'
2
2
  require 'ec2ssh/ssh_config'
3
3
 
4
4
  describe Ec2ssh::SshConfig do
5
+ let(:path) { Pathname('/ssh_config') }
6
+
7
+ subject(:ssh_config) do
8
+ described_class.new(path).tap(&:parse!)
9
+ end
10
+
11
+ before do
12
+ File.open(path, 'w') {|f| f.write config_str }
13
+ end
14
+
15
+ context 'expect be false' do
16
+ let(:config_str) { <<-END }
17
+ Host host1
18
+ HostName 0.0.0.0
19
+ END
20
+
21
+ it { expect(ssh_config.mark_exist?).to be_falsey }
22
+ end
23
+
24
+ describe '#mark_exist?' do
25
+ context 'expect be true' do
26
+ let(:config_str) { <<-END }
27
+ ### EC2SSH BEGIN ###
28
+ # Generated by ec2ssh http://github.com/mirakui/ec2ssh
29
+ # DO NOT edit this block!
30
+ # Updated 2013-01-01T00:00:00+00:00
31
+ Host db-01.ap-northeast-1
32
+ HostName ec2-1-1-1-1.ap-northeast-1.ec2.amazonaws.com
33
+
34
+ ### EC2SSH END ###
35
+ END
36
+
37
+ it { expect(ssh_config.mark_exist?).to be_truthy }
38
+ end
39
+ end
40
+
5
41
  describe '#parse!' do
6
42
  context 'when no section exists' do
7
- let(:path) { path = tmp_dir.join('ssh_config') }
8
- subject { described_class.new(path) }
9
-
10
- before do
11
- path.open('w') {|f| f.write <<-END }
43
+ let(:config_str) { ; <<-END }
12
44
  ### EC2SSH BEGIN ###
13
45
  # Generated by ec2ssh http://github.com/mirakui/ec2ssh
14
46
  # DO NOT edit this block!
@@ -18,20 +50,13 @@ Host db-01.ap-northeast-1
18
50
 
19
51
  ### EC2SSH END ###
20
52
  END
21
- subject.parse!
22
- end
23
53
 
24
- it { expect(subject.sections.size).to be == 1 }
25
- it { expect(subject.sections['default']).to be_an_instance_of Ec2ssh::SshConfig::Section }
54
+ it { expect(ssh_config.sections.size).to be == 1 }
55
+ it { expect(ssh_config.sections['default']).to be_an_instance_of Ec2ssh::SshConfig::Section }
26
56
  end
27
57
 
28
58
  context 'when a section exists' do
29
- let(:path) { path = tmp_dir.join('ssh_config') }
30
- subject { described_class.new(path) }
31
-
32
- before do
33
- path = tmp_dir.join('ssh_config')
34
- path.open('w') {|f| f.write <<-END }
59
+ let(:config_str) { <<-END }
35
60
  Host foo.bar.com
36
61
  HostName 1.2.3.4
37
62
  ### EC2SSH BEGIN ###
@@ -44,20 +69,13 @@ Host db-01.ap-northeast-1
44
69
 
45
70
  ### EC2SSH END ###
46
71
  END
47
- subject.parse!
48
- end
49
72
 
50
73
  it { expect(subject.sections.size).to be == 2 }
51
74
  it { expect(subject.sections['foo']).to be_an_instance_of Ec2ssh::SshConfig::Section }
52
75
  end
53
76
 
54
77
  context 'when multiple sections exist' do
55
- let(:path) { path = tmp_dir.join('ssh_config') }
56
- subject { described_class.new(path) }
57
-
58
- before do
59
- path = tmp_dir.join('ssh_config')
60
- path.open('w') {|f| f.write <<-END }
78
+ let(:config_str) { <<-END }
61
79
  Host foo.bar.com
62
80
  HostName 1.2.3.4
63
81
  ### EC2SSH BEGIN ###
@@ -73,53 +91,51 @@ Host db-02.ap-northeast-1
73
91
 
74
92
  ### EC2SSH END ###
75
93
  END
76
- subject.parse!
77
- end
78
94
 
79
- it { expect(subject.sections.size).to be == 3 }
80
- it { expect(subject.sections['foo']).to be_an_instance_of Ec2ssh::SshConfig::Section }
81
- it { expect(subject.sections['bar']).to be_an_instance_of Ec2ssh::SshConfig::Section }
95
+ it { expect(ssh_config.sections.size).to be == 3 }
96
+ it { expect(ssh_config.sections['foo']).to be_an_instance_of Ec2ssh::SshConfig::Section }
97
+ it { expect(ssh_config.sections['bar']).to be_an_instance_of Ec2ssh::SshConfig::Section }
82
98
  end
83
99
  end
100
+ end
84
101
 
85
- describe Ec2ssh::SshConfig::Section do
86
- describe '#append' do
87
- let(:section) { Ec2ssh::SshConfig::Section.new('test') }
88
- before { section.append('foo') }
102
+ describe Ec2ssh::SshConfig::Section do
103
+ describe '#append' do
104
+ let(:section) { Ec2ssh::SshConfig::Section.new('test') }
105
+ before { section.append('foo') }
89
106
 
90
- it { expect(section.text).to eq 'foo' }
91
- end
107
+ it { expect(section.text).to eq 'foo' }
108
+ end
92
109
 
93
- describe '#replace' do
94
- let(:section) { Ec2ssh::SshConfig::Section.new('test', 'foo') }
95
- before { section.replace!('bar') }
110
+ describe '#replace' do
111
+ let(:section) { Ec2ssh::SshConfig::Section.new('test', 'foo') }
112
+ before { section.replace!('bar') }
96
113
 
97
- it { expect(section.text).to eq 'bar' }
98
- end
114
+ it { expect(section.text).to eq 'bar' }
115
+ end
99
116
 
100
- describe '#to_s' do
101
- context 'when no text given' do
102
- let(:section) { Ec2ssh::SshConfig::Section.new('test') }
117
+ describe '#to_s' do
118
+ context 'when no text given' do
119
+ let(:section) { Ec2ssh::SshConfig::Section.new('test') }
103
120
 
104
- it { expect(section.to_s).to eq '' }
105
- end
121
+ it { expect(section.to_s).to eq '' }
122
+ end
106
123
 
107
- context 'when empty text given' do
108
- let(:section) { Ec2ssh::SshConfig::Section.new('test', '') }
124
+ context 'when empty text given' do
125
+ let(:section) { Ec2ssh::SshConfig::Section.new('test', '') }
109
126
 
110
- it { expect(section.to_s).to eq '' }
111
- end
127
+ it { expect(section.to_s).to eq '' }
128
+ end
112
129
 
113
- context 'when some text given' do
114
- let(:section) { Ec2ssh::SshConfig::Section.new('test', 'foo') }
130
+ context 'when some text given' do
131
+ let(:section) { Ec2ssh::SshConfig::Section.new('test', 'foo') }
115
132
 
116
- it {
117
- expect(section.to_s).to eq <<EOS
133
+ it {
134
+ expect(section.to_s).to eq <<EOS
118
135
  # section: test
119
136
  foo
120
137
  EOS
121
- }
122
- end
138
+ }
123
139
  end
124
140
  end
125
141
  end