Revision 8fc79619
Added by Jeff Ortel almost 6 years ago
src/katello/agent/goferd/legacy_plugin.py | ||
---|---|---|
from katello.repos import upload_enabled_repos_report
|
||
from katello.enabled_report import EnabledReport
|
||
|
||
from gofer.decorators import initializer, remote, action
|
||
from gofer.decorators import load, unload, remote, action
|
||
from gofer.agent.plugin import Plugin
|
||
from gofer.pmon import PathMonitor
|
||
from gofer.agent.rmi import Context
|
||
... | ... | |
except ImportError:
|
||
from subscription_manager.certlib import ConsumerIdentity
|
||
|
||
from rhsm.connection import UEPConnection, RemoteServerException
|
||
from rhsm.connection import UEPConnection, RemoteServerException, GoneException
|
||
|
||
from pulp.agent.lib.dispatcher import Dispatcher
|
||
from pulp.agent.lib.conduit import Conduit as HandlerConduit
|
||
... | ... | |
RHSM_CONFIG_PATH = '/etc/rhsm/rhsm.conf'
|
||
|
||
|
||
@initializer
|
||
def init_plugin():
|
||
@load
|
||
def plugin_loaded():
|
||
"""
|
||
Initialize the plugin.
|
||
Called (once) immediately after the plugin is loaded.
|
||
... | ... | |
- validate registration. If registered:
|
||
- setup plugin configuration.
|
||
"""
|
||
global path_monitor
|
||
path = ConsumerIdentity.certpath()
|
||
path_monitor = PathMonitor()
|
||
path_monitor.add(path, certificate_changed)
|
||
path_monitor.add(REPOSITORY_PATH, send_enabled_report)
|
||
path_monitor.start()
|
||
... | ... | |
sleep(60)
|
||
|
||
|
||
@unload
|
||
def plugin_unloaded():
|
||
"""
|
||
The plugin has been uploaded.
|
||
"""
|
||
path_monitor.abort()
|
||
|
||
|
||
def bundle(certificate):
|
||
"""
|
||
Bundle the key and cert and write to a file.
|
||
... | ... | |
uep = UEP()
|
||
consumer = uep.getConsumer(consumer_id)
|
||
registered = (consumer is not None)
|
||
except GoneException:
|
||
registered = False
|
||
except RemoteServerException, e:
|
||
if e.code != httplib.NOT_FOUND:
|
||
if e.code not in (httplib.NOT_FOUND, httplib.GONE):
|
||
log.warn(str(e))
|
||
raise
|
||
except Exception, e:
|
src/katello/agent/goferd/plugin.py | ||
---|---|---|
|
||
from katello.constants import REPOSITORY_PATH
|
||
|
||
from gofer.decorators import initializer, remote, action, FORK
|
||
from gofer.decorators import load, unload, remote, action, FORK
|
||
from gofer.agent.plugin import Plugin
|
||
from gofer.pmon import PathMonitor
|
||
from gofer.config import Config
|
||
... | ... | |
except ImportError:
|
||
from subscription_manager.certlib import ConsumerIdentity
|
||
|
||
from rhsm.connection import UEPConnection, RemoteServerException
|
||
from rhsm.connection import UEPConnection, RemoteServerException, GoneException
|
||
|
||
from katello.agent.pulp import Dispatcher
|
||
from katello.enabled_report import EnabledReport
|
||
... | ... | |
plugin = Plugin.find(__name__)
|
||
|
||
# Path monitoring
|
||
path_monitor = PathMonitor()
|
||
path_monitor = None
|
||
|
||
# Track registration status
|
||
registered = False
|
||
... | ... | |
RHSM_CONFIG_PATH = '/etc/rhsm/rhsm.conf'
|
||
|
||
|
||
@initializer
|
||
def init_plugin():
|
||
@load
|
||
def plugin_loaded():
|
||
"""
|
||
Initialize the plugin.
|
||
Called (once) immediately after the plugin is loaded.
|
||
... | ... | |
- validate registration. If registered:
|
||
- setup plugin configuration.
|
||
"""
|
||
global path_monitor
|
||
path = ConsumerIdentity.certpath()
|
||
path_monitor = PathMonitor()
|
||
path_monitor.add(path, certificate_changed)
|
||
path_monitor.add(REPOSITORY_PATH, send_enabled_report)
|
||
path_monitor.start()
|
||
... | ... | |
sleep(60)
|
||
|
||
|
||
@unload
|
||
def plugin_unloaded():
|
||
"""
|
||
The plugin has been uploaded.
|
||
"""
|
||
path_monitor.abort()
|
||
|
||
|
||
def bundle(certificate):
|
||
"""
|
||
Bundle the key and cert and write to a file.
|
||
... | ... | |
uep = UEP()
|
||
consumer = uep.getConsumer(consumer_id)
|
||
registered = (consumer is not None)
|
||
except GoneException:
|
||
registered = False
|
||
except RemoteServerException as e:
|
||
if e.code != http.NOT_FOUND:
|
||
if e.code not in (http.NOT_FOUND, http.GONE):
|
||
log.warn(str(e))
|
||
raise
|
||
except Exception as e:
|
test/test_katello/legacy_plugin_test.py | ||
---|---|---|
|
||
from mock import Mock, patch
|
||
|
||
from rhsm.connection import RemoteServerException
|
||
from rhsm.connection import RemoteServerException, GoneException
|
||
|
||
|
||
sys.path.append(os.path.join(os.path.dirname(__file__), '../../src/'))
|
||
... | ... | |
self.assertEqual(plugin_cfg.messaging.uuid, 'pulp.agent.%s' % consumer_id)
|
||
|
||
|
||
class TestInitializer(PluginTest):
|
||
class TestPluginLoaded(PluginTest):
|
||
|
||
@patch('katello.agent.goferd.legacy_plugin.path_monitor')
|
||
@patch('katello.agent.goferd.legacy_plugin.PathMonitor')
|
||
@patch('katello.agent.goferd.legacy_plugin.ConsumerIdentity.certpath')
|
||
@patch('katello.agent.goferd.legacy_plugin.validate_registration')
|
||
def test_init(self, fake_validate, fake_path, fake_pmon):
|
||
self.plugin.registered = False
|
||
|
||
# test
|
||
self.plugin.init_plugin()
|
||
self.plugin.plugin_loaded()
|
||
|
||
# validation
|
||
fake_path.assert_called_with()
|
||
fake_pmon.add.assert_any_call(fake_path(), self.plugin.certificate_changed)
|
||
fake_pmon.start.assert_called_with()
|
||
fake_pmon.return_value.add.assert_any_call(fake_path(), self.plugin.certificate_changed)
|
||
fake_pmon.return_value.start.assert_called_with()
|
||
fake_validate.assert_called_with()
|
||
|
||
@patch('katello.agent.goferd.legacy_plugin.PathMonitor')
|
||
@patch('katello.agent.goferd.legacy_plugin.update_settings')
|
||
@patch('katello.agent.goferd.legacy_plugin.validate_registration')
|
||
def test_registered(self, validate, update_settings):
|
||
def test_registered(self, validate, update_settings, path_monitor):
|
||
|
||
# test
|
||
self.plugin.init_plugin()
|
||
self.plugin.plugin_loaded()
|
||
|
||
# validation
|
||
validate.assert_called_with()
|
||
update_settings.assert_called_with()
|
||
self.assertEqual(self.plugin.path_monitor, path_monitor.return_value)
|
||
self.assertFalse(self.plugin.plugin.attach.called)
|
||
|
||
@patch('katello.agent.goferd.legacy_plugin.PathMonitor')
|
||
@patch('katello.agent.goferd.legacy_plugin.update_settings')
|
||
@patch('katello.agent.goferd.legacy_plugin.validate_registration')
|
||
def test_run_not_registered(self, validate, update_settings):
|
||
def test_run_not_registered(self, validate, update_settings, path_monitor):
|
||
self.plugin.registered = False
|
||
|
||
# test
|
||
self.plugin.init_plugin()
|
||
self.plugin.plugin_loaded()
|
||
|
||
# validation
|
||
validate.assert_called_with()
|
||
self.assertFalse(update_settings.called)
|
||
self.assertEqual(self.plugin.path_monitor, path_monitor.return_value)
|
||
self.assertFalse(self.plugin.plugin.attach.called)
|
||
|
||
@patch('katello.agent.goferd.legacy_plugin.sleep')
|
||
@patch('katello.agent.goferd.legacy_plugin.PathMonitor')
|
||
@patch('katello.agent.goferd.legacy_plugin.update_settings')
|
||
@patch('katello.agent.goferd.legacy_plugin.validate_registration')
|
||
def test_run_validate_failed(self, validate, update_settings, sleep):
|
||
def test_run_validate_failed(self, validate, update_settings, path_monitor, sleep):
|
||
validate.side_effect = [ValueError, None]
|
||
|
||
# test
|
||
self.plugin.init_plugin()
|
||
self.plugin.plugin_loaded()
|
||
|
||
# validation
|
||
validate.assert_called_with()
|
||
sleep.assert_called_once_with(60)
|
||
update_settings.assert_called_with()
|
||
self.assertEqual(self.plugin.path_monitor, path_monitor.return_value)
|
||
self.assertFalse(self.plugin.plugin.attach.called)
|
||
|
||
|
||
class TestPluginUnloaded(PluginTest):
|
||
|
||
@patch('katello.agent.goferd.legacy_plugin.path_monitor')
|
||
def test_unloaded(self, path_monitor):
|
||
self.plugin.plugin_unloaded()
|
||
path_monitor.abort.assert_called_once_with()
|
||
|
||
|
||
class TestConduit(PluginTest):
|
||
|
||
@patch('katello.agent.goferd.legacy_plugin.ConsumerIdentity.read')
|
||
... | ... | |
@patch('katello.agent.goferd.legacy_plugin.UEPConnection.getConsumer')
|
||
@patch('katello.agent.goferd.legacy_plugin.ConsumerIdentity.read')
|
||
@patch('katello.agent.goferd.legacy_plugin.ConsumerIdentity.existsAndValid')
|
||
def test_validate_registration_not_confirmed(self, valid, read, get_consumer):
|
||
def test_validate_registration_not_found(self, valid, read, get_consumer):
|
||
consumer_id = '1234'
|
||
valid.return_value = True
|
||
read.return_value.getConsumerId.return_value = consumer_id
|
||
... | ... | |
get_consumer.assert_called_with(consumer_id)
|
||
self.assertFalse(self.plugin.registered)
|
||
|
||
@patch('katello.agent.goferd.legacy_plugin.UEPConnection.getConsumer')
|
||
@patch('katello.agent.goferd.legacy_plugin.ConsumerIdentity.read')
|
||
@patch('katello.agent.goferd.legacy_plugin.ConsumerIdentity.existsAndValid')
|
||
def test_validate_registration_gone(self, valid, read, get_consumer):
|
||
consumer_id = '1234'
|
||
valid.return_value = True
|
||
read.return_value.getConsumerId.return_value = consumer_id
|
||
get_consumer.side_effect = GoneException(httplib.GONE, '', '')
|
||
|
||
# test
|
||
self.plugin.validate_registration()
|
||
|
||
# validation
|
||
get_consumer.assert_called_with(consumer_id)
|
||
self.assertFalse(self.plugin.registered)
|
||
|
||
@patch('katello.agent.goferd.legacy_plugin.UEPConnection.getConsumer')
|
||
@patch('katello.agent.goferd.legacy_plugin.ConsumerIdentity.read')
|
||
@patch('katello.agent.goferd.legacy_plugin.ConsumerIdentity.existsAndValid')
|
test/test_katello/test_plugin.py | ||
---|---|---|
|
||
from mock import patch, Mock
|
||
|
||
from rhsm.connection import RemoteServerException
|
||
from rhsm.connection import RemoteServerException, GoneException
|
||
|
||
import unittest2 as unittest
|
||
|
||
sys.path.append(os.path.join(os.path.dirname(__file__), '../../src/katello/agent'))
|
||
|
||
|
||
class Repository(object):
|
||
|
||
def __init__(self, repo_id, repofile, baseurl):
|
||
... | ... | |
plugin.plugin.scheduler = Mock()
|
||
plugin.plugin.scheduler.pending = Mock(PENDING='/tmp/pending', stream='abc')
|
||
plugin.plugin.cfg = plugin_cfg
|
||
plugin.path_monitor = Mock()
|
||
plugin.registered = True
|
||
return plugin
|
||
|
||
... | ... | |
|
||
|
||
@unittest.skipIf(sys.version_info[0] == 2 and sys.version_info[1] < 7, "goferd plugin requires python 2.7+")
|
||
class TestInitializer(PluginTest):
|
||
class TestPluginLoaded(PluginTest):
|
||
|
||
@patch('katello.agent.goferd.plugin.path_monitor')
|
||
@patch('katello.agent.goferd.plugin.PathMonitor')
|
||
@patch('katello.agent.goferd.plugin.ConsumerIdentity.certpath')
|
||
@patch('katello.agent.goferd.plugin.validate_registration')
|
||
def test_init(self, fake_validate, fake_path, fake_pmon):
|
||
self.plugin.registered = False
|
||
|
||
# test
|
||
self.plugin.init_plugin()
|
||
self.plugin.plugin_loaded()
|
||
|
||
# validation
|
||
fake_path.assert_called_with()
|
||
fake_pmon.add.assert_any_call(fake_path(), self.plugin.certificate_changed)
|
||
fake_pmon.start.assert_called_with()
|
||
fake_pmon.return_value.add.assert_any_call(fake_path(), self.plugin.certificate_changed)
|
||
fake_pmon.return_value.start.assert_called_with()
|
||
fake_validate.assert_called_with()
|
||
|
||
@patch('katello.agent.goferd.plugin.PathMonitor')
|
||
@patch('katello.agent.goferd.plugin.update_settings')
|
||
@patch('katello.agent.goferd.plugin.validate_registration')
|
||
def test_registered(self, validate, update_settings):
|
||
def test_registered(self, validate, update_settings, path_monitor):
|
||
|
||
# test
|
||
self.plugin.init_plugin()
|
||
self.plugin.plugin_loaded()
|
||
|
||
# validation
|
||
validate.assert_called_with()
|
||
update_settings.assert_called_with()
|
||
self.assertEqual(self.plugin.path_monitor, path_monitor.return_value)
|
||
self.assertFalse(self.plugin.plugin.attach.called)
|
||
|
||
@patch('katello.agent.goferd.plugin.PathMonitor')
|
||
@patch('katello.agent.goferd.plugin.update_settings')
|
||
@patch('katello.agent.goferd.plugin.validate_registration')
|
||
def test_run_not_registered(self, validate, update_settings):
|
||
def test_run_not_registered(self, validate, update_settings, path_monitor):
|
||
self.plugin.registered = False
|
||
|
||
# test
|
||
self.plugin.init_plugin()
|
||
self.plugin.plugin_loaded()
|
||
|
||
# validation
|
||
validate.assert_called_with()
|
||
self.assertFalse(update_settings.called)
|
||
self.assertEqual(self.plugin.path_monitor, path_monitor.return_value)
|
||
self.assertFalse(self.plugin.plugin.attach.called)
|
||
|
||
@patch('katello.agent.goferd.plugin.sleep')
|
||
@patch('katello.agent.goferd.plugin.PathMonitor')
|
||
@patch('katello.agent.goferd.plugin.update_settings')
|
||
@patch('katello.agent.goferd.plugin.validate_registration')
|
||
def test_run_validate_failed(self, validate, update_settings, sleep):
|
||
def test_run_validate_failed(self, validate, update_settings, path_monitor, sleep):
|
||
validate.side_effect = [ValueError, None]
|
||
|
||
# test
|
||
self.plugin.init_plugin()
|
||
self.plugin.plugin_loaded()
|
||
|
||
# validation
|
||
validate.assert_called_with()
|
||
sleep.assert_called_once_with(60)
|
||
update_settings.assert_called_with()
|
||
self.assertEqual(self.plugin.path_monitor, path_monitor.return_value)
|
||
self.assertFalse(self.plugin.plugin.attach.called)
|
||
|
||
|
||
@unittest.skipIf(sys.version_info[0] == 2 and sys.version_info[1] < 7, "goferd plugin requires python 2.7+")
|
||
class TestPluginUnloaded(PluginTest):
|
||
|
||
@patch('katello.agent.goferd.plugin.path_monitor')
|
||
def test_unloaded(self, path_monitor):
|
||
self.plugin.plugin_unloaded()
|
||
path_monitor.abort.assert_called_once_with()
|
||
|
||
|
||
@unittest.skipIf(sys.version_info[0] == 2 and sys.version_info[1] < 7, "goferd plugin requires python 2.7+")
|
||
class TestUEP(PluginTest):
|
||
|
||
... | ... | |
@patch('katello.agent.goferd.plugin.UEP.getConsumer')
|
||
@patch('katello.agent.goferd.plugin.ConsumerIdentity.read')
|
||
@patch('katello.agent.goferd.plugin.ConsumerIdentity.existsAndValid')
|
||
def test_validate_registration_not_confirmed(self, valid, read, get_consumer):
|
||
def test_validate_registration_not_found(self, valid, read, get_consumer):
|
||
consumer_id = '1234'
|
||
valid.return_value = True
|
||
read.return_value.getConsumerId.return_value = consumer_id
|
||
... | ... | |
get_consumer.assert_called_with(consumer_id)
|
||
self.assertFalse(self.plugin.registered)
|
||
|
||
@patch('katello.agent.goferd.plugin.UEP.getConsumer')
|
||
@patch('katello.agent.goferd.plugin.ConsumerIdentity.read')
|
||
@patch('katello.agent.goferd.plugin.ConsumerIdentity.existsAndValid')
|
||
def test_validate_registration_gone(self, valid, read, get_consumer):
|
||
consumer_id = '1234'
|
||
valid.return_value = True
|
||
read.return_value.getConsumerId.return_value = consumer_id
|
||
get_consumer.side_effect = GoneException(httplib.GONE, '', '')
|
||
|
||
# test
|
||
self.plugin.validate_registration()
|
||
|
||
# validation
|
||
get_consumer.assert_called_with(consumer_id)
|
||
self.assertFalse(self.plugin.registered)
|
||
|
||
@patch('katello.agent.goferd.plugin.UEP.getConsumer')
|
||
@patch('katello.agent.goferd.plugin.ConsumerIdentity.read')
|
||
@patch('katello.agent.goferd.plugin.ConsumerIdentity.existsAndValid')
|
Also available in: Unified diff
fixes #24006 - support plugin reload when queue not found.