You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
233 lines
5.0 KiB
233 lines
5.0 KiB
2 years ago
|
import 'dart:async';
|
||
|
import 'dart:convert';
|
||
|
import 'dart:io';
|
||
|
import 'dart:math';
|
||
|
|
||
|
import 'package:nextcloud/nextcloud.dart';
|
||
|
import 'package:process_run/cmd_run.dart';
|
||
|
import 'package:test/test.dart';
|
||
|
|
||
2 years ago
|
class DockerContainer {
|
||
|
DockerContainer({
|
||
|
required this.id,
|
||
|
required this.port,
|
||
|
});
|
||
|
|
||
|
final String id;
|
||
|
|
||
|
final int port;
|
||
2 years ago
|
|
||
|
Future runOccCommand(final List<String> args) async {
|
||
|
final result = await runExecutableArguments(
|
||
|
'docker',
|
||
|
[
|
||
|
'exec',
|
||
2 years ago
|
id,
|
||
2 years ago
|
'php',
|
||
|
'-f',
|
||
|
'occ',
|
||
|
...args,
|
||
|
],
|
||
|
stdout: stdout,
|
||
|
stderr: stderr,
|
||
|
);
|
||
|
if (result.exitCode != 0) {
|
||
|
throw Exception('Failed to run occ command');
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
void destroy() => unawaited(
|
||
|
runExecutableArguments(
|
||
|
'docker',
|
||
|
[
|
||
|
'kill',
|
||
|
id,
|
||
|
],
|
||
|
),
|
||
2 years ago
|
);
|
||
|
|
||
|
Future<String> collectLogs() async {
|
||
|
final apacheLogs = (await runExecutableArguments(
|
||
|
'docker',
|
||
|
[
|
||
|
'logs',
|
||
2 years ago
|
id,
|
||
2 years ago
|
],
|
||
|
stdoutEncoding: utf8,
|
||
|
))
|
||
|
.stdout as String;
|
||
|
final nextcloudLogs = (await runExecutableArguments(
|
||
|
'docker',
|
||
|
[
|
||
|
'exec',
|
||
2 years ago
|
id,
|
||
2 years ago
|
'cat',
|
||
|
'data/nextcloud.log',
|
||
|
],
|
||
|
stdoutEncoding: utf8,
|
||
|
))
|
||
|
.stdout as String;
|
||
|
|
||
|
return '$apacheLogs\n\n$nextcloudLogs';
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
class TestNextcloudClient extends NextcloudClient {
|
||
|
TestNextcloudClient(
|
||
|
super.baseURL, {
|
||
2 years ago
|
super.loginName,
|
||
2 years ago
|
super.username,
|
||
|
super.password,
|
||
|
super.language,
|
||
|
super.appType,
|
||
|
super.userAgentOverride,
|
||
2 years ago
|
super.cookieJar,
|
||
2 years ago
|
});
|
||
|
}
|
||
|
|
||
|
Future<TestNextcloudClient> getTestClient(
|
||
|
final DockerContainer container, {
|
||
2 years ago
|
final String? username = 'user1',
|
||
|
final String? password = 'user1',
|
||
2 years ago
|
final bool useAppPassword = false,
|
||
|
final AppType appType = AppType.unknown,
|
||
|
final String? userAgentOverride,
|
||
|
}) async {
|
||
|
// ignore: prefer_asserts_with_message
|
||
|
assert(!useAppPassword || (username != null && password != null));
|
||
2 years ago
|
|
||
2 years ago
|
var clientPassword = password;
|
||
|
if (useAppPassword) {
|
||
2 years ago
|
final inputStream = StreamController<List<int>>();
|
||
|
final process = runExecutableArguments(
|
||
|
'docker',
|
||
|
[
|
||
2 years ago
|
'exec',
|
||
|
'-i',
|
||
|
container.id,
|
||
|
'php',
|
||
2 years ago
|
'-f',
|
||
2 years ago
|
'occ',
|
||
|
'user:add-app-password',
|
||
|
username!,
|
||
2 years ago
|
],
|
||
|
stdin: inputStream.stream,
|
||
|
);
|
||
2 years ago
|
inputStream.add(utf8.encode(password!));
|
||
2 years ago
|
await inputStream.close();
|
||
|
|
||
|
final result = await process;
|
||
|
if (result.exitCode != 0) {
|
||
2 years ago
|
throw Exception('Failed to run generate app password command\n${result.stderr}\n${result.stdout}');
|
||
2 years ago
|
}
|
||
2 years ago
|
clientPassword = (result.stdout as String).split('\n')[1];
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
final client = TestNextcloudClient(
|
||
|
'http://localhost:${container.port}',
|
||
2 years ago
|
loginName: username,
|
||
2 years ago
|
username: username,
|
||
|
password: clientPassword,
|
||
|
appType: appType,
|
||
|
userAgentOverride: userAgentOverride,
|
||
2 years ago
|
cookieJar: CookieJar(),
|
||
2 years ago
|
);
|
||
2 years ago
|
|
||
2 years ago
|
while (true) {
|
||
|
// Test will timeout after 30s
|
||
|
try {
|
||
|
await client.core.getStatus();
|
||
|
break;
|
||
|
} catch (_) {
|
||
|
await Future.delayed(const Duration(milliseconds: 100));
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return client;
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
Future<DockerContainer> getDockerContainer(final DockerImage image) async {
|
||
|
late ProcessResult result;
|
||
|
late int port;
|
||
|
while (true) {
|
||
|
port = randomPort();
|
||
|
result = await runExecutableArguments(
|
||
|
'docker',
|
||
|
[
|
||
|
'run',
|
||
|
'--rm',
|
||
|
'-d',
|
||
|
'-p',
|
||
|
'$port:80',
|
||
|
'--add-host',
|
||
|
'host.docker.internal:host-gateway',
|
||
2 years ago
|
image,
|
||
2 years ago
|
],
|
||
|
);
|
||
|
// 125 means the docker run command itself has failed which indicated the port is already used
|
||
|
if (result.exitCode != 125) {
|
||
|
break;
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
if (result.exitCode != 0) {
|
||
|
throw Exception('Failed to run docker container: ${result.stderr}');
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return DockerContainer(
|
||
|
id: result.stdout.toString().replaceAll('\n', ''),
|
||
|
port: port,
|
||
|
);
|
||
|
}
|
||
|
|
||
2 years ago
|
typedef DockerImage = String;
|
||
2 years ago
|
|
||
2 years ago
|
Future<DockerImage> getDockerImage() async {
|
||
|
const dockerImageName = 'nextcloud-neon-dev';
|
||
2 years ago
|
|
||
2 years ago
|
final inputStream = StreamController<List<int>>();
|
||
|
final process = runExecutableArguments(
|
||
|
'docker',
|
||
|
[
|
||
|
'build',
|
||
|
'-t',
|
||
|
dockerImageName,
|
||
|
'-f',
|
||
|
'-',
|
||
2 years ago
|
'../../tool',
|
||
2 years ago
|
],
|
||
|
stdout: stdout,
|
||
|
stderr: stderr,
|
||
|
stdin: inputStream.stream,
|
||
|
);
|
||
2 years ago
|
inputStream.add(utf8.encode(File('../../tool/Dockerfile.dev').readAsStringSync()));
|
||
2 years ago
|
await inputStream.close();
|
||
|
|
||
|
final result = await process;
|
||
|
if (result.exitCode != 0) {
|
||
|
throw Exception('Failed to build docker image');
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return dockerImageName;
|
||
2 years ago
|
}
|
||
|
|
||
|
class TestNextcloudUser {
|
||
|
TestNextcloudUser(
|
||
2 years ago
|
this.username,
|
||
|
this.password, {
|
||
2 years ago
|
this.displayName,
|
||
|
});
|
||
|
|
||
|
final String username;
|
||
2 years ago
|
final String password;
|
||
2 years ago
|
final String? displayName;
|
||
|
}
|
||
|
|
||
2 years ago
|
int randomPort() => 1024 + Random().nextInt(65535 - 1024);
|
||
|
|
||
|
void expectDateInReasonableTimeRange(final DateTime actual, final DateTime expected) {
|
||
2 years ago
|
const duration = Duration(seconds: 10);
|
||
2 years ago
|
expect(actual.isAfter(expected.subtract(duration)), isTrue);
|
||
|
expect(actual.isBefore(expected.add(duration)), isTrue);
|
||
|
}
|