6
\$\begingroup\$

I am writing a unit test to be sure the code can connect to aws via boto, since I will be putting the credentials in place via some other process than code deployment. So far, I've only figured out how to check if I have a valid connection by looking at an S3 bucket, but am hoping someone has a more direct way to check.

import unittest
import boto


class TestTest(unittest.TestCase):
    def setUp(self):
        self.s3 = boto.connect_s3()

    def tearDown(self):
        pass

    def test_connection(self):
        assert self.s3 is not None

        # must be a better way to ensure connection works
        bucket = boto.s3.bucket.Bucket(connection=self.s3, name='test_this_bucket')
        all_keys = bucket.get_all_keys()
        assert len(all_keys) == 0
\$\endgroup\$
6
\$\begingroup\$

If you want just to test connection, checking boto.connect_s3() is good enough. According to the docs it raises an exception if something goes wrong.

In case if you want to do more advanced scenario, you can try to make another test with bucket creation and few keys inside.

I will structure the tests the following way:

import unittest
from time import time, sleep

import boto


class BasicConnectionTest(unittest.TestCase):
    def test_connection_works(self):
        # Please add some credentials if necessary
        connection = boto.connect_s3()
        # when using unittest, I'm trying to stick to helper methods
        # because they are more verbose
        self.assertIsNotNone(connection)


class AdvancedConnectionTest(unittest.TestCase):
    def setUp(self):
        self.connection = boto.connect_s3()

    def test_bucket_can_be_created(self):
        bucket = self.create_bucket()
        self.assertIsNotNone(bucket)
        self.assertEqual(len(bucket.get_all_keys()), 0)

    def test_keys_can_be_created(self):
        bucket = self.create_bucket()
        key = self.create_key(bucket, "mykey")
        key.set_contents_from_string("Hello World!")
        sleep(2)
        contents = key.get_contents_as_string()
        self.assertEqual(contents, "Hello World!")

    def create_bucket(self):
        bucket = self.connection.create_bucket('boto-demo-%s' % int(time()))
        self.addCleanup(bucket.delete)
        return bucket

    def create_key(self, bucket, name):
        key = bucket.new_key('mykey')
        self.addCleanup(key.delete)
        return key

In this case you will be safe that if something will go wrong in your tests, addCleanup will take care of removing bucket and key.

It is also possible to test some configs, e.g. http://boto.readthedocs.org/en/latest/ref/s3.html#boto.s3.bucket.Bucket.get_lifecycle_config

Just experiment :)

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ I would use the S3Connection library (boto.s3.connection.S3Connection) as it's more useful in terms of parameters that I can pass to it for e.g. I can pass an AWS profile and other params etc. \$\endgroup\$ – Raj Hassani Dec 1 '16 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.