11# This file is part of cloud-init. See LICENSE file for license information.
22
3- from cloudinit .cmd .devel import logs
4- from cloudinit .util import ensure_dir , load_file , subp , write_file
5- from cloudinit .tests .helpers import FilesystemMockingTestCase , wrap_and_call
63from datetime import datetime
7- import mock
84import os
5+ from six import StringIO
6+
7+ from cloudinit .cmd .devel import logs
8+ from cloudinit .sources import INSTANCE_JSON_SENSITIVE_FILE
9+ from cloudinit .tests .helpers import (
10+ FilesystemMockingTestCase , mock , wrap_and_call )
11+ from cloudinit .util import ensure_dir , load_file , subp , write_file
912
1013
14+ @mock .patch ('cloudinit.cmd.devel.logs.os.getuid' )
1115class TestCollectLogs (FilesystemMockingTestCase ):
1216
1317 def setUp (self ):
1418 super (TestCollectLogs , self ).setUp ()
1519 self .new_root = self .tmp_dir ()
1620 self .run_dir = self .tmp_path ('run' , self .new_root )
1721
18- def test_collect_logs_creates_tarfile (self ):
22+ def test_collect_logs_with_userdata_requires_root_user (self , m_getuid ):
23+ """collect-logs errors when non-root user collects userdata ."""
24+ m_getuid .return_value = 100 # non-root
25+ output_tarfile = self .tmp_path ('logs.tgz' )
26+ with mock .patch ('sys.stderr' , new_callable = StringIO ) as m_stderr :
27+ self .assertEqual (
28+ 1 , logs .collect_logs (output_tarfile , include_userdata = True ))
29+ self .assertEqual (
30+ 'To include userdata, root user is required.'
31+ ' Try sudo cloud-init collect-logs\n ' ,
32+ m_stderr .getvalue ())
33+
34+ def test_collect_logs_creates_tarfile (self , m_getuid ):
1935 """collect-logs creates a tarfile with all related cloud-init info."""
36+ m_getuid .return_value = 100
2037 log1 = self .tmp_path ('cloud-init.log' , self .new_root )
2138 write_file (log1 , 'cloud-init-log' )
2239 log2 = self .tmp_path ('cloud-init-output.log' , self .new_root )
2340 write_file (log2 , 'cloud-init-output-log' )
2441 ensure_dir (self .run_dir )
2542 write_file (self .tmp_path ('results.json' , self .run_dir ), 'results' )
43+ write_file (self .tmp_path (INSTANCE_JSON_SENSITIVE_FILE , self .run_dir ),
44+ 'sensitive' )
2645 output_tarfile = self .tmp_path ('logs.tgz' )
2746
2847 date = datetime .utcnow ().date ().strftime ('%Y-%m-%d' )
@@ -59,6 +78,11 @@ def fake_subp(cmd):
5978 # unpack the tarfile and check file contents
6079 subp (['tar' , 'zxvf' , output_tarfile , '-C' , self .new_root ])
6180 out_logdir = self .tmp_path (date_logdir , self .new_root )
81+ self .assertFalse (
82+ os .path .exists (
83+ os .path .join (out_logdir , 'run' , 'cloud-init' ,
84+ INSTANCE_JSON_SENSITIVE_FILE )),
85+ 'Unexpected file found: %s' % INSTANCE_JSON_SENSITIVE_FILE )
6286 self .assertEqual (
6387 '0.7fake\n ' ,
6488 load_file (os .path .join (out_logdir , 'dpkg-version' )))
@@ -82,8 +106,9 @@ def fake_subp(cmd):
82106 os .path .join (out_logdir , 'run' , 'cloud-init' , 'results.json' )))
83107 fake_stderr .write .assert_any_call ('Wrote %s\n ' % output_tarfile )
84108
85- def test_collect_logs_includes_optional_userdata (self ):
109+ def test_collect_logs_includes_optional_userdata (self , m_getuid ):
86110 """collect-logs include userdata when --include-userdata is set."""
111+ m_getuid .return_value = 0
87112 log1 = self .tmp_path ('cloud-init.log' , self .new_root )
88113 write_file (log1 , 'cloud-init-log' )
89114 log2 = self .tmp_path ('cloud-init-output.log' , self .new_root )
@@ -92,6 +117,8 @@ def test_collect_logs_includes_optional_userdata(self):
92117 write_file (userdata , 'user-data' )
93118 ensure_dir (self .run_dir )
94119 write_file (self .tmp_path ('results.json' , self .run_dir ), 'results' )
120+ write_file (self .tmp_path (INSTANCE_JSON_SENSITIVE_FILE , self .run_dir ),
121+ 'sensitive' )
95122 output_tarfile = self .tmp_path ('logs.tgz' )
96123
97124 date = datetime .utcnow ().date ().strftime ('%Y-%m-%d' )
@@ -132,4 +159,8 @@ def fake_subp(cmd):
132159 self .assertEqual (
133160 'user-data' ,
134161 load_file (os .path .join (out_logdir , 'user-data.txt' )))
162+ self .assertEqual (
163+ 'sensitive' ,
164+ load_file (os .path .join (out_logdir , 'run' , 'cloud-init' ,
165+ INSTANCE_JSON_SENSITIVE_FILE )))
135166 fake_stderr .write .assert_any_call ('Wrote %s\n ' % output_tarfile )
0 commit comments