Path: blob/develop/tests/unit/customizations/configure/test_writer.py
1569 views
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12import os13import tempfile14import shutil1516from awscli.customizations.configure.writer import ConfigFileWriter17from awscli.testutils import unittest, skip_if_windows181920class TestConfigFileWriter(unittest.TestCase):2122def setUp(self):23self.dirname = tempfile.mkdtemp()24self.config_filename = os.path.join(self.dirname, 'config')25self.writer = ConfigFileWriter()2627def tearDown(self):28shutil.rmtree(self.dirname)2930def assert_update_config(self, original_config_contents, updated_data,31updated_config_contents):32# Given the original_config, when it's updated with update_data,33# it should produce updated_config_contents.34with open(self.config_filename, 'w') as f:35f.write(original_config_contents)36self.writer.update_config(updated_data, self.config_filename)37with open(self.config_filename, 'r') as f:38new_contents = f.read()39if new_contents != updated_config_contents:40self.fail("Config file contents do not match.\n"41"Expected contents:\n"42"%s\n\n"43"Actual Contents:\n"44"%s\n" % (updated_config_contents, new_contents))4546def test_update_single_existing_value(self):47original = '[default]\nfoo = 1\nbar = 1'48updated = '[default]\nfoo = newvalue\nbar = 1'49self.assert_update_config(50original, {'foo': 'newvalue'}, updated)5152def test_update_value_with_square_brackets(self):53original = '[default]\nfoo = old[value]\nbar = 1'54updated = '[default]\nfoo = new[value]\nbar = 1'55self.assert_update_config(56original, {'foo': 'new[value]'}, updated)5758def test_update_single_existing_value_no_spaces(self):59original = '[default]\nfoo=1\nbar=1'60updated = '[default]\nfoo = newvalue\nbar=1'61self.assert_update_config(62original, {'foo': 'newvalue'}, updated)6364def test_update_single_new_values(self):65expected = '[default]\nfoo = 1\nbar = 2\nbaz = newvalue\n'66self.assert_update_config(67'[default]\nfoo = 1\nbar = 2',68{'baz': 'newvalue'}, expected)6970def test_handles_no_spaces(self):71expected = '[default]\nfoo=1\nbar=2\nbaz = newvalue\n'72self.assert_update_config(73'[default]\nfoo=1\nbar=2',74{'baz': 'newvalue'}, expected)7576def test_insert_values_in_middle_section(self):77original_contents = (78'[a]\n'79'foo = bar\n'80'baz = bar\n'81'\n'82'[b]\n'83'\n'84'foo = bar\n'85'[c]\n'86'foo = bar\n'87'baz = bar\n'88)89expected_contents = (90'[a]\n'91'foo = bar\n'92'baz = bar\n'93'\n'94'[b]\n'95'\n'96'foo = newvalue\n'97'[c]\n'98'foo = bar\n'99'baz = bar\n'100)101self.assert_update_config(102original_contents,103{'foo': 'newvalue', '__section__': 'b'},104expected_contents)105106def test_insert_new_value_in_middle_section(self):107original_contents = (108'[a]\n'109'foo = bar\n'110'\n'111'[b]\n'112'\n'113'foo = bar\n'114'\n'115'[c]\n'116'foo = bar\n'117)118expected_contents = (119'[a]\n'120'foo = bar\n'121'\n'122'[b]\n'123'\n'124'foo = bar\n'125'newvalue = newvalue\n'126'\n'127'[c]\n'128'foo = bar\n'129)130self.assert_update_config(131original_contents,132{'newvalue': 'newvalue', '__section__': 'b'},133expected_contents)134135def test_new_config_file(self):136self.assert_update_config(137'\n',138{'foo': 'value'},139'\n[default]\nfoo = value\n')140141def test_section_does_not_exist(self):142original_contents = (143'[notdefault]\n'144'foo = bar\n'145'baz = bar\n'146'\n'147'\n'148'\n'149'[other "section"]\n'150'\n'151'foo = bar\n'152)153appended_contents = (154'[default]\n'155'foo = value\n'156)157self.assert_update_config(158original_contents,159{'foo': 'value'},160original_contents + appended_contents)161162def test_config_file_does_not_exist(self):163self.writer.update_config({'foo': 'value'}, self.config_filename)164with open(self.config_filename, 'r') as f:165new_contents = f.read()166self.assertEqual(new_contents, '[default]\nfoo = value\n')167168@skip_if_windows("Test not valid on windows.")169def test_permissions_on_new_file(self):170self.writer.update_config({'foo': 'value'}, self.config_filename)171with open(self.config_filename, 'r') as f:172f.read()173self.assertEqual(os.stat(self.config_filename).st_mode & 0xFFF, 0o600)174175def test_update_config_with_comments(self):176original = (177'[default]\n'178'#foo = 1\n'179'bar = 1\n'180)181self.assert_update_config(182original, {'foo': 'newvalue'},183'[default]\n'184'#foo = 1\n'185'bar = 1\n'186'foo = newvalue\n'187)188189def test_update_config_with_commented_section(self):190original = (191'#[default]\n'192'[default]\n'193'#foo = 1\n'194'bar = 1\n'195)196self.assert_update_config(197original, {'foo': 'newvalue'},198'#[default]\n'199'[default]\n'200'#foo = 1\n'201'bar = 1\n'202'foo = newvalue\n'203)204205def test_spaces_around_key_names(self):206original = (207'[default]\n'208'foo = 1\n'209'bar = 1\n'210)211self.assert_update_config(212original, {'foo': 'newvalue'},213'[default]\n'214'foo = newvalue\n'215'bar = 1\n'216)217218def test_unquoted_profile_name(self):219original = (220'[profile foobar]\n'221'foo = 1\n'222'bar = 1\n'223)224self.assert_update_config(225original, {'foo': 'newvalue', '__section__': 'profile foobar'},226'[profile foobar]\n'227'foo = newvalue\n'228'bar = 1\n'229)230231def test_double_quoted_profile_name(self):232original = (233'[profile "foobar"]\n'234'foo = 1\n'235'bar = 1\n'236)237self.assert_update_config(238original, {'foo': 'newvalue', '__section__': 'profile foobar'},239'[profile "foobar"]\n'240'foo = newvalue\n'241'bar = 1\n'242)243244def test_profile_with_multiple_spaces(self):245original = (246'[profile "two spaces"]\n'247'foo = 1\n'248'bar = 1\n'249)250self.assert_update_config(251original, {252'foo': 'newvalue', '__section__': 'profile two spaces'},253'[profile "two spaces"]\n'254'foo = newvalue\n'255'bar = 1\n'256)257258def test_nested_attributes_new_file(self):259original = ''260self.assert_update_config(261original, {'__section__': 'default',262's3': {'signature_version': 's3v4'}},263'[default]\n'264's3 =\n'265' signature_version = s3v4\n')266267def test_add_to_nested_with_nested_in_the_middle(self):268original = (269'[default]\n'270's3 =\n'271' other = foo\n'272'ec2 = bar\n'273)274self.assert_update_config(275original, {'__section__': 'default',276's3': {'signature_version': 'newval'}},277'[default]\n'278's3 =\n'279' other = foo\n'280' signature_version = newval\n'281'ec2 = bar\n')282283def test_add_to_nested_with_nested_in_the_end(self):284original = (285'[default]\n'286's3 =\n'287' other = foo\n'288)289self.assert_update_config(290original, {'__section__': 'default',291's3': {'signature_version': 'newval'}},292'[default]\n'293's3 =\n'294' other = foo\n'295' signature_version = newval\n')296297def test_update_nested_attribute(self):298original = (299'[default]\n'300's3 =\n'301' signature_version = originalval\n'302)303self.assert_update_config(304original, {'__section__': 'default',305's3': {'signature_version': 'newval'}},306'[default]\n'307's3 =\n'308' signature_version = newval\n')309310def test_updated_nested_attribute_new_section(self):311original = (312'[default]\n'313's3 =\n'314' other = foo\n'315'[profile foo]\n'316'foo = bar\n'317)318self.assert_update_config(319original, {'__section__': 'default',320's3': {'signature_version': 'newval'}},321'[default]\n'322's3 =\n'323' other = foo\n'324' signature_version = newval\n'325'[profile foo]\n'326'foo = bar\n')327328def test_update_nested_attr_no_prior_nesting(self):329original = (330'[default]\n'331'foo = bar\n'332'[profile foo]\n'333'foo = bar\n'334)335self.assert_update_config(336original, {'__section__': 'default',337's3': {'signature_version': 'newval'}},338'[default]\n'339'foo = bar\n'340's3 =\n'341' signature_version = newval\n'342'[profile foo]\n'343'foo = bar\n')344345def test_can_handle_empty_section(self):346original = (347'[default]\n'348'[preview]\n'349'cloudfront = true\n'350)351self.assert_update_config(352original, {'region': 'us-west-2', '__section__': 'default'},353'[default]\n'354'region = us-west-2\n'355'[preview]\n'356'cloudfront = true\n'357)358359def test_appends_newline_on_new_section(self):360original = (361'[preview]\n'362'cloudfront = true'363)364self.assert_update_config(365original, {'region': 'us-west-2', '__section__': 'new-section'},366'[preview]\n'367'cloudfront = true\n'368'[new-section]\n'369'region = us-west-2\n'370)371372373