def test_list(self):
        policy1 = factory.create_policy(self.tenant_id)
        policy2 = factory.create_policy(self.tenant_id)
        list_res = functional.run("policy list -t %s" % self.tenant_id)

        self.assertEqual(sorted([policy1, policy2]),
                         sorted(yaml.load(list_res['out'])['policies']))
    def test_create(self):
        policy = factory.create_policy(tenant_id=self.tenant_id)

        create_res = self.command('unusable_ip_range create',
                                  policy_id=policy['id'],
                                  offset=1,
                                  length=2)

        ip_range = factory.model('ip_range', create_res)
        self.assertShows('ip_range',
                          ip_range,
                          command_name="unusable_ip_range",
                          parameters="id=%s policy_id=%s" % (ip_range['id'],
                                                             policy['id']))
        update_res = self.command('unusable_ip_range update',
                                  policy_id=policy['id'],
                                  id=ip_range['id'],
                                  offset=10,
                                  length=122)
        updated_ip_range = factory.model('ip_range', update_res)
        self.assertShows('ip_range',
                         updated_ip_range,
                         command_name="unusable_ip_range",
                        parameters="id=%s policy_id=%s" % (ip_range['id'],
                                                           policy['id']))

        another_create_res = self.command('unusable_ip_range create',
                                          policy_id=policy['id'],
                                          offset=1,
                                          length=2)

        another_ip_range = factory.model('ip_range', another_create_res)

        list_res = functional.run("unusable_ip_range list"
                       " policy_id={0} -t {1}".format(policy['id'],
                           self.tenant_id))

        self.assertEqual(sorted([updated_ip_range, another_ip_range]),
                         sorted(yaml.load(list_res['out'])['ip_ranges']))

        self.command("unusable_ip_range delete",
                     policy_id=policy['id'],
                     id=ip_range['id'])

        parameters = ("policy_id=%s id=%s" % (policy['id'],
                                              ip_range['id']))
        self.assertResourceNotFound("ip_range",
                                    ip_range,
                                    command_name="unusable_ip_range",
                                    parameters=parameters)
    def test_crud(self):
        policy = factory.create_policy(tenant_id=self.tenant_id)

        create_res = self.command('unusable_ip_octet create',
                                  policy_id=policy['id'],
                                  octet=255)
        ip_octet = factory.model('ip_octet', create_res)
        self.assertShows('ip_octet',
                         ip_octet,
                         command_name="unusable_ip_octet",
                         parameters="id=%s policy_id=%s" % (ip_octet['id'],
                                                            policy['id']))

        update_res = self.command('unusable_ip_octet update',
                                  policy_id=policy['id'],
                                  id=ip_octet['id'],
                                  octet=200)
        updated_ip_octet = factory.model('ip_octet', update_res)
        self.assertShows('ip_octet', updated_ip_octet,
                          command_name="unusable_ip_octet",
                          parameters="id=%s policy_id=%s" % (ip_octet['id'],
                                                             policy['id']))

        another_create_res = self.command('unusable_ip_octet create',
                                          policy_id=policy['id'],
                                          octet=200)
        another_ip_octet = factory.model('ip_octet', another_create_res)

        list_res = functional.run("unusable_ip_octet list policy_id={0}"
                                  " -t {1}".format(policy['id'],
                                                   self.tenant_id))

        self.assertEqual(sorted([updated_ip_octet, another_ip_octet]),
                         sorted(yaml.load(list_res['out'])['ip_octets']))

        self.command("unusable_ip_octet delete",
                     policy_id=policy['id'],
                     id=ip_octet['id'])

        parameters = "policy_id=%s id=%s" % (policy['id'],
                                              ip_octet['id'])
        self.assertResourceNotFound("ip_octet",
                                    ip_octet,
                                    command_name='unusable_ip_octet',
                                    parameters=parameters)