def action_generate_nodes_fixture(params): try: from oslo.serialization import jsonutils except ImportError: from oslo_serialization import jsonutils from nailgun.logger import logger from nailgun.utils import fake_generator logger.info('Generating new nodes fixture...') total_nodes_count = params.total_nodes fixtures_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'nailgun/fixtures/') file_path = os.path.join( fixtures_dir, '{0}_fake_nodes_environment.json'.format(total_nodes_count)) generator = fake_generator.FakeNodesGenerator() res = generator.generate_fake_nodes( total_nodes_count, error_nodes_count=params.error_nodes, offline_nodes_count=params.offline_nodes, min_ifaces_num=params.min_ifaces_num) with open(file_path, 'w') as file_to_write: jsonutils.dump(res, file_to_write, indent=4) logger.info('Done. New fixture was stored in {0} file'.format(file_path))
def test_dump_namedtuple(self): expected = '[1, 2]' json_dict = collections.namedtuple("foo", "bar baz")(1, 2) fp = six.StringIO() jsonutils.dump(json_dict, fp) self.assertEqual(expected, fp.getvalue())
def test_dump(self): expected = '{"a": "b"}' json_dict = {'a': 'b'} fp = six.StringIO() jsonutils.dump(json_dict, fp) self.assertEqual(expected, fp.getvalue())
def setUp(self): super(DefaultPolicyTestCase, self).setUp() tmpfilename = self.get_temp_file_path('policy.json') self.rules = { "default": '', "example:exist": '!', } with open(tmpfilename, "w") as policyfile: jsonutils.dump(self.rules, policyfile) cfg.CONF.set_override('policy_file', tmpfilename) policy.refresh() self.addCleanup(policy.reset) self.context = context.Context('fake', 'fake')
def setUp(self): super(DefaultPolicyTestCase, self).setUp() self.tempdir = self.useFixture(fixtures.TempDir()) tmpfilename = self.tempdir.join('policy.json') self.rules = { "default": '', "example:exist": '!', } with open(tmpfilename, "w") as policyfile: jsonutils.dump(self.rules, policyfile) cfg.CONF.set_override('policy_file', tmpfilename) policy.refresh() self.addCleanup(policy.reset) self.context = context.Context('fake', 'fake')
def setUp(self): """Copy live policy.json file and convert all actions to allow users of the specified role only """ super(RoleBasedPolicyFixture, self).setUp() policy = jsonutils.load(open(CONF.policy_file)) # Convert all actions to require specified role for action, rule in policy.iteritems(): policy[action] = "role:%s" % self.role self.policy_dir = self.useFixture(fixtures.TempDir()) self.policy_file_name = os.path.join(self.policy_dir.path, "policy.json") with open(self.policy_file_name, "w") as policy_file: jsonutils.dump(policy, policy_file) CONF.set_override("policy_file", self.policy_file_name) nova.policy.reset() nova.policy.init() self.addCleanup(nova.policy.reset)
def setUp(self): """Copy live policy.json file and convert all actions to allow users of the specified role only """ super(RoleBasedPolicyFixture, self).setUp() policy = jsonutils.load(open(CONF.policy_file)) # Convert all actions to require specified role for action, rule in policy.iteritems(): policy[action] = 'role:%s' % self.role self.policy_dir = self.useFixture(fixtures.TempDir()) self.policy_file_name = os.path.join(self.policy_dir.path, 'policy.json') with open(self.policy_file_name, 'w') as policy_file: jsonutils.dump(policy, policy_file) CONF.set_override('policy_file', self.policy_file_name) nova.policy.reset() nova.policy.init() self.addCleanup(nova.policy.reset)
def mapping_to_file(self, port, mapping, ips, device_owner): """Mapping to file. Converts the port mapping into file. """ if device_owner == n_constants.DEVICE_OWNER_DHCP: ips.append(METADATA_DEFAULT_IP) mapping_dict = { "policy-space-name": mapping['ptg_tenant'], "endpoint-group-name": (mapping['app_profile_name'] + "|" + mapping['endpoint_group_name']), "interface-name": port.port_name, "ip": ips, "mac": port.vif_mac, "uuid": port.vif_id, "promiscuous-mode": mapping['promiscuous_mode']} if 'vm-name' in mapping: mapping_dict['attributes'] = {'vm-name': mapping['vm-name']} filename = self.epg_mapping_file % port.vif_id if not os.path.exists(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename)) with open(filename, 'w') as f: jsonutils.dump(mapping_dict, f)
def action_generate_nodes_fixture(params): from oslo.serialization import jsonutils from nailgun.logger import logger from nailgun.utils import fake_generator logger.info('Generating new nodes fixture...') total_nodes_count = params.total_nodes fixtures_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'nailgun/fixtures/') file_path = os.path.join( fixtures_dir, '{0}_fake_nodes_environment.json'.format(total_nodes_count) ) generator = fake_generator.FakeNodesGenerator() res = generator.generate_fake_nodes( total_nodes_count, error_nodes_count=params.error_nodes, offline_nodes_count=params.offline_nodes, min_ifaces_num=params.min_ifaces_num) with open(file_path, 'w') as file_to_write: jsonutils.dump(res, file_to_write, indent=4) logger.info('Done. New fixture was stored in {0} file'.format(file_path))
def test_dump_ipaddr(self): thing = {'ip_addr': netaddr.IPAddress('1.2.3.4')} fp = six.StringIO() jsonutils.dump(thing, fp) self.assertEqual('{"ip_addr": "1.2.3.4"}', fp.getvalue())