File: test_cgroupSubsystem_linux.cpp

package info (click to toggle)
openjdk-25 25.0.1%2B8-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 825,408 kB
  • sloc: java: 5,585,680; cpp: 1,333,948; xml: 1,321,242; ansic: 488,034; asm: 404,003; objc: 21,088; sh: 15,106; javascript: 13,265; python: 8,319; makefile: 2,518; perl: 357; awk: 351; pascal: 103; exp: 83; sed: 72; jsp: 24
file content (629 lines) | stat: -rw-r--r-- 26,584 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
 * Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code 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.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

#ifdef LINUX

#include "runtime/os.hpp"
#include "cgroupSubsystem_linux.hpp"
#include "cgroupUtil_linux.hpp"
#include "cgroupV1Subsystem_linux.hpp"
#include "cgroupV2Subsystem_linux.hpp"
#include "unittest.hpp"
#include "utilities/globalDefinitions.hpp"

#include <stdio.h>

// for basename
#include <libgen.h>

typedef struct {
  const char* mount_path;
  const char* root_path;
  const char* cgroup_path;
  const char* expected_path;
} TestCase;

// Utilities
static bool file_exists(const char* filename) {
  struct stat st;
  return os::stat(filename, &st) == 0;
}

// we rely on temp_file returning modifiable memory in resource area.
static char* temp_file(const char* prefix) {
  const testing::TestInfo* test_info = ::testing::UnitTest::GetInstance()->current_test_info();
  stringStream path;
  path.print_raw(os::get_temp_directory());
  path.print_raw(os::file_separator());
  path.print("%s-test-jdk.pid%d.%s.%s", prefix, os::current_process_id(),
             test_info->test_case_name(), test_info->name());
  return path.as_string(true);
}

static void delete_file(const char* filename) {
  if (!file_exists(filename)) {
    return;
  }
  int ret = remove(filename);
  EXPECT_TRUE(ret == 0 || errno == ENOENT) << "failed to remove file '" << filename << "': "
      << os::strerror(errno) << " (" << errno << ")";
}

class TestController : public CgroupController {
private:
  char* _path;
public:
  TestController(char* p): _path(p) {}
  const char* subsystem_path() override {
    return _path;
  };
  bool is_read_only() override {
    return true; // doesn't matter
  }
};

static void fill_file(const char* path, const char* content) {
  delete_file(path);
  FILE* fp = os::fopen(path, "w");
  if (fp == nullptr) {
    return;
  }
  if (content != nullptr) {
    fprintf(fp, "%s", content);
  }
  fclose(fp);
}

TEST(cgroupTest, read_numerical_key_value_failure_cases) {
  char* test_file = temp_file("cgroups");
  const char* b = basename(test_file);
  EXPECT_TRUE(b != nullptr) << "basename was null";
  stringStream path;
  path.print_raw(os::file_separator());
  path.print_raw(b);
  const char* base_with_slash = path.as_string(true);

  TestController* controller = new TestController((char*)os::get_temp_directory());
  constexpr julong bad = 0xBAD;
  julong x = bad;

  fill_file(test_file, "foo ");
  bool is_ok = controller->read_numerical_key_value(base_with_slash, "foo", &x);
  EXPECT_FALSE(is_ok) << "Value is missing in key/value case, expecting false";
  EXPECT_EQ(bad, x) << "x must be unchanged";

  x = bad;
  fill_file(test_file, "faulty_start foo 101");
  is_ok = controller->read_numerical_key_value(base_with_slash, "foo", &x);
  EXPECT_FALSE(is_ok) << "key must be at the start";
  EXPECT_EQ(bad, x) << "x must be unchanged";

  x = bad;
  fill_file(test_file, nullptr);
  is_ok = controller->read_numerical_key_value(base_with_slash, "foo", &x);
  EXPECT_FALSE(is_ok) << "key not in empty file";
  EXPECT_EQ(bad, x) << "x must be unchanged";

  x = bad;
  fill_file(test_file, "foo\n");
  is_ok = controller->read_numerical_key_value(base_with_slash, "foo", &x);
  EXPECT_FALSE(is_ok) << "key must have a value";
  EXPECT_EQ(bad, x) << "x must be unchanged";

  x = bad;
  fill_file(test_file, "foof 1002");
  is_ok = controller->read_numerical_key_value(base_with_slash, "foo", &x);
  EXPECT_FALSE(is_ok) << "key must be exact match";
  EXPECT_EQ(bad, x) << "x must be unchanged";

  // Cleanup
  delete_file(test_file);
}

