container.py 2.12 KB
Newer Older
Phil Winder's avatar
Phil Winder committed
1 2 3
import argparse
import sys
import unittest
4
from util.Api import Api
Phil Winder's avatar
Phil Winder committed
5 6 7 8 9 10 11 12 13 14 15 16
from time import sleep

from util.Docker import Docker
from util.Dredd import Dredd

class CartContainerTest(unittest.TestCase):
    TAG = "latest"
    container_name = Docker().random_container_name('cart')
    mongo_container_name = Docker().random_container_name('cart-db')
    def __init__(self, methodName='runTest'):
        super(CartContainerTest, self).__init__(methodName)
        self.ip = ""
17
        
Phil Winder's avatar
Phil Winder committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    def setUp(self):
        Docker().start_container(container_name=self.mongo_container_name, image="mongo", host="cart-db")
        command = ['docker', 'run',
                   '-d',
                   '--name', CartContainerTest.container_name,
                   '-h', 'cart',
                   '--link',
                   CartContainerTest.mongo_container_name,
                   'weaveworksdemos/cart:' + self.TAG]
        Docker().execute(command)
        self.ip = Docker().get_container_ip(CartContainerTest.container_name)

    def tearDown(self):
        Docker().kill_and_remove(CartContainerTest.container_name)
        Docker().kill_and_remove(CartContainerTest.mongo_container_name)

    def test_api_validated(self):
35 36
        limit = 30
        while Api().noResponse('http://' + self.ip + ':80/carts/'):
Phil Winder's avatar
Phil Winder committed
37 38 39 40
            if limit == 0:
                self.fail("Couldn't get the API running")
            limit = limit - 1
            sleep(1)
41 42
        
        out = Dredd().test_against_endpoint("cart", "http://cart/", links=[self.mongo_container_name, self.container_name], env=[("MONGO_ENDPOINT", "mongodb://cart-db:27017/data")])
Phil Winder's avatar
Phil Winder committed
43 44 45 46 47 48 49 50 51 52 53 54
        self.assertGreater(out.find("0 failing"), -1)
        self.assertGreater(out.find("0 errors"), -1)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--tag', default="latest", help='The tag of the image to use. (default: latest)')
    parser.add_argument('unittest_args', nargs='*')
    args = parser.parse_args()
    CartContainerTest.TAG = args.tag
    # Now set the sys.argv to the unittest_args (leaving sys.argv[0] alone)
    sys.argv[1:] = args.unittest_args
    unittest.main()