Repository: 2gis/k8s-handle

Issue:
Empty section passes validation
Empty section string (-s "") passes validation and causes not wrapped KeyError. 

Discussion:

Patch:
diff --git a/k8s_handle/config.py b/k8s_handle/config.py
index df08de4..17cdb33 100644
--- a/k8s_handle/config.py
+++ b/k8s_handle/config.py
@@ -156,6 +156,9 @@ def _update_context_recursively(context, include_history=[]):
 
 
 def load_context_section(section):
+    if not section:
+        raise RuntimeError('Empty section specification is not allowed')
+
     if section == settings.COMMON_SECTION_NAME:
         raise RuntimeError('Section "{}" is not intended to deploy'.format(settings.COMMON_SECTION_NAME))
 
@@ -163,7 +166,8 @@ def load_context_section(section):
 
     if context is None:
         raise RuntimeError('Config file "{}" is empty'.format(settings.CONFIG_FILE))
-    if section and section not in context:
+
+    if section not in context:
         raise RuntimeError('Section "{}" not found in config file "{}"'.format(section, settings.CONFIG_FILE))
 
     # delete all sections except common and used section


Test patch:
diff --git a/tests/test_config.py b/tests/test_config.py
index ce3e228..82e978e 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -65,6 +65,11 @@ class TestContextGeneration(unittest.TestCase):
             config.load_context_section('no_templates_section')
         self.assertTrue('Section "templates" or "kubectl" not found in config file' in str(context.exception))
 
+    def test_empty_section(self):
+        with self.assertRaises(RuntimeError) as context:
+            config.load_context_section('')
+        self.assertEqual('Empty section specification is not allowed', str(context.exception))
+
     def test_common_section(self):
         with self.assertRaises(RuntimeError) as context:
             config.load_context_section(settings.COMMON_SECTION_NAME)