TEST(cgroupTest, read_numerical_key_value_success_cases) {
  char* test_file = temp_file("cgroups");
  const char* b = basename(test_file);
  EXPECT_TRUE(b != nullptr) << "basename was null";
  stringStream path;
  path.print_raw(os::file_separator());
  path.print_raw(b);
  const char* base_with_slash = path.as_string(true);

  TestController* controller = new TestController((char*)os::get_temp_directory());
  constexpr julong bad = 0xBAD;
  julong x = bad;

  fill_file(test_file, "foo 100");
  bool is_ok = controller->read_numerical_key_value(base_with_slash, "foo", &x);
  EXPECT_TRUE(is_ok);
  EXPECT_EQ((julong)100, x);

  x = bad;
  fill_file(test_file, "foo\t111");
  is_ok = controller->read_numerical_key_value(base_with_slash, "foo", &x);
  EXPECT_TRUE(is_ok);
  EXPECT_EQ((julong)111, x);

  x = bad;
  fill_file(test_file, "foo\nbar 333\nfoo\t111");
  is_ok = controller->read_numerical_key_value(base_with_slash, "foo", &x);
  EXPECT_TRUE(is_ok);
  EXPECT_EQ((julong)111, x);

  x = bad;
  fill_file(test_file, "foof 100\nfoo 133");
  is_ok = controller->read_numerical_key_value(base_with_slash, "foo", &x);
  EXPECT_TRUE(is_ok);
  EXPECT_EQ((julong)133, x);

  x = bad;
  fill_file(test_file, "foo\t333\nfoot 999");
  is_ok = controller->read_numerical_key_value(base_with_slash, "foo", &x);
  EXPECT_TRUE(is_ok);
  EXPECT_EQ((julong)333, x);

  x = bad;
  fill_file(test_file, "foo 1\nfoo car");
  is_ok = controller->read_numerical_key_value(base_with_slash, "foo", &x);
  EXPECT_TRUE(is_ok);
  EXPECT_EQ((julong)1, x);

  // Cleanup
  delete_file(test_file);
}

TEST(cgroupTest, read_number_null) {
  TestController* null_path_controller = new TestController((char*)nullptr);
  const char* test_file_path = "/not-used";
  constexpr julong bad = 0xBAD;
  julong a = bad;
  // null subsystem_path() case
  bool is_ok = null_path_controller->read_number(test_file_path, &a);
  EXPECT_FALSE(is_ok) << "Null subsystem path should be an error";
  EXPECT_EQ(bad, a) << "Expected untouched scan value";
}

TEST(cgroupTest, read_string_beyond_max_path) {
  char larger_than_max[MAXPATHLEN + 1];
  for (int i = 0; i < (MAXPATHLEN); i++) {
    larger_than_max[i] = 'A' + (i % 26);
  }
  larger_than_max[MAXPATHLEN] = '\0';
  TestController* too_large_path_controller = new TestController(larger_than_max);
  const char* test_file_path = "/file-not-found";
  char foo[1024];
  foo[0] = '\0';
  bool is_ok = too_large_path_controller->read_string(test_file_path, foo, 1024);
  EXPECT_FALSE(is_ok) << "Too long path should be an error";
  EXPECT_STREQ("", foo) << "Expected untouched scan value";
}

TEST(cgroupTest, read_number_file_not_exist) {
  TestController* unknown_path_ctrl = new TestController((char*)"/do/not/exist");
  const char* test_file_path = "/file-not-found";
  constexpr julong bad = 0xBAD;
  julong result = bad;
  bool is_ok = unknown_path_ctrl->read_number(test_file_path, &result);
  EXPECT_FALSE(is_ok) << "File not found should be an error";
  EXPECT_EQ(bad, result) << "Expected untouched scan value";
}

