summaryrefslogtreecommitdiffstats
path: root/src/test/common/test_fault_injector.cc
blob: dfa147478ea58110479e2cada71b21748f621d25 (plain)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2020 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation. See file COPYING.
 *
 */

#include "common/fault_injector.h"
#include "common/common_init.h"
#include "common/ceph_argparse.h"
#include <gtest/gtest.h>

TEST(FaultInjectorDeathTest, InjectAbort)
{
  constexpr FaultInjector f{false, InjectAbort{}};
  EXPECT_EQ(f.check(true), 0);
  EXPECT_DEATH([[maybe_unused]] int r = f.check(false), "FaultInjector");
}

TEST(FaultInjectorDeathTest, AssignAbort)
{
  FaultInjector<bool> f;
  ASSERT_EQ(f.check(false), 0);
  f.inject(false, InjectAbort{});
  EXPECT_DEATH([[maybe_unused]] int r = f.check(false), "FaultInjector");
}

// death tests have to run in single-threaded mode, so we can't initialize a
// CephContext until after those have run (gtest automatically runs them first)
class Fixture : public testing::Test {
  boost::intrusive_ptr<CephContext> cct;
  std::optional<NoDoutPrefix> prefix;
 protected:
  void SetUp() override {
    CephInitParameters params(CEPH_ENTITY_TYPE_CLIENT);
    cct = common_preinit(params, CODE_ENVIRONMENT_UTILITY,
                         CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
    prefix.emplace(cct.get(), ceph_subsys_context);
  }
  void TearDown() override {
    prefix.reset();
    cct.reset();
  }
  const DoutPrefixProvider* dpp() { return &*prefix; }
};

// test int as a Key type
using FaultInjectorInt = Fixture;

TEST_F(FaultInjectorInt, Default)
{
  constexpr FaultInjector<int> f;
  EXPECT_EQ(f.check(0), 0);
  EXPECT_EQ(f.check(1), 0);
  EXPECT_EQ(f.check(2), 0);
  EXPECT_EQ(f.check(3), 0);
}

TEST_F(FaultInjectorInt, InjectError)
{
  constexpr FaultInjector f{2, InjectError{-EINVAL}};
  EXPECT_EQ(f.check(0), 0);
  EXPECT_EQ(f.check(1), 0);
  EXPECT_EQ(f.check(2), -EINVAL);
  EXPECT_EQ(f.check(3), 0);
}

TEST_F(FaultInjectorInt, InjectErrorMessage)
{
  FaultInjector f{2, InjectError{-EINVAL, dpp()}};
  EXPECT_EQ(f.check(0), 0);
  EXPECT_EQ(f.check(1), 0);
  EXPECT_EQ(f.check(2), -EINVAL);
  EXPECT_EQ(f.check(3), 0);
}

TEST_F(FaultInjectorInt, AssignError)
{
  FaultInjector<int> f;
  ASSERT_EQ(f.check(0), 0);
  f.inject(0, InjectError{-EINVAL});
  EXPECT_EQ(f.check(0), -EINVAL);
}

TEST_F(FaultInjectorInt, AssignErrorMessage)
{
  FaultInjector<int> f;
  ASSERT_EQ(f.check(0), 0);
  f.inject(0, InjectError{-EINVAL, dpp()});
  EXPECT_EQ(f.check(0), -EINVAL);
}

// test std::string_view as a Key type
using FaultInjectorString = Fixture;

TEST_F(FaultInjectorString, Default)
{
  constexpr FaultInjector<std::string_view> f;
  EXPECT_EQ(f.check("Red"), 0);
  EXPECT_EQ(f.check("Green"), 0);
  EXPECT_EQ(f.check("Blue"), 0);
}

TEST_F(FaultInjectorString, InjectError)
{
  FaultInjector<std::string_view> f{"Red", InjectError{-EIO}};
  EXPECT_EQ(f.check("Red"), -EIO);
  EXPECT_EQ(f.check("Green"), 0);
  EXPECT_EQ(f.check("Blue"), 0);
}

TEST_F(FaultInjectorString, InjectErrorMessage)
{
  FaultInjector<std::string_view> f{"Red", InjectError{-EIO, dpp()}};
  EXPECT_EQ(f.check("Red"), -EIO);
  EXPECT_EQ(f.check("Green"), 0);
  EXPECT_EQ(f.check("Blue"), 0);
}

TEST_F(FaultInjectorString, AssignError)
{
  FaultInjector<std::string_view> f;
  ASSERT_EQ(f.check("Red"), 0);
  f.inject("Red", InjectError{-EINVAL});
  EXPECT_EQ(f.check("Red"), -EINVAL);
}

TEST_F(FaultInjectorString, AssignErrorMessage)
{
  FaultInjector<std::string_view> f;
  ASSERT_EQ(f.check("Red"), 0);
  f.inject("Red", InjectError{-EINVAL, dpp()});
  EXPECT_EQ(f.check("Red"), -EINVAL);
}

// test enum class as a Key type
using FaultInjectorEnum = Fixture;

enum class Color { Red, Green, Blue };

static std::ostream& operator<<(std::ostream& out, const Color& c) {
  switch (c) {
    case Color::Red: return out << "Red";
    case Color::Green: return out << "Green";
    case Color::Blue: return out << "Blue";
  }
  return out;
}

TEST_F(FaultInjectorEnum, Default)
{
  constexpr FaultInjector<Color> f;
  EXPECT_EQ(f.check(Color::Red), 0);
  EXPECT_EQ(f.check(Color::Green), 0);
  EXPECT_EQ(f.check(Color::Blue), 0);
}

TEST_F(FaultInjectorEnum, InjectError)
{
  FaultInjector f{Color::Red, InjectError{-EIO}};
  EXPECT_EQ(f.check(Color::Red), -EIO);
  EXPECT_EQ(f.check(Color::Green), 0);
  EXPECT_EQ(f.check(Color::Blue), 0);
}

TEST_F(FaultInjectorEnum, InjectErrorMessage)
{
  FaultInjector f{Color::Red, InjectError{-EIO, dpp()}};
  EXPECT_EQ(f.check(Color::Red), -EIO);
  EXPECT_EQ(f.check(Color::Green), 0);
  EXPECT_EQ(f.check(Color::Blue), 0);
}

TEST_F(FaultInjectorEnum, AssignError)
{
  FaultInjector<Color> f;
  ASSERT_EQ(f.check(Color::Red), 0);
  f.inject(Color::Red, InjectError{-EINVAL});
  EXPECT_EQ(f.check(Color::Red), -EINVAL);
}

TEST_F(FaultInjectorEnum, AssignErrorMessage)
{
  FaultInjector<Color> f;
  ASSERT_EQ(f.check(Color::Red), 0);
  f.inject(Color::Red, InjectError{-EINVAL, dpp()});
  EXPECT_EQ(f.check(Color::Red), -EINVAL);
}

// test custom move-only Key type
using FaultInjectorMoveOnly = Fixture;

struct MoveOnlyKey {
  MoveOnlyKey() = default;
  MoveOnlyKey(const MoveOnlyKey&) = delete;
  MoveOnlyKey& operator=(const MoveOnlyKey&) = delete;
  MoveOnlyKey(MoveOnlyKey&&) = default;
  MoveOnlyKey& operator=(MoveOnlyKey&&) = default;
  ~MoveOnlyKey() = default;
};

static bool operator==(const MoveOnlyKey&, const MoveOnlyKey&) {
  return true; // all keys are equal
}
static std::ostream& operator<<(std::ostream& out, const MoveOnlyKey&) {
  return out;
}

TEST_F(FaultInjectorMoveOnly, Default)
{
  constexpr FaultInjector<MoveOnlyKey> f;
  EXPECT_EQ(f.check(MoveOnlyKey{}), 0);
}

TEST_F(FaultInjectorMoveOnly, InjectError)
{
  FaultInjector f{MoveOnlyKey{}, InjectError{-EIO}};
  EXPECT_EQ(f.check(MoveOnlyKey{}), -EIO);
}

TEST_F(FaultInjectorMoveOnly, InjectErrorMessage)
{
  FaultInjector f{MoveOnlyKey{}, InjectError{-EIO, dpp()}};
  EXPECT_EQ(f.check(MoveOnlyKey{}), -EIO);
}

TEST_F(FaultInjectorMoveOnly, AssignError)
{
  FaultInjector<MoveOnlyKey> f;
  ASSERT_EQ(f.check({}), 0);
  f.inject({}, InjectError{-EINVAL});
  EXPECT_EQ(f.check({}), -EINVAL);
}

TEST_F(FaultInjectorMoveOnly, AssignErrorMessage)
{
  FaultInjector<MoveOnlyKey> f;
  ASSERT_EQ(f.check({}), 0);
  f.inject({}, InjectError{-EINVAL, dpp()});
  EXPECT_EQ(f.check({}), -EINVAL);
}