'After a reboot the containers are only started once all datasets are
available locally.
We disable the dataset agent during reboot in order to ensure as
much as possible that the container agent gets stale data from the
control service.'

@run_test_with(async_runner(timeout=timedelta(minutes=20)))
@require_cluster(2, require_container_agent=True)
def test_reboot(self, cluster):

    node = [node for node in cluster.nodes if (node.public_address != cluster.control_node.public_address)][0]
    def query():
        d = query_http_server(node.public_address, 8080)
        d.addCallback(loads)
        return d
    creating_dataset = create_dataset(self, cluster, node=node)
    def created_dataset(dataset):
        return create_python_container(self, cluster, {u'ports': [{u'internal': 8080, u'external': 8080}], u'node_uuid': node.uuid, u'volumes': [{u'dataset_id': unicode(dataset.dataset_id), u'mountpoint': u'/data'}]}, SCRIPTS.child('remember_boot_id.py'), additional_arguments=[u'/data'])
    creating_dataset.addCallback(created_dataset)
    creating_dataset.addCallback((lambda _: query()))
    def got_initial_result(initial_result):
        self.addCleanup(node.run_script, 'enable_service', 'flocker-dataset-agent')
        d = node.run_script('disable_service', 'flocker-dataset-agent')
        d.addCallback((lambda _: node.reboot()))
        d.addCallback((lambda _: sleep(20)))
        changed = d.addCallback((lambda _: verify_socket(node.public_address, 22)))
        changed.addCallback((lambda _: loop_until(reactor, (lambda : is_process_running(node, 'flocker-container-agent')))))
        def up_again(_):
            sleep(10)
            return node.run_script('enable_service', 'flocker-dataset-agent')
        changed.addCallback(up_again)
        changed.addCallback((lambda _: loop_until(reactor, (lambda : query().addCallback((lambda result: (result != initial_result)))))))
        changed.addCallback((lambda _: query()))
        def result_changed(new_result):
            self.assertEqual(dict(current_same=(new_result['current'] == initial_result['current']), written_same=(new_result['written'] == initial_result['written'])), dict(current_same=False, written_same=True))
        changed.addCallback(result_changed)
        return changed
    creating_dataset.addCallback(got_initial_result)
    return creating_dataset