TEST(cgroupTest, read_numerical_key_value_null) {
  TestController* null_path_controller = new TestController((char*)nullptr);
  const char* test_file_path = "/not-used";
  const char* key = "something";
  constexpr julong bad = 0xBAD;
  julong a = bad;
  // null subsystem_path() case
  bool is_ok = null_path_controller->read_numerical_key_value(test_file_path, key, &a);
  EXPECT_FALSE(is_ok) << "Null subsystem path should be an error";
  EXPECT_EQ(bad, a) << "Expected untouched scan value";
}

TEST(cgroupTest, read_number_tests) {
  char* test_file = temp_file("cgroups");
  const char* b = basename(test_file);
  constexpr julong bad = 0xBAD;
  EXPECT_TRUE(b != nullptr) << "basename was null";
  stringStream path;
  path.print_raw(os::file_separator());
  path.print_raw(b);
  const char* base_with_slash = path.as_string(true);
  fill_file(test_file, "8888");

  TestController* controller = new TestController((char*)os::get_temp_directory());
  julong foo = bad;
  bool ok = controller->read_number(base_with_slash, &foo);
  EXPECT_TRUE(ok) << "Number parsing should have been successful";
  EXPECT_EQ((julong)8888, foo) << "Wrong value for 'foo' (NOTE: 0xBAD == " << 0xBAD << ")";

  // Some interface files might have negative values, ensure we can read
  // them and manually cast them as needed.
  fill_file(test_file, "-1");
  foo = bad;
  ok = controller->read_number(base_with_slash, &foo);
  EXPECT_TRUE(ok) << "Number parsing should have been successful";
  EXPECT_EQ((jlong)-1, (jlong)foo) << "Wrong value for 'foo' (NOTE: 0xBAD == " << 0xBAD << ")";

  foo = bad;
  fill_file(test_file, nullptr);
  ok = controller->read_number(base_with_slash, &foo);
  EXPECT_FALSE(ok) << "Empty file should have failed";
  EXPECT_EQ(bad, foo) << "foo was altered";

  // Some interface files have numbers as well as the string
  // 'max', which means unlimited.
  jlong result = -10;
  fill_file(test_file, "max\n");
  ok = controller->read_number_handle_max(base_with_slash, &result);
  EXPECT_TRUE(ok) << "Number parsing for 'max' string should have been successful";
  EXPECT_EQ((jlong)-1, result) << "'max' means unlimited (-1)";

  result = -10;
  fill_file(test_file, "11114\n");
  ok = controller->read_number_handle_max(base_with_slash, &result);
  EXPECT_TRUE(ok) << "Number parsing for should have been successful";
  EXPECT_EQ((jlong)11114, result) << "Incorrect result";

  result = -10;
  fill_file(test_file, "-51114\n");
  ok = controller->read_number_handle_max(base_with_slash, &result);
  EXPECT_TRUE(ok) << "Number parsing for should have been successful";
  EXPECT_EQ((jlong)-51114, result) << "Incorrect result";

  delete_file(test_file);
}

