Example #1
0
    def test_start_container(self):
        docker = docker_api.from_env()

        # test that once an instance is started that it has an id, ip and key
        sawtooth_instance = SawtoothContainer()
        self.assertEqual(1, len(docker.containers.list()))
        self.assertIsNot(sawtooth_instance.id(), None)
        self.assertIsNot(sawtooth_instance.ip(), None)
        self.assertIsNot(sawtooth_instance.val_key(), None)

        container_ip = docker.containers.list()[0].exec_run("hostname -i").output.decode('utf-8').strip()
        container_val_key = docker.containers.list()[0].exec_run("cat {val_pub}".format(val_pub=VALIDATOR_KEY["pub"])) \
            .output.decode('utf-8').strip()
        container_user_key = docker.containers.list()[0].exec_run("cat {user_pub}".format(user_pub=USER_KEY["pub"])) \
            .output.decode('utf-8').strip()
        container_network = DEFAULT_DOCKER_NETWORK

        self.assertEqual(sawtooth_instance.id(), docker.containers.list()[0].id)
        self.assertEqual(sawtooth_instance.ip(), container_ip)
        self.assertEqual(sawtooth_instance.val_key(), container_val_key)
        self.assertEqual(sawtooth_instance.user_key(), container_user_key)
        self.assertNotEqual(sawtooth_instance.user_key(), sawtooth_instance.val_key())
        self.assertEqual(sawtooth_instance.attached_network(), container_network)

        number_of_running_processes = len(docker.containers.list()[0].top()['Processes'][0])
        # should only be 2 processes bash and tail -f /dev/null
        # each process has 4 columns so 2*4 = 8
        self.assertEqual(8, number_of_running_processes)

        # test that containers are made unique
        sawtooth_instance_2nd = SawtoothContainer()

        self.assertEqual(2, len(docker.containers.list()))
        # tests that the two instance to not have the same IP or Key
        self.assertNotEqual(sawtooth_instance.id(), sawtooth_instance_2nd.id())
        self.assertNotEqual(sawtooth_instance.ip(), sawtooth_instance_2nd.ip())
        self.assertNotEqual(sawtooth_instance.val_key(), sawtooth_instance_2nd.val_key())
        self.assertNotEqual(sawtooth_instance.user_key(), sawtooth_instance_2nd.user_key())

        # clean up
        docker.close()
Example #2
0
    def test_kill_container(self):
        docker = docker_api.from_env()

        sawtooth_instance = SawtoothContainer()
        self.assertEqual(1, len(docker.containers.list()))
        self.assertIn(sawtooth_instance.id(), get_container_ids())

        sawtooth_instance_2nd = SawtoothContainer()
        self.assertEqual(2, len(docker.containers.list()))
        self.assertIn(sawtooth_instance.id(), get_container_ids())
        self.assertIn(sawtooth_instance_2nd.id(), get_container_ids())

        # test that if one instance is stop only one instance stops
        del sawtooth_instance
        self.assertEqual(1, len(docker.containers.list()))
        self.assertIn(sawtooth_instance_2nd.id(), get_container_ids())

        del sawtooth_instance_2nd
        self.assertEqual(0, len(docker.containers.list()))

        # clean up
        docker.close()