Example #1
0
def test_create(mocks: Mocks):
    """Verifies that Configuration can be instantiated"""
    called = False

    def fn(config, flags):
        assert flags == yogi.ConfigurationFlags.NONE
        config.contents.value = 1234
        nonlocal called
        called = True
        return yogi.ErrorCode.OK

    mocks.MOCK_ConfigurationCreate(fn)

    configuration = yogi.Configuration()
    assert configuration._handle.value == 1234
    assert configuration.flags == yogi.ConfigurationFlags.NONE
    assert called

    def fn2(config, flags):
        assert flags == yogi.ConfigurationFlags.MUTABLE_CMD_LINE
        config.contents.value = 1234
        return yogi.ErrorCode.OK

    mocks.MOCK_ConfigurationCreate(fn2)

    yogi.Configuration(yogi.ConfigurationFlags.MUTABLE_CMD_LINE)
Example #2
0
    def test_update_from_command_line(self):
        cfg = yogi.Configuration()
        cfg.update_from_command_line(["exe", "-o", '{"age": 25}'],
                                     yogi.CommandLineOptions.OVERRIDES)

        jsn = json.loads(cfg.dump())
        self.assertEqual(jsn["age"], 25)
Example #3
0
 def test_command_line_override(self):
     cfg = yogi.Configuration(['test.py', make_config_path('a'), '--connection_target=my-host:1234',
                                  '--connection_timeout=0.555', '-i', 'Dude', '--location=/Home'])
     self.assertEqual(yogi.Path('/Home'), cfg.location)
     self.assertEqual('my-host:1234', cfg.connection_target)
     self.assertAlmostEqual(0.555, cfg.connection_timeout)
     self.assertEqual('Dude', cfg.connection_identification)
Example #4
0
    def test_write_to_file(self):
        filename = os.path.join(self.temp_dir, "dump.json")

        cfg = yogi.Configuration(yogi.ConfigurationFlags.DISABLE_VARIABLES)
        cfg.update_from_json({"age": 11})

        self.assertRaises(yogi.FailureException,
                          lambda: cfg.write_to_file(filename, True))

        cfg.write_to_file(filename)
        with open(filename) as f:
            content = f.read()
            self.assertFalse(" " in content)
            self.assertFalse("\n" in content)

            jsn = json.loads(content)
            self.assertEqual(jsn["age"], 11)

        cfg.write_to_file(filename, indentation=2)
        with open(filename) as f:
            content = f.read()
            self.assertTrue(" " in content)
            self.assertTrue("\n" in content)

            jsn = json.loads(content)
            self.assertEqual(jsn["age"], 11)
Example #5
0
    def setUp(self):
        self.pi = yogi.ProcessInterface(yogi.Configuration())
        self.scheduler = yogi.Scheduler()
        self.leaf = yogi.Leaf(self.scheduler)
        self.connection = yogi.LocalConnection(self.pi.leaf, self.leaf)

        self.operational_terminal = yogi.CachedConsumerTerminal(
            '/Process/Operational', yogi_00000001_pb2, leaf=self.leaf)
        self.errors_terminal = yogi.CachedConsumerTerminal('/Process/Errors',
                                                           yogi_0000040d_pb2,
                                                           leaf=self.leaf)
        self.warnings_terminal = yogi.CachedConsumerTerminal(
            '/Process/Warnings', yogi_0000040d_pb2, leaf=self.leaf)

        for tm in [
                self.operational_terminal, self.errors_terminal,
                self.warnings_terminal
        ]:
            while tm.get_binding_state() is yogi.BindingState.RELEASED:
                pass

            while True:
                try:
                    tm.get_cached_message()
                    break
                except yogi.Failure:
                    pass
Example #6
0
def configuration(mocks):
    """Provides a mocked Configuration instance"""
    def fn(config, flags):
        config.contents.value = 2222
        return yogi.ErrorCode.OK

    mocks.MOCK_ConfigurationCreate(fn)
    return yogi.Configuration()
Example #7
0
def create_configuration(mocks: Mocks, flags=yogi.ConfigurationFlags.NONE):
    """Helper function to create a configuration with certain flags"""
    def fn(config, flags):
        config.contents.value = 2222
        return yogi.ErrorCode.OK

    mocks.MOCK_ConfigurationCreate(fn)
    return yogi.Configuration(flags)