TEST(cgroupTest, read_string_tests) {
  char* test_file = temp_file("cgroups");
  const char* b = basename(test_file);
  EXPECT_TRUE(b != nullptr) << "basename was null";
  stringStream path;
  path.print_raw(os::file_separator());
  path.print_raw(b);
  const char* base_with_slash = path.as_string(true);
  fill_file(test_file, "foo-bar");

  TestController* controller = new TestController((char*)os::get_temp_directory());
  char result[1024];
  bool ok = controller->read_string(base_with_slash, result, 1024);
  EXPECT_TRUE(ok) << "String parsing should have been successful";
  EXPECT_STREQ("foo-bar", result);

  result[0] = '\0';
  fill_file(test_file, "1234");
  ok = controller->read_string(base_with_slash, result, 1024);
  EXPECT_TRUE(ok) << "String parsing should have been successful";
  EXPECT_STREQ("1234", result);

  // values with a space
  result[0] = '\0';
  fill_file(test_file, "abc def");
  ok = controller->read_string(base_with_slash, result, 1024);
  EXPECT_TRUE(ok) << "String parsing should have been successful";
  EXPECT_STREQ("abc def", result);

  result[0] = '\0';
  fill_file(test_file, "  \na");
  ok = controller->read_string(base_with_slash, result, 1024);
  EXPECT_TRUE(ok) << "String parsing should have been successful";
  EXPECT_STREQ("  ", result);

  // only the first line are being returned
  result[0] = '\0';
  fill_file(test_file, "test\nabc");
  ok = controller->read_string(base_with_slash, result, 1024);
  EXPECT_TRUE(ok) << "String parsing should have been successful";
  EXPECT_STREQ("test", result);

  result[0] = '\0';
  fill_file(test_file, nullptr);
  ok = controller->read_string(base_with_slash, result, 1024);
  EXPECT_FALSE(ok) << "Empty file should have failed";
  EXPECT_STREQ("", result) << "Expected untouched result";
  delete_file(test_file);

  // File contents larger than 1K
  // We only read in the first 1K - 1 bytes
  const size_t large_len = 2 * 1024;
  char too_large[large_len];
  for (size_t i = 0; i < large_len; i++) {
    too_large[i] = 'A' + (i % 26);
  }
  too_large[large_len - 1] = '\0';
  result[0] = '\0';
  fill_file(test_file, too_large);
  ok = controller->read_string(base_with_slash, result, 1024);
  EXPECT_TRUE(ok) << "String parsing should have been successful";
  EXPECT_TRUE(1023 == strlen(result)) << "Expected only the first 1023 chars to be read in";
  EXPECT_EQ(0, strncmp(too_large, result, 1023));
  EXPECT_EQ(result[1023], '\0') << "The last character must be the null character";
}

TEST(cgroupTest, read_number_tuple_test) {
  char* test_file = temp_file("cgroups");
  const char* b = basename(test_file);
  EXPECT_TRUE(b != nullptr) << "basename was null";
  stringStream path;
  path.print_raw(os::file_separator());
  path.print_raw(b);
  const char* base_with_slash = path.as_string(true);
  fill_file(test_file, "max 10000");

  TestController* controller = new TestController((char*)os::get_temp_directory());
  jlong result = -10;
  bool ok = controller->read_numerical_tuple_value(base_with_slash, true /* use_first */, &result);
  EXPECT_TRUE(ok) << "Should be OK to read value";
  EXPECT_EQ((jlong)-1, result) << "max should be unlimited (-1)";

  result = -10;
  ok = controller->read_numerical_tuple_value(base_with_slash, false /* use_first */, &result);
  EXPECT_TRUE(ok) << "Should be OK to read the value";
  EXPECT_EQ((jlong)10000, result);

  // non-max strings
  fill_file(test_file, "abc 10000");
  result = -10;
  ok = controller->read_numerical_tuple_value(base_with_slash, true /* use_first */, &result);
  EXPECT_FALSE(ok) << "abc should not be parsable";
  EXPECT_EQ((jlong)-10, result) << "result value should be unchanged";

  fill_file(test_file, nullptr);
  result = -10;
  ok = controller->read_numerical_tuple_value(base_with_slash, true /* use_first */, &result);
  EXPECT_FALSE(ok) << "Empty file should be an error";
  EXPECT_EQ((jlong)-10, result) << "result value should be unchanged";
}

TEST(cgroupTest, read_numerical_key_beyond_max_path) {
  char larger_than_max[MAXPATHLEN + 1];
  for (int i = 0; i < (MAXPATHLEN); i++) {
    larger_than_max[i] = 'A' + (i % 26);
  }
  larger_than_max[MAXPATHLEN] = '\0';
  TestController* too_large_path_controller = new TestController(larger_than_max);
  const char* test_file_path = "/file-not-found";
  const char* key = "something";
  julong a = 0xBAD;
  bool is_ok = too_large_path_controller->read_numerical_key_value(test_file_path, key, &a);
  EXPECT_FALSE(is_ok) << "Too long path should be an error";
  EXPECT_EQ((julong)0xBAD, a) << "Expected untouched scan value";
}

