File: FuseTests.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 (208 lines) | stat: -rw-r--r-- 6,937 bytes parent folder | download | duplicates (2)
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
/**
 * (c) 2013 by Mega Limited, Auckland, New Zealand
 *
 * This file is part of MEGAcmd.
 *
 * MEGAcmd is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright Simplified (2-clause) BSD License.
 *
 * You should have received a copy of the license along with this
 * program.
 */
#ifdef WITH_FUSE

#include <filesystem>

#include "TestUtils.h"
#include "MegaCmdTestingTools.h"

namespace fs = std::filesystem;

class FuseTests : public NOINTERACTIVELoggedInTest
{
    SelfDeletingTmpFolder mTmpDir;
#ifdef _WIN32
    TestInstrumentsEnvVarGuard mEnvGuard{"MEGACMD_FUSE_ALLOW_LOCAL_PATHS", "1"};
#endif
    void SetUp() override
    {
        NOINTERACTIVELoggedInTest::SetUp();
        TearDown();

        auto result = executeInClient({"mkdir", "-p", mountDirCloud() + "/"}).ok();
        ASSERT_TRUE(result);

        result = fs::create_directory(mountDirLocal());

        ASSERT_TRUE(result);
    }

    void TearDown() override
    {
        removeAllMounts();
    }

protected:
    void removeAllMounts()
    {
        auto result = executeInClient({"fuse-show"});
        ASSERT_TRUE(result.ok());

        auto lines = splitByNewline(result.out());
        for (size_t i = 1; i < lines.size(); ++i)
        {
            if (lines[i].empty() || megacmd::startsWith(lines[i], "Use " /* skip detail usage line */))
            {
                continue;
            }

            auto words = megacmd::split(lines[i], " ");
            ASSERT_TRUE(!words.empty());

            const std::string mountId = words[0];

            auto result = executeInClient({"fuse-disable", "--", mountId});
            ASSERT_TRUE(result.ok()) << "Could not disable mount " << mountId;

            result = executeInClient({"fuse-remove", "--", mountId});
            ASSERT_TRUE(result.ok()) << "Could not remove mount " << mountId;
        }
    }

    fs::path mountDirLocalPath() const
    {
        return mTmpDir.path() / "tests_integration_mount_dir";
    }

    std::string mountDirLocal() const
    {
        return mountDirLocalPath().string();
    }

    std::string mountDirCloud() const
    {
        return "/tests_integration_mount_dir";
    }

    std::string qw(const std::string& str) // quote wrap
    {
        return "\"" + str + "\"";
    }
};

TEST_F(FuseTests, NoMounts)
{
    auto result = executeInClient({"fuse-show"});
    ASSERT_TRUE(result.ok());
    EXPECT_THAT(result.out(), testing::HasSubstr("There are no mounts"));
}

TEST_F(FuseTests, AddAndRemoveMount)
{
#ifdef WIN32
    // Windows expects mount dir to not exist
    fs::remove_all(mountDirLocal());
#endif
    auto result = executeInClient({"fuse-add", "--disabled", mountDirLocal(), mountDirCloud()});
    ASSERT_TRUE(result.ok());
    EXPECT_THAT(result.out(), testing::HasSubstr("Added a new mount from " + qw(mountDirLocal()) + " to " + qw(mountDirCloud())));

    result = executeInClient({"fuse-show", "--disable-path-collapse"});
    ASSERT_TRUE(result.ok());
    EXPECT_THAT(result.out(), testing::Not(testing::HasSubstr("There are no mounts")));
    EXPECT_THAT(result.out(), testing::HasSubstr(mountDirLocal()));
    EXPECT_THAT(result.out(), testing::HasSubstr(mountDirCloud()));

    result = executeInClient({"fuse-show", "--only-enabled"});
    ASSERT_TRUE(result.ok());
    EXPECT_THAT(result.out(), testing::HasSubstr("There are no mounts"));

    result = executeInClient({"fuse-show", mountDirLocal()});
    ASSERT_TRUE(result.ok());
    EXPECT_THAT(result.out(), testing::ContainsRegex("Enabled:\\s*NO"));

    result = executeInClient({"fuse-remove", mountDirLocal()});
    ASSERT_TRUE(result.ok());
    EXPECT_THAT(result.out(), testing::HasSubstr("Removed mount"));
    EXPECT_THAT(result.out(), testing::HasSubstr("on " + qw(mountDirLocal())));
}

