4
from cloudinit.config import cc_chef
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
12
from .. import helpers as t_help
19
LOG = logging.getLogger(__name__)
21
CLIENT_TEMPL = os.path.sep.join(["templates", "chef_client.rb.tmpl"])
24
class TestChef(t_help.FilesystemMockingTestCase):
26
super(TestChef, self).setUp()
27
self.tmp = tempfile.mkdtemp()
28
self.addCleanup(shutil.rmtree, self.tmp)
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)
37
def test_no_config(self):
38
self.patchUtils(self.tmp)
39
self.patchOS(self.tmp)
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))
46
@t_help.skipIf(not os.path.isfile(CLIENT_TEMPL),
47
CLIENT_TEMPL + " is not available")
48
def test_basic_config(self):
50
test basic config looks sane
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
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
69
tpl_file = util.load_file('templates/chef_client.rb.tmpl')
70
self.patchUtils(self.tmp)
71
self.patchOS(self.tmp)
73
util.write_file('/etc/cloud/templates/chef_client.rb.tmpl', tpl_file)
76
'server_url': 'localhost',
77
'validation_name': 'bob',
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():
86
for k, v in cc_chef.CHEF_RB_TPL_DEFAULTS.items():
87
if isinstance(v, six.string_types):
89
c = util.load_file(cc_chef.CHEF_FB_PATH)
90
self.assertEqual({}, json.loads(c))
92
def test_firstboot_json(self):
93
self.patchUtils(self.tmp)
94
self.patchOS(self.tmp)
98
'server_url': 'localhost',
99
'validation_name': 'bob',
100
'run_list': ['a', 'b', 'c'],
101
'initial_attributes': {
106
cc_chef.handle('chef', cfg, self.fetch_cloud('ubuntu'), LOG, [])
107
c = util.load_file(cc_chef.CHEF_FB_PATH)
110
'run_list': ['a', 'b', 'c'],
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)
121
util.write_file('/etc/cloud/templates/chef_client.rb.tmpl', tpl_file)
124
'server_url': 'localhost',
125
'validation_name': 'bob',
126
'json_attribs': None,
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)