def test_host_list_find(self): args = self._mock_args( mode="find", object_type='node', ) t = tool.ToolCliFind(args) t._namedef = 'cp1048.example.com' res = [ node.Node('test', 'cache', 'ssl', 'cp1048.example.com'), node.Node('test', 'cache', 'http', 'cp1048.example.com'), ] t.entity.find = mock.MagicMock(return_value=res) tres = [o for o in t.host_list()] self.assertEquals(tres, res) t.entity.find.assert_called_with(t._namedef)
def test_host_list(self): mock_list = [] for i in range(10): mock_list.append( node.Node('dcA', 'clusterA', 'service{}'.format(i), 'foobar')) args = self._args() t = tool.ToolCliSimpleAction(args) t.entity.query = mock.MagicMock(return_value=mock_list) self.assertEqual(t.host_list(), mock_list)
def test_load_node(self): cluster = 'test' sdata = self.service_generator('espresso-machine', 2, initial=1) syncer.load_services(cluster, sdata.keys(), sdata, syncer.dummy_lock) serv = sdata.keys().pop() syncer.load_node('sofa', cluster, serv, 'one-off') n = node.Node('sofa', cluster, serv, 'one-off') self.assertTrue(n.exists) self.assertEquals(n.weight, sdata[serv]['default_values']['weight'])
def test_new_node(self, mocker): """New node creation""" mocker.return_value = 'default_value' self._mock_read(None) n = node.Node('dc', 'cluster', 'service', 'foo') # Test self.assertEqual(n.base_path(), 'pools') self.assertEqual(n.key, 'pools/dc/cluster/service/foo') self.assertFalse(n.exists) self.assertEqual(n.pooled, 'default_value') self.assertEqual(n.name, 'foo')
def test_create_returns_error(self): """ Test creation is not possible from confctl """ args = self.generate_args('set/pooled=yes cp1059') with self.assertRaises(SystemExit) as cm: tool.main(cmdline=args) # In error, we exit with status 1 self.assertEqual(cm.exception_code, 1) # This doesn't get announced to irc. self.assertEqual(self.irc.emit.called, False) n = node.Node('eqiad', 'cache_text', 'https', 'cp1059') self.assertFalse(n.exists)
def test_change_node_regexp(self): """ Changing values according to a regexp """ args = self.generate_args('set/pooled=yes re:cp105.') with self.assertRaises(SystemExit) as cm: tool.main(cmdline=args) self.assertEqual(cm.exception_code, 0) self.assertEqual(self.irc.emit.called, True) for hostname in ['cp1052', 'cp1053', 'cp1054', 'cp1055']: n = node.Node('eqiad', 'cache_text', 'https', hostname) self.assertTrue(n.exists) self.assertEqual(n.pooled, "yes")
def test_load_nodes(self): dc = 'sofa' cluster = 'test' sdata = self.service_generator('espresso-machine', 2) syncer.load_services(cluster, sdata.keys(), sdata, syncer.dummy_lock) data = self.node_generator(cluster, sdata.keys(), 20) syncer.load_nodes(dc, data, syncer.dummy_lock) for servname in sdata.keys(): for i in xrange(20): nodename = "node-%d" % i n = node.Node(dc, cluster, servname, nodename) self.assertTrue(n.exists) self.assertEquals(n.pooled, 'yes')
def test_remove_nodes(self): dc = 'testdc' cluster = 'test' services = self.service_generator('espresso-machine', 2) nodes = self.node_generator(cluster, list(services.keys()), 20) with temp_data(services, nodes) as basepath: sync = syncer.Syncer('/nonexistent', basepath) sync.load() nodes = self.node_generator(cluster, list(services.keys()), 10) with temp_data(services, nodes) as basepath: sync = syncer.Syncer('/nonexistent', basepath) sync.load() for servname in services: for i in range(10): nodename = "node-%d" % i n = node.Node(dc, cluster, servname, nodename) self.assertTrue(n.exists) for i in range(10, 20): nodename = "node-%d" % i n = node.Node(dc, cluster, servname, nodename) self.assertFalse(n.exists)
def test_host_multiple_services(self): """Set all services in a single host w/ and w/o the --host flag""" # The query return a single host with multiple services query_result = [ node.Node('dc', 'cluster', 'service_a', 'host_a'), node.Node('dc', 'cluster', 'service_b', 'host_a'), node.Node('dc', 'cluster', 'service_c', 'host_a') ] args = self._mock_args(selector='name=cp3009.esams.wmnet', host=False) cli = tool.ToolCliByLabel(args) cli._action = 'set' cli.entity.query = mock.MagicMock(return_value=query_result) # With args.host=False we expect raw_input question, answering yes with mock.patch('__builtin__.raw_input', return_value='y') as _raw: cli.host_list() _raw.assert_called_once_with('confctl>') # With args.host=False we expect raw_input question, answering no with mock.patch('__builtin__.raw_input', return_value='n') as _raw: self.assertRaises(SystemExit, cli.host_list) # With args.host=True we do not expect raw_input questions cli.args.host = True with mock.patch('__builtin__.raw_input') as _raw: cli.host_list() self.assertEquals(_raw.call_args_list, []) # Adding another host to the query result query_result.append(node.Node('dc', 'cluster', 'service_a', 'host_b')) cli.entity.query = mock.MagicMock(return_value=query_result) # With args.host=True we expect raw_input question, answering y with mock.patch('__builtin__.raw_input', return_value='y') as _raw: cli.host_list() _raw.assert_called_once_with('confctl>')
def test_tags(self): """Test tags are correctly reported""" self._mock_read({"pooled": "yes", "weight": 20}) n = node.Node('dc', 'cluster', 'service', 'foo') for k, v in n.tags.items(): self.assertEqual(k, v)
def test_failed_validation(self): """Test bad validation""" self._mock_read({"pooled": "maybe?", "weight": 20}) n = node.Node('dc', 'cluster', 'service', 'foo') self.assertEqual(n.pooled, "no")
def test_read(self): """Test that reading fetches correctly the values""" self._mock_read({"pooled": "yes", "weight": 20}) n = node.Node('dc', 'cluster', 'service', 'foo') self.assertEqual(n.weight, 20) self.assertEqual(n.pooled, "yes")
def delete_node(dc, cluster, servname, host): n = node.Node(dc, cluster, servname, host) if n.exists: print "%s: Removing node %s from cluster %s/%s" % (dc, host, cluster, servname) n.delete()
def load_node(dc, cluster, servname, host): n = node.Node(dc, cluster, servname, host) if not n.exists: print "%s: Creating node %s for cluster %s/%s" % (dc, host, cluster, servname) n.write()