Skip to content

Commit

Permalink
libyang: support backslashes in file names on Windows
Browse files Browse the repository at this point in the history
There are some failures with `gnpy` on Windows:

 ================================== FAILURES ===================================
 ____________ test_lint_yang[ietf-network-topology@2018-02-26.yang] ____________

 yang_model = WindowsPath('D:/a/oopt-gnpy/oopt-gnpy/gnpy/yang/ext/ietf-network-topology@2018-02-26.yang')

     @pytest.mark.parametrize("yang_model", [x for x in external_path().glob('*.yang')] + [x for x in model_path().glob('*.yang')], ids=_get_basename)
     def test_lint_yang(yang_model):
         '''Run a linter on each YANG model'''
         c = ly.Context(str(external_path()) + os.pathsep + str(model_path()),
                        ly.ContextOptions.NoYangLibrary | ly.ContextOptions.DisableSearchCwd)
         assert c.parse_module(yang_model, ly.SchemaFormat.YANG) is not None
 >       assert c.errors() == []
 E       assert <[TypeError("unhashable type: 'instancemethod'") raised in repr()] list object at 0x1880e5958c0> == []
 E         (pytest_assertion plugin: representation of details failed: C:\hostedtoolcache\windows\Python\3.11.4\x64\Lib\pprint.py:178: TypeError: unhashable type: 'instancemethod'.
 E          Probably an object has a faulty __repr__.)

 tests\test_yang_lint.py:30: AssertionError
 ---------------------------- Captured stderr call -----------------------------
 libyang[1]: File name "D:\a\oopt-gnpy\oopt-gnpy\gnpy\yang\ext\ietf-network-topology@2018-02-26.yang" does not match module name "ietf-network-topology".
 ________________ test_lint_yang[ietf-network@2018-02-26.yang] _________________

