def test_rebalance_with_quota_enabled(self):
        """
        Test rebalance with quota enabled on root.
        1. Create Volume of type distribute
        2. Set Quota limit on the root directory
        3. Do some IO to reach the Hard limit
        4. After IO ends, compute arequal checksum
        5. Add bricks to the volume.
        6. Start rebalance
        7. After rebalance is completed, check arequal checksum
        """
        # Enable Quota
        ret, _, _ = quota_enable(self.mnode, self.volname)
        self.assertEqual(
            ret, 0, ("Failed to enable quota on the volume %s", self.volname))
        g.log.info("Successfully enabled quota on volume %s", self.volname)

        # Set the Quota timeouts to 0 for strict accounting
        ret, _, _ = quota_set_hard_timeout(self.mnode, self.volname, 0)
        self.assertEqual(
            ret, 0, ("Failed to set hard-timeout to 0 for %s", self.volname))
        ret, _, _ = quota_set_soft_timeout(self.mnode, self.volname, 0)
        self.assertEqual(
            ret, 0, ("Failed to set soft-timeout to 0 for %s", self.volname))
        g.log.info("Quota soft and hard timeout has been set to 0 for %s",
                   self.volname)

        # Set the quota limit of 1 GB on root dir of the volume
        ret, _, _ = quota_limit_usage(self.mnode, self.volname, "/", "1GB")
        self.assertEqual(ret, 0, "Failed to set Quota for dir root")
        g.log.info("Successfully set quota limit for dir root")

        # Do some IO until hard limit is reached.
        cmd = ("/usr/bin/env python %s create_files "
               "-f 1024 --fixed-file-size 1M --base-file-name file %s" %
               (self.script_upload_path, self.mounts[0].mountpoint))
        proc = g.run_async(self.mounts[0].client_system,
                           cmd,
                           user=self.mounts[0].user)
        self.all_mounts_procs.append(proc)

        # Wait for IO to complete and validate IO
        self.assertTrue(
            wait_for_io_to_complete(self.all_mounts_procs, self.mounts[0]),
            "IO failed on some of the clients")
        g.log.info("IO completed on the clients")

        # Validate quota
        ret = quota_validate(self.mnode,
                             self.volname,
                             path='/',
                             hard_limit=1073741824,
                             sl_exceeded=True,
                             hl_exceeded=True)
        self.assertTrue(ret, "Quota validate Failed for '/'")
        g.log.info("Quota Validated for path '/'")

        # Compute arequal checksum.
        arequal_checksum_before_rebalance = collect_mounts_arequal(self.mounts)

        # Log Volume info and status before expanding volume.
        log_volume_info_and_status(self.mnode, self.volname)

        # Expand the volume.
        ret = expand_volume(self.mnode, self.volname, self.servers,
                            self.all_servers_info)
        self.assertTrue(ret, ("Failed to expand the volume %s", self.volname))
        g.log.info("Expanding volume is successful on "
                   "volume %s", self.volname)

        # Log volume info and status after expanding volume.
        log_volume_info_and_status(self.mnode, self.volname)

        # Perform rebalance start operation.
        ret, _, _ = rebalance_start(self.mnode, self.volname)
        self.assertEqual(ret, 0, ("Failed to  start rebalance on the volume "
                                  "%s", self.volname))
        g.log.info("Rebalance started.")

        # Check rebalance is in progress
        rebalance_status = get_rebalance_status(self.mnode, self.volname)
        ret = rebalance_status['aggregate']['statusStr']
        self.assertEqual(ret, "in progress", ("Rebalance is not in "
                                              "'in progress' state, either "
                                              "rebalance is in completed state"
                                              "  or failed to get rebalance "
                                              "status"))
        g.log.info("Rebalance is 'in progress' state")

        # Wait till rebalance ends.
        ret = wait_for_rebalance_to_complete(self.mnode, self.volname)
        self.assertTrue(ret, ("Rebalance is not yet complete on the volume "
                              "%s", self.volname))
        g.log.info("Rebalance is successfully complete on the volume %s",
                   self.volname)

        # Validate quota
        ret = quota_validate(self.mnode,
                             self.volname,
                             path='/',
                             hard_limit=1073741824,
                             sl_exceeded=True,
                             hl_exceeded=True)
        self.assertTrue(ret, "Quota validate Failed for '/'")
        g.log.info("Quota Validated for path '/'")

        # Compute arequal checksum.
        arequal_checksum_after_rebalance = collect_mounts_arequal(self.mounts)

        # Comparing arequals checksum before and after rebalance.
        self.assertEqual(arequal_checksum_before_rebalance,
                         arequal_checksum_after_rebalance,
                         "arequal checksum is NOT MATCHING")
        g.log.info("arequal checksum is SAME")
    def test_quota_add_brick(self):
        """
        Verifying quota functionality with respect to the
        add-brick without rebalance

        * Enable Quota
        * Set limit of 1GB on /
        * Mount the volume
        * Create some random amount of data inside each directory until quota
          is reached
        * Perform a quota list operation
        * Perform add-brick
        * Trying add files and see if quota is honored.
        """
        # Enable Quota
        g.log.info("Enabling quota on the volume %s", self.volname)
        ret, _, _ = quota_enable(self.mnode, self.volname)
        self.assertEqual(
            ret, 0, ("Failed to enable quota on the volume %s", self.volname))
        g.log.info("Successfully enabled quota on the volume %s", self.volname)

        # Path to set quota limit
        path = "/"

        # Set Quota limit on the root of the volume
        g.log.info("Set Quota Limit on the path %s of the volume %s", path,
                   self.volname)
        ret, _, _ = quota_limit_usage(self.mnode,
                                      self.volname,
                                      path=path,
                                      limit="1GB")
        self.assertEqual(ret, 0, ("Failed to set quota limit on path %s of "
                                  "the volume %s", path, self.volname))
        g.log.info("Successfully set the Quota limit on %s of the volume %s",
                   path, self.volname)

        # Set soft timeout to 0 second
        g.log.info("Set quota soft timeout:")
        ret, _, _ = quota_set_soft_timeout(self.mnode, self.volname, '0sec')
        self.assertEqual(ret, 0, ("Failed to set soft timeout"))
        g.log.info("Quota soft timeout set successful")

        # Set hard timeout to 0 second
        g.log.info("Set quota hard timeout:")
        ret, _, _ = quota_set_hard_timeout(self.mnode, self.volname, '0sec')
        self.assertEqual(ret, 0, ("Failed to set hard timeout"))
        g.log.info("Quota hard timeout set successful")

        mount_obj = self.mounts[0]
        mount_dir = mount_obj.mountpoint
        client = mount_obj.client_system

        # Create data inside each directory from mount point
        g.log.info("Creating Files on %s:%s", client, mount_dir)
        cmd = ("cd %s/ ; "
               "for i in `seq 100` ; "
               "do dd if=/dev/zero of=testfile1$i "
               "bs=10M "
               "count=1 ; "
               "done" % (mount_dir))
        ret, _, _ = g.run(client, cmd)
        self.assertEqual(ret, 0, ("Failed to create files"))
        g.log.info("Files created successfully")

        # Quota validate
        ret = quota_validate(self.mnode,
                             self.volname,
                             path=path,
                             hard_limit=1073741824,
                             sl_exceeded=True,
                             hl_exceeded=False)
        self.assertTrue(ret, "Quota validate Failed for /")

        # Add brick by forming the brick list
        # Form bricks list for add-brick command based on the voltype
        if 'replica_count' in self.volume['voltype']:
            new_bricks_count = self.volume['voltype']['replica_count']
        elif 'disperse_count' in self.volume['voltype']:
            new_bricks_count = self.volume['voltype']['disperse_count']
        else:
            new_bricks_count = 3
        bricks_list = form_bricks_list(self.mnode, self.volname,
                                       new_bricks_count, self.servers,
                                       self.all_servers_info)
        g.log.info("new brick list: %s", bricks_list)
        # Run add brick command
        ret, _, _ = add_brick(self.mnode, self.volname, bricks_list, False)
        self.assertEqual(ret, 0, "Failed to add the bricks to the volume")
        g.log.info("Successfully added bricks to volume")

        # Create data inside each directory from mount point
        g.log.info("Creating Files on %s:%s", client, mount_dir)
        cmd = ("cd %s/ ; "
               "for i in `seq 50` ; "
               "do dd if=/dev/zero of=testfile2$i "
               "bs=1M "
               "count=1 ; "
               "done" % (mount_dir))
        ret, _, _ = g.run(client, cmd)
        self.assertEqual(ret, 1, ("Failed: Files created successfully"))
        g.log.info("Quota limit honored")

        # Quota validate
        ret = quota_validate(self.mnode,
                             self.volname,
                             path=path,
                             hard_limit=1073741824,
                             sl_exceeded=True,
                             hl_exceeded=True)
        self.assertTrue(ret, "Quota validate Failed for /")
    def test_brick_removal_with_quota(self):
        """
        Test Brick removal with quota in place
        1. Create Volume of type distribute
        2. Set Quota limit on the directory
        3. Do some IO to reach the Hard limit
        4. After IO ends, remove bricks
        5. Quota validation should succeed.
        """
        # Enable Quota
        ret, _, _ = quota_enable(self.mnode, self.volname)
        self.assertEqual(
            ret, 0, ("Failed to enable quota on the volume 5s", self.volname))
        g.log.info("Successfully enabled quota on volume %s", self.volname)

        # Set the Quota timeouts to 0 for strict accounting
        ret, _, _ = quota_set_hard_timeout(self.mnode, self.volname, 0)
        self.assertEqual(
            ret, 0, ("Failed to set hard-timeout to 0 for %s", self.volname))
        ret, _, _ = quota_set_soft_timeout(self.mnode, self.volname, 0)
        self.assertEqual(
            ret, 0, ("Failed to set soft-timeout to 0 for %s", self.volname))
        g.log.info("Quota soft and hard timeout has been set to 0 for %s",
                   self.volname)

        # Set the quota limit of 100 MB on root dir of the volume
        ret, _, _ = quota_limit_usage(self.mnode, self.volname, "/", "100MB")
        self.assertEqual(ret, 0, "Failed to set Quota for dir root")
        g.log.info("Successfully set quota limit for dir root")

        # Do some IO until hard limit is reached.
        cmd = ("/usr/bin/env python %s create_files "
               "-f 100 --fixed-file-size 1M --base-file-name file %s" %
               (self.script_upload_path, self.mounts[0].mountpoint))
        proc = g.run_async(self.mounts[0].client_system,
                           cmd,
                           user=self.mounts[0].user)
        self.all_mounts_procs.append(proc)

        # Wait for IO to complete and validate IO
        self.assertTrue(
            wait_for_io_to_complete(self.all_mounts_procs, self.mounts[0]),
            "IO failed on some of the clients")
        g.log.info("IO completed on the clients")

        # Validate quota
        ret = quota_validate(self.mnode,
                             self.volname,
                             path='/',
                             hard_limit=104857600,
                             sl_exceeded=True,
                             hl_exceeded=True)
        self.assertTrue(ret, "Quota validate Failed for '/'")
        g.log.info("Quota Validated for path '/'")

        # Log Volume info and status before shrinking volume.
        log_volume_info_and_status(self.mnode, self.volname)

        # Shrink the volume.
        ret = shrink_volume(self.mnode, self.volname)
        self.assertTrue(ret, ("Failed to shrink volume on "
                              "volume %s", self.volname))
        g.log.info("Shrinking volume is successful on "
                   "volume %s", self.volname)

        # Log volume info and status after shrinking volume.
        log_volume_info_and_status(self.mnode, self.volname)

        # Perform rebalance start operation.
        ret, _, _ = rebalance_start(self.mnode, self.volname)
        self.assertEqual(ret, 0, ("Failed to  start rebalance on the volume "
                                  "%s", self.volname))
        g.log.info("Rebalance started.")

        # Wait till rebalance ends.
        ret = wait_for_rebalance_to_complete(self.mnode, self.volname)
        self.assertTrue(ret, ("Rebalance is not yet complete on the volume "
                              "%s", self.volname))
        g.log.info("Rebalance is successfully complete on the volume %s",
                   self.volname)

        # Validate quota
        ret = quota_validate(self.mnode,
                             self.volname,
                             path='/',
                             hard_limit=104857600,
                             sl_exceeded=True,
                             hl_exceeded=True)
        self.assertTrue(ret, "Quota validate Failed for '/'")
        g.log.info("Quota Validated for path '/'")
