~codersquid/charms/precise/block-storage-broker/basenode-support

« back to all changes in this revision

Viewing changes to hooks/test_util.py

  • Committer: David Britton
  • Date: 2014-09-09 16:48:49 UTC
  • mfrom: (54.2.6 bsb-retries-on-attach)
  • Revision ID: dpb@canonical.com-20140909164849-sn1ah8asskw3mw3l
Merging lp:~chad.smith/charms/precise/block-storage-broker/bsb-retries-on-volume-create-and-attach

Retry operation against openstack volume-create operations [a=chad.smith] [r=dpb] [f=lp:1358907]

Show diffs side-by-side

added added

removed removed

Lines of Context:
482
482
        result = self.assertRaises(
483
483
            SystemExit, self.storage._nova_describe_volumes)
484
484
        self.assertEqual(result.code, 1)
 
485
 
485
486
        message = (
486
487
            "ERROR: Command '%s' returned non-zero exit status 1" % command)
487
488
        self.assertIn(
632
633
        command = (
633
634
            "nova volume-create --display-name '%s' %s" % (volume_label, size))
634
635
 
635
 
        create = self.mocker.replace(subprocess.check_call)
 
636
        create = self.mocker.replace(subprocess.check_output)
636
637
        create(command, shell=True)
637
638
        self.mocker.replay()
638
639
 
658
659
        command = (
659
660
            "nova volume-create --display-name '%s' %s" % (volume_label, size))
660
661
 
661
 
        create = self.mocker.replace(subprocess.check_call)
 
662
        create = self.mocker.replace(subprocess.check_output)
662
663
        create(command, shell=True)
663
664
        self.mocker.replay()
664
665
        self.storage.get_volume_id = lambda label: None
673
674
        self.assertIn(
674
675
            message, util.hookenv._log_ERROR, "Not logged- %s" % message)
675
676
 
676
 
    def test_wb_nova_create_volume_error_command_failed(self):
 
677
    def test_wb_nova_create_volume_error_command_failed_with_retries(self):
677
678
        """
678
 
        L{_nova_create_volume} will log an error and exit when
679
 
        C{nova create-volume} command fails.
 
679
        L{_nova_create_volume} will log warnings and retry 3 times when an
 
680
        error is raised by the command  C{nova create-volume}. Upon failure of
 
681
        the third retry, L{_nova_create_volume} will log an error and exit 1.
680
682
        """
681
683
        instance_id = "i-123123"
682
684
        volume_label = "postgresql/0 unit volume"
684
686
        command = (
685
687
            "nova volume-create --display-name '%s' %s" % (volume_label, size))
686
688
 
687
 
        create = self.mocker.replace(subprocess.check_call)
 
689
        create = self.mocker.replace(subprocess.check_output)
688
690
        create(command, shell=True)
 
691
        self.mocker.count(4)
689
692
        self.mocker.throw(subprocess.CalledProcessError(1, command))
 
693
        sleep = self.mocker.replace("time.sleep")
 
694
        sleep(30)
 
695
        self.mocker.count(3)
690
696
        self.mocker.replay()
691
697
 
692
698
        result = self.assertRaises(
693
699
            SystemExit, self.storage._nova_create_volume, size, volume_label,
694
700
            instance_id)
695
701
        self.assertEqual(result.code, 1)
696
 
        message = (
697
 
            "ERROR: Command '%s' returned non-zero exit status 1" % command)
 
702
        self.assertEqual(len(util.hookenv._log_WARNING), 3)
 
703
        message = (
 
704
            "WARNING: Command '%s' returned non-zero exit status 1. "
 
705
            "Retrying 3 more times" % command)
 
706
        self.assertIn(
 
707
            message, util.hookenv._log_WARNING, "Not logged- %s" % message)
 
708
 
 
709
        message = (
 
710
            "ERROR: Command '%s' returned non-zero exit status 1." % command)
698
711
        self.assertIn(
699
712
            message, util.hookenv._log_ERROR, "Not logged- %s" % message)
700
713
 
738
751
            self.storage._nova_attach_volume(instance_id, volume_id),
739
752
            "")
740
753
 
741
 
    def test_wb_nova_attach_volume_command_error(self):
 
754
    def test_wb_nova_attach_volume_command_error_retries_three_times(self):
742
755
        """
743
 
        L{_nova_attach_volume} will exit in error when the
744
 
        C{nova volume-attach} command fails.
 
756
        L{_nova_attach_volume} will warn on command error and retry the command
 
757
        3 times with sleeps in between. When the command C{nova volume-attach}
 
758
        exits in error on the third retry, an error is logged and the method
 
759
        exits 1.
745
760
        """
746
761
        instance_id = "i-123123"
747
762
        volume_id = "123-123-123"
750
765
            (instance_id, volume_id))
751
766
        attach = self.mocker.replace(subprocess.check_output)
752
767
        attach(command, shell=True)
 
768
        self.mocker.count(4)
753
769
        self.mocker.throw(subprocess.CalledProcessError(1, command))
 
770
        sleep = self.mocker.replace("time.sleep")
 
771
        sleep(30)
 
772
        self.mocker.count(3)
754
773
        self.mocker.replay()
755
774
 
756
775
        result = self.assertRaises(
757
776
            SystemExit, self.storage._nova_attach_volume, instance_id,
758
777
            volume_id)
759
778
        self.assertEqual(result.code, 1)
760
 
        message = (
761
 
            "ERROR: Command '%s' returned non-zero exit status 1" % command)
 
779
        self.assertEqual(len(util.hookenv._log_WARNING), 3)
 
780
        message = (
 
781
            "WARNING: Command '%s' returned non-zero exit status 1. "
 
782
            "Retrying 3 more times" % command)
 
783
        self.assertIn(
 
784
            message, util.hookenv._log_WARNING, "Not logged- %s" % message)
 
785
        message = (
 
786
            "ERROR: Command '%s' returned non-zero exit status 1." % command)
762
787
        self.assertIn(
763
788
            message, util.hookenv._log_ERROR, "Not logged- %s" % message)
764
789