TEST(cgroupTest, read_numerical_key_file_not_exist) {
  TestController* unknown_path_ctrl = new TestController((char*)"/do/not/exist");
  const char* test_file_path = "/file-not-found";
  const char* key = "something";
  julong a = 0xBAD;
  bool is_ok = unknown_path_ctrl->read_numerical_key_value(test_file_path, key, &a);
  EXPECT_FALSE(is_ok) << "File not found should be an error";
  EXPECT_EQ((julong)0xBAD, a) << "Expected untouched scan value";
}

TEST(cgroupTest, set_cgroupv1_subsystem_path) {
  TestCase host = {
    "/sys/fs/cgroup/memory",                                             // mount_path
    "/",                                                                 // root_path
    "/user.slice/user-1000.slice/user@1000.service",                     // cgroup_path
    "/sys/fs/cgroup/memory/user.slice/user-1000.slice/user@1000.service" // expected_path
  };
  TestCase container_engine = {
    "/sys/fs/cgroup/mem",                            // mount_path
    "/user.slice/user-1000.slice/user@1000.service", // root_path
    "/user.slice/user-1000.slice/user@1000.service", // cgroup_path
    "/sys/fs/cgroup/mem"                             // expected_path
  };
  TestCase container_moving_cgroup = {
    "/sys/fs/cgroup/cpu,cpuacct",                                            // mount_path
    "/system.slice/garden.service/garden/good/2f57368b-0eda-4e52-64d8-af5c", // root_path
    "/system.slice/garden.service/garden/bad/2f57368b-0eda-4e52-64d8-af5c",  // cgroup_path
    "/sys/fs/cgroup/cpu,cpuacct"                                             // expected_path
  };
  int length = 3;
  TestCase* testCases[] = { &host,
                            &container_engine,
                            &container_moving_cgroup };
  for (int i = 0; i < length; i++) {
    CgroupV1Controller* ctrl = new CgroupV1Controller( (char*)testCases[i]->root_path,
                                                       (char*)testCases[i]->mount_path,
                                                       true /* read-only mount */);
    ctrl->set_subsystem_path((char*)testCases[i]->cgroup_path);
    ASSERT_STREQ(testCases[i]->expected_path, ctrl->subsystem_path());
  }
}

TEST(cgroupTest, set_cgroupv1_subsystem_path_adjusted) {
  TestCase memory = {
    "/sys/fs/cgroup/memory", // mount_path
    "/",                     // root_path
    "../test1",              // cgroup_path
    "/sys/fs/cgroup/memory"  // expected_path
  };
  TestCase cpu = {
    "/sys/fs/cgroup/cpu", // mount_path
    "/",                  // root_path
    "../../test2",        // cgroup_path
    "/sys/fs/cgroup/cpu"  // expected_path
  };
  CgroupCpuController* ccc = new CgroupV1CpuController(CgroupV1Controller((char*)cpu.root_path,
                                                                          (char*)cpu.mount_path,
                                                                          true /* read-only mount */));
  ccc->set_subsystem_path((char*)cpu.cgroup_path);
  EXPECT_TRUE(ccc->needs_hierarchy_adjustment());

  CgroupUtil::adjust_controller(ccc);
  ASSERT_STREQ(cpu.expected_path, ccc->subsystem_path());
  EXPECT_FALSE(ccc->needs_hierarchy_adjustment());

  CgroupMemoryController* cmc = new CgroupV1MemoryController(CgroupV1Controller((char*)memory.root_path,
                                                                                (char*)memory.mount_path,
                                                                                true /* read-only mount */));
  cmc->set_subsystem_path((char*)memory.cgroup_path);
  EXPECT_TRUE(cmc->needs_hierarchy_adjustment());

  CgroupUtil::adjust_controller(cmc);
  ASSERT_STREQ(memory.expected_path, cmc->subsystem_path());
  EXPECT_FALSE(cmc->needs_hierarchy_adjustment());
}