Esempio n. 4
0
    def test_quota_volume_subdir_limits(self):
        """
        Verifying directory quota functionality WRT limit-usage on volume
        as well as sub-directories in volume.

        * Enable quota
        * Set a limit of 1 GB on / of volume
        * Create 10 directories on mount point
        * Set a limit of 100 MB on all the sub-directories created
        * Create data inside the sub-directories on mount point till the limits
          are reached
        * Validate if the hard limit and available space fields inside the
          quota list command are appropriate
        """

        # Enable quota on the volume
        g.log.info("Enabling quota on the volume %s", self.volname)
        ret, _, _ = quota_enable(self.mnode, self.volname)
        self.assertEqual(
            ret, 0, ("Failed to enable quota on the volume %s", self.volname))
        g.log.info("Successfully enabled quota on the volume %s", self.volname)

        # Path to set quota limit
        path = "/"

        # Set a limit of 1 GB on the root of the volume
        g.log.info("Set Quota Limit on the path %s of the volume %s", path,
                   self.volname)
        ret, _, _ = quota_limit_usage(self.mnode,
                                      self.volname,
                                      path=path,
                                      limit="1GB")
        self.assertEqual(ret, 0, ("Failed to set quota limit on path %s of "
                                  "the volume %s", path, self.volname))
        g.log.info("Successfully set the Quota limit on %s of the volume %s",
                   path, self.volname)

        # Create 10 directories from the mount point
        mount_obj = self.mounts[0]
        mount_dir = mount_obj.mountpoint
        client = mount_obj.client_system

        g.log.info("Creating directories on %s:%s", client, mount_dir)
        for i in range(1, 11):
            ret = mkdir(client, "%s/foo%s" % (mount_dir, i))
            self.assertTrue(
                ret, ("Failed to create dir under %s-%s", client, mount_dir))
            g.log.info("Directory 'foo%s' created successfully", i)
        g.log.info("Successfully created directories on %s:%s", client,
                   mount_dir)

        # Set a limit of 100 MB on each directory
        g.log.info(
            "Setting a limit of 100 MB on all the directories inside "
            "the volume %s", self.volname)
        for j in range(1, 11):
            dir_name = "/foo" + str(j)
            ret, _, _ = quota_limit_usage(self.mnode,
                                          self.volname,
                                          path=dir_name,
                                          limit="100MB")
            self.assertEqual(ret, 0,
                             ("Failed to set quota limit on path "
                              "%s of the volume %s", dir_name, self.volname))
            g.log.info(
                "Successfully set the Quota limit on /foo%s of "
                "the volume %s", j, self.volname)
        g.log.info(
            "Successfully set the limit of 100 MB on all directories "
            "inside the volume %s", self.volname)

        # Validate if quota limit usage is set properly
        g.log.info("Validate quota limit usage on all directories")
        for k in range(1, 11):
            dir_name = "/foo" + str(k)
            ret = quota_validate(self.mnode,
                                 self.volname,
                                 path=dir_name,
                                 hard_limit=104857600)
            self.assertTrue(ret, ("Failed to validate quota limit usage on the"
                                  "directory %s", dir_name))
            g.log.info(
                "Successfully validated quota limit usage for the "
                "directory %s of volume %s", dir_name, self.volname)

        # Create data inside each directory from mount point
        g.log.info("Creating Files on %s:%s", client, mount_dir)
        for var1 in range(1, 11):
            cmd = ("cd %s/foo%s ; "
                   "for i in `seq 1 100` ; "
                   "do dd if=/dev/zero of=testfile$i "
                   "bs=1M "
                   "count=1 ; "
                   "done" % (mount_dir, var1))
            ret, _, _ = g.run(client, cmd)
            self.assertEqual(ret, 0,
                             ("Failed to create files in /foo%s", var1))
            g.log.info("Files created successfully in /foo%s", var1)
        g.log.info(
            "Files creation is successful on all directories of the "
            "volume %s", self.volname)

        # List the files inside each directory
        g.log.info("List all files and directories:")
        ret = list_all_files_and_dirs_mounts(self.mounts)
        self.assertTrue(ret, "Failed to list all files and dirs")
        g.log.info("Listing all files and directories is successful")

        # Validate the hard limit and available space fields are appropriate
        g.log.info("Validate quota hard limit and available space on all the "
                   "directories are appropriate")
        for var2 in range(1, 11):
            dir_name = "/foo" + str(var2)
            ret = quota_validate(self.mnode,
                                 self.volname,
                                 path=dir_name,
                                 hard_limit=104857600,
                                 avail_space=0,
                                 sl_exceeded=True,
                                 hl_exceeded=True,
                                 used_space=104857600)
            self.assertTrue(ret,
                            ("Failed to validate quota hard limit and "
                             "available space on the directory %s", dir_name))
            g.log.info(
                "Successfully validated quota hard limit and available"
                " space fields inside quota list for directory %s "
                "of volume %s", dir_name, self.volname)
    def test_alert_time_out(self):
        """
        Verifying directory quota functionality with respect to
        alert-time, soft-timeout and hard-timeout.

        * Enable quota
        * Set limit on '/'
        * Set default-soft-limit to 50%
        * Set alert time to 1 sec
        * Set soft-timeout to 0 sec
        * Set hard timeout to 0 sec
        * Check quota list
        * Perform some IO such that soft limit is not exceeded
        * Check for alert message in brick logfile (NO alert present)
        * Perform IO and exceed the soft limit
        * Check for alert message in brick logfile (Alert present)
        * Remove some files so that usage falls below soft limit
        * Create some files such that hard limit is exceeded
        * Check for alert message in brick logfile
        * Remove some files so that usage falls below soft limit
        * Check for alert message in brick logfile (NO alert present)

        """

        # pylint: disable=too-many-statements
        # Enable Quota
        g.log.info("Enabling quota on the volume %s", self.volname)
        ret, _, _ = quota_enable(self.mnode, self.volname)
        self.assertFalse(
            ret, "Failed to enable quota on the volume %s" % self.volname)
        g.log.info("Successfully enabled quota on the volume %s", self.volname)

        # Path to set quota limit
        path = "/"

        # Create a directory from mount point
        mount_obj = self.mounts[0]
        mount_dir = mount_obj.mountpoint
        client = mount_obj.client_system

        g.log.info("Creating dir named 'foo' from client %s", client)
        ret = mkdir(client, "%s/foo" % mount_dir)
        self.assertTrue(
            ret, "Failed to create dir under %s-%s" % (client, mount_dir))
        g.log.info("Directory 'foo' created successfully")

        # Set Quota limit on the root of the volume
        g.log.info("Set Quota Limit on the path %s of the volume %s", path,
                   self.volname)
        ret, _, _ = quota_limit_usage(self.mnode,
                                      self.volname,
                                      path=path,
                                      limit="100MB")
        self.assertEqual(ret, 0, ("Failed to set quota limit on path %s of "
                                  " the volume %s", path, self.volname))
        g.log.info("Successfully set the Quota limit on %s of the volume %s",
                   path, self.volname)

        # Set default soft limit to 50%
        g.log.info("Set default soft limit:")
        ret, _, _ = quota_set_default_soft_limit(self.mnode, self.volname,
                                                 '50%')
        self.assertEqual(ret, 0, ("Failed to set quota default soft limit"))
        g.log.info("Quota default soft limit set successfully")

        # Check quota list to validate limits
        g.log.info("List all files and directories:")
        ret = quota_validate(self.mnode,
                             self.volname,
                             path=path,
                             soft_limit_percent=50,
                             hard_limit=104857600)
        self.assertTrue(ret, "Failed to validate Quota list")
        g.log.info("Quota List successful")

        # Set alert time to 1 second
        g.log.info("Set quota alert timeout:")
        ret, _, _ = quota_set_alert_time(self.mnode, self.volname, '1sec')
        self.assertEqual(ret, 0, ("Failed to set alert timeout"))
        g.log.info("Quota alert time set successful")

        # Set soft timeout to 0 second
        g.log.info("Set quota soft timeout:")
        ret, _, _ = quota_set_soft_timeout(self.mnode, self.volname, '0sec')
        self.assertEqual(ret, 0, ("Failed to set soft timeout"))
        g.log.info("Quota soft timeout set successful")

        # Set hard timeout to 0 second
        g.log.info("Set quota hard timeout:")
        ret, _, _ = quota_set_hard_timeout(self.mnode, self.volname, '0sec')
        self.assertEqual(ret, 0, ("Failed to set hard timeout"))
        g.log.info("Quota hard timeout set successful")

        # Get the brick log file path for a random node
        bricks = get_all_bricks(self.mnode, self.volname)
        selected_node, brick_path = random.choice(bricks[0:6]).split(':')
        brickpath = str.replace(brick_path, '/', '-')
        brickpathfinal = brickpath[1:]
        brick_log = "/var/log/glusterfs/bricks/%s.log" % brickpathfinal

        # Append unique string to the brick log
        g.log.info("Appending string 'appended_string_1' to the log:")
        append_string_to_file(selected_node, brick_log, "appended_string_1")

        # Starting IO on the mounts without crossing the soft limit
        # file creation should be normal
        g.log.info("Creating Files on %s:%s", client, mount_dir)
        cmd = ("cd %s/foo ; "
               "for i in `seq 1 9` ; "
               "do dd if=/dev/urandom of=file$i "
               "bs=5M "
               "count=1 ; "
               "done" % mount_dir)
        ret, _, _ = g.run(client, cmd)
        self.assertEqual(ret, 0, ("Failed to create files on mountpoint"))
        g.log.info("Files created succesfully on mountpoint")

        # Append unique string to the brick log
        g.log.info("Appending string 'appended_string_2' to the log:")
        append_string_to_file(selected_node, brick_log, "appended_string_2")

        # Soft limit not crossed
        # Check if alert message is logged in the brick (shouldn't be logged)
        g.log.info("Check for alert message in logfile:")
        ret = search_pattern_in_file(selected_node, "120004", brick_log,
                                     "appended_string_1", "appended_string_2")
        self.assertFalse(ret, "Found unnecessary alert in logfile")
        g.log.info("Alert message not seen before crossing soft limit")

        # Continue IO on the mounts to exceed soft limit
        g.log.info("Creating Files on %s:%s", client, mount_dir)
        cmd = ("cd %s/foo ; "
               "for i in `seq 1 200` ; "
               "do dd if=/dev/urandom of=foo$i "
               "bs=100K "
               "count=1 ; "
               "done" % mount_dir)
        ret, _, _ = g.run(client, cmd)
        self.assertEqual(ret, 0, ("Failed to create files on mountpoint"))
        g.log.info("Files created succesfully on mountpoint and "
                   "exceed soft limit")

        # Check if quota soft limit exceeded
        g.log.info("Check soft limit:")
        ret = quota_validate(self.mnode,
                             self.volname,
                             path=path,
                             sl_exceeded=True)
        self.assertTrue(ret, "Failed: soft limit not exceeded")
        g.log.info('Quota soft limit exceeded')

        # Inserting sleep of 2 seconds so the alert message gets enough time
        # to be logged
        time.sleep(2)

        # Append unique string to the brick log
        g.log.info("Appending string 'appended_string_3' to the log:")
        append_string_to_file(selected_node, brick_log, "appended_string_3")

        # Check if alert message logged in the brick
        g.log.info("Check for message:")
        ret = search_pattern_in_file(selected_node, "120004", brick_log,
                                     "appended_string_2", "appended_string_3")
        self.assertTrue(ret, "Alert message not found")
        g.log.info("Pattern Found: got alert message")

        # Append unique string to the brick log
        g.log.info("Appending string 'appended_string_4' to the log:")
        append_string_to_file(selected_node, brick_log, "appended_string_4")

        # Continue IO on the mounts by removing data
        g.log.info("Removing Files on %s:%s", client, mount_dir)
        cmd = ("rm -rfv %s/foo/foo* " % mount_dir)
        ret, _, _ = g.run(client, cmd)
        self.assertEqual(ret, 0, ("Failed to delete files on mountpoint"))
        g.log.info("Files removed succesfully from mountpoint and reached "
                   "below soft limit")

        # Check if quota soft limit exceeded
        g.log.info("Check soft limit:")
        ret = quota_validate(self.mnode,
                             self.volname,
                             path=path,
                             sl_exceeded=False)
        self.assertTrue(ret, "Failed: soft limit exceeded")
        g.log.info('Quota soft limit not exceeded')

        # Inserting sleep of 2 seconds so the alert message gets enough time
        # to be logged
        time.sleep(2)

        # Append unique string to the brick log
        g.log.info("Appending string 'appended_string_5' to the log:")
        append_string_to_file(selected_node, brick_log, "appended_string_5")

        # Check if alert message is logged in the brick
        g.log.info("Check for message:")
        ret = search_pattern_in_file(selected_node, "120004", brick_log,
                                     "appended_string_4", "appended_string_5")
        self.assertFalse(ret, "Found unnecessary alert message in logfile")
        g.log.info("Alert message not seen before crossing soft limit")

        # Continue IO on the mounts to exceed hard limit
        g.log.info("Creating Files on %s:%s", client, mount_dir)
        cmd = ("cd %s/foo ; "
               "for i in `seq 11 20` ; "
               "do dd if=/dev/urandom of=file$i "
               "bs=10M "
               "count=1 ; "
               "done" % mount_dir)
        ret, _, _ = g.run(client, cmd)
        self.assertEqual(ret, 1, ("Failed: Files created successfully inspite "
                                  "of crossing hard-limit"))
        g.log.info("Files creation stopped on mountpoint once exceeded "
                   "hard limit")

        # Inserting sleep of 6 seconds so the alert message gets enough time
        # to be logged
        time.sleep(6)

        # Append unique string to the brick log
        g.log.info("Appending string 'appended_string_6' to the log:")
        append_string_to_file(selected_node, brick_log, "appended_string_6")

        # Check if alert message is logged in the brick
        g.log.info("Check for message:")
        ret = search_pattern_in_file(selected_node, "120004", brick_log,
                                     "appended_string_5", "appended_string_6")
        self.assertTrue(ret, "Alert message not seen in logfile")
        g.log.info("Pattern Found: got alert message")

        # Append unique string to the brick log
        g.log.info("Appending string 'Done_with_alert_check_7' to the log:")
        append_string_to_file(selected_node, brick_log,
                              "Done_with_alert_check_7")

        # Continue IO on the mounts by removing data to come below hard limit
        g.log.info("Removing Files on %s:%s", client, mount_dir)
        cmd = ("rm -rfv %s/foo/file{11..20}" % mount_dir)
        ret, _, _ = g.run(client, cmd)
        self.assertEqual(ret, 0, ("Failed to delete files on mountpoint"))
        g.log.info("Files removed succesfully on mountpoint and "
                   "reached below soft limit")

        # Inserting sleep of 2 seconds so the alert message gets enough time
        # to be logged
        time.sleep(2)

        # Append unique string to the brick log
        g.log.info("Appending string 'Done_with_alert_check_8' to the log:")
        append_string_to_file(selected_node, brick_log,
                              "Done_with_alert_check_8")

        # Check if alert message is logged in the brick
        g.log.info("Check for message:")
        ret = search_pattern_in_file(selected_node, "120004", brick_log,
                                     "Done_with_alert_check_7",
                                     "Done_with_alert_check_8")
        self.assertFalse(ret, "Found unnecessary alert in logfile")
        g.log.info("EXPECTED: Alert message not seen before crossing "
                   "soft limit")

        # have got below the soft and hard limit
        # check quota list
        g.log.info("List all files and directories:")
        ret = quota_validate(self.mnode,
                             self.volname,
                             path=path,
                             sl_exceeded=False,
                             hl_exceeded=False)
        self.assertTrue(
            ret, "Failed to validate Quota list with "
            "soft and hard limits")
        g.log.info("Quota List validated successfully")
    def test_quota_file_larger_than_limit(self):
        # pylint: disable=too-many-statements
        """
        Verifying directory Quota functionality with respect to the
        limit-usage option.

        If a limit is set and a file of size larger than limit is created
        then the file creation will stop when it will reach the limit.

        Quota list will show limit-set and size as same.

        * Enable Quota
        * Create a directory from mount point
        * Set a limit of 10 MB on the directory
        * Set Quota soft-timeout and hard-timeout to 0 seconds
        * Create a file of size larger than the Quota limit
          eg. 20 MB file
        * Perform Quota list operation to check if all the fields are
          appropriate such as hard_limit, available_space, sl_exceeded,
          hl_execeeded, etc.
        """
        # Enable Quota
        g.log.info("Enabling Quota on the volume %s", self.volname)
        ret, _, _ = quota_enable(self.mnode, self.volname)
        self.assertEqual(
            ret, 0, ("Failed to enable Quota on the volume %s", self.volname))
        g.log.info("Successfully enabled Quota on the volume %s", self.volname)

        # Path to set the Quota limit
        path = '/foo'

        # Create a directory 'foo' from the mount point
        mount_obj = self.mounts[0]
        mount_dir = mount_obj.mountpoint
        client = mount_obj.client_system

        g.log.info("Creating dir named 'foo' from client %s", client)
        ret = mkdir(client, "%s/foo" % mount_dir)
        self.assertTrue(
            ret, "Failed to create dir under %s-%s" % (client, mount_dir))
        g.log.info("Directory 'foo' created successfully")

        # Set Quota limit of 10 MB on the directory 'foo' of the volume
        g.log.info("Set Quota Limit on the path %s of the volume %s", path,
                   self.volname)
        ret, _, _ = quota_limit_usage(self.mnode,
                                      self.volname,
                                      path=path,
                                      limit="10MB")
        self.assertEqual(ret, 0, ("Failed to set Quota limit on path %s of "
                                  "the volume %s", path, self.volname))
        g.log.info("Successfully set the Quota limit on %s of the volume %s",
                   path, self.volname)

        # Set Quota soft-timeout to 0 seconds
        g.log.info("Set Quota soft timeout:")
        ret, _, _ = quota_set_soft_timeout(self.mnode, self.volname, '0sec')
        self.assertEqual(ret, 0, ("Failed to set soft timeout"))
        g.log.info("Quota soft timeout set successful")

        # Set Quota hard-timeout to 0 second
        g.log.info("Set Quota hard timeout:")
        ret, _, _ = quota_set_hard_timeout(self.mnode, self.volname, '0sec')
        self.assertEqual(ret, 0, ("Failed to set hard timeout"))
        g.log.info("Quota hard timeout set successful")

        # Validate if the Quota limit set is appropriate
        g.log.info(
            "Validate if the Quota limit set is correct for the "
            "directory %s of the volume %s", path, self.volname)
        ret = quota_validate(self.mnode,
                             self.volname,
                             path=path,
                             hard_limit=10485760)
        self.assertTrue(
            ret, ("Quota Limit of 10 MB was not set properly on "
                  "the directory %s of the volume %s", path, self.volname))
        g.log.info(
            "Successfully Validated Quota Limit of 10 MB is set on the"
            " directory %s of the volume %s", path, self.volname)

        # Create a single file of size 20 MB
        g.log.info("Creating Files on %s:%s", client, mount_dir)
        cmd = ("cd %s/foo ; "
               "dd if=/dev/zero of=20MBfile "
               "bs=1M "
               "count=20" % mount_dir)
        ret, _, _ = g.run(client, cmd)
        self.assertEqual(
            ret, 1, "Unexpected: File creation succeeded even "
            "after exceeding the hard-limit")
        g.log.info("Expected: File creation failed after exceeding "
                   "hard-limit")

        # List all files and dirs created
        g.log.info("List all files and directories:")
        ret = list_all_files_and_dirs_mounts(self.mounts)
        self.assertTrue(ret, "Failed to list all files and dirs")
        g.log.info("Listing all files and directories is successful")

        # Check if the file created above exists
        g.log.info("Checking if the file created exists in the volume %s",
                   self.volname)
        ret = file_exists(client, "%s/foo/20MBfile" % mount_dir)
        self.assertTrue(ret,
                        ("File does not exist in the volume %s", self.volname))
        g.log.info(
            "Successfully validated the presence of file in the "
            "volume %s", self.volname)

        # Validate if the Quota limit set is appropriate
        g.log.info(
            "Validate if the Quota list fields are appropriate for the "
            "directory %s of the volume %s", path, self.volname)
        ret = quota_validate(self.mnode,
                             self.volname,
                             path=path,
                             hard_limit=10485760,
                             avail_space=0,
                             sl_exceeded=True,
                             hl_exceeded=True)
        self.assertTrue(ret, ("Failed to validate the Quota limits on "
                              "the volume %s", self.volname))
        g.log.info(
            "Successfully Validated Quota Limit of 100 MB is set on the"
            " directory %s of the volume %s", path, self.volname)
