def test_root_dir_is_required(self): """ At minimum, the caller must provide a 'ROOT_DIR' setting. """ try: read_config("carbon-foo", FakeOptions()) except ValueError, e: self.assertEqual("ROOT_DIR needs to be provided.", e.message)
def test_root_dir_is_required(self): """ At minimum, the caller must provide a 'ROOT_DIR' setting. """ try: read_config("carbon-foo", FakeOptions(config=None)) except CarbonConfigException, e: self.assertEqual("Either ROOT_DIR or GRAPHITE_ROOT " "needs to be provided.", str(e))
def test_config_dir_from_environment(self): """ If the 'GRAPHITE_CONFIG_DIR' variable is set in the environment, then 'CONFIG_DIR' will be set to that directory. """ root_dir = mkdtemp() self.addCleanup(rmtree, root_dir) conf_dir = join(root_dir, "configs", "production") makedirs(conf_dir) self.makeFile(content="[foo]", basename="carbon.conf", dirname=conf_dir) orig_value = os.environ.get("GRAPHITE_CONF_DIR", None) if orig_value is not None: self.addCleanup(os.environ.__setitem__, "GRAPHITE_CONF_DIR", orig_value) else: self.addCleanup(os.environ.__delitem__, "GRAPHITE_CONF_DIR") os.environ["GRAPHITE_CONF_DIR"] = conf_dir settings = read_config("carbon-foo", FakeOptions(config=None, instance=None, pidfile=None, logdir=None), ROOT_DIR=root_dir) self.assertEqual(conf_dir, settings.CONF_DIR)
def test_config_is_not_required(self): """ If the '--config' option is not provided, it defaults to ROOT_DIR/conf/carbon.conf. """ root_dir = self.makeDir() conf_dir = join(root_dir, "conf") makedirs(conf_dir) self.makeFile(content="[foo]", basename="carbon.conf", dirname=conf_dir) options = FakeOptions(config=None, instance=None, pidfile=None, logdir=None) read_config("carbon-foo", options, ROOT_DIR=root_dir) self.assertEqual(join(root_dir, "conf", "carbon.conf"), options["config"])
def test_write_strategy_naive(self): """Create a metric cache, insert metrics, ensure naive writes""" config = self.makeFile(content="[foo]\nCACHE_WRITE_STRATEGY = naive") settings = read_config("carbon-foo", FakeOptions(config=config, instance=None, pidfile=None, logdir=None), ROOT_DIR="foo") cache = _MetricCache(method=settings.CACHE_WRITE_STRATEGY) self.assertEqual("naive", cache.method)
def test_pidfile_in_options_has_precedence(self): """ The 'pidfile' option from command line overrides the default setting. """ config = self.makeFile(content="[foo]") settings = read_config( "carbon-foo", FakeOptions(config=config, instance=None, pidfile="foo.pid", logdir=None), ROOT_DIR="foo") self.assertEqual("foo.pid", settings.pidfile)
def test_conf_dir_defaults_to_config_dirname(self): """ The 'CONF_DIR' setting defaults to the parent directory of the provided configuration file. """ config = self.makeFile(content="[foo]") settings = read_config( "carbon-foo", FakeOptions(config=config, instance=None, pidfile=None, logdir=None), ROOT_DIR="foo") self.assertEqual(dirname(config), settings.CONF_DIR)
def test_storage_dir_relative_to_root_dir(self): """ The 'STORAGE_DIR' setting defaults to the 'storage' directory relative to the 'ROOT_DIR' setting. """ config = self.makeFile(content="[foo]") settings = read_config( "carbon-foo", FakeOptions(config=config, instance=None, pidfile=None, logdir=None), ROOT_DIR="foo") self.assertEqual(join("foo", "storage"), settings.STORAGE_DIR)
def test_log_dir_from_config(self): """ Providing a 'LOG_DIR' in the configuration file overrides the storage-relative default. """ config = self.makeFile(content="[foo]\nLOG_DIR = baz") settings = read_config( "carbon-foo", FakeOptions(config=config, instance=None, pidfile=None, logdir=None), ROOT_DIR="foo") self.assertEqual("baz", settings.LOG_DIR)
def test_cache_write_strategy_from_config(self): """ Providing a 'CACHE_WRITE_STRATEGY' in the configuration file overrides the 'sorted' default. """ config = self.makeFile(content="[foo]\nCACHE_WRITE_STRATEGY = naive") settings = read_config( "carbon-foo", FakeOptions(config=config, instance=None, pidfile=None, logdir=None), ROOT_DIR="foo") self.assertEqual("naive", settings.CACHE_WRITE_STRATEGY)
def test_log_dir_for_instance_from_options(self): """ Providing a 'LOG_DIR' in the command line overrides the storage-relative default for the instance. """ config = self.makeFile(content="[foo]") settings = read_config( "carbon-foo", FakeOptions(config=config, instance="x", pidfile=None, logdir="baz"), ROOT_DIR="foo") self.assertEqual("baz", settings.LOG_DIR)
def test_log_dir_as_provided(self): """ Providing a 'LOG_DIR' in defaults overrides the storage-relative default. """ config = self.makeFile(content="[foo]") settings = read_config( "carbon-foo", FakeOptions(config=config, instance=None, pidfile=None, logdir=None), ROOT_DIR="foo", STORAGE_DIR="bar", LOG_DIR='baz') self.assertEqual("baz", settings.LOG_DIR)
def test_pidfile_relative_to_storage_dir(self): """ The 'pidfile' setting defaults to a program-specific filename relative to the 'STORAGE_DIR' setting. """ config = self.makeFile(content="[foo]") settings = read_config( "carbon-foo", FakeOptions(config=config, instance=None, pidfile=None, logdir=None), ROOT_DIR="foo") self.assertEqual(join("foo", "storage", "carbon-foo.pid"), settings.pidfile)
def test_log_dir_relative_to_provided_storage_dir(self): """ Providing a different 'STORAGE_DIR' in defaults overrides the default of being relative to 'ROOT_DIR'. """ config = self.makeFile(content="[foo]") settings = read_config( "carbon-foo", FakeOptions(config=config, instance=None, pidfile=None, logdir=None), ROOT_DIR="foo", STORAGE_DIR="bar") self.assertEqual(join("bar", "log", "carbon-foo"), settings.LOG_DIR)
def test_local_data_dir_depends_on_storage_dir(self): """ Tests 'STORAGE_DIR' dependency 'LOCAL_DATA_DIR' """ config = self.makeFile( content=("[foo]\n" "STORAGE_DIR = bar")) settings = read_config( "carbon-foo", FakeOptions(config=config, instance=None, pidfile=None, logdir=None), ROOT_DIR="foo") self.assertEqual(join("bar", "whisper"), settings.LOCAL_DATA_DIR)
def test_whitelists_dir_depends_on_storage_dir(self): """ Tests 'STORAGE_DIR' dependency 'WHITELISTS_DIR' """ config = self.makeFile( content=("[foo]\n" "STORAGE_DIR = bar")) settings = read_config( "carbon-foo", FakeOptions(config=config, instance=None, pidfile=None, logdir=None), ROOT_DIR="foo") self.assertEqual(join("bar", "lists"), settings.WHITELISTS_DIR)
def test_storage_dir_as_provided(self): """ Providing a 'STORAGE_DIR' in defaults overrides the root-relative default. """ config = self.makeFile(content="[foo]") settings = read_config("carbon-foo", FakeOptions(config=config, instance=None, pidfile=None, logdir=None), ROOT_DIR="foo", STORAGE_DIR="bar") self.assertEqual("bar", settings.STORAGE_DIR)
def test_log_dir_relative_to_provided_storage_dir(self): """ Providing a different 'STORAGE_DIR' in defaults overrides the default of being relative to 'ROOT_DIR'. """ config = self.makeFile(content="[foo]") settings = read_config("carbon-foo", FakeOptions(config=config, instance=None, pidfile=None, logdir=None), ROOT_DIR="foo", STORAGE_DIR="bar") self.assertEqual(join("bar", "log", "carbon-foo"), settings.LOG_DIR)
def test_log_dir_for_instance_relative_to_storage_dir(self): """ The 'LOG_DIR' setting defaults to a program-specific directory relative to the 'STORAGE_DIR' setting. In the case of an instance, the instance name is appended to the directory. """ config = self.makeFile(content="[foo]") settings = read_config( "carbon-foo", FakeOptions(config=config, instance="x", pidfile=None, logdir=None), ROOT_DIR="foo") self.assertEqual(join("foo", "storage", "log", "carbon-foo-x"), settings.LOG_DIR)
def test_pidfile_relative_to_storage_dir(self): """ The 'pidfile' setting defaults to a program-specific filename relative to the 'STORAGE_DIR' setting. """ config = self.makeFile(content="[foo]") settings = read_config("carbon-foo", FakeOptions(config=config, instance=None, pidfile=None, logdir=None), ROOT_DIR="foo") self.assertEqual(join("foo", "storage", "carbon-foo.pid"), settings.pidfile)
def test_log_dir_for_instance_relative_to_storage_dir(self): """ The 'LOG_DIR' setting defaults to a program-specific directory relative to the 'STORAGE_DIR' setting. In the case of an instance, the instance name is appended to the directory. """ config = self.makeFile(content="[foo]") settings = read_config( "carbon-foo", FakeOptions(config=config, instance="x", pidfile=None, logdir=None), ROOT_DIR="foo") self.assertEqual(join("foo", "storage", "log", "carbon-foo", "carbon-foo-x"), settings.LOG_DIR)
def test_log_dir_from_instance_config(self): """ Providing a 'LOG_DIR' for the specific instance in the configuration file overrides the storage-relative default. The actual value will have the instance name appended to it and ends with a forward slash. """ config = self.makeFile( content=("[foo]\nLOG_DIR = baz\n" "[foo:x]\nLOG_DIR = boo")) settings = read_config( "carbon-foo", FakeOptions(config=config, instance="x", pidfile=None, logdir=None), ROOT_DIR="foo") self.assertEqual("boo/carbon-foo-x", settings.LOG_DIR)
def test_config_is_not_required(self): """ If the '--config' option is not provided, it will default to the 'carbon.conf' file inside 'ROOT_DIR/conf'. """ root_dir = self.makeDir() conf_dir = join(root_dir, "conf") makedirs(conf_dir) self.makeFile(content="[foo]", basename="carbon.conf", dirname=conf_dir) settings = read_config("carbon-foo", FakeOptions(config=None, instance=None, pidfile=None, logdir=None), ROOT_DIR=root_dir) self.assertEqual(conf_dir, settings.CONF_DIR)
def test_write_strategy_max(self): """Create a metric cache, insert metrics, ensure naive writes""" config = self.makeFile(content="[foo]\nCACHE_WRITE_STRATEGY = max") settings = read_config("carbon-foo", FakeOptions(config=config, instance=None, pidfile=None, logdir=None), ROOT_DIR="foo") cache = _MetricCache(method=settings.CACHE_WRITE_STRATEGY) self.assertEqual("max", cache.method) now = time.time() datapoint1 = (now - 10, float(1)) datapoint2 = (now, float(2)) cache.store("d.e.f", datapoint1) cache.store("a.b.c", datapoint1) cache.store("a.b.c", datapoint2) (m, d) = cache.pop() self.assertEqual(("a.b.c", deque([datapoint1, datapoint2])), (m, d)) (m, d) = cache.pop() self.assertEqual(("d.e.f", deque([datapoint1])), (m, d)) self.assertEqual(0, cache.size)