'Given a dataset created and used with the previously installable
version of flocker, uninstalling the previous version of flocker and
installing HEAD does not destroy the data on the dataset.'

@skip_distribution(unsupported={'ubuntu-16.04'}, reason='Ubuntu-16.04    packages    cannot    be    downgraded    until    the    next    release.')
@skip_backend(unsupported={backends.LOOPBACK, backends.OPENSTACK}, reason="Loopback    backend    does    not    maintain    compute_instance_id    across    restarting    flocker    (and    didn't    as    of    most    recent    release).    OpenStack    backend    (as    of    1.15.0)    cannot    reliably    identify    the    compute    node    if    it    has    floating    IP    addresses.")
@run_test_with(async_runner(timeout=timedelta(minutes=6)))
@require_cluster(1)
def test_upgrade(self, cluster):

    node = cluster.nodes[0]
    SAMPLE_STR = ('123456' * 100)
    upgrade_from_version = get_installable_version(HEAD_FLOCKER_VERSION)
    d = cluster.client.version()
    original_package_source = [None]
    def setup_restore_original_flocker(version):
        version_bytes = version.get('flocker', u'').encode('ascii')
        original_package_source[0] = self._get_package_source(default_version=(version_bytes or None))
        self.addCleanup((lambda : cluster.install_flocker_version(original_package_source[0])))
        return version
    d.addCallback(setup_restore_original_flocker)
    d.addCallback((lambda _: cluster.clean_nodes()))
    d.addCallback((lambda _: cluster.install_flocker_version(PackageSource(version=upgrade_from_version), destroy_persisted_state=True)))
    d.addCallback((lambda _: create_dataset(self, cluster, node=node)))
    first_dataset = [None]
    def write_to_file(dataset):
        first_dataset[0] = dataset
        return node.run_as_root(['bash', '-c', ('echo    "%s"    >    %s' % (SAMPLE_STR, os.path.join(dataset.path.path, 'test.txt')))])
    d.addCallback(write_to_file)
    d.addCallback((lambda _: cluster.install_flocker_version(original_package_source[0])))
    d.addCallback((lambda _: create_dataset(self, cluster, node=node)))
    d.addCallback((lambda _: cluster.wait_for_dataset(first_dataset[0])))
    def cat_and_verify_file(dataset):
        output = []
        file_catting = node.run_as_root(['bash', '-c', ('cat    %s' % os.path.join(dataset.path.path, 'test.txt'))], handle_stdout=output.append)
        def verify_file(_):
            file_contents = ''.join(output)
            self.assertEqual(file_contents, SAMPLE_STR)
        file_catting.addCallback(verify_file)
        return file_catting
    d.addCallback(cat_and_verify_file)
    return d
