~smoser/ubuntu/vivid/cloud-init/snappy

« back to all changes in this revision

Viewing changes to tests/unittests/test_handler/test_handler_chef.py

  • Committer: Scott Moser
  • Date: 2015-02-27 20:55:58 UTC
  • mfrom: (355.2.8 vivid)
  • Revision ID: smoser@ubuntu.com-20150227205558-glrwdgxqkaz6zyxa
* Merge with vivid at 0.7.7~bzr1067-0ubuntu1
* New upstream snapshot.
  * fix broken consumption of gzipped user-data (LP: #1424900)
  * functional user-data on Azure again (LP: #1423972)
  * CloudStack: support fetching password from virtual router (LP: #1422388)
* New upstream snapshot.
  * Fix for ascii decode in DataSourceAzure (LP: #1422993).
* New upstream snapshot.
  * support for gpt partitioning, utilized in Azure [Daniel Watkins]
  * fix bug in exception handling in mount_cb.
* New upstream snapshot.
  * move to python3 (LP: #1247132)
  * systemd: run cloud-init before systemd-user-sessions.service
  * Use the GCE short hostname. (LP: #1383794)
  * Enable user-data encoding support for GCE. (LP: #1404311)
  * Update to use a newer and better OMNIBUS_URL
  * Be more tolerant of 'ssh_authorized_keys' types
  * Fix parse_ssh_config failing in ssh_util.py
  * Increase the robustness/configurability of the chef module.
  * retain trailing newline from template files when using
    jinja2 (LP: #1355343)
  * fix broken output handling (LP: #1387340)
  * digital ocean datasource
  * update url in config drive documentation
  * freebsd: enable correct behavior on Ec2.
  * freebsd: Use the proper virtio FreeBSD network interface name.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import json
 
2
import os
 
3
 
 
4
from cloudinit.config import cc_chef
 
5
 
 
6
from cloudinit import cloud
 
7
from cloudinit import distros
 
8
from cloudinit import helpers
 
9
from cloudinit import util
 
10
from cloudinit.sources import DataSourceNone
 
11
 
 
12
from .. import helpers as t_help
 
13
 
 
14
import six
 
15
import logging
 
16
import shutil
 
17
import tempfile
 
18
 
 
19
LOG = logging.getLogger(__name__)
 
20
 
 
21
CLIENT_TEMPL = os.path.sep.join(["templates", "chef_client.rb.tmpl"])
 
22
 
 
23
 
 
24
class TestChef(t_help.FilesystemMockingTestCase):
 
25
    def setUp(self):
 
26
        super(TestChef, self).setUp()
 
27
        self.tmp = tempfile.mkdtemp()
 
28
        self.addCleanup(shutil.rmtree, self.tmp)
 
29
 
 
30
    def fetch_cloud(self, distro_kind):
 
31
        cls = distros.fetch(distro_kind)
 
32
        paths = helpers.Paths({})
 
33
        distro = cls(distro_kind, {}, paths)
 
34
        ds = DataSourceNone.DataSourceNone({}, distro, paths, None)
 
35
        return cloud.Cloud(ds, paths, {}, distro, None)
 
36
 
 
37
    def test_no_config(self):
 
38
        self.patchUtils(self.tmp)
 
39
        self.patchOS(self.tmp)
 
40
 
 
41
        cfg = {}
 
42
        cc_chef.handle('chef', cfg, self.fetch_cloud('ubuntu'), LOG, [])
 
43
        for d in cc_chef.CHEF_DIRS:
 
44
            self.assertFalse(os.path.isdir(d))
 
45
 
 
46
    @t_help.skipIf(not os.path.isfile(CLIENT_TEMPL),
 
47
                   CLIENT_TEMPL + " is not available")
 
48
    def test_basic_config(self):
 
49
        """
 
50
        test basic config looks sane
 
51
 
 
52
        # This should create a file of the format...
 
53
        # Created by cloud-init v. 0.7.6 on Sat, 11 Oct 2014 23:57:21 +0000
 
54
        log_level              :info
 
55
        ssl_verify_mode        :verify_none
 
56
        log_location           "/var/log/chef/client.log"
 
57
        validation_client_name "bob"
 
58
        validation_key         "/etc/chef/validation.pem"
 
59
        client_key             "/etc/chef/client.pem"
 
60
        chef_server_url        "localhost"
 
61
        environment            "_default"
 
62
        node_name              "iid-datasource-none"
 
63
        json_attribs           "/etc/chef/firstboot.json"
 
64
        file_cache_path        "/var/cache/chef"
 
65
        file_backup_path       "/var/backups/chef"
 
66
        pid_file               "/var/run/chef/client.pid"
 
67
        Chef::Log::Formatter.show_time = true
 
68
        """
 
69
        tpl_file = util.load_file('templates/chef_client.rb.tmpl')
 
70
        self.patchUtils(self.tmp)
 
71
        self.patchOS(self.tmp)
 
72
 
 
73
        util.write_file('/etc/cloud/templates/chef_client.rb.tmpl', tpl_file)
 
74
        cfg = {
 
75
            'chef': {
 
76
                'server_url': 'localhost',
 
77
                'validation_name': 'bob',
 
78
            },
 
79
        }
 
80
        cc_chef.handle('chef', cfg, self.fetch_cloud('ubuntu'), LOG, [])
 
81
        for d in cc_chef.CHEF_DIRS:
 
82
            self.assertTrue(os.path.isdir(d))
 
83
        c = util.load_file(cc_chef.CHEF_RB_PATH)
 
84
        for k, v in cfg['chef'].items():
 
85
            self.assertIn(v, c)
 
86
        for k, v in cc_chef.CHEF_RB_TPL_DEFAULTS.items():
 
87
            if isinstance(v, six.string_types):
 
88
                self.assertIn(v, c)
 
89
        c = util.load_file(cc_chef.CHEF_FB_PATH)
 
90
        self.assertEqual({}, json.loads(c))
 
91
 
 
92
    def test_firstboot_json(self):
 
93
        self.patchUtils(self.tmp)
 
94
        self.patchOS(self.tmp)
 
95
 
 
96
        cfg = {
 
97
            'chef': {
 
98
                'server_url': 'localhost',
 
99
                'validation_name': 'bob',
 
100
                'run_list': ['a', 'b', 'c'],
 
101
                'initial_attributes': {
 
102
                    'c': 'd',
 
103
                }
 
104
            },
 
105
        }
 
106
        cc_chef.handle('chef', cfg, self.fetch_cloud('ubuntu'), LOG, [])
 
107
        c = util.load_file(cc_chef.CHEF_FB_PATH)
 
108
        self.assertEqual(
 
109
            {
 
110
                'run_list': ['a', 'b', 'c'],
 
111
                'c': 'd',
 
112
            }, json.loads(c))
 
113
 
 
114
    @t_help.skipIf(not os.path.isfile(CLIENT_TEMPL),
 
115
                   CLIENT_TEMPL + " is not available")
 
116
    def test_template_deletes(self):
 
117
        tpl_file = util.load_file('templates/chef_client.rb.tmpl')
 
118
        self.patchUtils(self.tmp)
 
119
        self.patchOS(self.tmp)
 
120
 
 
121
        util.write_file('/etc/cloud/templates/chef_client.rb.tmpl', tpl_file)
 
122
        cfg = {
 
123
            'chef': {
 
124
                'server_url': 'localhost',
 
125
                'validation_name': 'bob',
 
126
                'json_attribs': None,
 
127
                'show_time': None,
 
128
            },
 
129
        }
 
130
        cc_chef.handle('chef', cfg, self.fetch_cloud('ubuntu'), LOG, [])
 
131
        c = util.load_file(cc_chef.CHEF_RB_PATH)
 
132
        self.assertNotIn('json_attribs', c)
 
133
        self.assertNotIn('Formatter.show_time', c)