Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored type init #203

Merged
merged 2 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions clab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const (
dockerNetName = "clab"
dockerNetIPv4Addr = "172.20.20.0/24"
dockerNetIPv6Addr = "2001:172:20:20::/80"

srlDefaultType = "ixr6"
)

// supported kinds
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down
50 changes: 50 additions & 0 deletions clab/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
3 changes: 3 additions & 0 deletions clab/test_data/topo1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ topology:
license: node1.lic
binds:
- /node/src:/dst
node2:
kind: srl
license: node1.lic
5 changes: 4 additions & 1 deletion clab/test_data/topo2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions clab/test_data/topo3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ topology:
license: default.lic
binds:
- /default/src:/dst
type: ixrd2
nodes:
node1:
kind: srl
type: ixr6
node2:
kind: srl