Esempio n. 7
0
    def test_quota_with_renamed_dir(self):
        """
        Verifying directory quota functionality with respect to
        the limit-usage option.
        If a directory has limit set on it and the same directory is renamed ,
        then on doing a quota list the changed name should be reflected.

        * Enable quota on volume
        * Create a directory 'foo' from client
        * Set quota limit of 1GB on /foo
        * Check if quota limit set is correct
        * Rename directory 'foo' to 'bar' from client
        * Check if quota limit set on 'bar' is same as before
        """

        # Enable Quota on the volume
        g.log.info("Enabling Quota on the volume %s", self.volname)
        ret, _, _ = quota_enable(self.mnode, self.volname)
        self.assertFalse(ret,
                         "Failed to enable Quota on volume %s" % self.volname)

        # Create a directory named 'foo' under any mount dir
        mount_obj = self.mounts[0]
        mount_dir = mount_obj.mountpoint
        client = mount_obj.client_system

        g.log.info("Creating dir named 'foo' from client %s", client)
        ret = mkdir(client, "%s/foo" % mount_dir)
        self.assertTrue(
            ret, "Failed to create dir under %s-%s" % (client, mount_dir))
        g.log.info("Directory 'foo' created successfully")

        # Set Quota Limit of 1GB for dir foo
        g.log.info(
            "Setting a quota limit of 1GB on directory 'foo' inside "
            "volume %s", self.volname)
        ret, _, _ = quota_limit_usage(self.mnode, self.volname, "/foo", '1GB')
        self.assertFalse(ret, "Failed to set Quota for dir '/foo'")
        g.log.info("Set quota for dir '/foo' successfully")

        # Get the Quota list and check '/foo' has Quota Limit of 1GB
        g.log.info(
            "Validating if the Quota limit set is correct for the "
            "path '/foo' in volume %s", self.volname)
        ret = quota_validate(self.mnode,
                             self.volname,
                             path="/foo",
                             hard_limit=1073741824)
        self.assertTrue(ret, ("Quota Limit of 1GB was not set properly on the "
                              "path  /foo' in volume %s", self.volname))
        g.log.info(
            "Successfully Validated Quota Limit of 1GB is set on the "
            "path '/foo' in volume %s", self.volname)

        # Rename the dir foo to bar
        g.log.info("Renaming dir named 'foo' to 'bar' from client %s", client)
        ret = move_file(client, "%s/foo" % (mount_dir), "%s/bar" % (mount_dir))
        self.assertTrue(
            ret, "Failed to rename the directory 'foo' under "
            "%s-%s" % (client, mount_dir))
        g.log.info("Renamed the directory 'foo' to 'bar' successfully")

        # Again get the quota list to check if directory /bar is present
        g.log.info(
            "Validating if the Quota limit set is correct for the "
            "path '/bar' in volume %s", self.volname)
        ret = quota_validate(self.mnode,
                             self.volname,
                             path="/bar",
                             hard_limit=1073741824)
        self.assertTrue(ret, ("Failed to validate quota limit on the directory"
                              " 'bar'"))
        g.log.info(
            "Successfully Validated Quota Limit of 1GB is set on the "
            "path '/bar' in volume %s", self.volname)
