1 | var Session = require('../lib/session');
|
2 | var helpers = require('../lib/helpers');
|
3 | var assert = require('assert');
|
4 | var fs = require('fs');
|
5 |
|
6 | suite('Session', function() {
|
7 | suite('.copy()', function() {
|
8 | test('with password', function(done) {
|
9 | var session = new Session('host', {username: 'root', password: 'kuma'});
|
10 | session._doSpawn = function(command, callback) {
|
11 | assert.equal(command, 'sshpass -p kuma scp ./src root@host:~/dest');
|
12 | callback();
|
13 | };
|
14 | session.copy('./src', '~/dest', done);
|
15 | });
|
16 |
|
17 | test('with pem', function(done) {
|
18 | var session = new Session('host', {username: 'root', pem: 'pem-content'});
|
19 | var pemFile;
|
20 | session._doSpawn = function(command, callback) {
|
21 | var matched = command.match(/scp -i ([\w\/]*) .\/src root@host:~\/dest/);
|
22 | assert.ok(matched);
|
23 | pemFile = matched[1];
|
24 | var pemFileContent = fs.readFileSync(pemFile, 'utf8');
|
25 | assert.equal(pemFileContent, 'pem-content');
|
26 | callback();
|
27 | };
|
28 | session.copy('./src', '~/dest', function() {
|
29 | assert.equal(fs.existsSync(pemFile), false);
|
30 | done();
|
31 | });
|
32 | });
|
33 |
|
34 | test('no pem or password', function(done) {
|
35 | var session = new Session('host', {username: 'root'});
|
36 | assert.throws(function() {
|
37 | session.copy('./src', '~/dest', done);
|
38 | });
|
39 | done();
|
40 | });
|
41 |
|
42 | test('with vars', function(done) {
|
43 |
|
44 | var tmpFile = '/tmp/' + helpers.randomId();
|
45 | fs.writeFileSync(tmpFile, 'name: <%=name %>');
|
46 |
|
47 | var session = new Session('host', {username: 'root', password: 'kuma'});
|
48 | session._doSpawn = function(command, callback) {
|
49 | var matched = command.match(/sshpass -p kuma scp ([\w\/]*) root@host:~\/dest/);
|
50 | assert.ok(matched);
|
51 |
|
52 | var compiledFile = matched[1];
|
53 | assert.ok(compiledFile);
|
54 | var compiledContent = fs.readFileSync(compiledFile, {encoding: 'utf8'});
|
55 | assert.equal(compiledContent, 'name: arunoda');
|
56 |
|
57 | callback();
|
58 | };
|
59 | session.copy(tmpFile, '~/dest', {name: 'arunoda'}, done);
|
60 | });
|
61 |
|
62 | test('with ssh options', function(done) {
|
63 |
|
64 | var tmpFile = '/tmp/' + helpers.randomId();
|
65 | fs.writeFileSync(tmpFile, 'name: <%=name %>');
|
66 |
|
67 | var session = new Session('host', {username: 'root', password: 'kuma'}, { ssh: { foo: 'bar' }});
|
68 | session._doSpawn = function(command, callback) {
|
69 | fs.unlinkSync(tmpFile);
|
70 | var matched = command.match(/sshpass -p kuma scp -o foo=bar ([\w\/]*) root@host:~\/dest/);
|
71 | assert.ok(matched);
|
72 | callback();
|
73 | };
|
74 | session.copy(tmpFile, '~/dest', {name: 'arunoda'}, done);
|
75 | });
|
76 | });
|
77 |
|
78 | suite('.execute()', function() {
|
79 | test('with password', function(done) {
|
80 | var session = new Session('host', {username: 'root', password: 'kuma'});
|
81 | session._doSpawn = function(command, callback) {
|
82 | var matched = command.match(/sshpass -p kuma ssh root@host "bash -s" < (.*)/);
|
83 | var scriptLocation = matched[1];
|
84 | assert.ok(matched);
|
85 | assert.ok(command.indexOf(scriptLocation) > 0);
|
86 | var fileContent = fs.readFileSync(scriptLocation, {encoding: 'utf8'});
|
87 | assert.equal(fileContent, 'ls /');
|
88 | callback();
|
89 | };
|
90 | session.execute('ls /', done);
|
91 | });
|
92 |
|
93 | test('with pem', function(done) {
|
94 | var session = new Session('host', {username: 'root', pem: 'the-pem-content'});
|
95 | var pemFile;
|
96 | var scriptLocation;
|
97 | session._doSpawn = function(command, callback) {
|
98 | var matched = command.match(/ssh -i ([\w\/]*) root@host "bash -s" < (.*)/);
|
99 | assert.ok(matched);
|
100 |
|
101 | pemFile = matched[1];
|
102 | scriptLocation = matched[2];
|
103 |
|
104 | var fileContent = fs.readFileSync(scriptLocation, {encoding: 'utf8'});
|
105 | assert.equal(fileContent, 'ls /');
|
106 |
|
107 | var pemFileContent = fs.readFileSync(pemFile, 'utf8');
|
108 | assert.equal(pemFileContent, 'the-pem-content');
|
109 |
|
110 | callback();
|
111 | };
|
112 | session.execute('ls /', function() {
|
113 | assert.equal(fs.existsSync(pemFile), false);
|
114 | assert.equal(fs.existsSync(scriptLocation), false);
|
115 | done();
|
116 | });
|
117 | });
|
118 |
|
119 | test('no password or pem', function(done) {
|
120 | var session = new Session('host', {username: 'root'});
|
121 | assert.throws(function() {
|
122 | session.execute('ls /', done);
|
123 | });
|
124 | done();
|
125 | });
|
126 |
|
127 | test('with sshOptions', function(done) {
|
128 | var session = new Session('host', {username: 'root', password: 'kuma'}, { ssh: { foo: 'bar' }});
|
129 | session._doSpawn = function(command, callback) {
|
130 | var matched = command.match(/sshpass -p kuma ssh -o foo=bar root@host "bash -s" < (.*)/);
|
131 | assert.ok(matched);
|
132 | callback();
|
133 | };
|
134 | session.execute('ls /', done);
|
135 | });
|
136 | });
|
137 |
|
138 | suite('.executeScript', function() {
|
139 | test('file exists', function(done) {
|
140 | var session = new Session('host', {username: 'root', password: 'kuma'});
|
141 | session.execute = function(shellCommand, callback) {
|
142 | assert.equal(shellCommand, 'ls -all /');
|
143 | callback();
|
144 | };
|
145 | var file = '/tmp/' + Math.ceil(Math.random() * 9999999);
|
146 | fs.writeFileSync(file, 'ls -all /');
|
147 | session.executeScript(file, function() {
|
148 | fs.unlinkSync(file);
|
149 | done();
|
150 | });
|
151 | });
|
152 |
|
153 | test('file not exists', function(done) {
|
154 | var session = new Session('host', {username: 'root', password: 'kuma'});
|
155 | session.execute = function(shellCommand, callback) {
|
156 | assert.equal(shellCommand, 'ls -all /');
|
157 | callback();
|
158 | };
|
159 |
|
160 | session.executeScript('/tmp/ssdcs', function(err) {
|
161 | assert.ok(err);
|
162 | done();
|
163 | });
|
164 | });
|
165 |
|
166 | test('with ejs', function(done) {
|
167 | var session = new Session('host', {username: 'root', password: 'kuma'});
|
168 | session.execute = function(shellCommand, callback) {
|
169 | assert.equal(shellCommand, 'ls -all /');
|
170 | callback();
|
171 | };
|
172 | var file = '/tmp/' + Math.ceil(Math.random() * 9999999);
|
173 | fs.writeFileSync(file, 'ls <%= options %> /');
|
174 | session.executeScript(file, {options: '-all'}, function() {
|
175 | fs.unlinkSync(file);
|
176 | done();
|
177 | });
|
178 | });
|
179 |
|
180 | test('with ejs options', function(done) {
|
181 | var session = new Session('host', {username: 'root', password: 'kuma'}, {ejs: {
|
182 | open: '{{',
|
183 | close: '}}'
|
184 | }});
|
185 | session.execute = function(shellCommand, callback) {
|
186 | assert.equal(shellCommand, 'ls -all /');
|
187 | callback();
|
188 | };
|
189 | var file = '/tmp/' + Math.ceil(Math.random() * 9999999);
|
190 | fs.writeFileSync(file, 'ls {{= options }} /');
|
191 | session.executeScript(file, {options: '-all'}, function() {
|
192 | fs.unlinkSync(file);
|
193 | done();
|
194 | });
|
195 | });
|
196 | });
|
197 | }); |
\ | No newline at end of file |