summaryrefslogtreecommitdiffstats
path: root/src/test/rgw/test_rgw_lc.cc
blob: d10b482cbfcec96f7426c4aa8ad08a661f967a9b (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "rgw_xml.h"
#include "rgw_lc.h"
#include "rgw_lc_s3.h"
#include <gtest/gtest.h>
#include <string>
#include <vector>
#include <stdexcept>

static const char* xmldoc_1 =
R"(<Filter>
   <And>
      <Prefix>tax/</Prefix>
      <Tag>
         <Key>key1</Key>
         <Value>value1</Value>
      </Tag>
      <Tag>
         <Key>key2</Key>
         <Value>value2</Value>
      </Tag>
    </And>
</Filter>
)";

TEST(TestLCFilterDecoder, XMLDoc1)
{
  RGWXMLDecoder::XMLParser parser;
  ASSERT_TRUE(parser.init());
  ASSERT_TRUE(parser.parse(xmldoc_1, strlen(xmldoc_1), 1));
  LCFilter_S3 filter;
  auto result = RGWXMLDecoder::decode_xml("Filter", filter, &parser, true);
  ASSERT_TRUE(result);
  /* check repeated Tag element */
  auto tag_map = filter.get_tags().get_tags();
  auto val1 = tag_map.find("key1");
  ASSERT_EQ(val1->second, "value1");
  auto val2 = tag_map.find("key2");
  ASSERT_EQ(val2->second, "value2");
  /* check our flags */
  ASSERT_EQ(filter.get_flags(), 0);
}

static const char* xmldoc_2 =
R"(<Filter>
   <And>
      <ArchiveZone />
      <Tag>
         <Key>spongebob</Key>
         <Value>squarepants</Value>
      </Tag>
    </And>
</Filter>
)";

TEST(TestLCFilterDecoder, XMLDoc2)
{
  RGWXMLDecoder::XMLParser parser;
  ASSERT_TRUE(parser.init());
  ASSERT_TRUE(parser.parse(xmldoc_2, strlen(xmldoc_2), 1));
  LCFilter_S3 filter;
  auto result = RGWXMLDecoder::decode_xml("Filter", filter, &parser, true);
  ASSERT_TRUE(result);
  /* check tags */
  auto tag_map = filter.get_tags().get_tags();
  auto val1 = tag_map.find("spongebob");
  ASSERT_EQ(val1->second, "squarepants");
  /* check our flags */
  ASSERT_EQ(filter.get_flags(), LCFilter::make_flag(LCFlagType::ArchiveZone));
}

// invalid And element placement
static const char* xmldoc_3 =
R"(<Filter>
    <And>
      <Tag>
         <Key>miles</Key>
         <Value>davis</Value>
      </Tag>
    </And>
      <Tag>
         <Key>spongebob</Key>
         <Value>squarepants</Value>
      </Tag>
</Filter>
)";

TEST(TestLCFilterInvalidAnd, XMLDoc3)
{
  RGWXMLDecoder::XMLParser parser;
  ASSERT_TRUE(parser.init());
  ASSERT_TRUE(parser.parse(xmldoc_3, strlen(xmldoc_3), 1));
  LCFilter_S3 filter;
  auto result = RGWXMLDecoder::decode_xml("Filter", filter, &parser, true);
  ASSERT_TRUE(result);
  /* check repeated Tag element */
  auto tag_map = filter.get_tags().get_tags();
  auto val1 = tag_map.find("spongebob");
  ASSERT_TRUE(val1 == tag_map.end());
  /* because the invalid 2nd tag element was not recognized,
   * we cannot access it:
  ASSERT_EQ(val1->second, "squarepants");
  */
  /* check our flags */
  ASSERT_EQ(filter.get_flags(), uint32_t(LCFlagType::none));
}

struct LCWorkTimeTests : ::testing::Test
{
   CephContext* cct;
   std::unique_ptr<RGWLC::LCWorker> worker;

   // expects input in the form of "%m/%d/%y %H:%M:%S"; e.g., "01/15/23 23:59:01"
   utime_t get_utime_by_date_time_string(const std::string& date_time_str)
   {
      struct tm tm{};
      struct timespec ts = {0};

      strptime(date_time_str.c_str(), "%m/%d/%y %H:%M:%S", &tm);
      ts.tv_sec = mktime(&tm);

      return utime_t(ts);
   }

   // expects a map from input value (date & time string) to expected result (boolean)
   void run_should_work_test(const auto& test_values_to_expectations_map) {
      for (const auto& [date_time_str, expected_value] : test_values_to_expectations_map) {
         auto ut = get_utime_by_date_time_string(date_time_str);
         auto should_work = worker->should_work(ut);

         ASSERT_EQ(should_work, expected_value)
            << "input time: " << ut
            << " expected: " << expected_value
            << " should_work: " << should_work
            << " work-time-window: " << cct->_conf->rgw_lifecycle_work_time << std::endl;
      }
   }

