File: DisableBackupSync_test.cpp

package info (click to toggle)
megacmd 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 32,592 kB
  • sloc: cpp: 326,437; ansic: 34,524; python: 4,630; java: 3,965; sh: 2,869; objc: 2,459; makefile: 197; xml: 113
file content (235 lines) | stat: -rw-r--r-- 7,744 bytes parent folder | download
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
 * @file
 * @brief This file contains tests that involve operations on disabled backup syncs.
 */

#ifdef ENABLE_SYNC

#include "gtest_common.h"
#include "integration/mock_listeners.h"
#include "integration_test_utils.h"
#include "sdk_test_utils.h"
#include "SdkTest_test.h"

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <filesystem>

namespace fs = std::filesystem;
using namespace sdk_test;
using namespace testing;

/**
 * @class DisableBackupSync
 * @brief Test fixture that creates a backup sync, waits until all the files have been uploaded and
 * then disables it.
 */
class DisableBackupSync: public SdkTest
{
public:
    static constexpr auto MAX_TIMEOUT = 3min; // Timeout for operations in this tests suite
    static constexpr auto TIME_DELTA_CONSECUTIVE_TRIES = 10s; // Time to wait between consecutive
                                                              // calls in a waitFor or some sleeps

    void SetUp() override
    {
        SdkTest::SetUp();
        ASSERT_NO_FATAL_FAILURE(getAccountsForTest(1));
        ASSERT_NO_FATAL_FAILURE(ensureAccountDeviceNamesAttrExists(megaApi[0].get()));
        createInitialLocalFiles();
        mBackupId = backupFolder(megaApi[0].get(), path_u8string(getLocalTmpDir()));
        ASSERT_NE(mBackupId, UNDEF);
        ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
        ASSERT_TRUE(disableSync(megaApi[0].get(), mBackupId));
    }

    void TearDown() override
    {
        if (mBackupId != UNDEF)
        {
            removeSync(megaApi[0].get(), mBackupId);
        }
        SdkTest::TearDown();
    }

    /**
     * @brief Waits until all direct successors from both remote and local roots of the sync match.
     *
     * Asserts false if a timeout is exceeded.
     */
    void waitForSyncToMatchCloudAndLocal() const
    {
        const auto areLocalAndCloudSynched = [this]() -> bool
        {
            const auto childrenCloudName =
                getCloudFirstChildrenNames(megaApi[0].get(), getSync()->getMegaHandle());
            return childrenCloudName && Value(getLocalFirstChildrenNames(),
                                              UnorderedElementsAreArray(*childrenCloudName));
        };
        ASSERT_TRUE(waitFor(areLocalAndCloudSynched, MAX_TIMEOUT, TIME_DELTA_CONSECUTIVE_TRIES));
    }

    /**
     * @brief Resume the sync, wait for local and cloud to match assert it is running and there are
     * no stall issues.
     */
    void resumeAndValidateOK() const
    {
        ASSERT_TRUE(resumeSync(megaApi[0].get(), getBackupId()));
        std::this_thread::sleep_for(TIME_DELTA_CONSECUTIVE_TRIES);
        ASSERT_NO_FATAL_FAILURE(waitForSyncToMatchCloudAndLocal());
        ASSERT_EQ(getSyncRunState(), std::optional{MegaSync::RUNSTATE_RUNNING});
        ASSERT_TRUE(getStalls(megaApi[0].get()).empty());
    }

    /**
     * @brief Waits until the node at the given path has the given size.
     *
     * @param nodePath The path to the node relative to the backup root path
     * @param fileSize The expected size for that node
     */
    void waitForCloudNodeToMatchSize(const std::string_view nodePath, const int64_t nodeSize) const
    {
        const auto confirmSize = [nodePath, nodeSize, this]() -> bool
        {
            const auto node = getNodeByPath(nodePath);
            return node && node->getSize() == nodeSize;
        };
        waitFor(confirmSize, MAX_TIMEOUT, TIME_DELTA_CONSECUTIVE_TRIES);
    }

    /**
     * @brief Get the node inside the backup in the given relative path
     *
     * Returns nullptr if it is not found or there is no valid backup sync running.
     */
    std::unique_ptr<MegaNode> getNodeByPath(const std::string_view path) const
    {
        const auto backup = getSync();
        if (!backup)
            return nullptr;
        const std::string filePath = backup->getLastKnownMegaFolder() + "/"s + std::string(path);
        return std::unique_ptr<MegaNode>(megaApi[0]->getNodeByPath(filePath.c_str()));
    }

