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
|
#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include "util/etcd_delete.h"
#include "util/mock_etcd.h"
#include "util/status_test_util.h"
#include "util/sync_task.h"
#include "util/testing.h"
#include "util/thread_pool.h"
using std::bind;
using std::chrono::seconds;
using std::move;
using std::placeholders::_2;
using std::placeholders::_3;
using std::string;
using std::vector;
using testing::DoAll;
using testing::Exactly;
using testing::Expectation;
using testing::IgnoreResult;
using testing::Invoke;
using testing::InvokeWithoutArgs;
using testing::Mock;
using testing::MockFunction;
using testing::SaveArg;
using testing::StrictMock;
using testing::_;
using util::Status;
using util::SyncTask;
using util::Task;
using util::testing::StatusIs;
DECLARE_int32(etcd_delete_concurrency);
namespace cert_trans {
namespace {
class EtcdDeleteTest : public ::testing::Test {
protected:
EtcdDeleteTest() : pool_(1) {
FLAGS_etcd_delete_concurrency = 2;
}
ThreadPool pool_;
StrictMock<MockEtcdClient> client_;
};
typedef EtcdDeleteTest EtcdDeleteDeathTest;
TEST_F(EtcdDeleteDeathTest, ConcurrencyTooLow) {
FLAGS_etcd_delete_concurrency = 0;
SyncTask sync(&pool_);
EXPECT_DEATH(EtcdForceDeleteKeys(&client_, {}, sync.task()),
"FLAGS_etcd_delete_concurrency > 0");
sync.task()->Return();
sync.Wait();
}
TEST_F(EtcdDeleteDeathTest, NoClient) {
SyncTask sync(&pool_);
EXPECT_DEATH(EtcdForceDeleteKeys(nullptr, {}, sync.task()),
"'client' Must be non NULL");
sync.task()->Return();
sync.Wait();
}
TEST_F(EtcdDeleteDeathTest, NoTask) {
EXPECT_DEATH(EtcdForceDeleteKeys(&client_, {}, nullptr),
"'task' Must be non NULL");
}
TEST_F(EtcdDeleteTest, NothingToDo) {
SyncTask sync(&pool_);
EtcdForceDeleteKeys(&client_, {}, sync.task());
sync.Wait();
EXPECT_OK(sync.status());
}
TEST_F(EtcdDeleteTest, AlreadyCancelled) {
SyncTask sync(&pool_);
sync.Cancel();
EtcdForceDeleteKeys(&client_, {"/foo"}, sync.task());
sync.Wait();
EXPECT_THAT(sync.status(), StatusIs(util::error::CANCELLED));
}
TEST_F(EtcdDeleteTest, CancelDuring) {
vector<string> keys{"/one", "/two", "/three"};
ASSERT_LT(static_cast<size_t>(FLAGS_etcd_delete_concurrency), keys.size());
SyncTask sync(&pool_);
Task* first_task(nullptr);
Notification first;
Task* second_task(nullptr);
Notification second;
EXPECT_CALL(client_, ForceDelete("/one", _))
.WillOnce(DoAll(SaveArg<1>(&first_task),
InvokeWithoutArgs(&first, &Notification::Notify)));
EXPECT_CALL(client_, ForceDelete("/two", _))
.WillOnce(DoAll(SaveArg<1>(&second_task),
InvokeWithoutArgs(&second, &Notification::Notify)));
EtcdForceDeleteKeys(&client_, move(keys), sync.task());
ASSERT_TRUE(first.WaitForNotificationWithTimeout(seconds(1)));
ASSERT_TRUE(first_task);
ASSERT_TRUE(second.WaitForNotificationWithTimeout(seconds(1)));
ASSERT_TRUE(second_task);
sync.Cancel();
first_task->Return();
second_task->Return();
sync.Wait();
EXPECT_THAT(sync.status(), StatusIs(util::error::CANCELLED));
}
TEST_F(EtcdDeleteTest, WithinConcurrency) {
vector<string> keys{"/one", "/two", "/three"};
ASSERT_LT(static_cast<size_t>(FLAGS_etcd_delete_concurrency), keys.size());
SyncTask sync(&pool_);
Task* first_task(nullptr);
Notification first;
Task* second_task(nullptr);
Notification second;
EXPECT_CALL(client_, ForceDelete("/one", _))
.WillOnce(DoAll(SaveArg<1>(&first_task),
InvokeWithoutArgs(&first, &Notification::Notify)));
EXPECT_CALL(client_, ForceDelete("/two", _))
.WillOnce(DoAll(SaveArg<1>(&second_task),
InvokeWithoutArgs(&second, &Notification::Notify)));
EtcdForceDeleteKeys(&client_, move(keys), sync.task());
ASSERT_TRUE(first.WaitForNotificationWithTimeout(seconds(1)));
ASSERT_TRUE(first_task);
ASSERT_TRUE(second.WaitForNotificationWithTimeout(seconds(1)));
ASSERT_TRUE(second_task);
// Make sure all the expected calls were called.
Mock::VerifyAndClearExpectations(&client_);
MockFunction<void()> cleanup;
second_task->CleanupWhenDone(bind(&MockFunction<void()>::Call, &cleanup));
Expectation first_done(EXPECT_CALL(cleanup, Call()).Times(Exactly(1)));
// Set up the expectation for the third call, but only once one of
// the first requests "completes".
Task* third_task(nullptr);
Notification third;
EXPECT_CALL(client_, ForceDelete("/three", _))
.After(first_done)
.WillOnce(DoAll(SaveArg<1>(&third_task),
InvokeWithoutArgs(&third, &Notification::Notify)));
second_task->Return();
ASSERT_TRUE(third.WaitForNotificationWithTimeout(seconds(1)));
ASSERT_TRUE(third_task);
first_task->Return();
third_task->Return();
sync.Wait();
EXPECT_OK(sync.status());
}
TEST_F(EtcdDeleteTest, ErrorHandling) {
vector<string> keys{"/one", "/two", "/three"};
ASSERT_LT(static_cast<size_t>(FLAGS_etcd_delete_concurrency), keys.size());
SyncTask sync(&pool_);
Task* first_task(nullptr);
Notification first;
Task* second_task(nullptr);
Notification second;
EXPECT_CALL(client_, ForceDelete("/one", _))
.WillOnce(DoAll(SaveArg<1>(&first_task),
InvokeWithoutArgs(&first, &Notification::Notify)));
EXPECT_CALL(client_, ForceDelete("/two", _))
.WillOnce(DoAll(SaveArg<1>(&second_task),
InvokeWithoutArgs(&second, &Notification::Notify)));
EtcdForceDeleteKeys(&client_, move(keys), sync.task());
ASSERT_TRUE(first.WaitForNotificationWithTimeout(seconds(1)));
ASSERT_TRUE(first_task);
ASSERT_TRUE(second.WaitForNotificationWithTimeout(seconds(1)));
ASSERT_TRUE(second_task);
// Make sure all the expected calls were called.
Mock::VerifyAndClearExpectations(&client_);
second_task->Return(Status(util::error::DATA_LOSS, "things are bad"));
// Make sure the error propagates...
Notification first_cancelled;
first_task->WhenCancelled(bind(&Notification::Notify, &first_cancelled));
ASSERT_TRUE(first_cancelled.WaitForNotificationWithTimeout(seconds(1)));
first_task->Return();
sync.Wait();
EXPECT_THAT(sync.status(), StatusIs(util::error::DATA_LOSS));
}
} // namespace
} // namespace cert_trans
int main(int argc, char** argv) {
cert_trans::test::InitTesting(argv[0], &argc, &argv, true);
return RUN_ALL_TESTS();
}
|