def create_tempfiles(self, files, ext='.conf', default_encoding='utf-8'): """Safely create temporary files. :param files: Sequence of tuples containing (filename, file_contents). :type files: list of tuple :param ext: File name extension for the temporary file. :type ext: str :param default_encoding: Default file content encoding when it is not provided, used to decode the tempfile contents from a text string into a binary string. :type default_encoding: str :return: A list of str with the names of the files created. """ tempfiles = [] for f in files: if len(f) == 3: basename, contents, encoding = f else: basename, contents = f encoding = default_encoding fix = self.useFixture( createfile.CreateFileWithContent( filename=basename, contents=contents, ext=ext, encoding=encoding, )) tempfiles.append(fix.path) return tempfiles
def test_create_bad_encoding(self): f = createfile.CreateFileWithContent( "hrm", u'ಠ~ಠ', encoding='ascii', ) self.assertRaises(UnicodeError, f.setUp)
def setUp(self): """Set up common parts of all test-cases here.""" super(BaseAuditMiddlewareTest, self).setUp() global user_counter self.audit_map_file_fixture = self.useFixture( createfile.CreateFileWithContent('audit', audit_map_content_nova, ext=".yaml")) self.cfg = self.useFixture(cfg_fixture.Config()) self.msg = self.useFixture(msg_fixture.ConfFixture(self.cfg.conf)) self.cfg.conf([]) # service_name needs to be redefined by subclass self.service_name = None self.service_type = None self.project_id = str(uuid.uuid4().hex) self.user_id = str(uuid.uuid4().hex) self.username = "******" + str(user_counter) user_counter += 1 patcher = mock.patch('datadog.dogstatsd.DogStatsd._report') self.statsd_report_mock = patcher.start() self.addCleanup(patcher.stop)
def test_create_unicode_files(self): f = createfile.CreateFileWithContent( "no_approve", u'ಠ_ಠ', ) f.setUp() with open(f.path, 'rb') as f: contents = f.read() self.assertEqual(u'ಠ_ಠ', six.text_type(contents, encoding='utf-8'))
def setup_oslo_config_conf(testcase, content, conf_instance=None): conf_file_fixture = testcase.useFixture( createfile.CreateFileWithContent('barbican', content)) if conf_instance is None: conf_instance = cfg.CONF conf_instance([], project="barbican", default_config_files=[conf_file_fixture.path]) testcase.addCleanup(conf_instance.reset)
def test_create_unicode_files_encoding(self): f = createfile.CreateFileWithContent( "embarrassed", u'⊙﹏⊙', encoding='utf-8', ) f.setUp() with open(f.path, 'rb') as f: contents = f.read() self.assertEqual(u'⊙﹏⊙', six.text_type(contents, encoding='utf-8'))
def setUp(self): super(BaseAuditMiddlewareTest, self).setUp() self.audit_map_file_fixture = self.useFixture( createfile.CreateFileWithContent('audit', audit_map_content)) self.cfg = self.useFixture(cfg_fixture.Config()) self.msg = self.useFixture(msg_fixture.ConfFixture(self.cfg.conf)) self.cfg.conf([], project=self.PROJECT_NAME)
def setUp(self): super(TestAuthPluginLocalOsloConfig, self).setUp() self.project = uuid.uuid4().hex # NOTE(cdent): The options below are selected from those # which are statically registered by auth_token middleware # in the 'keystone_authtoken' group. Additional options, from # plugins, are registered dynamically so must not be used here. self.oslo_options = { 'www_authenticate_uri': uuid.uuid4().hex, 'identity_uri': uuid.uuid4().hex, } self.local_oslo_config = cfg.ConfigOpts() self.local_oslo_config.register_group( cfg.OptGroup(name='keystone_authtoken')) self.local_oslo_config.register_opts(_opts._OPTS, group='keystone_authtoken') self.local_oslo_config.register_opts(_auth.OPTS, group='keystone_authtoken') for option, value in self.oslo_options.items(): self.local_oslo_config.set_override(option, value, 'keystone_authtoken') self.local_oslo_config(args=[], project=self.project) self.file_options = { 'auth_type': 'password', 'www_authenticate_uri': uuid.uuid4().hex, 'password': uuid.uuid4().hex, } content = ("[DEFAULT]\n" "test_opt=15\n" "[keystone_authtoken]\n" "auth_type=%(auth_type)s\n" "www_authenticate_uri=%(www_authenticate_uri)s\n" "auth_url=%(www_authenticate_uri)s\n" "password=%(password)s\n" % self.file_options) self.conf_file_fixture = self.useFixture( createfile.CreateFileWithContent(self.project, content))
def test_ext(self): f = createfile.CreateFileWithContent('testing', '', ext='.ending') f.setUp() basename = os.path.basename(f.path) self.assertTrue(basename.endswith('.ending'))
def test_prefix(self): f = createfile.CreateFileWithContent('testing', '') f.setUp() basename = os.path.basename(f.path) self.assertTrue(basename.startswith('testing'))