   // expects a map from input value (a tuple of date & time strings) to expected result (seconds)
   void run_schedule_next_start_time_test(const auto& test_values_to_expectations_map) {
      for (const auto& [date_time_str_tuple, expected_value] : test_values_to_expectations_map) {
         auto work_started_at = get_utime_by_date_time_string(std::get<0>(date_time_str_tuple));
         auto work_completed_at = get_utime_by_date_time_string(std::get<1>(date_time_str_tuple));
         auto wait_secs_till_next_start = worker->schedule_next_start_time(work_started_at, work_completed_at);

         ASSERT_EQ(wait_secs_till_next_start, expected_value)
            << "work_started_at: " << work_started_at
            << " work_completed_at: " << work_completed_at
            << " expected: " << expected_value
            << " wait_secs_till_next_start: " << wait_secs_till_next_start
            << " work-time-window: " << cct->_conf->rgw_lifecycle_work_time << std::endl;
      }
   }

protected:

   void SetUp() override {
      cct = (new CephContext(CEPH_ENTITY_TYPE_ANY))->get();

      cct->_conf->set_value("rgw_lc_max_wp_worker", 0, 0); // no need to create a real workpool
      worker = std::make_unique<RGWLC::LCWorker>(nullptr, cct, nullptr, 0);
   }

   void TearDown() override {
      worker.reset();
      cct->put();
   }
};

TEST_F(LCWorkTimeTests, ShouldWorkDefaultWorkTime)
{
   std::unordered_map<std::string, bool> test_values_to_expectations = {
      {"01/01/23 00:00:00", true},
      {"01/01/24 00:00:00", true}, // date is not relevant, but only the time-window
      {"01/01/23 00:00:01", true},
      {"01/01/23 03:00:00", true},
      {"01/01/23 05:59:59", true},
      {"01/01/23 06:00:00", true},
      {"01/01/23 06:00:59", true}, // seconds don't matter, but only hours and minutes
      {"01/01/23 06:01:00", false},
      {"01/01/23 23:59:59", false},
      {"01/02/23 23:59:59", false},
      {"01/01/23 12:00:00", false},
      {"01/01/23 14:00:00", false}
   };

   run_should_work_test(test_values_to_expectations);
}

TEST_F(LCWorkTimeTests, ShouldWorkCustomWorkTimeEndTimeInTheSameDay)
{
   cct->_conf->rgw_lifecycle_work_time = "14:00-16:00";

   std::unordered_map<std::string, bool> test_values_to_expectations = {
      {"01/01/23 00:00:00", false},
      {"01/01/23 12:00:00", false},
      {"01/01/24 13:59:59", false},
      {"01/01/23 14:00:00", true},
      {"01/01/23 16:00:00", true},
      {"01/01/23 16:00:59", true},
      {"01/01/23 16:01:00", false},
      {"01/01/23 17:00:00", false},
      {"01/01/23 23:59:59", false},
   };

   run_should_work_test(test_values_to_expectations);
}

TEST_F(LCWorkTimeTests, ShouldWorkCustomWorkTimeEndTimeInTheSameDay24Hours)
{
   cct->_conf->rgw_lifecycle_work_time = "00:00-23:59";

   std::unordered_map<std::string, bool> test_values_to_expectations = {
      {"01/01/23 23:59:00", true},
      {"01/01/23 23:59:59", true},
      {"01/01/23 00:00:00", true},
      {"01/01/23 00:00:01", true},
      {"01/01/23 00:01:00", true},
      {"01/01/23 01:00:00", true},
      {"01/01/23 12:00:00", true},
      {"01/01/23 17:00:00", true},
      {"01/01/23 23:00:00", true}
   };

   run_should_work_test(test_values_to_expectations);
}


TEST_F(LCWorkTimeTests, ShouldWorkCustomWorkTimeEndTimeInTheNextDay)
{
   cct->_conf->rgw_lifecycle_work_time = "14:00-01:00";

   std::unordered_map<std::string, bool> test_values_to_expectations = {
      {"01/01/23 13:59:00", false},
      {"01/01/23 13:59:59", false},
      {"01/01/24 14:00:00", true}, // used-to-fail
      {"01/01/24 17:00:00", true}, // used-to-fail
      {"01/01/24 23:59:59", true}, // used-to-fail
      {"01/01/23 00:00:00", true}, // used-to-fail
      {"01/01/23 00:59:59", true}, // used-to-fail
      {"01/01/23 01:00:00", true}, // used-to-fail
      {"01/01/23 01:00:59", true}, // used-to-fail
      {"01/01/23 01:01:00", false},
      {"01/01/23 05:00:00", false},
      {"01/01/23 12:00:00", false},
      {"01/01/23 13:00:00", false}
   };

   run_should_work_test(test_values_to_expectations);
}

