-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathssh_client_test.dart
More file actions
140 lines (115 loc) · 4.09 KB
/
ssh_client_test.dart
File metadata and controls
140 lines (115 loc) · 4.09 KB
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
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2019 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:fuchsia_ctl/fuchsia_ctl.dart';
import 'package:fuchsia_ctl/src/ssh_client.dart';
import 'package:fuchsia_ctl/src/operation_result.dart';
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';
import 'package:test/test.dart';
import 'fakes.dart';
void main() {
const String targetIp = '127.0.0.2';
const String identityFilePath = '.ssh/pkey';
test('interactive', () async {
final MockProcessManager processManager = MockProcessManager();
when(processManager.start(any)).thenAnswer((_) async {
return FakeProcess(0, <String>[''], <String>['']);
});
final SshClient ssh = SshClient(processManager: processManager);
final OperationResult result = await ssh.interactive(
targetIp,
identityFilePath: identityFilePath,
);
final List<String> capturedStartArgs =
verify(processManager.start(captureAny))
.captured
.cast<List<String>>()
.single;
expect(
capturedStartArgs,
ssh.getSshArguments(
identityFilePath: identityFilePath,
targetIp: targetIp,
));
expect(result.success, true);
});
test('command', () async {
const List<String> command = <String>['ls', '-al'];
final MockProcessManager processManager = MockProcessManager();
when(processManager.start(any)).thenAnswer((_) async {
return FakeProcess(0, <String>['abc'], <String>['cdf']);
});
final SshClient ssh = SshClient(processManager: processManager);
final OperationResult result = await ssh.runCommand(
targetIp,
identityFilePath: identityFilePath,
command: command,
);
final List<String> capturedStartArgs =
verify(processManager.start(captureAny))
.captured
.cast<List<String>>()
.single;
expect(
capturedStartArgs,
ssh.getSshArguments(
identityFilePath: identityFilePath,
targetIp: targetIp,
command: command,
));
expect(result.info, 'abc\ncdf\n');
expect(result.success, true);
});
test('Command output is written to a log file', () async {
const List<String> command = <String>['ls', '-al'];
final MockProcessManager processManager = MockProcessManager();
when(processManager.start(any)).thenAnswer((_) async {
return FakeProcess(0, <String>['ef'], <String>['abc']);
});
final SshClient ssh = SshClient(processManager: processManager);
final FileSystem fs = MemoryFileSystem();
final OperationResult result = await ssh.runCommand(
targetIp,
identityFilePath: identityFilePath,
command: command,
fs: fs,
logFilePath: 'myfile.txt',
);
final String content = await fs.file('myfile.txt').readAsString();
expect(content, contains('WARN abc'));
expect(content, contains('INFO ef'));
expect(result.success, true);
});
test('getSshArgs', () {
const SshClient ssh = SshClient();
final List<String> args = ssh.getSshArguments(
identityFilePath: identityFilePath,
targetIp: targetIp,
command: const <String>['ls', '-al'],
);
expect(args.last, 'ls -al');
});
test('sshCommand times out', () async {
final MockProcessManager processManager = MockProcessManager();
when(processManager.start(any)).thenAnswer((_) async {
await Future<void>.delayed(const Duration(milliseconds: 3));
return FakeProcess(0, <String>[''], <String>['']);
});
final SshClient ssh = SshClient(processManager: processManager);
try {
await ssh.runCommand(
targetIp,
identityFilePath: identityFilePath,
command: const <String>['ls', '-al'],
timeoutMs: const Duration(milliseconds: 1),
);
} catch (e) {
expect(e, isA<TimeoutException>());
}
});
}
class MockProcessManager extends Mock implements ProcessManager {}