def _init_cluster(self, cfg):
        """Initializes the cfg object"""
        me = netutils.Hostname()
        ip = constants.IP4_ADDRESS_LOCALHOST
        # master_ip must not conflict with the node ip address
        master_ip = "127.0.0.2"

        cluster_config = objects.Cluster(
            serial_no=1,
            rsahostkeypub="",
            dsahostkeypub="",
            highest_used_port=(constants.FIRST_DRBD_PORT - 1),
            mac_prefix="aa:00:00",
            volume_group_name="xenvg",
            drbd_usermode_helper="/bin/true",
            nicparams={constants.PP_DEFAULT: constants.NICC_DEFAULTS},
            ndparams=constants.NDC_DEFAULTS,
            tcpudp_port_pool=set(),
            enabled_hypervisors=[constants.HT_FAKE],
            master_node=me.name,
            master_ip=master_ip,
            master_netdev=constants.DEFAULT_BRIDGE,
            cluster_name="cluster.local",
            file_storage_dir="/tmp",
            uid_pool=[],
        )

        master_node_config = objects.Node(name=me.name,
                                          primary_ip=me.ip,
                                          secondary_ip=ip,
                                          serial_no=1,
                                          master_candidate=True)

        bootstrap.InitConfig(constants.CONFIG_VERSION, cluster_config,
                             master_node_config, self.cfg_file)
    def testAssignGroupNodes(self):
        me = netutils.Hostname()
        cfg = self._get_object()

        # Create two groups
        grp1 = objects.NodeGroup(name="grp1",
                                 members=[],
                                 uuid="2f2fadf7-2a70-4a23-9ab5-2568c252032c")
        grp1_serial = 1
        cfg.AddNodeGroup(grp1, "job")

        grp2 = objects.NodeGroup(name="grp2",
                                 members=[],
                                 uuid="798d0de3-680f-4a0e-b29a-0f54f693b3f1")
        grp2_serial = 1
        cfg.AddNodeGroup(grp2, "job")
        self.assertEqual(
            set(
                map(operator.attrgetter("name"),
                    cfg.GetAllNodeGroupsInfo().values())),
            set(["grp1", "grp2", constants.INITIAL_NODE_GROUP_NAME]))

        # No-op
        cluster_serial = cfg.GetClusterInfo().serial_no
        cfg.AssignGroupNodes([])
        cluster_serial += 1

        # Create two nodes
        node1 = objects.Node(name="node1",
                             group=grp1.uuid,
                             ndparams={},
                             uuid="node1-uuid")
        node1_serial = 1
        node2 = objects.Node(name="node2",
                             group=grp2.uuid,
                             ndparams={},
                             uuid="node2-uuid")
        node2_serial = 1
        cfg.AddNode(node1, "job")
        cfg.AddNode(node2, "job")
        cluster_serial += 2
        self.assertEqual(
            set(cfg.GetNodeList()),
            set([
                "node1-uuid", "node2-uuid",
                cfg.GetNodeInfoByName(me.name).uuid
            ]))

        (grp1, grp2) = [cfg.GetNodeGroup(grp.uuid) for grp in (grp1, grp2)]

        def _VerifySerials():
            self.assertEqual(cfg.GetClusterInfo().serial_no, cluster_serial)
            self.assertEqual(node1.serial_no, node1_serial)
            self.assertEqual(node2.serial_no, node2_serial)
            self.assertEqual(grp1.serial_no, grp1_serial)
            self.assertEqual(grp2.serial_no, grp2_serial)

        _VerifySerials()

        self.assertEqual(set(grp1.members), set(["node1-uuid"]))
        self.assertEqual(set(grp2.members), set(["node2-uuid"]))

        # Check invalid nodes and groups
        self.assertRaises(errors.ConfigurationError, cfg.AssignGroupNodes, [
            ("unknown.node.example.com", grp2.uuid),
        ])
        self.assertRaises(errors.ConfigurationError, cfg.AssignGroupNodes, [
            (node1.name, "unknown-uuid"),
        ])

        self.assertEqual(node1.group, grp1.uuid)
        self.assertEqual(node2.group, grp2.uuid)
        self.assertEqual(set(grp1.members), set(["node1-uuid"]))
        self.assertEqual(set(grp2.members), set(["node2-uuid"]))

        # Another no-op
        cfg.AssignGroupNodes([])
        cluster_serial += 1
        _VerifySerials()

        # Assign to the same group (should be a no-op)
        self.assertEqual(node2.group, grp2.uuid)
        cfg.AssignGroupNodes([
            (node2.uuid, grp2.uuid),
        ])
        cluster_serial += 1
        self.assertEqual(node2.group, grp2.uuid)
        _VerifySerials()
        self.assertEqual(set(grp1.members), set(["node1-uuid"]))
        self.assertEqual(set(grp2.members), set(["node2-uuid"]))

        # Assign node 2 to group 1
        self.assertEqual(node2.group, grp2.uuid)
        cfg.AssignGroupNodes([
            (node2.uuid, grp1.uuid),
        ])
        (grp1, grp2) = [cfg.GetNodeGroup(grp.uuid) for grp in (grp1, grp2)]
        (node1,
         node2) = [cfg.GetNodeInfo(node.uuid) for node in (node1, node2)]
        cluster_serial += 1
        node2_serial += 1
        grp1_serial += 1
        grp2_serial += 1
        self.assertEqual(node2.group, grp1.uuid)
        _VerifySerials()
        self.assertEqual(set(grp1.members), set(["node1-uuid", "node2-uuid"]))
        self.assertFalse(grp2.members)

        # And assign both nodes to group 2
        self.assertEqual(node1.group, grp1.uuid)
        self.assertEqual(node2.group, grp1.uuid)
        self.assertNotEqual(grp1.uuid, grp2.uuid)
        cfg.AssignGroupNodes([
            (node1.uuid, grp2.uuid),
            (node2.uuid, grp2.uuid),
        ])
        (grp1, grp2) = [cfg.GetNodeGroup(grp.uuid) for grp in (grp1, grp2)]
        (node1,
         node2) = [cfg.GetNodeInfo(node.uuid) for node in (node1, node2)]
        cluster_serial += 1
        node1_serial += 1
        node2_serial += 1
        grp1_serial += 1
        grp2_serial += 1
        self.assertEqual(node1.group, grp2.uuid)
        self.assertEqual(node2.group, grp2.uuid)
        _VerifySerials()
        self.assertFalse(grp1.members)
        self.assertEqual(set(grp2.members), set(["node1-uuid", "node2-uuid"]))