File: backup_sync_operations_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 (157 lines) | stat: -rw-r--r-- 5,287 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
/**
 * @file backup_sync_operations_test.cpp
 * @brief This file contains tests for the public interfaces available to manage backups or syncs,
 * stop them and archive or remove a deconfigured backup.
 */

#ifdef ENABLE_SYNC

#include "integration_test_utils.h"
#include "mock_listeners.h"
#include "SdkTest_test.h"

using namespace sdk_test;
using namespace testing;

/**
 * @brief SdkTestBackup class implementing basic operations for Backups and Syncs (to be extended).
 * It initializes one testing account and ensures that the device name is configured.
 */
class SdkTestBackupSync: public SdkTest
{
private:
    const fs::path mLocalFolderName{getFilePrefix() + "dir"};
    const LocalTempDir mLocalFolder{fs::current_path() / mLocalFolderName};

protected:
    MegaHandle mBackupID{INVALID_HANDLE};
    const std::string mBackupName{"myBackup"};

public:
    static constexpr auto mMaxTimeout{3min};

    void SetUp() override
    {
        SdkTest::SetUp();
        ASSERT_NO_FATAL_FAILURE(getAccountsForTest(1));
        ASSERT_NO_FATAL_FAILURE(ensureAccountDeviceNamesAttrExists(megaApi[0].get()));
    }

    const fs::path& getLocalFolder() const
    {
        return mLocalFolder.getPath();
    }

    void setupBackupSync()
    {
        LOG_debug << "Creating a backup";
        ASSERT_EQ(mBackupID, INVALID_HANDLE) << "There is already a backup/sync created.";
        mBackupID = backupFolder(megaApi[0].get(), path_u8string(getLocalFolder()), mBackupName);
        ASSERT_NE(mBackupID, INVALID_HANDLE) << "Invalid Backup ID";
    }

    void removeSync()
    {
        ASSERT_NE(mBackupID, INVALID_HANDLE) << "Cant't remove backup/sync. Invalid Backup ID";
        if (const std::unique_ptr<MegaSync> sync{megaApi[0]->getSyncByBackupId(mBackupID)}; sync)
        {
            ASSERT_TRUE(::removeSync(megaApi[0].get(), mBackupID));
        }
    }
};

/**
 * @brief The SdkTestBackupOperations class
 *
 * Test fixture that creates a backup and a destination directory to archive backups when moved.
 * It offers functionality to test a clash when archiving a backup.
 */
class SdkTestBackupOperations: public SdkTestBackupSync
{
private:
    MegaHandle mBackupRootHandle{INVALID_HANDLE};
    const fs::path mDestinationFolderName{"BackupArchive"};
    MegaHandle mDestinationFolderHandle{INVALID_HANDLE};

public:
    void SetUp() override
    {
        SdkTestBackupSync::SetUp();
        ASSERT_NO_FATAL_FAILURE(setupBackupSync());
        ASSERT_NO_FATAL_FAILURE(setupDestinationDirectory());
        const std::unique_ptr<MegaSync> sync{megaApi[0]->getSyncByBackupId(mBackupID)};
        ASSERT_TRUE(sync);
        mBackupRootHandle = sync->getMegaHandle();
    }

    void TearDown() override
    {
        if (const std::unique_ptr<MegaSync> sync{megaApi[0]->getSyncByBackupId(mBackupID)}; sync)
        {
            ::removeSync(megaApi[0].get(), mBackupID);
        }
        SdkTestBackupSync::TearDown();
    }

    void setupDestinationDirectory()
    {
        unique_ptr<MegaNode> rootnode{megaApi[0]->getRootNode()};
        ASSERT_TRUE(rootnode) << "Account root node not available.";
        mDestinationFolderHandle =
            createFolder(0, path_u8string(mDestinationFolderName).c_str(), rootnode.get());
        ASSERT_NE(mDestinationFolderHandle, INVALID_HANDLE) << "Invalid destination folder handle";
    }

    void duplicateDestinationBackupFolder()
    {
        // Get parent folder
        const unique_ptr<MegaNode> parentFolder{
            megaApi[0]->getNodeByHandle(mDestinationFolderHandle)};
        ASSERT_TRUE(parentFolder);

        // Create a folder in the destination with the same name as the backup
        const MegaHandle newFolder = createFolder(0, mBackupName.c_str(), parentFolder.get());
        ASSERT_NE(newFolder, INVALID_HANDLE) << "Invalid destination folder handle";
    }

    bool removeBackupNode(error expectedError = API_OK)
    {
        return moveOrRemoveBackupNode(expectedError);
    }

    bool archiveBackupNode(error expectedError = API_OK)
    {
        return moveOrRemoveBackupNode(expectedError, mDestinationFolderHandle);
    }

private:
    bool moveOrRemoveBackupNode(error expectedError = API_OK,
                                MegaHandle destination = INVALID_HANDLE)
    {
        NiceMock<MockRequestListener> reqTracker{megaApi[0].get()};
        reqTracker.setErrorExpectations(expectedError);
        megaApi[0]->moveOrRemoveDeconfiguredBackupNodes(mBackupRootHandle,
                                                        destination,
                                                        &reqTracker);
        return reqTracker.waitForFinishOrTimeout(mMaxTimeout);
    }
};

TEST_F(SdkTestBackupOperations, RemoveDestinationClash)
{
    static const auto logPre{getLogPrefix()};

    LOG_debug << logPre << "Duplicate destination folder to cause a clash.";
    duplicateDestinationBackupFolder();

    LOG_debug << logPre << "Remove backup sync";
    removeSync();

    LOG_debug << logPre << "Try to move backup root node to the cloud";
    ASSERT_TRUE(archiveBackupNode(API_EEXIST)) << "Desination node should already exist and fail.";

    LOG_debug << logPre << "Remove backup contents";
    ASSERT_TRUE(removeBackupNode()) << "Can't remove backup contents.";
}

#endif