TEST(cgroupTest, set_cgroupv2_subsystem_path_adjusted) {
  TestCase memory = {
    "/sys/fs/cgroup", // mount_path
    "/",              // root_path
    "../test1",       // cgroup_path
    "/sys/fs/cgroup"  // expected_path
  };
  TestCase cpu = {
    "/sys/fs/cgroup", // mount_path
    "/",              // root_path
    "../../test2",    // cgroup_path
    "/sys/fs/cgroup"  // expected_path
  };
  CgroupCpuController* ccc = new CgroupV2CpuController(CgroupV2Controller((char*)cpu.mount_path,
                                                                          (char*)cpu.cgroup_path,
                                                                          true /* read-only mount */));
  EXPECT_TRUE(ccc->needs_hierarchy_adjustment());

  CgroupUtil::adjust_controller(ccc);
  ASSERT_STREQ(cpu.expected_path, ccc->subsystem_path());
  EXPECT_FALSE(ccc->needs_hierarchy_adjustment());

  CgroupMemoryController* cmc = new CgroupV2MemoryController(CgroupV2Controller((char*)memory.mount_path,
                                                                                (char*)memory.cgroup_path,
                                                                                true /* read-only mount */));
  EXPECT_TRUE(cmc->needs_hierarchy_adjustment());

  CgroupUtil::adjust_controller(cmc);
  ASSERT_STREQ(memory.expected_path, cmc->subsystem_path());
  EXPECT_FALSE(cmc->needs_hierarchy_adjustment());
}

TEST(cgroupTest, set_cgroupv2_subsystem_path) {
  TestCase at_mount_root = {
    "/sys/fs/cgroup",       // mount_path
    nullptr,                // root_path, ignored
    "/",                    // cgroup_path
    "/sys/fs/cgroup"        // expected_path
  };
  TestCase sub_path = {
    "/sys/fs/cgroup",       // mount_path
    nullptr,                // root_path, ignored
    "/foobar",              // cgroup_path
    "/sys/fs/cgroup/foobar" // expected_path
  };
  int length = 2;
  TestCase* testCases[] = { &at_mount_root,
                            &sub_path };
  for (int i = 0; i < length; i++) {
    CgroupV2Controller* ctrl = new CgroupV2Controller( (char*)testCases[i]->mount_path,
                                                       (char*)testCases[i]->cgroup_path,
                                                       true /* read-only mount */);
    ASSERT_STREQ(testCases[i]->expected_path, ctrl->subsystem_path());
  }
}

TEST(cgroupTest, cgroupv2_is_hierarchy_walk_needed) {
  bool controller_read_only = false; // value irrelevant;
  CgroupV2Controller* test = new CgroupV2Controller((char*)"/sys/fs/cgroup",
                                                    (char*)"/" /* cgroup_path */,
                                                    controller_read_only);
  EXPECT_FALSE(test->needs_hierarchy_adjustment());
  test = new CgroupV2Controller((char*)"/sys/fs/cgroup",
                                (char*)"/bar" /* cgroup_path */,
                                controller_read_only);
  EXPECT_TRUE(test->needs_hierarchy_adjustment());
  test = new CgroupV2Controller((char*)"/sys/fs/cgroup/b",
                                (char*)"/a/b" /* cgroup_path */,
                                controller_read_only);
  EXPECT_TRUE(test->needs_hierarchy_adjustment());

  CgroupCpuController* test2 = new CgroupV2CpuController(CgroupV2Controller((char*)"/sys/fs/cgroup",
                                                                            (char*)"/" /* cgroup_path */,
                                                                            controller_read_only));
  EXPECT_FALSE(test2->needs_hierarchy_adjustment());
  test2 = new CgroupV2CpuController(CgroupV2Controller((char*)"/sys/fs/cgroup",
                                                       (char*)"/bar" /* cgroup_path */,
                                                       controller_read_only));
  EXPECT_TRUE(test2->needs_hierarchy_adjustment());
  test2 = new CgroupV2CpuController(CgroupV2Controller((char*)"/sys/fs/cgroup/b",
                                                       (char*)"/a/b" /* cgroup_path */,
                                                       controller_read_only));
  EXPECT_TRUE(test2->needs_hierarchy_adjustment());

  CgroupMemoryController* test3 = new CgroupV2MemoryController(CgroupV2Controller((char*)"/sys/fs/cgroup",
                                                                                  (char*)"/" /* cgroup_path */,
                                                                                  controller_read_only));
  EXPECT_FALSE(test3->needs_hierarchy_adjustment());
  test3 = new CgroupV2MemoryController(CgroupV2Controller((char*)"/sys/fs/cgroup",
                                                          (char*)"/bar" /* cgroup_path */,
                                                          controller_read_only));
  EXPECT_TRUE(test3->needs_hierarchy_adjustment());
  test3 = new CgroupV2MemoryController(CgroupV2Controller((char*)"/sys/fs/cgroup/b",
                                                          (char*)"/a/b" /* cgroup_path */,
                                                          controller_read_only));
  EXPECT_TRUE(test3->needs_hierarchy_adjustment());
}

