def test_get_password(self):
        fake_file_storage = FakeFileStorage()
        passwords = fake_file_storage.load()
        resource = six.next(six.iterkeys(passwords))
        user, expected = six.next(six.iteritems(passwords[resource]))

        self._mock_file_storage(fake_file_storage)
        password_storage = passwd.PasswordFileStorage('fake_file_path')

        result = password_storage.get_password(resource, user)
        self.assertEqual(expected, result)

        result = password_storage.get_password(resource.upper(), user)
        self.assertIsNone(result)
    def do_setup(self, context):
        """Perform validations and establish connection to server.

        :param context: Context information
        """

        super(ISCSIVolumeDriver, self).do_setup(context)

        password_storage_path = getattr(self.configuration,
                                        'datacore_iscsi_chap_storage', None)
        if (self.configuration.datacore_iscsi_chap_enabled
                and not password_storage_path):
            raise cinder_exception.InvalidInput(
                _("datacore_iscsi_chap_storage not set."))
        elif password_storage_path:
            self._password_storage = passwd.PasswordFileStorage(
                self.configuration.datacore_iscsi_chap_storage)
    def test_delete_password(self):
        fake_file_storage = FakeFileStorage()
        passwords = fake_file_storage.load()
        resource1, resource2 = 'resource1', 'resource2'
        user1 = six.next(six.iterkeys(passwords[resource1]))
        user2 = six.next(six.iterkeys(passwords[resource2]))

        self._mock_file_storage(fake_file_storage)
        password_storage = passwd.PasswordFileStorage('fake_file_path')

        password_storage.delete_password(resource1, user1)
        passwords = fake_file_storage.load()
        self.assertIn(resource1, passwords)
        self.assertNotIn(user1, passwords[resource1])

        password_storage.delete_password(resource2, user2)
        passwords = fake_file_storage.load()
        self.assertNotIn(resource2, passwords)
    def test_set_password(self):
        fake_file_storage = FakeFileStorage()
        user = '******'
        resource1 = 'resource2'
        password1 = 'resource2-user3'
        resource2 = 'resource3'
        password2 = 'resource3-user3'

        self._mock_file_storage(fake_file_storage)
        password_storage = passwd.PasswordFileStorage('fake_file_path')

        password_storage.set_password(resource1, user, password1)
        passwords = fake_file_storage.load()
        self.assertIn(resource1, passwords)
        self.assertIn(user, passwords[resource1])
        self.assertEqual(password1, passwords[resource1][user])

        password_storage.set_password(resource2, user, password2)
        passwords = fake_file_storage.load()
        self.assertIn(resource2, passwords)
        self.assertIn(user, passwords[resource2])
        self.assertEqual(password2, passwords[resource2][user])