    /**
     * @brief Get the names of the first successors in the current local sync root.
     */
    std::vector<std::string> getLocalFirstChildrenNames() const
    {
        return getLocalFirstChildrenNames_if(getLocalSyncRoot().value_or(getLocalTmpDir()),
                                             [](const std::string& name)
                                             {
                                                 return name.front() != '.' && name != DEBRISFOLDER;
                                             });
    }

    handle getBackupId() const
    {
        return mBackupId;
    }

    /**
     * @brief Returns the initiated sync object
     */
    std::unique_ptr<MegaSync> getSync() const
    {
        return std::unique_ptr<MegaSync>(megaApi[0]->getSyncByBackupId(mBackupId));
    }

    /**
     * @brief Returns the current sync state if initiated
     */
    std::optional<int> getSyncRunState() const
    {
        const auto sync = getSync();
        if (!sync)
            return {};
        return sync->getRunState();
    }

    /**
     * @brief Returns the current path the sync is using as root. If there is no sync, nullopt is
     * returned
     */
    std::optional<std::filesystem::path> getLocalSyncRoot() const
    {
        const auto sync = getSync();
        if (!sync)
            return {};
        return sync->getLocalFolder();
    }

    static const fs::path& getLocalTmpDir()
    {
        // Prevent parallel test from the same suite writing to the same dir
        thread_local const fs::path localTmpDir{"./DISABLE_BACKUP_SYNC_AUX_TMP_DIR_" +
                                                getThisThreadIdStr()};
        return localTmpDir;
    }

private:
    LocalTempDir tmpDir{getLocalTmpDir()};
    handle mBackupId{UNDEF};

    void createInitialLocalFiles() const
    {
        sdk_test::createFile(getLocalTmpDir() / "testFile", 1);
    }
};

/**
 * @brief DisableBackupSync.RemoveLocalFile:
 * - Remove file while the sync is disabled
 * - Resume it
 * - Confirm the file gets deleted on the cloud and the backup keeps running
 */
TEST_F(DisableBackupSync, RemoveLocalFile)
{
    fs::remove(getLocalTmpDir() / "testFile");
    ASSERT_NO_FATAL_FAILURE(resumeAndValidateOK());
    ASSERT_TRUE(getLocalFirstChildrenNames().empty());
}

/**
 * @brief DisableBackupSync.ModifyLocalFile:
 * - Remove file while the sync is disabled
 * - Resume it
 * - Confirm the file gets updated on the cloud and the backup keeps running
 */
TEST_F(DisableBackupSync, ModifyLocalFile)
{
    sdk_test::createFile(getLocalTmpDir() / "testFile", 5);
    ASSERT_NO_FATAL_FAILURE(resumeAndValidateOK());
    ASSERT_THAT(getLocalFirstChildrenNames(), UnorderedElementsAre("testFile"));
    ASSERT_NO_FATAL_FAILURE(waitForCloudNodeToMatchSize("testFile", 5));
}

/**
 * @brief DisableBackupSync.CreateNewLocalFile:
 * - Create a new file while the sync is disabled
 * - Resume it
 * - Confirm the file gets uploaded to the cloud and the backup keeps running
 */
TEST_F(DisableBackupSync, CreateNewLocalFile)
{
    sdk_test::createFile(getLocalTmpDir() / "testFile2", 1);
    ASSERT_NO_FATAL_FAILURE(resumeAndValidateOK());
    ASSERT_THAT(getLocalFirstChildrenNames(), UnorderedElementsAre("testFile", "testFile2"));
}

/**
 * @brief DisableBackupSync.RenameLocalFile:
 * - Rename a local file while the sync is disabled.
 * - Resume the sync.
 * - Confirm that the move is detected and the backup keeps running.
 */
TEST_F(DisableBackupSync, RenameLocalFile)
{
    fs::rename(getLocalTmpDir() / "testFile", getLocalTmpDir() / "testFile2");
    ASSERT_NO_FATAL_FAILURE(resumeAndValidateOK());
    ASSERT_THAT(getLocalFirstChildrenNames(), UnorderedElementsAre("testFile2"));
}

#endif