File: 008-cgroup_process_v2_mount.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 (191 lines) | stat: -rw-r--r-- 4,895 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
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
/* SPDX-License-Identifier: LGPL-2.1-only */
/**
 * libcgroup googletest for cgroup_process_v2_mnt()
 *
 * Copyright (c) 2020 Oracle and/or its affiliates.
 * Author: Tom Hromatka <tom.hromatka@oracle.com>
 */

#include <ftw.h>
#include <mntent.h>

#include "gtest/gtest.h"
#include "libcgroup-internal.h"

static const char * const PARENT_DIR = "test008cgroup";
static const char * const PARENT2_DIR = "test008cgroup2";
static const mode_t MODE = S_IRWXU | S_IRWXG | S_IRWXO;

static const char * const CONTROLLERS[] = {
	"cpuset",
	"cpu",
	"io",
	"memory",
	"pids",
	"rdma",
};
static const int CONTROLLERS_CNT =
	sizeof(CONTROLLERS) / sizeof(CONTROLLERS[0]);

static int mnt_tbl_idx = 0;

class CgroupProcessV2MntTest : public ::testing::Test {
	protected:

	void CreateHierarchy(const char * const dir)
	{
		char tmp_path[FILENAME_MAX];
		int i, ret;
		FILE *f;

		ret = mkdir(dir, MODE);
		ASSERT_EQ(ret, 0);

		memset(tmp_path, 0, sizeof(tmp_path));
		snprintf(tmp_path, FILENAME_MAX - 1, "%s/cgroup.controllers",
			 dir);

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

		for (i = 0; i < CONTROLLERS_CNT; i++)
			fprintf(f, "%s ", CONTROLLERS[i]);

		fclose(f);
	}

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

		CreateHierarchy(PARENT_DIR);

		/* make another directory to test the duplicate logic */
		CreateHierarchy(PARENT2_DIR);
	}

	/*
	 * 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 = 0;

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

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

TEST_F(CgroupProcessV2MntTest, AddV2Mount)
{
	char *mnt_dir = strdup(PARENT_DIR);
	struct mntent ent = (struct mntent) {
		.mnt_fsname = "cgroup2",
		.mnt_dir = mnt_dir,
		.mnt_type = "cgroup2",
		.mnt_opts = "rw,relatime,seclabel",
	};
	int ret;

	ret = cgroup_process_v2_mnt(&ent, &mnt_tbl_idx);

	ASSERT_EQ(ret, 0);
	ASSERT_EQ(mnt_tbl_idx, 7);
	ASSERT_STREQ(cg_mount_table[0].name, "cpuset");
	ASSERT_STREQ(cg_mount_table[1].name, "cpu");
	ASSERT_STREQ(cg_mount_table[2].name, "io");
	ASSERT_STREQ(cg_mount_table[3].name, "memory");
	ASSERT_STREQ(cg_mount_table[4].name, "pids");
	ASSERT_STREQ(cg_mount_table[5].name, "rdma");
	ASSERT_STREQ(cg_mount_table[6].name, "cgroup");

	ASSERT_STREQ(cg_mount_table[0].mount.path, ent.mnt_dir);
	ASSERT_STREQ(cg_mount_table[1].mount.path, ent.mnt_dir);
	ASSERT_STREQ(cg_mount_table[2].mount.path, ent.mnt_dir);
	ASSERT_STREQ(cg_mount_table[3].mount.path, ent.mnt_dir);
	ASSERT_STREQ(cg_mount_table[4].mount.path, ent.mnt_dir);
	ASSERT_STREQ(cg_mount_table[5].mount.path, ent.mnt_dir);
	ASSERT_STREQ(cg_mount_table[6].mount.path, ent.mnt_dir);
}

TEST_F(CgroupProcessV2MntTest, AddV2Mount_Duplicate)
{
	char *mnt_dir = strdup(PARENT2_DIR);
	struct mntent ent = (struct mntent) {
		.mnt_fsname = "cgroup2",
		.mnt_dir = mnt_dir,
		.mnt_type = "cgroup2",
		.mnt_opts = "rw,relatime,seclabel",
	};
	int ret;

	ret = cgroup_process_v2_mnt(&ent, &mnt_tbl_idx);

	ASSERT_EQ(ret, 0);
	ASSERT_EQ(mnt_tbl_idx, 7);
	ASSERT_STREQ(cg_mount_table[0].name, "cpuset");
	ASSERT_STREQ(cg_mount_table[1].name, "cpu");
	ASSERT_STREQ(cg_mount_table[2].name, "io");
	ASSERT_STREQ(cg_mount_table[3].name, "memory");
	ASSERT_STREQ(cg_mount_table[4].name, "pids");
	ASSERT_STREQ(cg_mount_table[5].name, "rdma");
	ASSERT_STREQ(cg_mount_table[6].name, "cgroup");

	ASSERT_STREQ(cg_mount_table[0].mount.next->path, ent.mnt_dir);
	ASSERT_STREQ(cg_mount_table[1].mount.next->path, ent.mnt_dir);
	ASSERT_STREQ(cg_mount_table[2].mount.next->path, ent.mnt_dir);
	ASSERT_STREQ(cg_mount_table[3].mount.next->path, ent.mnt_dir);
	ASSERT_STREQ(cg_mount_table[4].mount.next->path, ent.mnt_dir);
	ASSERT_STREQ(cg_mount_table[5].mount.next->path, ent.mnt_dir);
	ASSERT_STREQ(cg_mount_table[6].mount.next->path, ent.mnt_dir);
}

/*
 * This test must be last because it makes destructive changes to the cgroup hierarchy
 */
TEST_F(CgroupProcessV2MntTest, EmptyControllersFile)
{
	char tmp_path[FILENAME_MAX];
	char *mnt_dir = strdup(PARENT_DIR);
	struct mntent ent = (struct mntent) {
		.mnt_fsname = "cgroup2",
		.mnt_dir = mnt_dir,
		.mnt_type = "cgroup2",
		.mnt_opts = "rw,relatime,seclabel",
	};
	FILE *f;
	int ret;

	memset(tmp_path, 0, sizeof(tmp_path));
	snprintf(tmp_path, FILENAME_MAX - 1, "%s/cgroup.controllers",
		 PARENT_DIR);

	/* clear the cgroup.controllers file */
	f = fopen(tmp_path, "w");
	ASSERT_NE(f, nullptr);
	fclose(f);

	/* reset the mount table count */
	mnt_tbl_idx = 0;

	ret = cgroup_process_v2_mnt(&ent, &mnt_tbl_idx);

	ASSERT_EQ(ret, ECGEOF);
	ASSERT_EQ(mnt_tbl_idx, 0);
}