Example #1
0
    def test_on_snapshot_loaded(self):
        m_response = Mock()

        endpoint_on_host = Mock()
        endpoint_on_host.key = "/calico/v1/host/hostname/workload/" "orch/wlid/endpoint/epid"
        endpoint_on_host.value = ENDPOINT_STR

        bad_endpoint_on_host = Mock()
        bad_endpoint_on_host.key = "/calico/v1/host/hostname/workload/" "orch/wlid/endpoint/epid2"
        bad_endpoint_on_host.value = ENDPOINT_STR[:10]

        endpoint_not_on_host = Mock()
        endpoint_not_on_host.key = "/calico/v1/host/other/workload/" "orch/wlid/endpoint/epid"
        endpoint_not_on_host.value = ENDPOINT_STR

        still_ready = Mock()
        still_ready.key = "/calico/v1/Ready"
        still_ready.value = "true"

        m_response.children = [endpoint_on_host, bad_endpoint_on_host, endpoint_not_on_host, still_ready]
        with patch.object(self.watcher, "clean_up_endpoint_statuses") as m_clean:
            self.watcher._on_snapshot_loaded(m_response)

        # Cleanup should only get the endpoints on our host.
        m_clean.assert_called_once_with(set([EndpointId("hostname", "orch", "wlid", "epid")]))
def mock_read_no_pools(path):
    """
    EtcdClient mock side effect for read with no IPv4 pools.
    """
    result = Mock(spec=EtcdResult)
    assert path == IPV4_POOLS_PATH
    result.children = []
    return result
Example #3
0
    def load_plugin(self, klass):
        plugin_dir = Mock()
        # TODO: neater without tuple (add test to node for this)
        plugin_dir.children = (self.create_plugin_dir(klass),)

        plugin_loader = PluginLoader()
        plugins = plugin_loader.load(plugin_dir)
        return plugins[0]
Example #4
0
    def create_plugin_dir(self, name, plugin_class):
        plugin_dir = Mock()
        plugin_dir.name = name
        plugin_dir.children = Mock()

        plugin_file = self.create_plugin_file(name, plugin_class)
        plugin_dir.find.return_value = plugin_file

        return plugin_dir
Example #5
0
    def setup_method(self, method):
        plugin_dir = Mock()
        alias_dir = self.create_plugin_dir("alias", Alias)
        matches_dir = self.create_plugin_dir("matches", Matches)
        multiplealiases_dir = self.create_plugin_dir("multiplealiases", MultipleAliases)
        plugin_dir.children = (alias_dir, matches_dir, multiplealiases_dir)

        plugin_loader = PluginLoader()
        self.plugins = plugin_loader.load(plugin_dir)
def mock_read_no_node_bgppeers(path):
    """
    EtcdClient mock side effect for read with no IPv4 BGP Peers
    """
    result = Mock(spec=EtcdResult)
    assert path == TEST_NODE_BGP_PEERS_PATH

    # Bug in etcd seems to return the parent when enumerating children if there
    # are no children.  We handle this in the datastore.
    node = Mock(spec=EtcdResult)
    node.value = None
    node.key = TEST_NODE_BGP_PEERS_PATH
    result.children = iter([node])
    return result
def mock_read_2_pools(path):
    """
    EtcdClient mock side effect for read with 2 IPv4 pools.
    """
    result = Mock(spec=EtcdResult)
    assert_equal(path, IPV4_POOLS_PATH)
    children = []
    for net in ["192.168.3.0/24", "192.168.5.0/24"]:
        node = Mock(spec=EtcdResult)
        node.value = '{"cidr": "%s"}' % net
        node.key = IPV4_POOLS_PATH + "/" + net.replace("/", "-")
        children.append(node)
    result.children = iter(children)
    return result
def mock_read_2_peers(path):
    """
    EtcdClient mock side effect for read with 2 IPv4 peers.
    """
    result = Mock(spec=EtcdResult)
    assert_equal(path, BGP_PEERS_PATH)
    children = []
    for ip in ["192.168.3.1", "192.168.5.1"]:
        node = Mock(spec=EtcdResult)
        node.value = '{"ip": "%s", "as_num": 32245}' % ip
        node.key = BGP_PEERS_PATH + str(ip)
        children.append(node)
    result.children = iter(children)
    return result
Example #9
0
    def test_load_plugins(self):
        class Plugin1:
            pass

        class Plugin2:
            pass

        plugin_dir = Mock()
        plugin1_dir = self.create_plugin_dir("plugin1", Plugin1)
        plugin2_dir = self.create_plugin_dir("plugin2", Plugin2)
        plugin_dir.children = (plugin1_dir, plugin2_dir)

        plugin_loader = PluginLoader()

        assert plugin_loader.load(plugin_dir) == [Plugin1, Plugin2]
 def _get_block(self, block_info):
     # Build spec
     spec = ["scope_ids", "runtime", "has_children", "children"]
     for attr in block_info.get("attrs", []):
         spec.append(attr)
     # Assemble block
     block = Mock(spec=spec)
     scope_ids_mock = Mock()
     scope_ids_mock.usage_id = block_info.get("usage_id")
     block.scope_ids = scope_ids_mock
     block.runtime = self.runtime_mock
     block.children = []
     for attr, val in block_info.get("attrs", {}).items():
         setattr(block, attr, val)
     return block
Example #11
0
    def create_plugin_dir(self, plugin_name, plugin_class):
        plugin_dir = Mock()
        plugin_dir.name = plugin_name
        plugin_dir.children = Mock()

        plugin_file = self.create_plugin_file(plugin_name, plugin_class)

        def find(name, type):
            assert type == "py"
            if name is plugin_name:
                return plugin_file

        plugin_dir.find = find

        return plugin_dir
def mock_read_2_node_peers(path):
    """
    EtcdClient mock side effect for read with 2 IPv4 peers.  Assumes host is
    "TEST_HOST" otherwise raises EtcdKeyNotFound.
    """
    result = Mock(spec=EtcdResult)
    if path != TEST_NODE_BGP_PEERS_PATH:
        raise EtcdKeyNotFound()
    children = []
    for ip in ["192.169.3.1", "192.169.5.1"]:
        node = Mock(spec=EtcdResult)
        node.value = '{"ip": "%s", "as_num": 32245}' % ip
        node.key = TEST_NODE_BGP_PEERS_PATH + str(ip)
        children.append(node)
    result.children = iter(children)
    return result
def mock_read_2_profiles(path, recursive):
    assert path == ALL_PROFILES_PATH
    assert recursive
    nodes = [
        CALICO_V_PATH + "/policy/profile/TEST",
        CALICO_V_PATH + "/policy/profile/TEST/tags",
        CALICO_V_PATH + "/policy/profile/TEST/rules",
        CALICO_V_PATH + "/policy/profile/UNIT",
        CALICO_V_PATH + "/policy/profile/UNIT/tags",
        CALICO_V_PATH + "/policy/profile/UNIT/rules",
    ]
    children = []
    for node in nodes:
        result = Mock(spec=EtcdResult)
        result.key = node
        children.append(result)
    results = Mock(spec=EtcdResult)
    results.children = iter(children)
    return results
def mock_read_no_profiles(path, recursive):
    assert path == ALL_PROFILES_PATH
    assert recursive
    results = Mock(spec=EtcdResult)
    results.children = iter([])
    return results