From dcbc19cf8cd4aba5212611d945fc6ca8e33d8d65 Mon Sep 17 00:00:00 2001 From: Martchus Date: Sat, 3 Nov 2018 20:44:11 +0100 Subject: [PATCH] Adapt mocking to latest changes * Cover all required requests so the connection can be established without errors * Add more events to test error handling * Fix definition of LIB_SYNCTHING_CONNECTOR_CONNECTION_MOCKED after syncthingconnection.cpp split --- connector/CMakeLists.txt | 3 +- connector/syncthingconnection.cpp | 5 ++ connector/syncthingconnectionmockhelpers.cpp | 35 ++++++---- connector/testfiles/mocks/empty.json | 1 + connector/testfiles/mocks/events-01.json | 14 ++++ connector/testfiles/mocks/events-06.json | 18 +++++ connector/testfiles/mocks/events-07.json | 67 +++++++++++++++++++ .../testfiles/mocks/folderstatus-03.json | 15 +++++ connector/testfiles/mocks/pullerrors-01.json | 9 +++ connector/testfiles/mocks/version.json | 6 ++ 10 files changed, 158 insertions(+), 15 deletions(-) create mode 100644 connector/testfiles/mocks/empty.json create mode 100644 connector/testfiles/mocks/events-07.json create mode 100644 connector/testfiles/mocks/folderstatus-03.json create mode 100644 connector/testfiles/mocks/pullerrors-01.json create mode 100644 connector/testfiles/mocks/version.json diff --git a/connector/CMakeLists.txt b/connector/CMakeLists.txt index e2516f4..96acabb 100644 --- a/connector/CMakeLists.txt +++ b/connector/CMakeLists.txt @@ -95,7 +95,8 @@ if(SYNCTHING_CONNECTION_MOCKED) syncthingconnectionmockhelpers.cpp ) set_property( - SOURCE syncthingconnection_requests.cpp + SOURCE syncthingconnection.cpp + syncthingconnection_requests.cpp syncthingconnectionmockhelpers.h syncthingconnectionmockhelpers.cpp APPEND PROPERTY COMPILE_DEFINITIONS ${META_PROJECT_VARNAME_UPPER}_CONNECTION_MOCKED diff --git a/connector/syncthingconnection.cpp b/connector/syncthingconnection.cpp index f4f2bca..1d01cc6 100644 --- a/connector/syncthingconnection.cpp +++ b/connector/syncthingconnection.cpp @@ -3,6 +3,10 @@ #include "./syncthingconnectionsettings.h" #include "./utils.h" +#ifdef LIB_SYNCTHING_CONNECTOR_CONNECTION_MOCKED +#include "./syncthingconnectionmockhelpers.h" +#endif + #include #include @@ -103,6 +107,7 @@ SyncthingConnection::SyncthingConnection(const QString &syncthingUrl, const QByt m_autoReconnectTimer.setTimerType(Qt::VeryCoarseTimer); m_autoReconnectTimer.setInterval(SyncthingConnectionSettings::defaultReconnectInterval); QObject::connect(&m_autoReconnectTimer, &QTimer::timeout, this, &SyncthingConnection::autoReconnect); + #ifdef LIB_SYNCTHING_CONNECTOR_CONNECTION_MOCKED setupTestData(); #endif diff --git a/connector/syncthingconnectionmockhelpers.cpp b/connector/syncthingconnectionmockhelpers.cpp index 5e941d1..e644317 100644 --- a/connector/syncthingconnectionmockhelpers.cpp +++ b/connector/syncthingconnectionmockhelpers.cpp @@ -27,15 +27,8 @@ namespace Data { */ namespace TestData { static bool initialized = false; -static string config; -static string status; -static string folderStats; -static string deviceStats; -static string errors; -static string folderStatus; -static string folderStatus2; -static string connections; -static string events[6]; +static string config, status, folderStats, deviceStats, errors, folderStatus, folderStatus2, folderStatus3, pullErrors, connections, version, empty; +static string events[7]; } // namespace TestData /*! @@ -71,10 +64,11 @@ void setupTestData() const TestApplication testApp(0, nullptr); // read mock files for REST-API - const char *const fileNames[] - = { "config", "status", "folderstats", "devicestats", "errors", "folderstatus-01", "folderstatus-02", "connections" }; + const char *const fileNames[] = { "config", "status", "folderstats", "devicestats", "errors", "folderstatus-01", "folderstatus-02", + "folderstatus-03", "pullerrors-01", "connections", "version", "empty" }; const char *const *fileName = fileNames; - for (string *testDataVariable : { &config, &status, &folderStats, &deviceStats, &errors, &folderStatus, &folderStatus2, &connections }) { + for (string *testDataVariable : { &config, &status, &folderStats, &deviceStats, &errors, &folderStatus, &folderStatus2, &folderStatus3, + &pullErrors, &connections, &version, &empty }) { *testDataVariable = readMockFile(testApp.testFilePath(argsToString("mocks/", *fileName, ".json"))); ++fileName; } @@ -177,25 +171,38 @@ MockedReply *MockedReply::forRequest(const QString &method, const QString &path, buffer = &folderStatus; } else if (folder == QLatin1String("zX8xfl3ygn-")) { buffer = &folderStatus2; + } else if (folder == QLatin1String("forever-alone")) { + buffer = &folderStatus3; + } + } else if (path == QLatin1String("folder/pullerrors")) { + const QString folder(query.queryItemValue(QStringLiteral("folder"))); + if (folder == QLatin1String("GXWxf-3zgnU")) { + buffer = &pullErrors; } } else if (path == QLatin1String("system/connections")) { buffer = &connections; + } else if (path == QLatin1String("system/version")) { + buffer = &version; } else if (path == QLatin1String("events")) { buffer = &events[s_eventIndex]; + cerr << "mocking: at event index " << s_eventIndex << endl; // "emit" the first event almost immediately and further events each 2.5 seconds switch (s_eventIndex) { case 0: delay = 200; ++s_eventIndex; break; - case 5: + case 6: // continue emitting the last event every 10 seconds delay = 10000; break; default: - delay = 2500; + delay = 2000; ++s_eventIndex; } + } else if (path == QLatin1String("events/disk")) { + buffer = ∅ + delay = 5000; } } } diff --git a/connector/testfiles/mocks/empty.json b/connector/testfiles/mocks/empty.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/connector/testfiles/mocks/empty.json @@ -0,0 +1 @@ +[] diff --git a/connector/testfiles/mocks/events-01.json b/connector/testfiles/mocks/events-01.json index b1a64fa..ac5596d 100644 --- a/connector/testfiles/mocks/events-01.json +++ b/connector/testfiles/mocks/events-01.json @@ -70,6 +70,20 @@ } } }, + { + "id": 3, + "type": "FolderErrors", + "time": "2000-06-26T13:39:24.697401384+02:00", + "data": { + "errors": [ + { + "error": "should be ignored", + "path": "some path" + } + ], + "folder": "GXWxf-3zgnU" + } + }, { "id": 3, "type": "FolderErrors", diff --git a/connector/testfiles/mocks/events-06.json b/connector/testfiles/mocks/events-06.json index bd64634..0801012 100644 --- a/connector/testfiles/mocks/events-06.json +++ b/connector/testfiles/mocks/events-06.json @@ -1,6 +1,24 @@ [ { "id": 8, + "type": "FolderErrors", + "time": "2018-12-13T00:34:12.9876937Z", + "data": { + "errors": [ + { + "error": "some error", + "path": "some file" + }, + { + "error": "another error", + "path": "another file" + } + ], + "folder": "zX8xfl3ygn-" + } + }, + { + "id": 9, "type": "DownloadProgress", "time": "2018-12-13T00:34:12.9876937Z", "data": { diff --git a/connector/testfiles/mocks/events-07.json b/connector/testfiles/mocks/events-07.json new file mode 100644 index 0000000..8fac6e8 --- /dev/null +++ b/connector/testfiles/mocks/events-07.json @@ -0,0 +1,67 @@ +[ + { + "id": 10, + "globalID": 8, + "type": "StateChanged", + "time": "2018-12-13T00:34:12.9876937Z", + "data": { + "folder": "zX8xfl3ygn-", + "from": "error", + "duration": 0.19782869900000002, + "to": "syncing" + } + }, + { + "id": 10, + "globalID": 8, + "type": "FolderSummary", + "time": "2018-12-13T00:34:12.9876937Z", + "data": { + "folder": "zX8xfl3ygn-", + "globalBytes": 24525452, + "globalDeleted": 8, + "globalFiles": 543, + "globalFolders": 3, + "localBytes": 24525452, + "localDeleted": 8, + "localFiles": 343, + "localFolders": 4, + "inSyncBytes": 13173473780, + "inSyncFiles": 42106, + "needBytes": 0, + "needFiles": 0, + "ignorePatterns": true, + "invalid": "", + "state": "syncing", + "stateChanged": "2018-12-13T00:34:12.9876937Z", + "version": 71989, + "pullErrors": 25 + } + }, + { + "id": 11, + "type": "FolderSummary", + "time": "2018-12-13T00:34:12.9876937Z", + "data": { + "folder": "GXWxf-3zgnU", + "summary": { + "globalBytes": 23173473780, + "globalDeleted": 2847, + "globalFiles": 82106, + "localBytes": 23173471732, + "localDeleted": 2847, + "localFiles": 82081, + "inSyncBytes": 23173473780, + "inSyncFiles": 82081, + "needBytes": 2048, + "needFiles": 25, + "ignorePatterns": true, + "invalid": "", + "state": "error", + "stateChanged": "2016-03-16T21:47:28.750853241+01:00", + "version": 71989, + "pullErrors": 25 + } + } + } +] diff --git a/connector/testfiles/mocks/folderstatus-03.json b/connector/testfiles/mocks/folderstatus-03.json new file mode 100644 index 0000000..91ab5aa --- /dev/null +++ b/connector/testfiles/mocks/folderstatus-03.json @@ -0,0 +1,15 @@ +{ + "globalBytes": 1, + "globalDeleted": 2, + "globalFiles": 3, + "globalFolders": 4, + "localBytes": 1, + "localDeleted": 2, + "localFiles": 3, + "localFolders": 4, + "ignorePatterns": true, + "invalid": "", + "state": "idle", + "stateChanged": "2018-03-16T21:47:28.750853241+01:00", + "version": 21 +} diff --git a/connector/testfiles/mocks/pullerrors-01.json b/connector/testfiles/mocks/pullerrors-01.json new file mode 100644 index 0000000..9d9861d --- /dev/null +++ b/connector/testfiles/mocks/pullerrors-01.json @@ -0,0 +1,9 @@ +{ + "folder": "GXWxf-3zgnU", + "errors": [ + { + "error": "open /Users/jb/src/github.com/syncthing/syncthing/test/s2/h2j/.syncthing.aslkjd.tmp: permission denied", + "path": "h2j/aslkjd" + } + ] +} diff --git a/connector/testfiles/mocks/version.json b/connector/testfiles/mocks/version.json new file mode 100644 index 0000000..ec7a8a4 --- /dev/null +++ b/connector/testfiles/mocks/version.json @@ -0,0 +1,6 @@ +{ + "arch": "amd64", + "longVersion": "syncthing v0.10.27+3-gea8c3de (go1.4 darwin-amd64 default) jb@syno 2015-03-16 11:01:29 UTC", + "os": "darwin", + "version": "v0.10.27+3-gea8c3de" +}