Add a unit test for v2_weight_to_v1().

[----------] 5 tests from CgroupV2WeightToV1
[ RUN      ] CgroupV2WeightToV1.NullWeightVal
[       OK ] CgroupV2WeightToV1.NullWeightVal (0 ms)
[ RUN      ] CgroupV2WeightToV1.EmptyWeightVal
[       OK ] CgroupV2WeightToV1.EmptyWeightVal (0 ms)
[ RUN      ] CgroupV2WeightToV1.ValidWeight1
[       OK ] CgroupV2WeightToV1.ValidWeight1 (0 ms)
[ RUN      ] CgroupV2WeightToV1.ValidWeight2
[       OK ] CgroupV2WeightToV1.ValidWeight2 (0 ms)
[ RUN      ] CgroupV2WeightToV1.ValidWeight3
[       OK ] CgroupV2WeightToV1.ValidWeight3 (0 ms)
[----------] 5 tests from CgroupV2WeightToV1 (0 ms total)

Signed-off-by: Tom Hromatka <tom.hroma...@oracle.com>
---
 gunit/017-v2_weight_to_v1.cpp | 91 +++++++++++++++++++++++++++++++++++
 gunit/Makefile.am             |  3 +-
 2 files changed, 93 insertions(+), 1 deletion(-)
 create mode 100644 gunit/017-v2_weight_to_v1.cpp

diff --git a/gunit/017-v2_weight_to_v1.cpp b/gunit/017-v2_weight_to_v1.cpp
new file mode 100644
index 000000000000..c8ccb3c55440
--- /dev/null
+++ b/gunit/017-v2_weight_to_v1.cpp
@@ -0,0 +1,91 @@
+/**
+ * libcgroup googletest for v2_weight_to_v1()
+ *
+ * Copyright (c) 2021 Oracle and/or its affiliates.
+ * Author: Tom Hromatka <tom.hroma...@oracle.com>
+ */
+
+/*
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This library 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses>.
+ */
+
+#include "gtest/gtest.h"
+#include "abstraction-common.h"
+#include "libcgroup-internal.h"
+
+class CgroupV2WeightToV1: public ::testing::Test {
+};
+
+TEST_F(CgroupV2WeightToV1, NullWeightVal)
+{
+       struct cgroup_controller cgc = {0};
+       char *weight_val = NULL;
+       int ret;
+
+       ret = v2_weight_to_v1(&cgc, weight_val);
+       ASSERT_EQ(ret, ECGINVAL);
+       ASSERT_EQ(weight_val, nullptr);
+       ASSERT_EQ(cgc.index, 0);
+}
+
+TEST_F(CgroupV2WeightToV1, EmptyWeightVal)
+{
+       struct cgroup_controller cgc = {0};
+       char *weight_val = "";
+       int ret;
+
+       ret = v2_weight_to_v1(&cgc, weight_val);
+       ASSERT_EQ(ret, 0);
+       ASSERT_EQ(cgc.index, 1);
+       ASSERT_STREQ(cgc.values[0]->name, "cpu.shares");
+       ASSERT_STREQ(cgc.values[0]->value, "");
+}
+
+TEST_F(CgroupV2WeightToV1, ValidWeight1)
+{
+       struct cgroup_controller cgc = {0};
+       char *weight_val = "100";
+       int ret;
+
+       ret = v2_weight_to_v1(&cgc, weight_val);
+       ASSERT_EQ(ret, 0);
+       ASSERT_EQ(cgc.index, 1);
+       ASSERT_STREQ(cgc.values[0]->name, "cpu.shares");
+       ASSERT_STREQ(cgc.values[0]->value, "1024\n");
+}
+
+TEST_F(CgroupV2WeightToV1, ValidWeight2)
+{
+       struct cgroup_controller cgc = {0};
+       char *weight_val = "400";
+       int ret;
+
+       ret = v2_weight_to_v1(&cgc, weight_val);
+       ASSERT_EQ(ret, 0);
+       ASSERT_EQ(cgc.index, 1);
+       ASSERT_STREQ(cgc.values[0]->name, "cpu.shares");
+       ASSERT_STREQ(cgc.values[0]->value, "4096\n");
+}
+
+TEST_F(CgroupV2WeightToV1, ValidWeight3)
+{
+       struct cgroup_controller cgc = {0};
+       char *weight_val = "50";
+       int ret;
+
+       ret = v2_weight_to_v1(&cgc, weight_val);
+       ASSERT_EQ(ret, 0);
+       ASSERT_EQ(cgc.index, 1);
+       ASSERT_STREQ(cgc.values[0]->name, "cpu.shares");
+       ASSERT_STREQ(cgc.values[0]->value, "512\n");
+}
diff --git a/gunit/Makefile.am b/gunit/Makefile.am
index 11a96f5f8c6a..caf19a04c24a 100644
--- a/gunit/Makefile.am
+++ b/gunit/Makefile.am
@@ -53,6 +53,7 @@ gtest_SOURCES = gtest.cpp \
                013-cgroup_build_tasks_procs_path.cpp \
                014-cgroupv2_get_subtree_control.cpp \
                015-cgroupv2_controller_enabled.cpp \
-               016-v1_shares_to_v2.cpp
+               016-v1_shares_to_v2.cpp \
+               017-v2_weight_to_v1.cpp
 gtest_LDFLAGS = -L$(top_builddir)/googletest/googletest -l:libgtest.so \
                -rpath $(abs_top_builddir)/googletest/googletest
-- 
2.26.2



_______________________________________________
Libcg-devel mailing list
Libcg-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to