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

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

class CartContainerTest(unittest.TestCase):
    TAG = "latest"
Aleksandr Guljajev's avatar
Aleksandr Guljajev committed
13
    COMMIT = ""
Alexandru Giurgiu's avatar
Alexandru Giurgiu committed
14
    container_name = Docker().random_container_name('carts')
15
    mongo_container_name = Docker().random_container_name('carts-db')
Phil Winder's avatar
Phil Winder committed
16 17 18
    def __init__(self, methodName='runTest'):
        super(CartContainerTest, self).__init__(methodName)
        self.ip = ""
19
        
Phil Winder's avatar
Phil Winder committed
20
    def setUp(self):
21
        Docker().start_container(container_name=self.mongo_container_name, image="mongo", host="carts-db")
Phil Winder's avatar
Phil Winder committed
22 23 24
        command = ['docker', 'run',
                   '-d',
                   '--name', CartContainerTest.container_name,
Alexandru Giurgiu's avatar
Alexandru Giurgiu committed
25
                   '-h', 'carts',
Phil Winder's avatar
Phil Winder committed
26 27
                   '--link',
                   CartContainerTest.mongo_container_name,
Alexandru Giurgiu's avatar
Alexandru Giurgiu committed
28
                   'weaveworksdemos/carts:' + self.COMMIT]
Phil Winder's avatar
Phil Winder committed
29 30 31 32 33 34 35 36
        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):
37 38
        limit = 30
        while Api().noResponse('http://' + self.ip + ':80/carts/'):
Phil Winder's avatar
Phil Winder committed
39 40 41 42
            if limit == 0:
                self.fail("Couldn't get the API running")
            limit = limit - 1
            sleep(1)
43
        
44
        out = Dredd().test_against_endpoint(
Alexandru Giurgiu's avatar
Alexandru Giurgiu committed
45
            "carts", "http://carts/",
46
            links=[self.mongo_container_name, self.container_name],
47
            env=[("MONGO_ENDPOINT", "mongodb://carts-db:27017/data")],
48
            dump_streams=True)
Phil Winder's avatar
Phil Winder committed
49 50
        self.assertGreater(out.find("0 failing"), -1)
        self.assertGreater(out.find("0 errors"), -1)
Aleksandr Guljajev's avatar
Aleksandr Guljajev committed
51
        print(out)
Phil Winder's avatar
Phil Winder committed
52 53 54

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
Aleksandr Guljajev's avatar
Aleksandr Guljajev committed
55 56
    default_tag = "latest"
    parser.add_argument('--tag', default=default_tag, help='The tag of the image to use. (default: latest)')
Phil Winder's avatar
Phil Winder committed
57 58 59
    parser.add_argument('unittest_args', nargs='*')
    args = parser.parse_args()
    CartContainerTest.TAG = args.tag
60

Aleksandr Guljajev's avatar
Aleksandr Guljajev committed
61 62
    if CartContainerTest.TAG == "":
        CartContainerTest.TAG = default_tag
63

Aleksandr Guljajev's avatar
Aleksandr Guljajev committed
64
    CartContainerTest.COMMIT = os.environ["COMMIT"]   
Phil Winder's avatar
Phil Winder committed
65 66 67
    # Now set the sys.argv to the unittest_args (leaving sys.argv[0] alone)
    sys.argv[1:] = args.unittest_args
    unittest.main()