Example #8
0
    def test_to_json(self):
        cfg = yogi.Configuration(yogi.ConfigurationFlags.DISABLE_VARIABLES)
        cfg.update_from_json({"age": 42})

        self.assertRaises(yogi.FailureException, lambda: cfg.to_json(True))

        jsn = cfg.to_json()
        self.assertEqual(jsn["age"], 42)
Example #9
0
    def test_dump(self):
        cfg = yogi.Configuration(yogi.ConfigurationFlags.DISABLE_VARIABLES)
        cfg.update_from_json({"age": 42})

        self.assertRaises(yogi.FailureException, lambda: cfg.dump(True))

        self.assertFalse(" " in cfg.dump())
        self.assertFalse("\n" in cfg.dump())
        self.assertTrue(" " in cfg.dump(indentation=2))
        self.assertTrue("\n" in cfg.dump(indentation=2))
Example #10
0
    def test_update_from_file(self):
        filename = os.path.join(self.temp_dir, "cfg.json")
        with open(filename, "w") as f:
            f.write('{"age": 66}')

        cfg = yogi.Configuration()
        cfg.update_from_file(filename)

        jsn = json.loads(cfg.dump())
        self.assertEqual(jsn["age"], 66)
Example #11
0
 def test_str(self):
     cfg = yogi.Configuration(['test.py', make_config_path('a')])
     self.assertRegexpMatches(str(cfg), r'.*localhost:12345.*')
Example #12
0
 def test_bad_command_line(self):
     self.assertRaises(yogi.BadCommandLine, lambda: yogi.Configuration(['test.py', '--hey-dude']))
Example #13
0
 def test_bad_configuration_file(self):
     self.assertRaises(yogi.BadConfiguration, lambda: yogi.Configuration(['test.py', make_config_path('c')]))
Example #14
0
 def test_json_overrides(self):
     cfg = yogi.Configuration(['test.py', make_config_path('a'), '--json={"my-age": 42}', '-j', '{"my-id": 55}',
                                  '--json={"yogi": {"location": "/Somewhere"}}', '--location=/Home'])
     self.assertEqual(42, cfg.config['my-age'])
     self.assertEqual(55, cfg.config['my-id'])
     self.assertEqual(yogi.Path('/Home'), cfg.location)
Example #15
0
 def test_flags_property(self):
     cfg = yogi.Configuration(yogi.ConfigurationFlags.MUTABLE_CMD_LINE)
     self.assertEqual(cfg.flags, yogi.ConfigurationFlags.MUTABLE_CMD_LINE)
Example #16
0
 def test_config_file_priority(self):
     cfg = yogi.Configuration(['test.py', make_config_path('a'), make_config_path('b')])
     self.assertEqual(yogi.Path('/Pudding'), cfg.location)
     self.assertIsNone(cfg.connection_target)
Example #17
0
 def test_config_file(self):
     cfg = yogi.Configuration(['test.py', make_config_path('a')])
     self.assertEqual(yogi.Path('/Test'), cfg.location)
     self.assertEqual('localhost:12345', cfg.connection_target)
     self.assertAlmostEqual(1.234, cfg.connection_timeout)
     self.assertEqual('Hello World', cfg.connection_identification)
Example #18
0
 def test_update(self):
     cfg = yogi.Configuration(['test.py', make_config_path('a')])
     cfg.update('{"yogi": {"location": "/Home"}}')
     self.assertEqual(yogi.Path("/Home"), cfg.location)
     self.assertRaises(yogi.BadConfiguration, lambda: cfg.update('{'))
Example #19
0
 def test_empty_configuration(self):
     cfg = yogi.Configuration(argv=None)
     self.assertEqual(yogi.Path('/'), cfg.location)
     self.assertIsNone(cfg.connection_target)
     self.assertIsNone(cfg.connection_timeout)
     self.assertIsNone(cfg.connection_identification)
Example #20
0
    def test_update_from_json(self):
        cfg = yogi.Configuration()
        cfg.update_from_json({"age": 42})

        jsn = json.loads(cfg.dump())
        self.assertEqual(jsn["age"], 42)