Example #1
0
    def test_load_backends(self):
        with open(self.inventory_filename, "w") as f:
            f.write(TEST_INVENTORY_ONE)

        inventory = service_discovery.ServiceInventory(self.inventory_filename)

        backends = inventory.get_backends()
        self.assertEqual(len(backends), 1)
        backends = inventory.get_backends()
        self.assertEqual(len(backends), 1)
        self.assertEqual(backends[0].id, 205)
        self.assertEqual(backends[0].name, "i-258fc8b6")
        self.assertEqual(backends[0].endpoint.address.host, "10.0.1.2")
        self.assertEqual(backends[0].endpoint.address.port, 9090)
        self.assertEqual(backends[0].weight, 1)

        with open(self.inventory_filename, "w") as f:
            f.write(TEST_INVENTORY_TWO)

        # force update the mtime into the future so we don't have to
        # wait around for the filesystem's minimum resolution
        os.utime(self.inventory_filename, (time.time(), time.time()))

        backends = inventory.get_backends()
        self.assertEqual(len(backends), 3)
        backends = inventory.get_backends()
        self.assertEqual(len(backends), 3)
Example #2
0
    def test_single_get(self):
        with open(self.inventory_filename, "w") as f:
            f.write(TEST_INVENTORY_ONE)

        inventory = service_discovery.ServiceInventory(self.inventory_filename)
        backend = inventory.get_backend()
        self.assertEqual(backend.id, 205)
Example #3
0
    def test_file_starts_out_missing_then_appears(self):
        inventory = service_discovery.ServiceInventory(self.inventory_filename)

        backends = inventory.get_backends()
        self.assertEqual(backends, [])

        with open(self.inventory_filename, "w") as f:
            f.write(TEST_INVENTORY_ONE)

        backends = inventory.get_backends()
        self.assertEqual(len(backends), 1)
Example #4
0
    def test_file_exists_then_goes_missing(self):
        with open(self.inventory_filename, "w") as f:
            f.write(TEST_INVENTORY_ONE)

        inventory = service_discovery.ServiceInventory(self.inventory_filename)
        backends = inventory.get_backends()
        self.assertEqual(len(backends), 1)

        os.unlink(self.inventory_filename)

        backends = inventory.get_backends()
        self.assertEqual(len(backends), 1)
Example #5
0
 def test_no_backends_available(self):
     inventory = service_discovery.ServiceInventory(self.inventory_filename)
     with self.assertRaises(service_discovery.NoBackendsAvailableError):
         inventory.get_backend()
Example #6
0
 def setUp(self):
     self.mock_filewatcher = mock.Mock(spec=FileWatcher)
     self.inventory = service_discovery.ServiceInventory("/whatever")
     self.inventory._filewatcher = self.mock_filewatcher