Exemplo n.º 1
0
  def testEnvVarMustNotOverideMissingOptionInGet(self):
    # Test that a missing option is not replaced via environment variable in
    # Config.get()
    #
    # Config applies overrides only to existing options in order to stay
    # compatible with the rest of the ConfigParser API.
    section = "config_test_database"
    option = "no_such_option"

    configName = "test.conf"
    with self._redirectConfigBase(configName,
                                  _SAMPLE_CONF_CONTENTS) as baseConfigDir:
      c = config.Config(configName, baseConfigDir)

      # Check that it doesn't exist without the override
      with self.assertRaises(ConfigParser.NoOptionError):
        c.get(section, option)
      del c

      # Now, try with an override, and make sure that it still raises
      envVarName = config.Config("test.conf",
                                 baseConfigDir)._getEnvVarOverrideName(
        configName, section, option)

      overrideValues = {
        envVarName : "grok rules!"
      }

      with patch.dict(config.os.environ, values=overrideValues):
        c = config.Config(configName, baseConfigDir)

        with self.assertRaises(ConfigParser.NoOptionError):
          c.get(section, option)
Exemplo n.º 2
0
  def testEnvVarOveridesExistingOptionInGet(self):
    # Test override of an existing option via environment variable
    # in Config.get()
    configName = "test.conf"
    section = "config_test_database"
    option = "db"

    with self._redirectConfigBase(configName,
                                  _SAMPLE_CONF_CONTENTS) as baseConfigDir:
      c = config.Config(configName, baseConfigDir)
      self.assertEqual(c.get(section, option), "grok")
      del c

      # Now, try with an override
      envVarName = config.Config("test.conf",
                                 baseConfigDir)._getEnvVarOverrideName(
        configName, section, option)

      overrideValues = {
        envVarName : "grok rules!"
      }

      with patch.dict(config.os.environ, values=overrideValues):
        c = config.Config(configName, baseConfigDir)
        self.assertEqual(c.get(section, option), overrideValues[envVarName])
Exemplo n.º 3
0
  def testEnvVarOveridesExistingOptionWithBoolean(self):
    # Test override of an existing integer option via environment variable
    # in Config.getint()
    configName = "test.conf"
    section = "config_test_database"
    option = "port"

    with self._redirectConfigBase(configName,
                                  _SAMPLE_CONF_CONTENTS) as baseConfigDir:
      c = config.Config(configName, baseConfigDir)
      self.assertEqual(c.getint(section, option), 3306)
      del c

      # Now, try with an override
      envVarName = config.Config("test.conf",
                                 baseConfigDir)._getEnvVarOverrideName(
        configName, section, option)

      overrideValues = {
        envVarName : 'true'
      }

      with patch.dict(config.os.environ, values=overrideValues):
        c = config.Config(configName, baseConfigDir)
        self.assertEqual(c.getboolean(section, option), True)
Exemplo n.º 4
0
  def testReloadConfigAfterOverrideObjectIsCreated(self):
    configName = "test.conf"
    with self._redirectConfigBase(configName,
                                  _SAMPLE_CONF_CONTENTS) as baseConfigDir:
      c1 = config.Config(configName, baseConfigDir)
      self.assertEqual(c1.get("config_test_database", "db"), "grok")
      self.assertEqual(c1.get("rabbit", "host"), "localhost")


      c2 = config.Config(configName,
                         baseConfigDir,
                         mode=config.Config.MODE_OVERRIDE_ONLY)
      c2.add_section("config_test_database")
      c2.set("config_test_database", "new_option", "new_value")
      c2.add_section("rabbit")
      c2.set("rabbit", "host", "new_host")
      self.assertEqual(c2.get("config_test_database", "new_option"),
                       "new_value")
      c2.save()
      del c2

      # Check for "new_option" before and after reloading c1
      with self.assertRaises(ConfigParser.NoOptionError):
        c1.get("config_test_database", "new_option")

      # Reload config and verify that the new option is now found and
      # the overridden option has the new value
      c1.loadConfig()

      self.assertEqual(c1.get("config_test_database", "new_option"),
                       "new_value")
      self.assertEqual(c1.get("rabbit", "host"), "new_host")

      # and that the original option still has the expected value
      self.assertEqual(c1.get("config_test_database", "db"), "grok")
