|
| 1 | +//////////////////////////////////////////////////////////////////////////////// |
| 2 | +/// DISCLAIMER |
| 3 | +/// |
| 4 | +/// Copyright 2021-2021 ArangoDB GmbH, Cologne, Germany |
| 5 | +/// |
| 6 | +/// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +/// you may not use this file except in compliance with the License. |
| 8 | +/// You may obtain a copy of the License at |
| 9 | +/// |
| 10 | +/// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +/// |
| 12 | +/// Unless required by applicable law or agreed to in writing, software |
| 13 | +/// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +/// See the License for the specific language governing permissions and |
| 16 |
1E79
+/// limitations under the License. |
| 17 | +/// |
| 18 | +/// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +/// |
| 20 | +/// @author Michael Hackstein |
| 21 | +//////////////////////////////////////////////////////////////////////////////// |
| 22 | + |
| 23 | +#include "gtest/gtest.h" |
| 24 | + |
| 25 | +#include "Mocks/Servers.h" |
| 26 | + |
| 27 | +#include "Auth/UserManager.h" |
| 28 | +#include "GeneralServer/AuthenticationFeature.h" |
| 29 | + |
| 30 | +namespace arangodb { |
| 31 | +namespace tests { |
| 32 | +namespace auth_info_test { |
| 33 | + |
| 34 | +namespace { |
| 35 | +static char const* FailureOnLoadDB = "UserManager::performDBLookup"; |
| 36 | +} |
| 37 | + |
| 38 | +class UserManagerClusterTest : public ::testing::Test { |
| 39 | + public: |
| 40 | + mocks::MockCoordinator _server{}; |
| 41 | + |
| 42 | + protected: |
| 43 | + auth::UserManager* userManager() { |
| 44 | + auto um = _server.getFeature<AuthenticationFeature>().userManager(); |
| 45 | + TRI_ASSERT(um != nullptr); |
| 46 | + return um; |
| 47 | + } |
| 48 | + |
| 49 | + void simulateOneHeartbeat() { |
| 50 | + /* |
| 51 | + * NOTE: Sorry i gave up on this. Something that |
| 52 | + * requires the heartbeat is absolutely untestable |
| 53 | + * it does everything at once, and you need to setup |
| 54 | + * a complete functioning world to somehow execute it. |
| 55 | + * Furthermore it has tons of undesired side-effects. |
| 56 | + * |
| 57 | + * All i wanted to do, is let the heartbeat detect |
| 58 | + * the UserVersion and inject it the way it does into |
| 59 | + * the UserManager. |
| 60 | + */ |
| 61 | + } |
| 62 | + |
| 63 | + uint64_t getAgencyUserVersion() { |
| 64 | + // Simply read the UserVersion from the Agency. |
| 65 | + // This is copy pasted from HeartbeatThread. |
| 66 | + auto& cache = _server.getFeature<ClusterFeature>().agencyCache(); |
| 67 | + auto [acb, idx] = cache.read( |
| 68 | + std::vector<std::string>{AgencyCommHelper::path("Sync/UserVersion")}); |
| 69 | + auto result = acb->slice(); |
| 70 | + VPackSlice slice = result[0].get(std::vector<std::string>( |
| 71 | + {AgencyCommHelper::path(), "Sync", "UserVersion"})); |
| 72 | + TRI_ASSERT(slice.isInteger()); |
| 73 | + // there is a UserVersion, and it has to be an UINT |
| 74 | + return slice.getUInt(); |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +#ifdef ARANGODB_ENABLE_FAILURE_TESTS |
| 79 | +TEST_F(UserManagerClusterTest, regression_forgotten_update) { |
| 80 | + /* The following order of events did lead to a missing update: |
| 81 | + * 1. um->triggerLocalReload(); |
| 82 | + * 2. um->triggerGlobalReload(); |
| 83 | + * 3. heartbeat |
| 84 | + * 4. um->loadFromDB(); |
| 85 | + * |
| 86 | + * 1. and 2. moved internal versions forward two times |
| 87 | + * 3. heartbeat resets one of the movings |
| 88 | + * 4. Does not perform the actual load, as the heartbeat reset indicates everything is okay. |
| 89 | + */ |
| 90 | + |
| 91 | + TRI_AddFailurePointDebugging(FailureOnLoadDB); |
| 92 | + auto guard = arangodb::scopeGuard( |
| 93 | + []() { TRI_RemoveFailurePointDebugging(FailureOnLoadDB); }); |
| 94 | + |
| 95 | + auto um = userManager(); |
| 96 | + // If for some reason this EXPECT ever triggers, we can |
| 97 | + // inject either the AgencyValue into the UserManager |
| 98 | + // or vice-versa. This is just an assertion that we |
| 99 | + // expect everything to start at default (1). |
| 100 | + EXPECT_EQ(um->globalVersion(), getAgencyUserVersion()); |
| 101 | + |
| 102 | + um->triggerLocalReload(); |
| 103 | + EXPECT_EQ(um->globalVersion(), getAgencyUserVersion()); |
| 104 | + |
| 105 | + um->triggerGlobalReload(); |
| 106 | + EXPECT_EQ(um->globalVersion(), getAgencyUserVersion()); |
| 107 | + |
| 108 | + /* |
| 109 | + * This is the correct test here, the heartbeat has |
| 110 | + * a side-effect, but that is simply untestable in the |
| 111 | + * current design. So the only thing i could |
| 112 | + * fall back to is to assert that the UserVersions in the agency |
| 113 | + * do stay aligned. |
| 114 | + * |
| 115 | + * this->simulateOneHeartbeat(); |
| 116 | + */ |
| 117 | + // This needs to trigger a reload from DB |
| 118 | + try { |
| 119 | + um->userExists("unknown user"); |
| 120 | + FAIL(); |
| 121 | + } catch (arangodb::basics::Exception const& e) { |
| 122 | + // This Execption indicates that we got past the version |
| 123 | + // checks and would contact DBServer now. |
| 124 | + // This is not under test here, we only want to test that we load |
| 125 | + // the plan |
| 126 | + EXPECT_EQ(e.code(), TRI_ERROR_DEBUG); |
| 127 | + } catch (...) { |
| 128 | + FAIL(); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +TEST_F(UserManagerClusterTest, cacheRevalidationShouldKeepVersionsInLine) { |
| 133 | + TRI_AddFailurePointDebugging(FailureOnLoadDB); |
| 134 | + auto guard = arangodb::scopeGuard( |
| 135 | + []() { TRI_RemoveFailurePointDebugging(FailureOnLoadDB); }); |
| 136 | + |
| 137 | + auto um = userManager(); |
| 138 | + // If for some reason this EXPECT ever triggers, we can |
| 139 | + // inject either the AgencyValue into the UserManager |
| 140 | + // or vice-versa. This is just an assertion that we |
| 141 | + // expect everything to start at default (1). |
| 142 | + EXPECT_EQ(um->globalVersion(), getAgencyUserVersion()); |
| 143 | + |
| 144 | + try { |
| 145 | + // This needs to trigger a reload from DB |
| 146 | + // Internally it will do local, global, and loadFromDB. |
| 147 | + um->triggerCacheRevalidation(); |
| 148 | + FAIL(); |
| 149 | + } catch (arangodb::basics::Exception const& e) { |
| 150 | + // This Execption indicates that we got past the version |
| 151 | + // checks and would contact DBServer now. |
| 152 | + // This is not under test here, we only want to test that we load |
| 153 | + // the plan |
| 154 | + EXPECT_EQ(e.code(), TRI_ERROR_DEBUG); |
| 155 | + } catch (...) { |
| 156 | + FAIL(); |
| 157 | + } |
| 158 | + EXPECT_EQ(um->globalVersion(), getAgencyUserVersion()); |
| 159 | +} |
| 160 | + |
| 161 | +TEST_F(UserManagerClusterTest, triggerLocalReloadShouldNotUpdateClusterVersion) { |
| 162 | + TRI_AddFailurePointDebugging(FailureOnLoadDB); |
| 163 | + auto guard = arangodb::scopeGuard( |
| 164 | + []() { TRI_RemoveFailurePointDebugging(FailureOnLoadDB); }); |
| 165 | + |
| 166 | + auto um = userManager(); |
| 167 | + // If for some reason this EXPECT ever triggers, we can |
| 168 | + // inject either the AgencyValue into the UserManager |
| 169 | + // or vice-versa. This is just an assertion that we |
| 170 | + // expect everything to start at default (1). |
| 171 | + EXPECT_EQ(um->globalVersion(), getAgencyUserVersion()); |
| 172 | + |
| 173 | + uint64_t versionBefore = getAgencyUserVersion(); |
| 174 | + |
| 175 | + um->triggerLocalReload(); |
| 176 | + EXPECT_EQ(versionBefore, getAgencyUserVersion()); |
| 177 | + |
| 178 | + /* |
| 179 | + * This is the correct test here, see above |
| 180 | + * |
| 181 | + * this->simulateOneHeartbeat(); |
| 182 | + */ |
| 183 | + // This needs to trigger a reload from DB |
| 184 | + try { |
| 185 | + um->userExists("unknown user"); |
| 186 | + FAIL(); |
| 187 | + } catch (arangodb::basics::Exception const& e) { |
| 188 | + // This Execption indicates that we got past the version |
| 189 | + // checks and would contact DBServer now. |
| 190 | + // This is not under test here, we only want to test that we load |
| 191 | + // the plan |
| 192 | + EXPECT_EQ(e.code(), TRI_ERROR_DEBUG); |
| 193 | + } catch (...) { |
| 194 | + FAIL(); |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +TEST_F(UserManagerClusterTest, triggerGlobalReloadShouldUpdateClusterVersion) { |
| 199 | + TRI_AddFailurePointDebugging(FailureOnLoadDB); |
| 200 | + auto guard = arangodb::scopeGuard( |
| 201 | + []() { TRI_RemoveFailurePointDebugging(FailureOnLoadDB); }); |
| 202 | + |
| 203 | + auto um = userManager(); |
| 204 | + // If for some reason this EXPECT ever triggers, we can |
| 205 | + // inject either the AgencyValue into the UserManager |
| 206 | + // or vice-versa. This is just an assertion that we |
| 207 | + // expect everything to start at default (1). |
| 208 | + EXPECT_EQ(um->globalVersion(), getAgencyUserVersion()); |
| 209 | + |
| 210 | + uint64_t versionBefore = getAgencyUserVersion(); |
| 211 | + |
| 212 | + um->triggerGlobalReload(); |
| 213 | + // The version in the Agency needs to be increased |
| 214 | + EXPECT_LT(versionBefore, getAgencyUserVersion()); |
| 215 | + |
| 216 | + /* |
| 217 | + * This is the correct test here, see above |
| 218 | + * |
| 219 | + * this->simulateOneHeartbeat(); |
| 220 | + */ |
| 221 | + // This needs to trigger a reload from DB |
| 222 | + try { |
| 223 | + um->userExists("unknown user"); |
| 224 | + FAIL(); |
| 225 | + } catch (arangodb::basics::Exception const& e) { |
| 226 | + // This Execption indicates that we got past the version |
| 227 | + // checks and would contact DBServer now. |
| 228 | + // This is not under test here, we only want to test that we load |
| 229 | + // the plan |
| 230 | + EXPECT_EQ(e.code(), TRI_ERROR_DEBUG); |
| 231 | + } catch (...) { |
| 232 | + FAIL(); |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +#endif |
| 237 | + |
| 238 | +} // namespace auth_info_test |
| 239 | +} // namespace tests |
| 240 | +} // namespace arangodb |
0 commit comments