TEST_F(FuseTests, AddMountEnabled)
{
#ifdef WIN32
    // Windows expects mount dir to not exist
    fs::remove_all(mountDirLocal());
#endif
    auto result = executeInClient({"fuse-add", mountDirLocal(), mountDirCloud()});
    ASSERT_TRUE(result.ok());
    EXPECT_THAT(result.out(), testing::HasSubstr("Added a new mount from " + qw(mountDirLocal()) + " to " + qw(mountDirCloud())));
    EXPECT_THAT(result.out(), testing::HasSubstr("Enabled mount"));

    result = executeInClient({"fuse-show", mountDirLocal()});
    ASSERT_TRUE(result.ok());
    EXPECT_THAT(result.out(), testing::ContainsRegex("Enabled:\\s*YES"));

    result = executeInClient({"fuse-disable", mountDirLocal()});
    ASSERT_TRUE(result.ok());
    EXPECT_THAT(result.out(), testing::HasSubstr("Disabled mount"));
    EXPECT_THAT(result.out(), testing::HasSubstr("on " + qw(mountDirLocal())));

    result = executeInClient({"fuse-remove", mountDirLocal()});
    ASSERT_TRUE(result.ok());
    EXPECT_THAT(result.out(), testing::HasSubstr("Removed mount"));
    EXPECT_THAT(result.out(), testing::HasSubstr("on " + qw(mountDirLocal())));
}

TEST_F(FuseTests, EnsureUniqueId)
{
    const std::string dir1 = (mountDirLocalPath() / "dir1").string();
    const std::string dir2 = (mountDirLocalPath() / "dir2").string();
#ifndef WIN32
    fs::create_directory(dir1);
    fs::create_directory(dir2);
#endif

    auto result = executeInClient({"fuse-add", dir1, mountDirCloud(), "--name=name1"});
    ASSERT_TRUE(result.ok());
    EXPECT_THAT(result.out(), testing::HasSubstr("Added a new mount from " + qw(dir1) + " to " + qw(mountDirCloud())));
    EXPECT_THAT(result.out(), testing::HasSubstr("Enabled mount"));

    result = executeInClient({"fuse-add", dir2, mountDirCloud(), "--name=name2"});
    ASSERT_TRUE(result.ok());
    EXPECT_THAT(result.out(), testing::HasSubstr("Added a new mount from " + qw(dir2) + " to " + qw(mountDirCloud())));
    EXPECT_THAT(result.out(), testing::HasSubstr("Enabled mount"));

    result = executeInClient({"fuse-show"});
    ASSERT_TRUE(result.ok());

    auto lines = splitByNewline(result.out());
    ASSERT_EQ(lines.size(), 5); // Column names + 2 mounts + newline + detail usage

    const std::string firstMountId = megacmd::split(lines[1], " ")[0];
    const std::string secondMountId = megacmd::split(lines[2], " ")[0];
    EXPECT_NE(firstMountId, secondMountId);

    // change names
    result = executeInClient({"fuse-config", "name2",  "--name=name1"});
    ASSERT_FALSE(result.ok()); // not allowed: name clash
    result = executeInClient({"fuse-config", "name2",  "--name=name3"});
    ASSERT_TRUE(result.ok()); // allowed

    {
        result = executeInClient({"fuse-show"});
        ASSERT_TRUE(result.ok());

        auto lines = splitByNewline(result.out());
        ASSERT_EQ(lines.size(), 5); // Column names + 2 mounts + newline + detail usage

        const std::string secondMountId = megacmd::split(lines[2], " ")[0];
        EXPECT_EQ(secondMountId, "name3");
    }

    removeAllMounts();
}

#endif // WITH_FUSE