Exemplo n.º 5
0
  def testEnvVarMustNotOverideMissingOptionInItems(self):
    # Test that a missing option is not replaced via environment variable in
    # Config.items()
    #
    # Config applies overrides only to existing options in order to stay
    # compatible with the rest of the ConfigParser API.

    section = "config_test_database"
    option = "no_such_option"

    configName = "test.conf"
    with self._redirectConfigBase(configName,
                                  _SAMPLE_CONF_CONTENTS) as baseConfigDir:
      # Check that it doesn't exist without the override first
      c = config.Config(configName, baseConfigDir)
      originalItemsMap = dict(c.items(section))
      self.assertEqual(len(originalItemsMap), 5)
      self.assertNotIn(option, originalItemsMap)
      del c

      # Compare against ConfigParser.ConfigParser
      c = ConfigParser.ConfigParser()
      c.read(os.path.join(config.Config("test.conf", baseConfigDir)
                                .baseConfigDir,
                          configName))
      groundTruthItemsMap = dict(c.items(section))
      self.assertEqual(originalItemsMap, groundTruthItemsMap)
      del c

      # Now, try with an override, and make sure that it's still missing
      envVarName = config.Config("test.conf",
                                 baseConfigDir)._getEnvVarOverrideName(
        configName, section, option)

      overrideValues = {
        envVarName : "grok rules!"
      }

      with patch.dict(config.os.environ, values=overrideValues):
        c = config.Config(configName, baseConfigDir)
        newItemsMap = dict(c.items(section))
        self.assertNotIn(option, newItemsMap)
        del c

      # Verify that the other options were not altered
      self.assertEqual(newItemsMap, originalItemsMap)
    def testConfigNamePropertyGetter(self):

        configName = "test.conf"
        with self._redirectConfigBase(configName,
                                      _SAMPLE_CONF_CONTENTS) as baseConfigDir:
            conf = config.Config(configName, baseConfigDir)

            self.assertEqual(conf.configName, configName)
Exemplo n.º 7
0
  def testEnvVarOveridesExistingOptionInItems(self):
    # Test override of an existing option via environment variable
    # in Config.items()

    configName = "test.conf"
    section = "config_test_database"
    option = "db"

    with self._redirectConfigBase(configName,
                                  _SAMPLE_CONF_CONTENTS) as baseConfigDir:
      # Try without the override first
      c = config.Config(configName, baseConfigDir)
      originalItemsMap = dict(c.items(section))
      self.assertEqual(len(originalItemsMap), 5)
      self.assertEqual(originalItemsMap[option], "grok")
      del c

      # Compare against ConfigParser.ConfigParser
      c = ConfigParser.ConfigParser()
      c.read(os.path.join(config.Config("test.conf", baseConfigDir)
                                .baseConfigDir,
                          configName))
      groundTruthItemsMap = dict(c.items(section))
      self.assertEqual(originalItemsMap, groundTruthItemsMap)
      del c

      # Now, try with an override
      envVarName = config.Config("test.conf",
                                 baseConfigDir)._getEnvVarOverrideName(
        configName, section, option)

      overrideValues = {
        envVarName : "grok rules!"
      }

      with patch.dict(config.os.environ, values=overrideValues):
        c = config.Config(configName, baseConfigDir)
        newItemsMap = dict(c.items(section))
        self.assertEqual(newItemsMap[option], overrideValues[envVarName])
        del c

      # Verify that the other options were not altered
      newItemsMap.pop(option)
      originalItemsMap.pop(option)
      self.assertEqual(newItemsMap, originalItemsMap)
Exemplo n.º 8
0
  def testBaselineConfigObjectNotFound(self):
    # Expects ValueError if baseline config object isn't found

    configName = "test.conf"
    with self._redirectConfigBase(configName, contents=None) as baseConfigDir:
      with self.assertRaises(ValueError) as cm:
        config.Config(configName, baseConfigDir)

      self.assertIn("Baseline configuration object not found",
                    cm.exception.args[0])
Exemplo n.º 9
0
  def testLoadBaselineConfig(self):
    # Expects to find the baseline config object

    configName = "test.conf"
    with self._redirectConfigBase(configName,
                                  _SAMPLE_CONF_CONTENTS) as baseConfigDir:
      c = config.Config(configName, baseConfigDir)

      self.assertEqual(c.get("config_test_database", "db"), "grok")
      self.assertEqual(c.CONFIG_NAME, configName)
Exemplo n.º 10
0
  def testHierarchicalConfigNameTypeError(self):
    # Expects TypeError when given configName is hierarchical
    configName = "a/b/c/hieararchical_config_name.conf"
    filePath = os.path.join(self._tempDir, configName)

    with self.assertRaises(TypeError) as cm:
      config.Config(filePath, self._tempDir)

    self.assertIn("Hierarchical configuration object names not supported",
                  cm.exception.args[0])
Exemplo n.º 11
0
 def testGetEnvVarOverrideName(self):
   # Test Config.getEnvVarOverrideName
   configName = "model-swapper.conf"
   with self._redirectConfigBase(configName,
                                 _SAMPLE_CONF_CONTENTS) as baseConfigDir:
     envVarName = config.Config("model-swapper.conf",
                                baseConfigDir)._getEnvVarOverrideName(
       "model-swapper.conf", "interface_bus", "input_queue")
     self.assertEqual(
       envVarName,
       "_NTA_UTILS_CONFIG__MODEL_SWAPPER__INTERFACE_BUS__INPUT_QUEUE")
    def testRepr(self):

        configName = "test.conf"
        with self._redirectConfigBase(configName,
                                      _SAMPLE_CONF_CONTENTS) as baseConfigDir:
            conf = config.Config(configName, baseConfigDir)

            reprText = repr(conf)
            self.assertEqual(
                reprText, "Config<name=test.conf, mode=1, baseDir={}>".format(
                    baseConfigDir))