~free.ekanayaka/charms/trusty/landscape-client/drop-unit-name-from-juju-info

« back to all changes in this revision

Viewing changes to hooks/test_hooks.py

  • Committer: Adam Collard
  • Date: 2013-10-14 08:15:41 UTC
  • mfrom: (27.1.9 landscape-client)
  • Revision ID: adam.collard@canonical.com-20131014081541-sxhvjxz1oyu567fj
Merged lp:~adam-collard/charms/precise/landscape-client/upgrade-charm-hook [r=free.ekanayaka,bjornt]

Add an upgrade-charm hook which will move any Juju information found in the old metadata.d directory into the new juju-info.json file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import json
 
2
import os
 
3
from shutil import rmtree
 
4
from tempfile import mkdtemp
 
5
from unittest import TestCase
 
6
 
 
7
from landscape.deployment import Configuration
 
8
from hooks import migrate_old_juju_info
 
9
 
 
10
 
 
11
class MigrateOldJujuInfoTest(TestCase):
 
12
 
 
13
    def setUp(self):
 
14
        self.config = Configuration()
 
15
        self.config.data_path = mkdtemp(prefix=self.__class__.__name__)
 
16
        self.config.meta_data_path = os.path.join(
 
17
            self.config.data_path, "metadata.d")
 
18
        self.addCleanup(rmtree, self.config.data_path)
 
19
        os.makedirs(self.config.meta_data_path)
 
20
        super(MigrateOldJujuInfoTest, self).setUp()
 
21
 
 
22
    def setup_old_juju_info(self, juju_info):
 
23
        for filename, contents in juju_info.iteritems():
 
24
            juju_path = os.path.join(self.config.meta_data_path, filename)
 
25
            with open(juju_path, "w") as juju_file:
 
26
                juju_file.write(contents)
 
27
 
 
28
    def test_successful_migration(self):
 
29
        """
 
30
        Juju info in an old metadata.d directory is converted into a
 
31
        single JSON file.
 
32
        """
 
33
        juju_info = {"juju-env-uuid": "0afcd28d-6263-43fb-8131-afa696a984f8",
 
34
                     "juju-unit-name": "postgresql/0",
 
35
                     "juju-api-addresses": "10.0.3.1:17070",
 
36
                     "juju-private-address": "10.0.3.205"}
 
37
        self.setup_old_juju_info(juju_info)
 
38
        migrate_old_juju_info(self.config)
 
39
        juju_info_path = os.path.join(self.config.data_path, "juju-info.json")
 
40
        self.assertTrue(os.path.exists(juju_info_path))
 
41
        with open(juju_info_path) as juju_info_fh:
 
42
            juju_json_info = json.load(juju_info_fh)
 
43
        self.assertEqual(
 
44
            {"environment-uuid": "0afcd28d-6263-43fb-8131-afa696a984f8",
 
45
             "unit-name": "postgresql/0",
 
46
             "api-addresses": "10.0.3.1:17070",
 
47
             "private-address": "10.0.3.205"}, juju_json_info)
 
48
 
 
49
    def test_old_juju_info_deleted_after_successful_migration(self):
 
50
        """
 
51
        Old Juju files are deleted when the information is succesfully
 
52
        converted.
 
53
        """
 
54
        juju_info = {"juju-env-uuid": "0afcd28d-6263-43fb-8131-afa696a984f8",
 
55
                     "juju-unit-name": "postgresql/0",
 
56
                     "juju-api-addresses": "10.0.3.1:17070",
 
57
                     "juju-private-address": "10.0.3.205"}
 
58
        self.setup_old_juju_info(juju_info)
 
59
        migrate_old_juju_info(self.config)
 
60
        self.assertEqual([], os.listdir(self.config.meta_data_path))
 
61
 
 
62
    def test_incomplete_juju_info_is_error(self):
 
63
        """
 
64
        If only part of the Juju info is in meta-data.d directory then the
 
65
        function returns non-zero and the files aren't deleted.
 
66
        """
 
67
        juju_info = {"juju-env-uuid": "0afcd28d-6263-43fb-8131-afa696a984f8",
 
68
                     "juju-api-addresses": "10.0.3.1:17070",
 
69
                     "juju-private-address": "10.0.3.205"}
 
70
        self.setup_old_juju_info(juju_info)
 
71
        self.assertNotEqual(0, migrate_old_juju_info(self.config))
 
72
        self.assertGreater(len(os.listdir(self.config.meta_data_path)), 0)
 
73
 
 
74
    def test_idempotent(self):
 
75
        """
 
76
        Running the upgrade code more than once has no effect and is not
 
77
        an error.
 
78
        """
 
79
        juju_info = {"juju-env-uuid": "0afcd28d-6263-43fb-8131-afa696a984f8",
 
80
                     "juju-unit-name": "postgresql/0",
 
81
                     "juju-api-addresses": "10.0.3.1:17070",
 
82
                     "juju-private-address": "10.0.3.205"}
 
83
        self.setup_old_juju_info(juju_info)
 
84
        migrate_old_juju_info(self.config)
 
85
        self.assertEqual(0, migrate_old_juju_info(self.config))