diff --git a/clab/config.go b/clab/config.go index ed974ae4b..964fee78e 100644 --- a/clab/config.go +++ b/clab/config.go @@ -20,6 +20,8 @@ const ( dockerNetName = "clab" dockerNetIPv4Addr = "172.20.20.0/24" dockerNetIPv6Addr = "2001:172:20:20::/80" + + srlDefaultType = "ixr6" ) // supported kinds @@ -226,11 +228,22 @@ func (c *cLab) groupInitialization(nodeCfg *NodeConfig, kind string) string { return c.Config.Topology.Defaults.Group } -func (c *cLab) typeInitialization(nodeCfg *NodeConfig, kind string) string { - if nodeCfg.Type != "" { +// initialize SRL HW type +func (c *cLab) typeInit(nodeCfg *NodeConfig, kind string) string { + switch { + case nodeCfg.Type != "": return nodeCfg.Type + case c.Config.Topology.Kinds[nodeCfg.Kind].Type != "": + return c.Config.Topology.Kinds[nodeCfg.Kind].Type + case c.Config.Topology.Defaults.Type != "": + return c.Config.Topology.Defaults.Type } - return c.Config.Topology.Kinds[kind].Type + // default type if not defined + switch nodeCfg.Kind { + case "srl": + return srlDefaultType + } + return "" } func (c *cLab) configInitialization(nodeCfg *NodeConfig, kind string) string { @@ -376,7 +389,7 @@ func (c *cLab) NewNode(nodeName string, nodeCfg NodeConfig, idx int) error { node.Image = c.imageInitialization(&nodeCfg, node.Kind) node.Group = c.groupInitialization(&nodeCfg, node.Kind) - node.NodeType = c.typeInitialization(&nodeCfg, node.Kind) + node.NodeType = c.typeInit(&nodeCfg, node.Kind) node.Position = c.positionInitialization(&nodeCfg, node.Kind) if filename, found := srlTypes[node.NodeType]; found { @@ -423,7 +436,7 @@ func (c *cLab) NewNode(nodeName string, nodeCfg NodeConfig, idx int) error { node.License = "" node.Image = c.imageInitialization(&nodeCfg, node.Kind) node.Group = c.groupInitialization(&nodeCfg, node.Kind) - node.NodeType = c.typeInitialization(&nodeCfg, node.Kind) + node.NodeType = c.typeInit(&nodeCfg, node.Kind) node.Position = c.positionInitialization(&nodeCfg, node.Kind) node.Cmd = c.cmdInit(&nodeCfg, node.Kind) diff --git a/clab/config_test.go b/clab/config_test.go index 3727354f0..bb477521f 100644 --- a/clab/config_test.go +++ b/clab/config_test.go @@ -102,3 +102,53 @@ func TestBindsInit(t *testing.T) { }) } } + +func TestTypeInit(t *testing.T) { + tests := map[string]struct { + got string + node string + want string + }{ + "undefined_type_returns_default": { + got: "test_data/topo1.yml", + node: "node2", + want: "ixr6", + }, + "node_type_override_kind_type": { + got: "test_data/topo2.yml", + node: "node2", + want: "ixr10", + }, + "node_inherits_kind_type": { + got: "test_data/topo2.yml", + node: "node1", + want: "ixrd2", + }, + "node_inherits_default_type": { + got: "test_data/topo3.yml", + node: "node2", + want: "ixrd2", + }, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + opts := []ClabOption{ + WithTopoFile(tc.got), + } + c := NewContainerLab(opts...) + if err := c.ParseTopology(); err != nil { + t.Fatal(err) + } + + nodeCfg := c.Config.Topology.Nodes[tc.node] + node := Node{} + node.Kind = strings.ToLower(c.kindInitialization(&nodeCfg)) + + ntype := c.typeInit(&nodeCfg, node.Kind) + if !reflect.DeepEqual(ntype, tc.want) { + t.Fatalf("wanted %q got %q", tc.want, ntype) + } + }) + } +} diff --git a/clab/test_data/topo1.yml b/clab/test_data/topo1.yml index b8ffd637e..5404acd90 100644 --- a/clab/test_data/topo1.yml +++ b/clab/test_data/topo1.yml @@ -7,3 +7,6 @@ topology: license: node1.lic binds: - /node/src:/dst + node2: + kind: srl + license: node1.lic diff --git a/clab/test_data/topo2.yml b/clab/test_data/topo2.yml index 4aaf63eb1..b35722466 100644 --- a/clab/test_data/topo2.yml +++ b/clab/test_data/topo2.yml @@ -3,10 +3,13 @@ topology: kinds: srl: license: kind.lic + type: ixrd2 nodes: node1: kind: srl - type: ixr6 binds: - /node/src1:/dst1 - /node/src2:/dst2 + node2: + kind: srl + type: ixr10 diff --git a/clab/test_data/topo3.yml b/clab/test_data/topo3.yml index 6f2979772..62df31ad4 100644 --- a/clab/test_data/topo3.yml +++ b/clab/test_data/topo3.yml @@ -4,7 +4,10 @@ topology: license: default.lic binds: - /default/src:/dst + type: ixrd2 nodes: node1: kind: srl type: ixr6 + node2: + kind: srl