/
scp_spec.rb
129 lines (102 loc) · 3.63 KB
/
scp_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# encoding: utf-8
require File.expand_path('../../spec_helper.rb', __FILE__)
describe Backup::Storage::SCP do
let(:scp) do
Backup::Storage::SCP.new do |scp|
scp.username = 'my_username'
scp.password = 'my_password'
scp.ip = '123.45.678.90'
scp.port = 22
scp.path = '~/backups/'
scp.keep = 20
end
end
before do
Backup::Configuration::Storage::SCP.clear_defaults!
end
it 'should have defined the configuration properly' do
scp.username.should == 'my_username'
scp.password.should == 'my_password'
scp.ip.should == '123.45.678.90'
scp.port.should == 22
scp.path.should == 'backups/'
scp.keep.should == 20
end
it 'should use the defaults if a particular attribute has not been defined' do
Backup::Configuration::Storage::SCP.defaults do |scp|
scp.username = 'my_default_username'
scp.password = 'my_default_password'
scp.path = '~/backups'
end
scp = Backup::Storage::SCP.new do |scp|
scp.password = 'my_password'
scp.ip = '123.45.678.90'
end
scp.username.should == 'my_default_username'
scp.password.should == 'my_password'
scp.ip.should == '123.45.678.90'
scp.port.should == 22
end
it 'should have its own defaults' do
scp = Backup::Storage::SCP.new
scp.port.should == 22
scp.path.should == 'backups'
end
describe '#connection' do
it 'should establish a connection to the remote server using the provided ip address and credentials' do
Net::SSH.expects(:start).with('123.45.678.90', 'my_username', :password => 'my_password', :port => 22)
scp.send(:connection)
end
end
describe '#transfer!' do
let(:connection) { mock('Net::SCP') }
before do
Net::SSH.stubs(:start).returns(connection)
scp.stubs(:create_remote_directories!)
end
it 'should transfer the provided file to the path' do
Backup::Model.new('blah', 'blah') {}
file = mock("Backup::Storage::SCP::File")
scp.expects(:create_remote_directories!)
ssh_scp = mock('Net::SSH::SCP')
connection.expects(:scp).returns(ssh_scp)
ssh_scp.expects(:upload!).with(
File.join(Backup::TMP_PATH, "#{ Backup::TIME }.#{ Backup::TRIGGER }.tar"),
File.join('backups/myapp', Backup::TIME, "#{ Backup::TRIGGER }.tar")
)
scp.send(:transfer!)
end
end
describe '#remove!' do
let(:connection) { mock('Net::SCP') }
before do
Net::SSH.stubs(:start).returns(connection)
end
it 'should remove the remote path from the remote server' do
connection.expects(:exec!).with("rm -rf 'backups/myapp/#{ Backup::TIME }'")
scp.send(:remove!)
end
end
describe '#create_remote_directories!' do
let(:connection) { mock('Net::SSH') }
before do
Net::SSH.stubs(:start).returns(connection)
end
it 'should properly create remote directories one by one' do
scp.path = 'backups/some_other_folder/another_folder'
connection.expects(:exec!).with("mkdir 'backups'")
connection.expects(:exec!).with("mkdir 'backups/some_other_folder'")
connection.expects(:exec!).with("mkdir 'backups/some_other_folder/another_folder'")
connection.expects(:exec!).with("mkdir 'backups/some_other_folder/another_folder/myapp'")
connection.expects(:exec!).with("mkdir 'backups/some_other_folder/another_folder/myapp/#{ Backup::TIME }'")
scp.send(:create_remote_directories!)
end
end
describe '#perform' do
it 'should invoke transfer! and cycle!' do
scp.expects(:transfer!)
scp.expects(:cycle!)
scp.perform!
end
end
end