def setUp(self, _poll): _poll.return_value = self.DATA # def __init__(self, baseurl, nodename, jenkins_obj): self.J = mock.MagicMock() # Jenkins object self.n = Node('http://', 'bobnit', self.J)
def create_node(self, name, num_executors=2, node_description=None, remote_fs='/var/lib/jenkins', labels=None, exclusive=False): """ Create a new slave node by name. :param name: fqdn of slave, str :param num_executors: number of executors, int :param node_description: a freetext field describing the node :param remote_fs: jenkins path, str :param labels: labels to associate with slave, str :param exclusive: tied to specific job, boolean :return: node obj """ NODE_TYPE = 'hudson.slaves.DumbSlave$DescriptorImpl' MODE = 'NORMAL' if self.has_node(name): return Node(nodename=name, baseurl=self.get_node_url(nodename=name), jenkins_obj=self) if exclusive: MODE = 'EXCLUSIVE' params = { 'name': name, 'type': NODE_TYPE, 'json': json.dumps({ 'name': name, 'nodeDescription': node_description, 'numExecutors': num_executors, 'remoteFS': remote_fs, 'labelString': labels, 'mode': MODE, 'type': NODE_TYPE, 'retentionStrategy': { 'stapler-class': 'hudson.slaves.RetentionStrategy$Always' }, 'nodeProperties': { 'stapler-class-bag': 'true' }, 'launcher': { 'stapler-class': 'hudson.slaves.JNLPLauncher' } }) } url = self.get_node_url( ) + "doCreateItem?%s" % urllib.urlencode(params) self.requester.get_and_confirm_status(url) return Node(nodename=name, baseurl=self.get_node_url(nodename=name), jenkins_obj=self)
class TestNode(unittest.TestCase): DATA = {"actions": [], "displayName": "bobnit", "executors": [{}], "icon": "computer.png", "idle": True, "jnlpAgent": False, "launchSupported": True, "loadStatistics": {}, "manualLaunchAllowed": True, "monitorData": {"hudson.node_monitors.SwapSpaceMonitor": {"availablePhysicalMemory": 7681417216, "availableSwapSpace": 12195983360, "totalPhysicalMemory": 8374497280, "totalSwapSpace": 12195983360}, "hudson.node_monitors.ArchitectureMonitor": "Linux (amd64)", "hudson.node_monitors.ResponseTimeMonitor": {"average": 64}, "hudson.node_monitors.TemporarySpaceMonitor": {"path": "/tmp", "size": 250172776448}, "hudson.node_monitors.DiskSpaceMonitor": {"path": "/home/sal/jenkins", "size": 170472026112}, "hudson.node_monitors.ClockMonitor": {"diff": 6736}}, "numExecutors": 1, "offline": False, "offlineCause": None, "oneOffExecutors": [], "temporarilyOffline": False} @mock.patch.object(Node, '_poll') # pylint: disable=W0221 def setUp(self, _poll): _poll.return_value = self.DATA # def __init__(self, baseurl, nodename, jenkins_obj): self.J = mock.MagicMock() # Jenkins object self.n = Node('http://', 'bobnit', self.J) def testRepr(self): # Can we produce a repr string for this object repr(self.n) def testName(self): with self.assertRaises(AttributeError): self.n.id() self.assertEquals(self.n.name, 'bobnit') @mock.patch.object(Node, '_poll') def test_online(self, _poll): _poll.return_value = self.DATA return self.assertEquals(self.n.is_online(), True)
class TestNode(unittest.TestCase): DATA = {"actions": [], "displayName": "bobnit", "executors": [{}], "icon": "computer.png", "idle": True, "jnlpAgent": False, "launchSupported": True, "loadStatistics": {}, "manualLaunchAllowed": True, "monitorData": {"hudson.node_monitors.SwapSpaceMonitor": {"availablePhysicalMemory": 7681417216, "availableSwapSpace": 12195983360, "totalPhysicalMemory": 8374497280, "totalSwapSpace": 12195983360}, "hudson.node_monitors.ArchitectureMonitor": "Linux (amd64)", "hudson.node_monitors.ResponseTimeMonitor": {"average": 64}, "hudson.node_monitors.TemporarySpaceMonitor": {"path": "/tmp", "size": 250172776448}, "hudson.node_monitors.DiskSpaceMonitor": {"path": "/home/sal/jenkins", "size": 170472026112}, "hudson.node_monitors.ClockMonitor": {"diff": 6736}}, "numExecutors": 1, "offline": False, "offlineCause": None, "oneOffExecutors": [], "temporarilyOffline": False} @mock.patch.object(Node, '_poll') def setUp(self, _poll): _poll.return_value = self.DATA # def __init__(self, baseurl, nodename, jenkins_obj): self.J = mock.MagicMock() # Jenkins object self.n = Node('http://', 'bobnit', self.J) def testRepr(self): # Can we produce a repr string for this object repr(self.n) def testName(self): with self.assertRaises(AttributeError): self.n.id() self.assertEquals(self.n.name, 'bobnit') @mock.patch.object(Node, '_poll') def test_online(self, _poll): _poll.return_value = self.DATA return self.assertEquals(self.n.is_online(), True)
def node(monkeypatch, mocker): def fake_poll(cls, tree=None): # pylint: disable=unused-argument return DATA monkeypatch.setattr(Node, '_poll', fake_poll) jenkins = mocker.MagicMock() return Node(jenkins, 'http://foo:8080', 'bobnit', {})
def iteritems(self): for item in self._data['computer']: nodename = item['displayName'] if nodename.lower() == 'master': nodeurl = '%s/(%s)' % (self.baseurl, nodename) else: nodeurl = '%s/%s' % (self.baseurl, nodename) yield item['displayName'], Node(nodeurl, nodename, self.jenkins)
def __getitem__(self, nodename): if nodename in self: if nodename.lower() == 'master': nodeurl = '%s/(%s)' % (self.baseurl, nodename) else: nodeurl = '%s/%s' % (self.baseurl, nodename) return Node(self.jenkins, nodeurl, nodename, node_dict={}) raise UnknownNode(nodename)
def create_node(self, name, node_dict): """ Create a new slave node :param str name: name of slave :param dict node_dict: node dict (See Node class) :return: node obj """ if name in self: return node = Node(jenkins_obj=self.jenkins, baseurl=None, nodename=name, node_dict=node_dict, poll=False) url = "%s/computer/doCreateItem?%s" % (self.jenkins.baseurl, urlencode(node.get_node_attributes())) data = {"json": urlencode(node.get_node_attributes())} self.jenkins.requester.post_and_confirm_status(url, data=data) self.poll() return self[name]
def _make_node(self, nodename): """ Creates an instance of Node for the given nodename. This function assumes the returned node exists. """ if nodename.lower() == 'master': nodeurl = '%s/(%s)' % (self.baseurl, nodename) else: nodeurl = '%s/%s' % (self.baseurl, nodename) return Node(self.jenkins, nodeurl, nodename, node_dict={})
def create_node(self, name, node_dict): """ Create a new slave node :param str name: name of slave :param dict node_dict: node dict (See Node class) :return: node obj """ if name in self: return self[name] node = Node(jenkins_obj=self.jenkins, baseurl=None, nodename=name, node_dict=node_dict, poll=False) url = ('%s/computer/doCreateItem?%s' % (self.jenkins.baseurl, urlencode(node.get_node_attributes()))) data = {'json': urlencode(node.get_node_attributes())} self.jenkins.requester.post_and_confirm_status(url, data=data) self.poll() return self[name]
def iteritems(self): for item in self._data['computer']: nodename = item['displayName'] if nodename.lower() == 'master': nodeurl = '%s/(%s)' % (self.baseurl, nodename) else: nodeurl = '%s/%s' % (self.baseurl, nodename) try: yield item['displayName'], Node(self.jenkins, nodeurl, nodename, node_dict={}) except Exception: raise JenkinsAPIException('Unable to iterate nodes')
def iteritems(self): for item in self._data['computer']: nodename = item['displayName'] if nodename.lower() == 'master': nodeurl = '%s/(%s)' % (self.baseurl, nodename) else: nodeurl = '%s/%s' % (self.baseurl, nodename) try: yield item['displayName'], Node(nodeurl, nodename, self.jenkins) except Exception: import ipdb ipdb.set_trace()
def create_node(self, name, num_executors=2, node_description=None, remote_fs='/var/lib/jenkins', labels=None, exclusive=False): """ Create a new slave node by name. :param name: fqdn of slave, str :param num_executors: number of executors, int :param node_description: a freetext field describing the node :param remote_fs: jenkins path, str :param labels: labels to associate with slave, str :param exclusive: tied to specific job, boolean :return: node obj """ NODE_TYPE = 'jenkins.slaves.DumbSlave$DescriptorImpl' MODE = 'NORMAL' if self.has_node(name): return Node(nodename=name, baseurl=self.get_node_url(nodename=name), jenkins_obj=self) if exclusive: MODE = 'EXCLUSIVE' params = { 'name': name, 'type': NODE_TYPE, 'json': json.dumps({ 'name': name, 'nodeDescription': node_description, 'numExecutors': num_executors, 'remoteFS': remote_fs, 'labelString': labels, 'mode': MODE, 'type': NODE_TYPE, 'retentionStrategy': { 'stapler-class': 'jenkins.slaves.RetentionStrategy$Always' }, 'nodeProperties': { 'stapler-class-bag': 'true' }, 'launcher': { 'stapler-class': 'jenkins.slaves.JNLPLauncher' } }) } url = "%(nodeurl)s/doCreateItem?%(params)s" % { 'nodeurl': self.get_node_url(), 'params': urllib.urlencode(params) } print url fn_urlopen = self.get_jenkins_obj().get_opener() try: fn_urlopen(url).read() except urllib2.HTTPError, e: log.debug("Error reading %s" % url) log.exception(e) raise
def get_node(self, nodename): """Get a node object for a specific node""" node_url = self.python_api_url(self.get_node_url(nodename)) return Node(node_url, nodename, jenkins_obj=self)
'remoteFS': remote_fs, 'labelString': labels, 'mode': MODE, 'type': NODE_TYPE, 'retentionStrategy': { 'stapler-class': 'jenkins.slaves.RetentionStrategy$Always' }, 'nodeProperties': { 'stapler-class-bag': 'true' }, 'launcher': { 'stapler-class': 'jenkins.slaves.JNLPLauncher' } }) } url = "%(nodeurl)s/doCreateItem?%(params)s" % { 'nodeurl': self.get_node_url(), 'params': urllib.urlencode(params) } print url fn_urlopen = self.get_jenkins_obj().get_opener() try: fn_urlopen(url).read() except urllib2.HTTPError, e: log.debug("Error reading %s" % url) log.exception(e) raise return Node(nodename=name, baseurl=self.get_node_url(nodename=name), jenkins_obj=self)