TEST_F(LCWorkTimeTests, ShouldWorkCustomWorkTimeEndTimeInTheNextDay24Hours)
{
   cct->_conf->rgw_lifecycle_work_time = "14:00-13:59";

   // all of the below cases used-to-fail
   std::unordered_map<std::string, bool> test_values_to_expectations = {
      {"01/01/23 00:00:00", true},
      {"01/01/23 00:00:01", true},
      {"01/01/23 00:01:00", true},
      {"01/01/24 01:00:00", true},
      {"01/01/24 12:00:00", true},
      {"01/01/24 13:00:00", true},
      {"01/01/24 13:59:00", true},
      {"01/01/24 13:59:59", true},
      {"01/01/23 14:00:00", true},
      {"01/01/23 14:00:01", true},
      {"01/01/23 14:01:00", true},
      {"01/01/23 16:00:00", true},
      {"01/01/23 23:59:00", true},
      {"01/01/23 23:59:59", true},
   };

   run_should_work_test(test_values_to_expectations);
}

TEST_F(LCWorkTimeTests, ShouldWorkCustomWorkTimeEndTimeInTheNextDayIrregularMins)
{
   cct->_conf->rgw_lifecycle_work_time = "22:15-03:33";

   std::unordered_map<std::string, bool> test_values_to_expectations = {
      {"01/01/23 22:14:59", false},
      {"01/01/23 22:15:00", true}, // used-to-fail
      {"01/01/24 00:00:00", true}, // used-to-fail
      {"01/01/24 01:00:00", true}, // used-to-fail
      {"01/01/24 02:00:00", true}, // used-to-fail
      {"01/01/23 03:33:00", true}, // used-to-fail
      {"01/01/23 03:33:59", true}, // used-to-fail
      {"01/01/23 03:34:00", false},
      {"01/01/23 04:00:00", false},
      {"01/01/23 12:00:00", false},
      {"01/01/23 22:00:00", false},
   };

   run_should_work_test(test_values_to_expectations);
}

TEST_F(LCWorkTimeTests, ShouldWorkCustomWorkTimeStartEndSameHour)
{
   cct->_conf->rgw_lifecycle_work_time = "22:15-22:45";

   std::unordered_map<std::string, bool> test_values_to_expectations = {
      {"01/01/23 22:14:59", false},
      {"01/01/23 22:15:00", true},
      {"01/01/24 22:44:59", true},
      {"01/01/24 22:45:59", true},
      {"01/01/24 22:46:00", false},
      {"01/01/23 23:00:00", false},
      {"01/01/23 00:00:00", false},
      {"01/01/23 12:00:00", false},
      {"01/01/23 21:00:00", false},
   };

   run_should_work_test(test_values_to_expectations);
}

TEST_F(LCWorkTimeTests, ScheduleNextStartTime)
{
   cct->_conf->rgw_lifecycle_work_time = "22:15-03:33";

   // items of the map: [ (work_started_time, work_completed_time), expected_value (seconds) ]
   //
   // expected_value is the difference between configured start time (i.e, 22:15:00) and
   // the second item of the tuple (i.e., work_completed_time).
   //
   // Note that "seconds" of work completion time is taken into account but date is not relevant.
   // e.g., the first testcase: 75713 == 01:13:07 - 22:15:00 (https://tinyurl.com/ydm86752)
   std::map<std::tuple<std::string, std::string>, int> test_values_to_expectations = {
      {{"01/01/23 22:15:05", "01/01/23 01:13:07"}, 75713},
      {{"01/01/23 22:15:05", "01/02/23 01:13:07"}, 75713},
      {{"01/01/23 22:15:05", "01/01/23 22:17:07"}, 86273},
      {{"01/01/23 22:15:05", "01/02/23 22:17:07"}, 86273},
      {{"01/01/23 22:15:05", "01/01/23 22:14:00"}, 60},
      {{"01/01/23 22:15:05", "01/02/23 22:14:00"}, 60},
      {{"01/01/23 22:15:05", "01/01/23 22:15:00"}, 24 * 60 * 60},
      {{"01/01/23 22:15:05", "01/02/23 22:15:00"}, 24 * 60 * 60},
      {{"01/01/23 22:15:05", "01/01/23 22:15:01"}, 24 * 60 * 60 - 1},
      {{"01/01/23 22:15:05", "01/02/23 22:15:01"}, 24 * 60 * 60 - 1},
   };

   run_schedule_next_start_time_test(test_values_to_expectations);
}