That complaint about __repr__ is an impedance mismatch between pybind11
and Python (pybind/pybind11#2722), but in this case that whole thing is
triggered through a bug in libyang which assumes that the directory
separator is always `/`.

Bug: CESNET/libyang#2093
  • Loading branch information
jktjkt committed Aug 31, 2023
1 parent 532744a commit 7fb9771
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libyang
9 changes: 8 additions & 1 deletion tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import oopt_gnpy_libyang as ly
from pathlib import Path

TEST_ROOT = Path(__file__).parent

def test_no_module(context_no_libyang):
with pytest.raises(ly.Error, match="Can't load module 'yay'"):
context_no_libyang.load_module('yay')
Expand All @@ -11,6 +13,12 @@ def test_empty():
with pytest.raises(ly.Error, match="Can't load module 'yay'"):
c.load_module('yay')

def test_module_parsing_basic(context_no_libyang):
assert context_no_libyang.parse_module(
TEST_ROOT / 'yang' / 'ietf-network@2018-02-26.yang',
ly.SchemaFormat.YANG) is not None
assert context_no_libyang.errors() == []

def test_features(context_no_libyang):
context_no_libyang.load_module('iana-if-type')
m = context_no_libyang.load_module('ietf-interfaces')
Expand All @@ -32,7 +40,6 @@ def test_no_shared_errors(context_no_libyang):
assert context_no_libyang.errors() == []

def test_explicit_loading(context_no_libyang):
TEST_ROOT = Path(__file__).parent
with pytest.raises(ly.Error, match="Can't parse module: LY_ESYS"):
context_no_libyang.parse_module(TEST_ROOT / 'no-such-path.yang', ly.SchemaFormat.YANG)
m1 = context_no_libyang.parse_module(TEST_ROOT / 'yang' / 'iana-if-type@2017-01-19.yang', ly.SchemaFormat.YANG)
Expand Down
192 changes: 192 additions & 0 deletions tests/yang/ietf-network@2018-02-26.yang
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
module ietf-network {
yang-version 1.1;
namespace "urn:ietf:params:xml:ns:yang:ietf-network";
prefix nw;

import ietf-inet-types {
prefix inet;
reference
"RFC 6991: Common YANG Data Types";
}

organization
"IETF I2RS (Interface to the Routing System) Working Group";

contact
"WG Web: <https://datatracker.ietf.org/wg/i2rs/>
WG List: <mailto:i2rs@ietf.org>
Editor: Alexander Clemm
<mailto:ludwig@clemm.org>
Editor: Jan Medved
<mailto:jmedved@cisco.com>
Editor: Robert Varga
<mailto:robert.varga@pantheon.tech>
Editor: Nitin Bahadur
<mailto:nitin_bahadur@yahoo.com>
Editor: Hariharan Ananthakrishnan
<mailto:hari@packetdesign.com>
Editor: Xufeng Liu
<mailto:xufeng.liu.ietf@gmail.com>";
description
"This module defines a common base data model for a collection
of nodes in a network. Node definitions are further used
in network topologies and inventories.
Copyright (c) 2018 IETF Trust and the persons identified as
authors of the code. All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, is permitted pursuant to, and subject
to the license terms contained in, the Simplified BSD License
set forth in Section 4.c of the IETF Trust's Legal Provisions
Relating to IETF Documents
(https://trustee.ietf.org/license-info).
This version of this YANG module is part of RFC 8345;
see the RFC itself for full legal notices.";

revision 2018-02-26 {
description
"Initial revision.";
reference
"RFC 8345: A YANG Data Model for Network Topologies";
}

typedef node-id {
type inet:uri;
description
"Identifier for a node. The precise structure of the node-id
will be up to the implementation. For example, some
implementations MAY pick a URI that includes the network-id
as part of the path. The identifier SHOULD be chosen
such that the same node in a real network topology will
always be identified through the same identifier, even if
the data model is instantiated in separate datastores. An
implementation MAY choose to capture semantics in the
identifier -- for example, to indicate the type of node.";
}

typedef network-id {
type inet:uri;
description
"Identifier for a network. The precise structure of the
network-id will be up to the implementation. The identifier
SHOULD be chosen such that the same network will always be
identified through the same identifier, even if the data model
is instantiated in separate datastores. An implementation MAY
choose to capture semantics in the identifier -- for example,
to indicate the type of network.";
}

grouping network-ref {
description
"Contains the information necessary to reference a network --
for example, an underlay network.";
leaf network-ref {
type leafref {
path "/nw:networks/nw:network/nw:network-id";
require-instance false;
}
description
"Used to reference a network -- for example, an underlay
network.";
}
}

grouping node-ref {
description
"Contains the information necessary to reference a node.";
leaf node-ref {
type leafref {
path "/nw:networks/nw:network[nw:network-id=current()/../"+
"network-ref]/nw:node/nw:node-id";
require-instance false;
}
description
"Used to reference a node.
Nodes are identified relative to the network that
contains them.";
}
uses network-ref;
}

container networks {
description
"Serves as a top-level container for a list of networks.";
list network {
key "network-id";
description
"Describes a network.
A network typically contains an inventory of nodes,
topological information (augmented through the
network-topology data model), and layering information.";
leaf network-id {
type network-id;
description
"Identifies a network.";
}
container network-types {
description
"Serves as an augmentation target.
The network type is indicated through corresponding
presence containers augmented into this container.";
}
list supporting-network {
key "network-ref";
description
"An underlay network, used to represent layered network
topologies.";
leaf network-ref {
type leafref {
path "/nw:networks/nw:network/nw:network-id";
require-instance false;
}
description
"References the underlay network.";
}
}

list node {
key "node-id";
description
"The inventory of nodes of this network.";
leaf node-id {
type node-id;
description
"Uniquely identifies a node within the containing
network.";
}
list supporting-node {
key "network-ref node-ref";
description
"Represents another node that is in an underlay network
and that supports this node. Used to represent layering
structure.";
leaf network-ref {
type leafref {
path "../../../nw:supporting-network/nw:network-ref";
require-instance false;
}
description
"References the underlay network of which the
underlay node is a part.";
}
leaf node-ref {
type leafref {
path "/nw:networks/nw:network/nw:node/nw:node-id";
require-instance false;
}
description
"References the underlay node itself.";
}
}
}
}
}
}

0 comments on commit 7fb9771

Please sign in to comment.