def test_set_credential_file(self): ident = self.identity_class() user = "******" key = "fakeapikey" with utils.SelfDeletingTempfile() as tmpname: with open(tmpname, "wb") as ff: ff.write("[rackspace_cloud]\n") ff.write("username = %s\n" % user) ff.write("api_key = %s\n" % key) ident.set_credential_file(tmpname) self.assertEqual(ident.username, user) self.assertEqual(ident.api_key, key) # File doesn't exist self.assertRaises(exc.FileNotFound, ident.set_credential_file, "doesn't exist") # Missing section with utils.SelfDeletingTempfile() as tmpname: with open(tmpname, "wb") as ff: ff.write("user = x\n") self.assertRaises(exc.InvalidCredentialFile, ident.set_credential_file, tmpname) # Incorrect section with utils.SelfDeletingTempfile() as tmpname: with open(tmpname, "wb") as ff: ff.write("[bad_section]\nusername = x\napi_key = y\n") self.assertRaises(exc.InvalidCredentialFile, ident.set_credential_file, tmpname) # Incorrect option with utils.SelfDeletingTempfile() as tmpname: with open(tmpname, "wb") as ff: ff.write("[rackspace_cloud]\nuserbad = x\napi_key = y\n") self.assertRaises(exc.InvalidCredentialFile, ident.set_credential_file, tmpname)
def upload(fileobj, content_type, etag, headers): if isinstance(fileobj, basestring): # This is an empty directory file fsize = 0 else: fsize = get_file_size(fileobj) if fsize < self.max_file_size: # We can just upload it as-is. return self.connection.put_object(cont.name, obj_name, contents=fileobj, content_type=content_type, etag=etag, headers=headers) # Files larger than self.max_file_size must be segmented # and uploaded separately. num_segments = int(math.ceil(float(fsize) / self.max_file_size)) digits = int(math.log10(num_segments)) + 1 # NOTE: This could be greatly improved with threading or other # async design. for segment in xrange(num_segments): sequence = str(segment + 1).zfill(digits) seg_name = "%s.%s" % (fname, sequence) with utils.SelfDeletingTempfile() as tmpname: with open(tmpname, "wb") as tmp: tmp.write(fileobj.read(self.max_file_size)) with open(tmpname, "rb") as tmp: # We have to calculate the etag for each segment etag = utils.get_checksum(tmp) self.connection.put_object(cont.name, seg_name, contents=tmp, content_type=content_type, etag=etag, headers=headers) # Upload the manifest headers["X-Object-Meta-Manifest"] = "%s." % fname return self.connection.put_object(cont.name, fname, contents=None, headers=headers)
def test_self_deleting_temp_file(self): with utils.SelfDeletingTempfile() as tmp: self.assert_(isinstance(tmp, basestring)) self.assert_(os.path.exists(tmp)) self.assert_(os.path.isfile(tmp)) # File shoud be deleted after exiting the block self.assertFalse(os.path.exists(tmp))
def test_read_config_bad(self): sav_region = pyrax.default_region dummy_cfg = fakes.fake_config_file # Test invalid setting dummy_cfg = dummy_cfg.replace("custom_user_agent", "fake") sav_USER_AGENT = pyrax.USER_AGENT with utils.SelfDeletingTempfile() as cfgfile: open(cfgfile, "w").write(dummy_cfg) pyrax._read_config_settings(cfgfile) self.assertEqual(pyrax.USER_AGENT, sav_USER_AGENT) # Test bad file with utils.SelfDeletingTempfile() as cfgfile: open(cfgfile, "w").write("FAKE") self.assertRaises(exc.InvalidConfigurationFile, pyrax._read_config_settings, cfgfile) pyrax.default_region = sav_region pyrax.USER_AGENT = sav_USER_AGENT
def test_set_bad_credential_file(self): with utils.SelfDeletingTempfile() as tmpname: with open(tmpname, "wb") as tmp: tmp.write("[rackspace_cloud]\n") tmp.write("username = bad\n") tmp.write("api_key = creds\n") self.assertRaises(exc.AuthenticationFailed, pyrax.set_credential_file, tmpname) self.assertIsNone(pyrax.identity)
def test_set_credential_file(self): ident = self.rax_identity_class() user = "******" # Use percent signs in key to ensure it doesn't get interpolated. key = "fake%api%key" ident.authenticate = Mock() with utils.SelfDeletingTempfile() as tmpname: with open(tmpname, "wb") as ff: ff.write("[rackspace_cloud]\n") ff.write("username = %s\n" % user) ff.write("api_key = %s\n" % key) ident.set_credential_file(tmpname, authenticate=True) self.assertEqual(ident.username, user) self.assertEqual(ident.password, key) # Using 'password' instead of 'api_key' with utils.SelfDeletingTempfile() as tmpname: with open(tmpname, "wb") as ff: ff.write("[rackspace_cloud]\n") ff.write("username = %s\n" % user) ff.write("password = %s\n" % key) ident.set_credential_file(tmpname) self.assertEqual(ident.username, user) self.assertEqual(ident.password, key) # File doesn't exist self.assertRaises(exc.FileNotFound, ident.set_credential_file, "doesn't exist") # Missing section with utils.SelfDeletingTempfile() as tmpname: with open(tmpname, "wb") as ff: ff.write("user = x\n") self.assertRaises(exc.InvalidCredentialFile, ident.set_credential_file, tmpname) # Incorrect section with utils.SelfDeletingTempfile() as tmpname: with open(tmpname, "wb") as ff: ff.write("[bad_section]\nusername = x\napi_key = y\n") self.assertRaises(exc.InvalidCredentialFile, ident.set_credential_file, tmpname) # Incorrect option with utils.SelfDeletingTempfile() as tmpname: with open(tmpname, "wb") as ff: ff.write("[rackspace_cloud]\nuserbad = x\napi_key = y\n") self.assertRaises(exc.InvalidCredentialFile, ident.set_credential_file, tmpname)
def test_set_credential_file(self): with utils.SelfDeletingTempfile() as tmpname: with open(tmpname, "wb") as tmp: tmp.write("[rackspace_cloud]\n") tmp.write("username = %s\n" % self.username) tmp.write("api_key = %s\n" % self.password) pyrax.set_credential_file(tmpname) self.assertEqual(pyrax.identity.username, self.username) self.assertEqual(pyrax.identity.password, self.password) self.assert_(pyrax.identity.authenticated)
def test_set_bad_credential_file(self): with utils.SelfDeletingTempfile() as tmpname: with open(tmpname, "wb") as tmp: tmp.write(b"[keystone]\n") tmp.write(b"username = bad\n") tmp.write(b"password = creds\n") tmp.write(b"tenant_id = stuff\n") self.assertRaises(exc.AuthenticationFailed, pyrax.set_credential_file, tmpname) self.assertFalse(pyrax.identity.authenticated)
def test_get_checksum_from_file(self): test = "some random text" md = hashlib.md5() md.update(test) expected = md.hexdigest() with utils.SelfDeletingTempfile() as tmp: with open(tmp, "w") as testfile: testfile.write(test) with open(tmp, "r") as testfile: received = utils.get_checksum(testfile) self.assertEqual(expected, received)
def test_read_config(self): dummy_cfg = fakes.fake_config_file sav_region = pyrax.default_region sav_USER_AGENT = pyrax.USER_AGENT with utils.SelfDeletingTempfile() as cfgfile: open(cfgfile, "w").write(dummy_cfg) pyrax._read_config_settings(cfgfile) self.assertEqual(pyrax.default_region, "FAKE") self.assertTrue(pyrax.USER_AGENT.startswith("FAKE ")) pyrax.default_region = sav_region pyrax.USER_AGENT = sav_USER_AGENT
def test_set_credential_file(self): with utils.SelfDeletingTempfile() as tmpname: with open(tmpname, "w") as tmp: tmp.write("[keystone]\n") tmp.write("username = %s\n" % self.username) tmp.write("password = %s\n" % self.password) tmp.write("tenant_id = %s\n" % self.tenant_id) pyrax.set_credential_file(tmpname) self.assertEqual(pyrax.identity.username, self.username) self.assertEqual(pyrax.identity.password, self.password) self.assertTrue(pyrax.identity.authenticated)
def test_read_config(self): dummy_cfg = fakes.fake_config_file sav_region = pyrax.default_region sav_USER_AGENT = pyrax.USER_AGENT with utils.SelfDeletingTempfile() as cfgfile: with open(cfgfile, "w") as cfg: cfg.write(dummy_cfg) pyrax.settings.read_config(cfgfile) self.assertEqual(pyrax.get_setting("region"), "FAKE") self.assertTrue(pyrax.get_setting("user_agent").startswith("FAKE ")) pyrax.default_region = sav_region pyrax.USER_AGENT = sav_USER_AGENT
def test_read_config_creds(self): dummy_cfg = fakes.fake_config_file sav_region = pyrax.default_region sav_USER_AGENT = pyrax.USER_AGENT with utils.SelfDeletingTempfile() as cfgfile: with open(cfgfile, "w") as cfg: cfg.write(dummy_cfg) # Add password entry cfg.write("password = fake\n") with warnings.catch_warnings(record=True) as warn: pyrax.settings.read_config(cfgfile) self.assertEqual(len(warn), 1) pyrax.default_region = sav_region pyrax.USER_AGENT = sav_USER_AGENT
def test_set_credential_file_keystone(self): ident = pyrax.keystone_identity.KeystoneIdentity(username=self.username, password=self.password) user = "******" password = "******" tenant_id = "faketenantid" with utils.SelfDeletingTempfile() as tmpname: with file(tmpname, "wb") as ff: ff.write("[keystone]\n") ff.write("username = %s\n" % user) ff.write("password = %s\n" % password) ff.write("tenant_id = %s\n" % tenant_id) ident.set_credential_file(tmpname) self.assertEqual(ident.username, user) self.assertEqual(ident.password, password)
def test_upload_file(self): cont = self.container cont.client.connection.head_container = Mock() cont.client.connection.put_object = Mock() gobj = cont.client.get_object cont.client.get_object = Mock(return_value=self.fake_object) with utils.SelfDeletingTempfile() as tmpname: small_file_contents = "Test Value " * 25 cont.client.max_file_size = len(small_file_contents) + 1 with open(tmpname, "wb") as tmp: tmp.write(small_file_contents) fname = os.path.basename(tmpname) fake_type = "test/test" cont.upload_file(tmpname, content_type=fake_type) self.assertEqual(cont.client.connection.put_object.call_count, 1) cont.client.get_object = gobj
def test_upload_large_file(self): client = self.client client.connection.head_container = Mock() client.connection.put_object = Mock() cont = client.get_container(self.cont_name) gobj = client.get_object client.get_object = Mock(return_value=self.fake_object) with utils.SelfDeletingTempfile() as tmpname: small_file_contents = "Test Value " * 25 client.max_file_size = len(small_file_contents) - 1 with open(tmpname, "wb") as tmp: tmp.write(small_file_contents) fname = os.path.basename(tmpname) fake_type = "test/test" client.upload_file(cont, tmpname, content_type=fake_type) # Large files require 1 call for manifest, plus one for each # segment. This should be a 2-segment file upload. self.assertEqual(client.connection.put_object.call_count, 3) client.get_object = gobj
def store_object(self, container, obj_name, data, content_type=None, etag=None, content_encoding=None): """ Creates a new object in the specified container, and populates it with the given data. """ cont = self.get_container(container) headers = {} if content_encoding is not None: headers["Content-Encoding"] = content_encoding with utils.SelfDeletingTempfile() as tmp: with open(tmp, "wb") as tmpfile: try: tmpfile.write(data) except UnicodeEncodeError: udata = data.encode("utf-8") tmpfile.write(udata) with open(tmp, "rb") as tmpfile: self.connection.put_object(cont.name, obj_name, contents=tmpfile, content_type=content_type, etag=etag, headers=headers) return self.get_object(container, obj_name)
import pyrax.exceptions as exc import pyrax.utils as utils pyrax.set_setting("identity_type", "rackspace") creds_file = os.path.expanduser("~/.rackspace_cloud_credentials") pyrax.set_credential_file(creds_file) cf = pyrax.cloudfiles cont_name = pyrax.utils.random_name(8) cont = cf.create_container(cont_name) text = """First Line Indented Second Line Last Line""" # pyrax has a utility for creating temporary local files that clean themselves up. with utils.SelfDeletingTempfile() as tmpname: print "Creating text file with the following content:" print "-" * 44 print text print "-" * 44 with open(tmpname, "w") as tmp: tmp.write(text) nm = os.path.basename(tmpname) print print "Uploading file: %s" % nm cf.upload_file(cont, tmpname, content_type="text/text") # Let's verify that the file is there obj = cont.get_object(nm) print print "Stored Object:", obj print "Retrieved Content:"