From a97c23c57c693b77724970d2f99702d7be18b4bc Mon Sep 17 00:00:00 2001 From: Mike Fleetwood Date: Wed, 17 Jul 2019 13:08:08 +0100 Subject: [PATCH] Add initial create ext2 only FileSystem interface class test (!49) This is the first step of adding testing of the derived FileSystem interface classes which call the file system specific executables. Rather than mocking command execution and returned output the tests run the real commands, effectively making this integration testing. Test case setup determines the file system supported actions using get_filesystem_support() and individual tests are skipped if a feature is not supported, just as GParted does for it's actions. Each test creates it's own sparse image file and a fresh file system, performs a test on one FileSystem interface call and deletes the image file. This makes each test independent and allows them to run as a non-root user, provided the file system command itself doesn't require root. Errors reported for a failed interface call will include the GParted OperationDetails, which in turn includes the file system specific command used and stdout and stderr from it's execution. For example, temporarily breaking the test code to create a 10 KiB image file instead of 256 MiB one produces this: $ ./test_ext2 Running main() from test_ext2.cc [==========] Running 1 test from 1 test case. [----------] Global test environment set-up. [----------] 1 test from ext2Test [ RUN ] ext2Test.Create test_ext2.cc:199: Failure Value of: s_ext2_obj->create(m_partition, m_operation_detail) Actual: false Expected: true Operation details: mkfs.ext2 -F -L '' '/home/centos/programming/c/gparted/tests/test_ext2.img' 00:00:00 (ERROR) mke2fs 1.42.9 (28-Dec-2013) /home/centos/programming/c/gparted/tests/test_ext2.img: Not enough space to build proposed filesystem while setting up superblock [ FAILED ] ext2Test.Create (25 ms) [----------] 1 test from ext2Test (25 ms total) [----------] Global test environment tear-down [==========] 1 test from 1 test case ran. (30 ms total) [ PASSED ] 0 tests. [ FAILED ] 1 test, listed below: [ FAILED ] ext2Test.Create 1 FAILED TEST $ echo $? 1 Also as Utils:: and FileSystem::execute_command() are needed, this requires linking with GParted_Core for GParted_Core::mainthread and therefore with most of the non-UI classes in gpartedbin. Closes !49 - Add file system interface tests --- .gitignore | 1 + .gitlab-ci.yml | 4 + tests/Makefile.am | 45 +++++++++ tests/test_ext2.cc | 221 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 271 insertions(+) create mode 100644 tests/test_ext2.cc diff --git a/.gitignore b/.gitignore index 6a366eb5..be0c6a20 100644 --- a/.gitignore +++ b/.gitignore @@ -56,3 +56,4 @@ tests/test_BlockSpecial tests/test_PasswordRAMStore tests/test_PipeCapture tests/test_dummy +tests/test_ext2 diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 65db2789..e670c64c 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,6 +10,8 @@ stages: - yum install -y which gnome-common yelp-tools glib2-devel intltool gcc-c++ libuuid-devel parted-devel gtkmm30-devel make polkit file + # Extra packages only needed during the test stage. + - yum install -y e2fsprogs - cat /etc/os-release .ubuntu_image_template: &ubuntu_image_definition @@ -20,6 +22,8 @@ stages: - apt-get install -y gnome-common yelp-tools libglib2.0-dev-bin uuid-dev libparted-dev libgtkmm-3.0-dev make policykit-1 + # Extra packages only needed during the test stage. + - apt-get install -y e2fsprogs - cat /etc/os-release .build_stage_template: &build_stage_definition diff --git a/tests/Makefile.am b/tests/Makefile.am index 3b49a5a9..7fd5e485 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -12,6 +12,7 @@ LDADD = \ # Programs to be built by "make check" check_PROGRAMS = \ test_dummy \ + test_ext2 \ test_BlockSpecial \ test_PasswordRAMStore \ test_PipeCapture @@ -21,6 +22,50 @@ TESTS = $(check_PROGRAMS) test_dummy_SOURCES = test_dummy.cc +test_ext2_SOURCES = test_ext2.cc +test_ext2_LDADD = \ + $(top_builddir)/src/BlockSpecial.$(OBJEXT) \ + $(top_builddir)/src/CopyBlocks.$(OBJEXT) \ + $(top_builddir)/src/DMRaid.$(OBJEXT) \ + $(top_builddir)/src/Device.$(OBJEXT) \ + $(top_builddir)/src/FS_Info.$(OBJEXT) \ + $(top_builddir)/src/FileSystem.$(OBJEXT) \ + $(top_builddir)/src/GParted_Core.$(OBJEXT) \ + $(top_builddir)/src/LUKS_Info.$(OBJEXT) \ + $(top_builddir)/src/LVM2_PV_Info.$(OBJEXT) \ + $(top_builddir)/src/Mount_Info.$(OBJEXT) \ + $(top_builddir)/src/Operation.$(OBJEXT) \ + $(top_builddir)/src/OperationCopy.$(OBJEXT) \ + $(top_builddir)/src/OperationDetail.$(OBJEXT) \ + $(top_builddir)/src/Partition.$(OBJEXT) \ + $(top_builddir)/src/PartitionLUKS.$(OBJEXT) \ + $(top_builddir)/src/PartitionVector.$(OBJEXT) \ + $(top_builddir)/src/PipeCapture.$(OBJEXT) \ + $(top_builddir)/src/Proc_Partitions_Info.$(OBJEXT) \ + $(top_builddir)/src/ProgressBar.$(OBJEXT) \ + $(top_builddir)/src/SWRaid_Info.$(OBJEXT) \ + $(top_builddir)/src/Utils.$(OBJEXT) \ + $(top_builddir)/src/btrfs.$(OBJEXT) \ + $(top_builddir)/src/exfat.$(OBJEXT) \ + $(top_builddir)/src/ext2.$(OBJEXT) \ + $(top_builddir)/src/f2fs.$(OBJEXT) \ + $(top_builddir)/src/fat16.$(OBJEXT) \ + $(top_builddir)/src/hfs.$(OBJEXT) \ + $(top_builddir)/src/hfsplus.$(OBJEXT) \ + $(top_builddir)/src/jfs.$(OBJEXT) \ + $(top_builddir)/src/linux_swap.$(OBJEXT) \ + $(top_builddir)/src/luks.$(OBJEXT) \ + $(top_builddir)/src/lvm2_pv.$(OBJEXT) \ + $(top_builddir)/src/minix.$(OBJEXT) \ + $(top_builddir)/src/nilfs2.$(OBJEXT) \ + $(top_builddir)/src/ntfs.$(OBJEXT) \ + $(top_builddir)/src/reiser4.$(OBJEXT) \ + $(top_builddir)/src/reiserfs.$(OBJEXT) \ + $(top_builddir)/src/udf.$(OBJEXT) \ + $(top_builddir)/src/xfs.$(OBJEXT) \ + $(GTEST_LIBS) \ + $(top_builddir)/lib/gtest/lib/libgtest.la + test_BlockSpecial_SOURCES = test_BlockSpecial.cc test_BlockSpecial_LDADD = \ $(top_builddir)/src/BlockSpecial.$(OBJEXT) \ diff --git a/tests/test_ext2.cc b/tests/test_ext2.cc new file mode 100644 index 00000000..f3da218c --- /dev/null +++ b/tests/test_ext2.cc @@ -0,0 +1,221 @@ +/* Copyright (C) 2019 Mike Fleetwood + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +/* Test ext2 + * + * Test the derived FileSystem interface classes which call the file system specific + * executables. Rather than mocking command execution and returned output just run real + * commands, effectively making this integration testing. + * + * Test case setup determines the file system supported actions using + * get_filesystem_support() and individual tests are skipped if a feature is not + * supported, just as GParted does for it's actions. + * + * Each test creates it's own sparse image file and a fresh file system, performs a test + * on one FileSystem interface call and deletes the image file. This makes each test + * independent and allows them to run as a non-root user, provided the file system command + * itself doesn't require root. Errors reported for a failed interface call will include + * the GParted OperationDetails, which in turn includes the file system specific command + * used and stdout and stderr from it's execution. + */ + + +#include "GParted_Core.h" +#include "FileSystem.h" +#include "OperationDetail.h" +#include "Partition.h" +#include "Utils.h" +#include "ext2.h" +#include "gtest/gtest.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +namespace GParted +{ + + +// Print method for OperationDetailStatus. +std::ostream& operator<<(std::ostream& out, const OperationDetailStatus od_status) +{ + switch (od_status) + { + case STATUS_NONE: out << "NONE"; break; + case STATUS_EXECUTE: out << "EXECUTE"; break; + case STATUS_SUCCESS: out << "SUCCESS"; break; + case STATUS_ERROR: out << "ERROR"; break; + case STATUS_INFO: out << "INFO"; break; + case STATUS_WARNING: out << "WARNING"; break; + default: break; + } + return out; +} + + +// Print method for an OperationDetail object. +std::ostream& operator<<(std::ostream& out, const OperationDetail& od) +{ + // FIXME: Strip markup from the printed description + out << od.get_description(); + Glib::ustring elapsed = od.get_elapsed_time(); + if (! elapsed.empty()) + out << " " << elapsed; + if (od.get_status() != STATUS_NONE) + out << " (" << od.get_status() << ")"; + out << "\n"; + + for (unsigned int i = 0; i < od.get_childs().size(); i++) + { + out << *od.get_childs()[i]; + } + return out; +} + + +// Google Test 1.8.1 (and earlier) doesn't implement run-time test skipping so implement +// our own for GParted run-time detected of unsupported file system features. +// Ref: +// Skipping tests at runtime with GTEST_SKIP() #1544 +// https://github.com/google/googletest/pull/1544 +// (Merged after Google Test 1.8.1) +#define SKIP_IF_FS_DOESNT_SUPPORT(opt) \ + if (s_ext2_support.opt != FS::EXTERNAL) \ + { \ + std::cout << __FILE__ << ":" << __LINE__ << ": Skip test. " \ + << "Not supported or support not found" << std::endl; \ + return; \ + } + + +class ext2Test : public ::testing::Test +{ +protected: + // Initialise top-level operation detail object with description ... + ext2Test() : m_operation_detail("Operation details:", STATUS_NONE) {}; + + virtual void extra_setup(); + virtual void TearDown(); + + static void SetUpTestCase(); + static void TearDownTestCase(); + + static FileSystem* s_ext2_obj; + static FS s_ext2_support; + static const char* s_image_name; + + Partition m_partition; + OperationDetail m_operation_detail; +}; + + +FileSystem* ext2Test::s_ext2_obj = NULL; +FS ext2Test::s_ext2_support; +const char* ext2Test::s_image_name = "test_ext2.img"; + + +void ext2Test::extra_setup() +{ + const Byte_Value ImageSize = 256*MEBIBYTE; + + // Create new 256M image file to work with. + unlink(s_image_name); + int fd = open(s_image_name, O_WRONLY|O_CREAT|O_NONBLOCK, 0666); + ASSERT_GE(fd, 0) << "Failed to create image file '" << s_image_name << "'. errno=" + << errno << "," << strerror(errno); + ASSERT_EQ(ftruncate(fd, (off_t)ImageSize), 0) << "Failed to set image file '" << s_image_name << "' to size " + << ImageSize << ". errno=" << errno << "," << strerror(errno); + close(fd); + + // Use libparted to get the sector size etc. of the image file. + PedDevice* lp_device = ped_device_get(s_image_name); + ASSERT_TRUE(lp_device != NULL); + + // Prepare partition object spanning whole of the image file. + m_partition.set_unpartitioned(s_image_name, + lp_device->path, + FS_EXT2, + lp_device->length, + lp_device->sector_size, + false); + + ped_device_destroy(lp_device); + lp_device = NULL; +} + + +void ext2Test::TearDown() +{ + unlink(s_image_name); +} + + +// Common test case initialisation creating ext2 interface object and querying supported +// operations. +void ext2Test::SetUpTestCase() +{ + s_ext2_obj = new ext2(FS_EXT2); + s_ext2_support = s_ext2_obj->get_filesystem_support(); +} + + +// Common test case teardown destroying the ext2 interface object. +void ext2Test::TearDownTestCase() +{ + delete s_ext2_obj; + s_ext2_obj = NULL; +} + + +TEST_F(ext2Test, Create) +{ + SKIP_IF_FS_DOESNT_SUPPORT(create); + extra_setup(); + // Call create, check for success and print operation details on failure. + ASSERT_TRUE(s_ext2_obj->create(m_partition, m_operation_detail)) << m_operation_detail; +} + + +} // namespace GParted + + +// Custom Google Test main(). +// Reference: +// * Google Test, Primer, Writing the main() function +// https://github.com/google/googletest/blob/master/googletest/docs/primer.md#writing-the-main-function +int main(int argc, char** argv) +{ + printf("Running main() from %s\n", __FILE__); + testing::InitGoogleTest(&argc, argv); + + // Initialise threading in GParted to allow FileSystem interface classes to + // successfully use Utils:: and Filesystem::execute_command(). + GParted::GParted_Core::mainthread = Glib::Thread::self(); + Gtk::Main gtk_main = Gtk::Main(); + + return RUN_ALL_TESTS(); +}