Esempio n. 8
0
    def test_quota_limit_dir_breadth(self):
        """
        Verifying directory quota functionality with respect to the
        limit-usage option. Set limits on various directories [breadth]
        and check for the quota list of all the directories.

        * Enable Quota
        * Create 10 directories and set limit of 1GB on each directory
        * Perform a quota list operation
        * Create some random amount of data inside each directory
        * Perform a quota list operation
        """
        # Enable Quota
        g.log.info("Enabling quota on the volume %s", self.volname)
        ret, _, _ = quota_enable(self.mnode, self.volname)
        self.assertEqual(
            ret, 0, ("Failed to enable quota on the volume %s", self.volname))
        g.log.info("Successfully enabled quota on the volume %s", self.volname)

        # Create Directories in the mount point
        mount_obj = self.mounts[0]
        mount_dir = mount_obj.mountpoint
        client = mount_obj.client_system

        g.log.info("Creating Directories on %s:%s", client, mount_dir)
        cmd = ('python %s create_deep_dir -d 0 -l 10 %s' %
               (self.script_upload_path, mount_dir))

        proc = g.run_async(client, cmd, user=mount_obj.user)
        self.all_mounts_procs.append(proc)

        # Validate IO
        self.assertTrue(validate_io_procs(self.all_mounts_procs, self.mounts),
                        "IO failed on some of the clients")

        # Get dir list
        g.log.info('Getting dir list in %s', self.volname)
        cmd = ('ls %s' % mount_dir)
        ret, out, err = g.run(client, cmd)
        self.assertFalse(ret, err)
        dir_list = out.split()
        for dir_name in dir_list:
            dir_list[dir_list.index(dir_name)] = '/%s' % dir_name

        # Set limit of 1 GB on every directory created inside the mountpoint
        g.log.info("Set Quota Limit on each directory of the volume %s",
                   self.volname)
        for dir_name in dir_list:
            ret, _, _ = quota_limit_usage(self.mnode, self.volname, dir_name,
                                          '1GB')
            self.assertFalse(ret, "Failed to set Quota for dir %s" % dir_name)
            g.log.info("Set quota for dir %s successfully", dir_name)
        g.log.info(
            "Successfully set the Quota limit on each path of the "
            "volume %s", self.volname)

        # Get Quota List and Verify Hard-Limit on each Directory of the Volume
        g.log.info(
            "Get Quota list and validate the hardlimit for every"
            " directory on the volume %s", self.volname)
        for dir_name in dir_list:
            ret = quota_validate(self.mnode,
                                 self.volname,
                                 path=dir_name,
                                 hard_limit=1073741824)
            self.assertTrue(
                ret,
                ("Failed to validate quota limit usage on the"
                 " directory %s of the volume %s", dir_name, self.volname))
            g.log.info(
                "Hard limit matches the actual limit-usage "
                "set on the directory %s", dir_name)

        # Create some data inside each directory
        self.all_mounts_procs = []
        g.log.info("Creating Files on %s:%s", client, mount_dir)
        for i in range(1, 11):
            dir_name = "/user" + str(i)
            cmd = ("python %s create_files -f 10 --fixed-file-size 1M "
                   "%s/%s" % (self.script_upload_path, mount_dir, dir_name))

            ret, _, _ = g.run(client, cmd)
            self.assertFalse(ret, "Failed to create files in %s" % dir_name)
            g.log.info("Files created successfully in %s", dir_name)

        # Get Quota List and Verify Hard-Limit on each Directory of the Volume
        g.log.info(
            "Get Quota list and validate the hardlimit for every"
            " directory on the volume %s", self.volname)
        for dir_name in dir_list:
            ret = quota_validate(self.mnode,
                                 self.volname,
                                 path=dir_name,
                                 hard_limit=1073741824)
            self.assertTrue(ret,
                            ("Hard limit does not match the actual "
                             "limit-usage set on the directory %s" % dir_name))
            g.log.info(
                "Hard limit matches the actual limit-usage "
                "set on the directory %s", dir_name)
