Skip to content

Commit

Permalink
Merge pull request #1067 from nextcloud/refactor/dev-container
Browse files Browse the repository at this point in the history
  • Loading branch information
provokateurin authored Nov 1, 2023
2 parents a7a860b + eefcdf8 commit 02a1ec6
Show file tree
Hide file tree
Showing 20 changed files with 81 additions and 156 deletions.
3 changes: 1 addition & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ You can read the full documentation at https://www.conventionalcommits.org.
## Tools
We maintain a collection of scripts in `./tool/`.
They range from setting up a local Nextcloud server (`./tool/dev.sh`) to generating assets.
Be aware that due to limitations in Apache the dev server will crash when you resize the terminal window.


## Monorepo
For easier development we use a monorepo structure.
This means that we have multiple packages in one git repository.
Expand Down
2 changes: 1 addition & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"external",
"packages/file_icons/lib/src/data.dart",
"packages/neon_lints/lib",
"packages/nextcloud/test/files"
"tool/dev/static"
],
"dictionaries": [
"bash",
Expand Down
5 changes: 1 addition & 4 deletions packages/nextcloud/test/core_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ void main() {
group(
'core',
() {
late DockerImage image;
setUpAll(() async => image = await getDockerImage());

late DockerContainer container;
late TestNextcloudClient client;
setUp(() async {
container = await getDockerContainer(image);
container = await getDockerContainer();
client = await getTestClient(container);
});
tearDown(() => container.destroy());
Expand Down
5 changes: 1 addition & 4 deletions packages/nextcloud/test/dashboard_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ void main() {
group(
'dashboard',
() {
late DockerImage image;
setUpAll(() async => image = await getDockerImage());

late DockerContainer container;
late TestNextcloudClient client;
setUp(() async {
container = await getDockerContainer(image);
container = await getDockerContainer();
client = await getTestClient(container);
});
tearDown(() => container.destroy());
Expand Down
56 changes: 19 additions & 37 deletions packages/nextcloud/test/helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,24 @@ Future<TestNextcloudClient> getTestClient(
return client;
}

Future<DockerContainer> getDockerContainer(final DockerImage image, {final bool useApache = false}) async {
late ProcessResult result;
Future<DockerContainer> getDockerContainer() async {
const dockerImageName = 'ghcr.io/nextcloud/neon/dev';

var result = await runExecutableArguments(
'docker',
[
'images',
'-q',
dockerImageName,
],
);
if (result.exitCode != 0) {
throw Exception('Querying docker image failed: ${result.stderr}');
}
if (result.stdout.toString().isEmpty) {
throw Exception('Missing docker image $dockerImageName. Please build it using ./tool/build-dev-container.sh');
}

late int port;
while (true) {
port = randomPort();
Expand All @@ -171,12 +187,7 @@ Future<DockerContainer> getDockerContainer(final DockerImage image, {final bool
'host.docker.internal:host-gateway',
'-p',
'$port:80',
image,
if (!useApache) ...[
'php',
'-S',
'0.0.0.0:80',
],
dockerImageName,
],
);
// 125 means the docker run command itself has failed which indicated the port is already used
Expand All @@ -195,35 +206,6 @@ Future<DockerContainer> getDockerContainer(final DockerImage image, {final bool
);
}

typedef DockerImage = String;

Future<DockerImage> getDockerImage() async {
const dockerImageName = 'nextcloud-neon-dev';

final inputStream = StreamController<List<int>>();
final process = runExecutableArguments(
'docker',
[
'build',
'-t',
dockerImageName,
'-f',
'-',
'../../tool',
],
stdin: inputStream.stream,
);
inputStream.add(utf8.encode(File('../../tool/Dockerfile.dev').readAsStringSync()));
await inputStream.close();

final result = await process;
if (result.exitCode != 0) {
throw Exception('Failed to build docker image');
}

return dockerImageName;
}

class TestNextcloudUser {
TestNextcloudUser(
this.username,
Expand Down
46 changes: 6 additions & 40 deletions packages/nextcloud/test/news_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,29 @@ import 'dart:async';
import 'package:nextcloud/news.dart' as news;
import 'package:nextcloud/nextcloud.dart';
import 'package:test/test.dart';
import 'package:universal_io/io.dart';

import 'helper.dart';

void main() {
group(
'news',
() {
late DockerImage image;
late HttpServer rssServer;
setUpAll(() async {
image = await getDockerImage();
rssServer = await getRssServer();
});
tearDownAll(() async => rssServer.close(force: true));

late DockerContainer container;
late TestNextcloudClient client;
setUp(() async {
container = await getDockerContainer(image);
container = await getDockerContainer();
client = await getTestClient(container);
});
tearDown(() => container.destroy());

Future<DynamiteResponse<news.ListFeeds, void>> addWikipediaFeed([final int? folderID]) async =>
client.news.addFeed(
url: 'http://host.docker.internal:${rssServer.port}/wikipedia.xml',
url: 'http://localhost/static/wikipedia.xml',
folderId: folderID,
);

Future<DynamiteResponse<news.ListFeeds, void>> addNasaFeed() async => client.news.addFeed(
url: 'http://host.docker.internal:${rssServer.port}/nasa.xml',
url: 'http://localhost/static/nasa.xml',
);

test('Is supported', () async {
Expand All @@ -58,7 +49,7 @@ void main() {
expect(response.body.starredCount, null);
expect(response.body.newestItemId, isNotNull);
expect(response.body.feeds, hasLength(1));
expect(response.body.feeds[0].url, 'http://host.docker.internal:${rssServer.port}/wikipedia.xml');
expect(response.body.feeds[0].url, 'http://localhost/static/wikipedia.xml');

response = await client.news.listFeeds();
expect(response.statusCode, 200);
Expand All @@ -67,7 +58,7 @@ void main() {
expect(response.body.starredCount, 0);
expect(response.body.newestItemId, isNotNull);
expect(response.body.feeds, hasLength(1));
expect(response.body.feeds[0].url, 'http://host.docker.internal:${rssServer.port}/wikipedia.xml');
expect(response.body.feeds[0].url, 'http://localhost/static/wikipedia.xml');
});

test('Delete feed', () async {
Expand Down Expand Up @@ -401,7 +392,7 @@ void main() {
expect(response.body.newestItemId, isNotNull);
expect(response.body.feeds, hasLength(1));
expect(response.body.feeds[0].folderId, 1);
expect(response.body.feeds[0].url, 'http://host.docker.internal:${rssServer.port}/wikipedia.xml');
expect(response.body.feeds[0].url, 'http://localhost/static/wikipedia.xml');
});

test('Mark folder as read', () async {
Expand Down Expand Up @@ -430,28 +421,3 @@ void main() {
timeout: timeout,
);
}

Future<HttpServer> getRssServer() async {
final wikipediaRss = File('test/files/wikipedia.xml').readAsStringSync();
final nasaRss = File('test/files/nasa.xml').readAsStringSync();
while (true) {
try {
final port = randomPort();
final server = await HttpServer.bind(InternetAddress.anyIPv6, port);
unawaited(
server.forEach((final request) async {
switch (request.uri.path) {
case '/wikipedia.xml':
request.response.write(wikipediaRss);
case '/nasa.xml':
request.response.write(nasaRss);
default:
request.response.statusCode = HttpStatus.badRequest;
}
await request.response.close();
}),
);
return server;
} catch (_) {}
}
}
5 changes: 1 addition & 4 deletions packages/nextcloud/test/notes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ void main() {
group(
'notes',
() {
late DockerImage image;
setUpAll(() async => image = await getDockerImage());

late DockerContainer container;
late TestNextcloudClient client;
setUp(() async {
container = await getDockerContainer(image);
container = await getDockerContainer();
client = await getTestClient(container);
});
tearDown(() => container.destroy());
Expand Down
10 changes: 2 additions & 8 deletions packages/nextcloud/test/notifications_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import 'helper.dart';

void main() {
group('notifications', () {
late DockerImage image;
setUpAll(() async => image = await getDockerImage());

late DockerContainer container;
late TestNextcloudClient client;
setUp(() async {
container = await getDockerContainer(image);
container = await getDockerContainer();
client = await getTestClient(
container,
username: 'admin',
Expand Down Expand Up @@ -103,13 +100,10 @@ void main() {
});

group('Push', () {
late DockerImage image;
setUpAll(() async => image = await getDockerImage());

late DockerContainer container;
late TestNextcloudClient client;
setUp(() async {
container = await getDockerContainer(image);
container = await getDockerContainer();
client = await getTestClient(
container,
username: 'admin',
Expand Down
5 changes: 1 addition & 4 deletions packages/nextcloud/test/provisioning_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ void main() {
group(
'provisioning_api',
() {
late DockerImage image;
setUpAll(() async => image = await getDockerImage());

late DockerContainer container;
late TestNextcloudClient client;
setUp(() async {
container = await getDockerContainer(image);
container = await getDockerContainer();
client = await getTestClient(
container,
username: 'admin',
Expand Down
47 changes: 24 additions & 23 deletions packages/nextcloud/test/settings_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@ import 'package:test/test.dart';

import 'helper.dart';

Future<void> main() async {
await run(await getDockerImage());
}

Future<void> run(final DockerImage image) async {
group('settings', () {
late DockerContainer container;
late TestNextcloudClient client;
setUp(() async {
container = await getDockerContainer(image);
client = await getTestClient(
container,
username: 'admin',
);
});
tearDown(() => container.destroy());
void main() {
group(
'settings',
() {
late DockerContainer container;
late TestNextcloudClient client;
setUp(() async {
container = await getDockerContainer();
client = await getTestClient(
container,
username: 'admin',
);
});
tearDown(() => container.destroy());

group('Logs', () {
test('Download', () async {
final response = await client.settings.logSettings.download();
final logs = utf8.decode(response.body);
expect(logs, await container.nextcloudLogs());
group('Logs', () {
test('Download', () async {
final response = await client.settings.logSettings.download();
final logs = utf8.decode(response.body);
expect(logs, await container.nextcloudLogs());
});
});
});
});
},
retry: retryCount,
timeout: timeout,
);
}
8 changes: 1 addition & 7 deletions packages/nextcloud/test/spreed_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,10 @@ void main() {
group(
'spreed',
() {
late DockerImage image;
setUpAll(() async => image = await getDockerImage());

late DockerContainer container;
late TestNextcloudClient client1;
setUp(() async {
container = await getDockerContainer(
image,
useApache: true,
);
container = await getDockerContainer();
client1 = await getTestClient(container);
});
tearDown(() => container.destroy());
Expand Down
5 changes: 1 addition & 4 deletions packages/nextcloud/test/uppush_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ void main() {
group(
'uppush',
() {
late DockerImage image;
setUpAll(() async => image = await getDockerImage());

late DockerContainer container;
late TestNextcloudClient client;
setUp(() async {
container = await getDockerContainer(image);
container = await getDockerContainer();
client = await getTestClient(
container,
username: 'admin',
Expand Down
5 changes: 1 addition & 4 deletions packages/nextcloud/test/user_status_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ void main() {
group(
'user_status',
() {
late DockerImage image;
setUpAll(() async => image = await getDockerImage());

late DockerContainer container;
late TestNextcloudClient client;
setUp(() async {
container = await getDockerContainer(image);
container = await getDockerContainer();
client = await getTestClient(container);
});
tearDown(() => container.destroy());
Expand Down
Loading

0 comments on commit 02a1ec6

Please sign in to comment.