TEST(cgroupTest, cgroupv1_is_hierarchy_walk_needed) {
  bool controller_read_only = true; // shouldn't matter;
  CgroupV1Controller* test = new CgroupV1Controller((char*)"/a/b/c" /* root */,
                                                    (char*)"/sys/fs/cgroup/memory" /* mount_path */,
                                                    controller_read_only);
  test->set_subsystem_path((char*)"/a/b/c");
  EXPECT_FALSE(test->needs_hierarchy_adjustment());
  test->set_subsystem_path((char*)"/");
  EXPECT_TRUE(test->needs_hierarchy_adjustment());
  test = new CgroupV1Controller((char*)"/a/b/c" /* root */,
                                (char*)"/"/* mount_path */,
                                controller_read_only);
  test->set_subsystem_path((char*)"/");
  EXPECT_TRUE(test->needs_hierarchy_adjustment());

  CgroupCpuController* test2 = new CgroupV1CpuController(CgroupV1Controller((char*)"/a/b/c" /* root */,
                                                                            (char*)"/sys/fs/cgroup/memory" /* mount_path */,
                                                                            controller_read_only));
  static_cast<CgroupV1CpuController*>(test2)->set_subsystem_path((char*)"/a/b/c");
  EXPECT_FALSE(test2->needs_hierarchy_adjustment());
  static_cast<CgroupV1CpuController*>(test2)->set_subsystem_path((char*)"/");
  EXPECT_TRUE(test2->needs_hierarchy_adjustment());
  test2 = new CgroupV1CpuController(CgroupV1Controller((char*)"/a/b/c" /* root */,
                                                       (char*)"/"/* mount_path */,
                                                       controller_read_only));
  static_cast<CgroupV1CpuController*>(test2)->set_subsystem_path((char*)"/");
  EXPECT_TRUE(test2->needs_hierarchy_adjustment());

  CgroupMemoryController* test3 = new CgroupV1MemoryController(CgroupV1Controller((char*)"/a/b/c" /* root */,
                                                                                  (char*)"/sys/fs/cgroup/memory" /* mount_path */,
                                                                                  controller_read_only));
  static_cast<CgroupV1MemoryController*>(test3)->set_subsystem_path((char*)"/a/b/c");
  EXPECT_FALSE(test3->needs_hierarchy_adjustment());
  static_cast<CgroupV1MemoryController*>(test3)->set_subsystem_path((char*)"/");
  EXPECT_TRUE(test3->needs_hierarchy_adjustment());
  test3 = new CgroupV1MemoryController(CgroupV1Controller((char*)"/a/b/c" /* root */,
                                                          (char*)"/"/* mount_path */,
                                                          controller_read_only));
  static_cast<CgroupV1MemoryController*>(test3)->set_subsystem_path((char*)"/");
  EXPECT_TRUE(test3->needs_hierarchy_adjustment());
}

#endif // LINUX