Esempio n. 9
0
    def test_quota_unique_soft_limit(self):
        """
        Validating directory quota functionality WRT soft-limit

        * Enable quota
        * Create 10 directories
        * Set a hard-limit of 100 MB and a unique soft-limit on each directory
          example : 5%, 15%, 25%, 35%, ...
        * Create some data inside the directories such that the
          soft-limit is exceeded
        * Perform quota list operation to validate if the
          soft-limit is exceeded
        """

        # pylint: disable=too-many-statements
        # Enable Quota on the volume
        g.log.info("Enabling quota on the volume %s", self.volname)
        ret, _, _ = quota_enable(self.mnode, self.volname)
        self.assertEqual(
            ret, 0, ("Failed to enable quota on the volume %s", self.volname))
        g.log.info("Successfully enabled quota on the volume %s", self.volname)

        # Set soft timeout to 0 second
        g.log.info("Set quota soft timeout:")
        ret, _, _ = quota_set_soft_timeout(self.mnode, self.volname, '0sec')
        self.assertEqual(ret, 0, ("Failed to set soft timeout"))
        g.log.info("Quota soft timeout set successful")

        # Create 10 directories from the mount point
        mount_obj = self.mounts[0]
        mount_dir = mount_obj.mountpoint
        client = mount_obj.client_system

        g.log.info("Creating directories on %s:%s", client, mount_dir)
        for i in range(1, 11):
            ret = mkdir(client, "%s/foo%s" % (mount_dir, i))
            self.assertTrue(
                ret, ("Failed to create dir under %s-%s", client, mount_dir))
            g.log.info("Directory 'foo%s' created successfully", i)
        g.log.info("Successfully created directories on %s:%s", client,
                   mount_dir)

        # Set a hard-limit of 100 MB with a unique soft-limit
        # having a difference of 10% on each directory
        g.log.info(
            "Setting a limit of 100 MB and unique soft-limit on all "
            "the directories inside the volume %s", self.volname)
        var1 = 1
        for var2 in range(5, 100, 10):
            dir_name = "/foo" + str(var1)
            softlim = str(var2) + "%"
            ret, _, _ = quota_limit_usage(self.mnode,
                                          self.volname,
                                          path=dir_name,
                                          limit="100MB",
                                          soft_limit=softlim)
            self.assertEqual(ret, 0,
                             ("Failed to set quota limits on path "
                              "%s with soft-limit %s for the "
                              "volume %s", dir_name, softlim, self.volname))
            g.log.info(
                "Successfully set the Quota limits on %s of "
                "the volume %s", dir_name, self.volname)
            var1 = var1 + 1

        # Validate hard/soft limits set above , Create data inside the
        # directories such that soft limit is exceeded and validate with
        # quota list command
        var3 = 1
        for var4 in range(5, 100, 10):
            dir_name = "/foo" + str(var3)
            softlim = var4
            data = var4 + 1

            # Validate if the hard limit and soft limit is set properly
            # on all directories
            g.log.info(
                "Validate quota limit usage on the directory %s of the "
                "volume %s", dir_name, self.volname)
            ret = quota_validate(self.mnode,
                                 self.volname,
                                 path=dir_name,
                                 hard_limit=104857600,
                                 soft_limit_percent=softlim)
            self.assertTrue(
                ret,
                ("Failed to validate quota limit usage on the"
                 " directory %s of the volume %s", dir_name, self.volname))
            g.log.info(
                "Successfully validated quota limit usage for the "
                "directory %s of the volume %s", dir_name, self.volname)

            # Perform IO on each Directory
            g.log.info("Creating Files on %s:%s", client, mount_dir)
            cmd = ("cd %s%s ; "
                   "dd if=/dev/zero of=foo%s "
                   "bs=%sM "
                   "count=1 " % (mount_dir, dir_name, var3, data))
            ret, _, _ = g.run(client, cmd)
            self.assertEqual(ret, 0,
                             ("Failed to create files on %s", dir_name))
            g.log.info("Files created successfully on %s", dir_name)

            time.sleep(1)

            # Validate quota list and check if soft-limit is exceeded for
            # each directory
            g.log.info(
                "Validate quota limit usage and soft-limit on the "
                "directory %s", dir_name)
            ret = quota_validate(self.mnode,
                                 self.volname,
                                 path=dir_name,
                                 hard_limit=104857600,
                                 soft_limit_percent=softlim,
                                 sl_exceeded=True)
            self.assertTrue(
                ret,
                ("Failed to validate quota limit usage on the"
                 " directory %s of the volume %s", dir_name, self.volname))
            g.log.info(
                "Successfully validated quota limit usage for the "
                "directory %s of volume %s", dir_name, self.volname)
            var3 = var3 + 1
    def test_limit_usage_deep_dir(self):
        # pylint: disable=too-many-statements
        """
        Verifying directory quota functionality with respect to the
        limit-usage option. Set limits on various directories [breadth]
        and check for the quota list of all the directories.

        * Enable Quota
        * Create 10 directories one inside the other and set limit of 1GB
          on each directory
        * Perform a quota list operation
        * Create some random amount of data inside each directory
        * Perform a quota list operation
        * Remove the quota limit and delete the data
        """
        # Enable Quota
        g.log.info("Enabling quota on the volume %s", self.volname)
        ret, _, _ = quota_enable(self.mnode, self.volname)
        self.assertEqual(ret, 0, ("Failed to enable quota on the volume %s",
                                  self.volname))
        g.log.info("Successfully enabled quota on the volume %s", self.volname)

        # Create deep directories in the mount point
        for mount_object in self.mounts:
            g.log.info("Creating directories on %s:%s",
                       mount_object.client_system, mount_object.mountpoint)
            ret = mkdir(mount_object.client_system,
                        "%s/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10"
                        % (mount_object.mountpoint), parents=True)
            self.assertTrue(ret, ("Failed to create dir under %s-%s",
                                  mount_object.client_system,
                                  mount_object.mountpoint))
            g.log.info("Successfully created deep directories on %s:%s",
                       mount_object.client_system, mount_object.mountpoint)

        # Validate IO
        self.assertTrue(
            validate_io_procs(self.all_mounts_procs, self.mounts),
            "IO failed on some of the clients"
        )

        # Set soft timeout to 1 second
        g.log.info("Set quota soft timeout:")
        ret, _, _ = quota_set_soft_timeout(self.mnode, self.volname, '1sec')
        self.assertEqual(ret, 0, ("Failed to set soft timeout"))
        g.log.info("Quota soft timeout set successful")

        # Set hard timeout to 0 second
        g.log.info("Set quota hard timeout:")
        ret, _, _ = quota_set_hard_timeout(self.mnode, self.volname, '0sec')
        self.assertEqual(ret, 0, ("Failed to set hard timeout"))
        g.log.info("Quota hard timeout set successful")

        # Get dir list
        g.log.info('Getting dir list in %s', self.volname)
        cmd = ("ls -R %s | grep ':' | tr -d :" % self.mounts[0].mountpoint)
        ret, out, err = g.run(self.mounts[0].client_system, cmd)
        g.log.info('mountpoint %s', self.mounts[0].mountpoint)
        self.assertFalse(ret, err)
        dir_list = out.split()
        for dir_name in dir_list:
            # Parsed to remove the mount point as quota doesn't work when
            # passed with mountpoint.
            tmp_name = dir_name.replace(self.mounts[0].mountpoint, "")
            dir_list[dir_list.index(dir_name)] = '%s' % tmp_name
        dir_list.pop(0)
        # The first entry of ls -R is the current directory which is not
        # necessary.

        # Set limit of 1 GB on every directory created inside the mountpoint
        g.log.info("Set Quota Limit on each directory of the volume %s",
                   self.volname)
        for dir_name in dir_list:
            ret, _, _ = quota_limit_usage(self.mnode, self.volname,
                                          dir_name, '1GB')
            self.assertFalse(ret, "Failed to set Quota for dir %s" %
                             dir_name)
            g.log.info("Set quota for dir %s successfully", dir_name)
        g.log.info("Successfully set the Quota limit on each path of the "
                   "volume %s", self.volname)

        # Validate quota on every Directory of the Volume
        g.log.info("Get Quota list for every directory on the volume %s",
                   self.volname)
        for dir_name in dir_list:
            ret = quota_validate(self.mnode, self.volname, path=dir_name,
                                 hard_limit=1073741824)
            self.assertTrue(ret, "Quota validate Failed for dir %s" %
                            dir_name)

        # Create some data inside each directory and do a quota validate
        self.all_mounts_procs = []
        for mount_object in self.mounts:
            g.log.info("Creating Files on %s:%s", mount_object.client_system,
                       mount_object.mountpoint)
            # Data creation
            # Creates one file of rand[0] size in each dir
            rand = random.sample([1, 10, 512], 1)
            cmd = ("/usr/bin/env python %s create_files "
                   "--fixed-file-size %sk %s/%s" % (
                       self.script_upload_path,
                       rand[0], mount_object.mountpoint, dir_list[0]))

            ret, _, _ = g.run(mount_object.client_system, cmd)
            self.assertFalse(ret, "Failed to create files")

            # quota_validate for each dir
            for dir_num, dir_name in enumerate(dir_list):
                # To calculate the dir usage for quota
                usage = (rand[0] * 1024) + \
                         ((len(dir_list) - (dir_num + 1)) * rand[0] * 1024)
                if usage >= 1073741824:
                    raise ExecutionError("usage crossed hardlimit")
                ret = quota_validate(self.mnode, self.volname, path=dir_name,
                                     hard_limit=1073741824, used_space=usage)
                self.assertTrue(ret, "Quota validate Failed for dir %s" %
                                dir_name)
                g.log.info("Quota list validate  and file created successful "
                           "for %s", dir_name)
            g.log.info("Files created and quota validated successfully")

        # Deleting data and validating quota
        self.all_mounts_procs = []
        # Deleting deep directories in the mount point
        for mount_object in self.mounts:
            ret = rmdir(mount_object.client_system, "%s/dir1/dir2" %
                        (mount_object.mountpoint), force=True)
            self.assertTrue(ret, ("Failed to delete dir under %s/dir1/dir2"
                                  % (mount_object.mountpoint)))
            g.log.info("Successfully deleted deep directories")
            # Quota validate
            # converting into bytes
            usage = (rand[0] * 1024)
            ret = quota_validate(self.mnode, self.volname,
                                 path=dir_list[0],
                                 used_space=usage)
            self.assertTrue(ret, "Quota validate Failed for dir /dir1")
            g.log.info("Quota list validate successful for /dir1")

        # Remove Quota limit
        g.log.info("Get Quota list for every directory on the volume %s",
                   self.volname)
        ret = quota_remove(self.mnode, self.volname, path=dir_list[0])
        self.assertTrue(ret, "Failed to remove Quota for dir %s" % dir_name)
        g.log.info("Quota remove  for dir %s successfully", dir_name)
    def test_quota_multi_value_limits(self):
        # pylint: disable=too-many-statements
        """
        Verifying directory quota functionality with respect to
        the limit-usage option. Set limits of various values being
        big, small and decimal values. eg. 1GB, 10GB, 2.5GB, etc.
        Set limits on various directories and check for the quota
        list of all the directories.

        * Enable Quota.
        * Create some data on mount point.
        * Set limit of 1 GB on path "/" , perform some I/O from mount
          such that the hard limit is reached and validate with quota list.
        * Set limit of 1.5 GB on path "/" , perform some I/O from mounts
          such that the hard limit is reached and validate with quota list.
        * Set limit of 10 GB on path "/" , perform some I/O from mounts
          such that the hard limit is reached and validate with quota list.
        """

        # Enable Quota
        g.log.info("Enabling quota on the volume %s", self.volname)
        ret, _, _ = quota_enable(self.mnode, self.volname)
        self.assertEqual(
            ret, 0, ("Failed to enable quota on the volume %s", self.volname))
        g.log.info("Successfully enabled quota on the volume %s", self.volname)

        # Perform some I/O on mounts
        mount_obj = self.mounts[0]
        mount_dir = mount_obj.mountpoint
        client = mount_obj.client_system

        g.log.info("Creating Files on %s:%s", client, mount_dir)
        cmd = ("cd %s ; "
               "for i in `seq 1 10` ; "
               "do dd if=/dev/zero of=foo$i "
               "bs=10M "
               "count=1 ; "
               "done" % mount_dir)
        ret, _, _ = g.run(client, cmd)
        self.assertEqual(ret, 0, "Failed to create files on mountpoint")
        g.log.info("Files created successfully on mountpoint")

        # Set soft timeout to 1 second
        g.log.info("Set quota soft timeout:")
        ret, _, _ = quota_set_soft_timeout(self.mnode, self.volname, '1sec')
        self.assertEqual(ret, 0, ("Failed to set soft timeout"))
        g.log.info("Quota soft timeout set successfully")

        # Set hard timeout to 0 second
        g.log.info("Set quota hard timeout:")
        ret, _, _ = quota_set_hard_timeout(self.mnode, self.volname, '0sec')
        self.assertEqual(ret, 0, ("Failed to set hard timeout"))
        g.log.info("Quota hard timeout set successfully")

        # Path to set quota limit
        path = "/"

        # Set Quota limit of 1 GB on the root of the volume
        g.log.info("Set Quota Limit on the path %s of the volume %s", path,
                   self.volname)
        ret, _, _ = quota_limit_usage(self.mnode,
                                      self.volname,
                                      path=path,
                                      limit="1GB")
        self.assertEqual(ret, 0, ("Failed to set quota limit on path %s of "
                                  "the volume %s", path, self.volname))
        g.log.info("Successfully set the Quota limit on %s of the volume %s",
                   path, self.volname)

        # Create data such that soft limit is crossed but not the hard limit
        g.log.info("Creating Files on %s:%s", client, mount_dir)
        cmd = ("cd %s ; "
               "for i in `seq 11 102` ; "
               "do dd if=/dev/zero of=foo$i "
               "bs=10M "
               "count=1 ; "
               "done" % mount_dir)
        ret, _, _ = g.run(client, cmd)
        self.assertEqual(ret, 0, "Failed to create files on mountpoint")
        g.log.info("Files created successfully on mountpoint")

        # Create data such that hard limit is crossed
        g.log.info("Creating Files on %s:%s", client, mount_dir)
        cmd = ("cd %s ; "
               "for i in `seq 1 10` ; "
               "do dd if=/dev/zero of=small$i "
               "bs=3M "
               "count=1 ; "
               "done" % mount_dir)
        ret, _, _ = g.run(client, cmd)
        self.assertEqual(
            ret, 1, "Failed: Files created successfully in spite "
            "of crossing hard-limit")
        g.log.info("Files creation stopped on mountpoint once exceeded "
                   "hard limit")

        # Validate if the Quota limit set is correct and hard limit is reached
        # by checking the quota list
        g.log.info(
            "Validate if the Quota limit set is correct for the "
            "volume %s", self.volname)
        ret = quota_validate(self.mnode,
                             self.volname,
                             path=path,
                             hard_limit=1073741824,
                             avail_space=0,
                             sl_exceeded=True,
                             hl_exceeded=True)
        self.assertTrue(ret, ("Quota Limit of 1 GB was not set properly on the"
                              " volume %s", self.volname))
        g.log.info(
            "Successfully Validated Quota Limit of 1 GB is set on "
            "volume %s", self.volname)

        # Set Quota limit as decimal value eg. 1.5 GB on the root of the volume
        g.log.info("Set Quota Limit on the path %s of the volume %s", path,
                   self.volname)
        ret, _, _ = quota_limit_usage(self.mnode,
                                      self.volname,
                                      path=path,
                                      limit="1.5GB")
        self.assertEqual(ret, 0, ("Failed to set quota limit on path %s of "
                                  "the volume %s", path, self.volname))
        g.log.info("Successfully set the Quota limit on %s of the volume %s",
                   path, self.volname)

        # Create data such that soft limit is crossed but not the hard limit
        g.log.info("Creating Files on %s:%s", client, mount_dir)
        cmd = ("cd %s ; "
               "for i in `seq 103 152` ; "
               "do dd if=/dev/zero of=foo$i "
               "bs=10M "
               "count=1 ; "
               "done" % mount_dir)
        ret, _, _ = g.run(client, cmd)
        self.assertEqual(ret, 0, "Failed to create files on mountpoint")
        g.log.info("Files created successfully on mountpoint")

        # Create data such that hard limit is crossed
        g.log.info("Creating Files on %s:%s", client, mount_dir)
        cmd = ("cd %s ; "
               "for i in `seq 1 10` ; "
               "do dd if=/dev/zero of=medium$i "
               "bs=5M "
               "count=1 ; "
               "done" % mount_dir)
        ret, _, _ = g.run(client, cmd)
        self.assertEqual(
            ret, 1, "Failed: Files created successfully in spite "
            "of crossing hard-limit")
        g.log.info("Files creation stopped on mountpoint once exceeded "
                   "hard limit")

        # Validate if the Quota limit set is correct and hard limit is reached
        # by checking the quota list
        g.log.info(
            "Validate if the Quota limit set is correct for the "
            "volume %s", self.volname)
        ret = quota_validate(self.mnode,
                             self.volname,
                             path=path,
                             hard_limit=1610612736,
                             avail_space=0,
                             sl_exceeded=True,
                             hl_exceeded=True)
        self.assertTrue(ret, ("Quota Limit of 1.5GB was not set properly on "
                              "the volume %s", self.volname))
        g.log.info(
            "Successfully Validated Quota Limit of 1.5 GB is set on "
            "volume %s", self.volname)

        # Set Quota limit of 10 GB on the root of the volume
        g.log.info("Set Quota Limit on the path %s of the volume %s", path,
                   self.volname)
        ret, _, _ = quota_limit_usage(self.mnode,
                                      self.volname,
                                      path=path,
                                      limit="10GB")
        self.assertEqual(ret, 0, ("Failed to set quota limit on path %s of "
                                  "the volume %s", path, self.volname))
        g.log.info("Successfully set the Quota limit on %s of the volume %s",
                   path, self.volname)

        # Create data such that soft limit is crossed but not the hard limit
        g.log.info("Creating Files on %s:%s", client, mount_dir)
        cmd = ("cd %s ; "
               "for i in `seq 1 17` ; "
               "do dd if=/dev/zero of=large$i "
               "bs=500M "
               "count=1 ; "
               "done" % mount_dir)
        ret, _, _ = g.run(client, cmd)
        self.assertEqual(ret, 0, "Failed to create files on mountpoint")
        g.log.info("Files created successfully on mountpoint")

        # Create data such that hard limit is crossed
        g.log.info("Creating Files on %s:%s", client, mount_dir)
        cmd = ("cd %s ; "
               "for i in `seq 1 30` ; "
               "do dd if=/dev/zero of=largelimit$i "
               "bs=10M "
               "count=1 ; "
               "done" % mount_dir)
        ret, _, _ = g.run(client, cmd)
        self.assertEqual(
            ret, 1, "Failed: Files created successfully in spite "
            "of crossing hard-limit")
        g.log.info("Files creation stopped on mountpoint once exceeded "
                   "hard limit")

        # Validate if the Quota limit set is correct and hard limit is reached
        # by checking the quota list
        g.log.info(
            "Validate if the Quota limit set is correct for the "
            "volume %s", self.volname)
        ret = quota_validate(self.mnode,
                             self.volname,
                             path=path,
                             hard_limit=10737418240,
                             avail_space=0,
                             sl_exceeded=True,
                             hl_exceeded=True)
        self.assertTrue(ret, ("Quota Limit of 10GB was not set properly on the"
                              " volume %s", self.volname))
        g.log.info(
            "Successfully Validated Quota Limit of 10 GB is set on "
            "volume %s", self.volname)

        # Remove Quota from the Volume
        g.log.info("Remove Quota Limit set on path %s of the volume %s", path,
                   self.volname)
        ret, _, _ = quota_remove(self.mnode, self.volname, path=path)
        self.assertEqual(ret, 0, ("Failed to remove quota limit on path %s of "
                                  "the volume %s", path, self.volname))
        g.log.info(
            "Successfully removed the Quota limit on path %s of the "
            "volume %s", path, self.volname)
    def test_no_dir(self):
        """
        * Enable quota on the volume
        * Set the quota on the non-existing directory
        * Create the directory as above and set limit
        * Validate the quota on the volume
        * Delete the directory
        * Validate the quota on volume
        * Recreate the directory
        * Validate the quota on volume
        * Check for volume status for all processes being online.
        """
        # Enable Quota
        g.log.info("Enabling quota on the volume %s", self.volname)
        ret, _, _ = quota_enable(self.mnode, self.volname)
        self.assertEqual(ret, 0, ("Failed to enable quota on the volume %s",
                                  self.volname))
        g.log.info("Successfully enabled quota on the volume %s", self.volname)

        # Non existent path to set quota limit
        path = "/foo"

        # Set Quota limit on /foo of the volume
        g.log.info("Set Quota Limit on the path %s of the volume %s",
                   path, self.volname)
        ret, _, err = quota_limit_usage(self.mnode, self.volname,
                                        path=path, limit="1GB")
        self.assertIn("No such file or directory", err, "Quota limit set "
                      "on path /foo which does not exist")

        mount_obj = self.mounts[0]
        mount_dir = mount_obj.mountpoint
        client = mount_obj.client_system

        # Create the directory on which limit was tried to be set
        ret = mkdir(client, "%s/foo" % (mount_dir))
        self.assertTrue(ret, ("Failed to create dir under %s-%s",
                              client, mount_dir))
        g.log.info("Directory 'foo' created successfully")

        # Set Quota limit on /foo of the volume
        g.log.info("Set Quota Limit on the path %s of the volume %s",
                   path, self.volname)
        ret, _, err = quota_limit_usage(self.mnode, self.volname,
                                        path=path, limit="1GB")
        self.assertEqual(ret, 0, ("Failed to set quota limit on path %s of "
                                  "the volume %s", path, self.volname))
        g.log.info("Successfully set the Quota limit on %s of the volume %s",
                   path, self.volname)

        # Validate quota list
        g.log.info("Get Quota list for foo and see if hardlimit is 1GB")
        ret = quota_validate(self.mnode, self.volname, path=path,
                             hard_limit=1073741824)
        self.assertTrue(ret, "Quota validate Failed for dir foo")

        # Delete the directory
        ret = rmdir(client, "%s/foo" %
                    (mount_dir), force=True)
        self.assertTrue(ret, ("Failed to delete dir /foo"))
        g.log.info("Successfully deleted /foo")

        # Validate quota list
        g.log.info("Get empty quota list")
        quota_list1 = quota_fetch_list(self.mnode, self.volname, path=None)
        self.assertIsNone(quota_list1, ("unexpected quota list entries found"))
        g.log.info("Successfully validated quota limit usage for the "
                   "deleted directory foo")

        # Recreate the same deleted directory
        ret = mkdir(client, "%s/foo" % (mount_dir))
        self.assertTrue(ret, ("Failed to create dir under %s-%s",
                              client, mount_dir))
        g.log.info("Directory 'foo' created successfully")

        # Validate quota list
        g.log.info("Get Quota list for foo and see if hardlimit is N/A")
        ret = quota_validate(self.mnode, self.volname, path=path,
                             hard_limit='N/A')
        self.assertTrue(ret, "Quota validate Failed for dir foo")
        g.log.info("Successfully validated quota limit usage for the "
                   "recreated directory foo")

        # Verify volume's all process are online
        g.log.info("Volume %s: Verifying that all process are online",
                   self.volname)
        ret = verify_all_process_of_volume_are_online(self.mnode,
                                                      self.volname)
        self.assertTrue(ret, ("Volume %s : All process are not online ",
                              self.volname))
        g.log.info("Volume %s: All process are online", self.volname)