示例#1
0
 def setUp(self):
     StorageLocalConfig().write(u"test.json")
     self.hooks = StorageHooks(None, CONFIG, u"test.json", OS_ENV)
示例#2
0
class TestStorageHooks(object):
    # return {'stdout': INFOS_STDOUT, 'stderr': None, 'returncode': 0}

    def setUp(self):
        StorageLocalConfig().write(u"test.json")
        self.hooks = StorageHooks(None, CONFIG, u"test.json", OS_ENV)

    def tearDown(self):
        os.remove(u"test.json")

    def test_class_properties(self):
        assert_equal(self.hooks.brick(), u"{0}:/mnt/somewhere/bricks/exp14".format(self.hooks.private_address))
        assert_equal(self.hooks.bricks_path, u"/mnt/somewhere/bricks")
        assert_equal(self.hooks.volume, u"medias_volume_14")

    def test_volume_set_allowed_ips_ok(self):
        self.hooks.config.allowed_ips = u"192.168.1.*,10.10.*"
        self.hooks.local_config.allowed_ips = [u"129.194.185.47"]
        self.hooks.volume_do = mock_cmd(
            u"\nVolume Name: medias_volume_14\nType: Distribute\nStatus: Started\nNumber of Bricks: 1\n"
            "Transport-type: tcp\nBricks:\nBrick1: ip-10-36-122-169.ec2.internal:/mnt/bricks/exp14\n"
            "Options Reconfigured:\nauth.allow: 10.10.*,129.194.185.47,192.168.1.*\n"
        )
        self.hooks.volume_set_allowed_ips()
        assert_equal(
            self.hooks.volume_do.call_args_list,
            [
                call(
                    u"set",
                    fail=False,
                    volume=u"medias_volume_14",
                    options=u'auth.allow "10.10.*,129.194.185.47,192.168.1.*"',
                ),
                call(u"info", fail=False, volume=u"medias_volume_14"),
                call(u"info", fail=False, volume=u"medias_volume_14"),
            ],
        )
        assert_equal(self.hooks.allowed_ips_string, u"10.10.*,129.194.185.47,192.168.1.*")

    @raises(ValueError)
    def test_volume_set_allowed_ips_raise(self):
        self.hooks.config.allowed_ips = u"192.168.1.*,10.10.*"
        self.hooks.local_config.allowed_ips = [u"129.194.185.47"]
        self.hooks.volume_do = mock_cmd(
            u"\nVolume Name: medias_volume_14\nType: Distribute\nStatus: Started\nNumber of Bricks: 1\n"
            "Transport-type: tcp\nBricks:\nBrick1: ip-10-36-122-169.ec2.internal:/mnt/bricks/exp14\n"
            "Options Reconfigured:\nauth.allow: 192.168.1.*\n"
        )
        self.hooks.volume_set_allowed_ips()

    def test_volume_infos(self):
        self.hooks.volume_do = mock_cmd(
            u"\nVolume Name: medias_volume_14\nType: Distribute\nStatus: Started\nNumber of Bricks: 1\n"
            "Transport-type: tcp\nBricks:\nBrick1: ip-10-36-122-169.ec2.internal:/mnt/bricks/exp14\n"
            "Options Reconfigured:\nauth.allow: 192.168.0.104\n"
        )
        infos = self.hooks.volume_infos()
        assert_equal(
            infos,
            {
                u"name": u"medias_volume_14",
                u"type": u"Distribute",
                u"status": u"Started",
                u"transport": u"tcp",
                u"bricks": [u"ip-10-36-122-169.ec2.internal:/mnt/bricks/exp14"],
                u"auth_allow": u"192.168.0.104",
            },
        )
        self.hooks.volume_do.assert_called_with(u"info", volume=None, fail=False)

    def test_install_replica_1_with_client(self):
        self.hooks.local_config.allowed_ips = []

        py_unittest.MOCK_SIDE_EFFECT_RETURNS = [
            u"",
            u"",
            u"",
            u"",
            u"",
            u"",
            u"",  # apt-get + ntp
            u"",
            u"",  # volume_create_or_expand
            {
                u"stdout": u"\nVolume Name: medias_volume_14\nType: Distribute\nStatus: Started"
                u"\nNumber of Bricks: 1\nTransport-type: tcp\nBricks:"
                u"\nBrick1: 10.1.1.10:/exp14\n"
            },
            {u"stdout": u""},
        ]

        self.hooks.private_address = u"10.1.1.10"
        self.hooks.hook_uninstall = Mock(return_value=None)
        self.hooks.cmd = Mock(side_effect=mock_side_effect)
        self.hooks.volume_set_allowed_ips = Mock(return_value=None)
        self.hooks.trigger(hook_name=u"install")
        self.hooks.volume_set_allowed_ips.assert_called_once()
        calls = self.hooks.cmd.call_args_list
        assert_equal(calls[0], call(u"locale-gen fr_CH.UTF-8"))
        assert_equal(calls[1], call(u"dpkg-reconfigure locales"))
        assert_equal(calls[2], call(u"apt-get -y update", fail=False))
        assert_equal(calls[3], call(u"apt-get -y -f install"))
        assert_equal(calls[4], call(u"apt-get -y upgrade"))
        assert_equal(calls[5], call(u"apt-get -y install ntp glusterfs-server nfs-common"))
        assert_equal(calls[6], call(u"service ntp restart"))
        assert_equal(
            calls[7],
            call(
                u"gluster volume create medias_volume_14  10.1.1.10:/mnt/somewhere/bricks/exp14",
                fail=True,
                input=None,
                cli_input=None,
            ),
        )
        assert_equal(calls[8], call(u"gluster volume start medias_volume_14 ", fail=True, input=None, cli_input=None))
        assert_equal(calls[9], call(u"gluster volume info medias_volume_14 ", fail=False, input=None, cli_input=None))
        assert_equal(
            calls[10], call(u"gluster volume rebalance medias_volume_14 status", fail=False, input=None, cli_input=None)
        )

        self.hooks.relation_set = Mock(return_value=None)
        self.hooks.relation_get = Mock(return_value=u"129.194.185.47")
        self.hooks.trigger(hook_name=u"storage_relation_joined")
        self.hooks.relation_set.assert_called_with(mountpoint=u"medias_volume_14", options=u"", fstype=u"glusterfs")
        self.hooks.relation_get.assert_called_with(u"private-address")
        assert_equal(self.hooks.local_config.allowed_ips, [u"129.194.185.47"])

        self.hooks.relation_set = Mock(return_value=None)
        self.hooks.relation_get = Mock(return_value=u"129.194.185.50")
        self.hooks.trigger(hook_name=u"storage_relation_joined")
        self.hooks.relation_set.assert_called_with(mountpoint=u"medias_volume_14", options=u"", fstype=u"glusterfs")
        self.hooks.relation_get.assert_called_with(u"private-address")
        assert_equal(self.hooks.local_config.allowed_ips, [u"129.194.185.47", u"129.194.185.50"])