File: 009-cgroup_set_values_recursive.cpp

package info (click to toggle)
libcgroup 3.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,764 kB
  • sloc: ansic: 14,997; cpp: 9,957; python: 8,340; sh: 5,194; yacc: 470; makefile: 400; lex: 38
file content (123 lines) | stat: -rw-r--r-- 2,695 bytes parent folder | download
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
/* SPDX-License-Identifier: LGPL-2.1-only */
/**
 * libcgroup googletest for cgroup_set_values_recursive()
 *
 * Copyright (c) 2020 Oracle and/or its affiliates.
 * Author: Tom Hromatka <tom.hromatka@oracle.com>
 */

#include <ftw.h>

#include "gtest/gtest.h"

#include "libcgroup-internal.h"

static const char * const PARENT_DIR = "test009cgroup/";

static const char * const NAMES[] = {
	"cpu.weight",
	"cpu.weight.nice",
	"cpu.foo",
	"cpu.bar"
};
static const int NAMES_CNT = sizeof(NAMES) / sizeof(NAMES[0]);

static const char * const VALUES[] = {
	"999",
	"15",
	"random",
	"data"
};
static const int VALUES_CNT = sizeof(VALUES) / sizeof(VALUES[0]);


class SetValuesRecursiveTest : public ::testing::Test {
	protected:

	void SetUp() override {
		char tmp_path[FILENAME_MAX];
		int ret, i;
		FILE *f;

		ASSERT_EQ(NAMES_CNT, VALUES_CNT);

		ret = mkdir(PARENT_DIR, S_IRWXU | S_IRWXG | S_IRWXO);
		ASSERT_EQ(ret, 0);

		for (i = 0; i < NAMES_CNT; i++) {
			memset(tmp_path, 0, sizeof(tmp_path));
			ret = snprintf(tmp_path, FILENAME_MAX - 1, "%s%s",
				       PARENT_DIR, NAMES[i]);
			ASSERT_GT(ret, 0);

			f = fopen(tmp_path, "w");
			fclose(f);
		}
	}

	/*
	 * https://stackoverflow.com/questions/5467725/how-to-delete-a-directory-and-its-contents-in-posix-c
	 */
	static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag,
		      struct FTW *ftwbuf)
	{
		return remove(fpath);
	}

	int rmrf(const char * const path)
	{
		return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
	}

	void TearDown() override {
		int ret;

		ret = rmrf(PARENT_DIR);
		ASSERT_EQ(ret, 0);
	}
};

TEST_F(SetValuesRecursiveTest, SuccessfulSetValues)
{
	char tmp_path[FILENAME_MAX], buf[4092];
	struct cgroup_controller ctrlr = {0};
	int ret, i;
	char *val;
	FILE *f;

	ret = snprintf(ctrlr.name, CONTROL_NAMELEN_MAX, "cpu");
	ASSERT_GT(ret, 0);

	for (i = 0; i < NAMES_CNT; i++) {
		ctrlr.values[i] = (struct control_value *)calloc(1,
					sizeof(struct control_value));
		ASSERT_NE(ctrlr.values[i], nullptr);

		strncpy(ctrlr.values[i]->name, NAMES[i], FILENAME_MAX);
		strncpy(ctrlr.values[i]->value, VALUES[i],
			CG_CONTROL_VALUE_MAX);
		if (i == 0)
			ctrlr.values[i]->dirty = true;
		else
			ctrlr.values[i]->dirty = false;
		ctrlr.index++;
	}

	ret = cgroup_set_values_recursive(PARENT_DIR, &ctrlr, false);
	ASSERT_EQ(ret, 0);

	for (i = 0; i < NAMES_CNT; i++) {
		memset(tmp_path, 0, sizeof(tmp_path));
		ret = snprintf(tmp_path, FILENAME_MAX - 1, "%s%s",
			       PARENT_DIR, NAMES[i]);
		ASSERT_GT(ret, 0);

		f = fopen(tmp_path, "r");
		ASSERT_NE(f, nullptr);

		val = fgets(buf, sizeof(buf), f);
		ASSERT_NE(val, nullptr);
		ASSERT_STREQ(buf, VALUES[i]);
		fclose(f);
	}
}