Skip to content

Commit

Permalink
added tests for binds propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
hellt committed Nov 30, 2020
1 parent 74d514b commit b748fc3
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 17 deletions.
25 changes: 9 additions & 16 deletions clab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,26 +183,16 @@ func (c *cLab) kindInitialization(nodeCfg *NodeConfig) string {
return c.Config.Topology.Defaults.Kind
}

func (c *cLab) bindsInit(nodeCfg *NodeConfig) ([]string, error) {

func (c *cLab) bindsInit(nodeCfg *NodeConfig) []string {
switch {
case len(nodeCfg.Binds) != 0:
err := resolveBindPaths(nodeCfg.Binds)
if err != nil {
return nodeCfg.Binds, err
}
return nodeCfg.Binds
case len(c.Config.Topology.Kinds[nodeCfg.Kind].Binds) != 0:
err := resolveBindPaths(c.Config.Topology.Kinds[nodeCfg.Kind].Binds)
if err != nil {
return c.Config.Topology.Kinds[nodeCfg.Kind].Binds, err
}
return c.Config.Topology.Kinds[nodeCfg.Kind].Binds
case len(c.Config.Topology.Defaults.Binds) != 0:
err := resolveBindPaths(c.Config.Topology.Defaults.Binds)
if err != nil {
return c.Config.Topology.Defaults.Binds, err
}
return c.Config.Topology.Defaults.Binds
}
return nil, nil
return nil
}

// portsInit produces the nat.PortMap out of the slice of string representation of port bindings
Expand Down Expand Up @@ -304,7 +294,10 @@ func (c *cLab) NewNode(nodeName string, nodeCfg NodeConfig, idx int) error {
// Kind initialization is either coming from `topology.nodes` section or from `topology.defaults`
// normalize the data to lower case to compare
node.Kind = strings.ToLower(c.kindInitialization(&nodeCfg))
binds, err := c.bindsInit(&nodeCfg)

// initialize bind mounts
binds := c.bindsInit(&nodeCfg)
err := resolveBindPaths(binds)
if err != nil {
return err
}
Expand Down
50 changes: 50 additions & 0 deletions clab/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clab

import (
"path/filepath"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -58,3 +59,52 @@ func abspath(s string) string {
p, _ := filepath.Abs(s)
return p
}

func TestBindsInit(t *testing.T) {
tests := map[string]struct {
got string
want []string
}{
"node_sing_bind": {
got: "test_data/topo1.yml",
want: []string{"/node/src:/dst"},
},
"node_many_binds": {
got: "test_data/topo2.yml",
want: []string{"/node/src1:/dst1", "/node/src2:/dst2"},
},
"kind_binds": {
got: "test_data/topo5.yml",
want: []string{"/kind/src:/dst"},
},
"default_binds": {
got: "test_data/topo3.yml",
want: []string{"/default/src:/dst"},
},
"node_binds_override": {
got: "test_data/topo4.yml",
want: []string{"/node/src:/dst"},
},
}

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["node1"]
node := Node{}
node.Kind = strings.ToLower(c.kindInitialization(&nodeCfg))

binds := c.bindsInit(&nodeCfg)
if !reflect.DeepEqual(binds, tc.want) {
t.Fatalf("wanted %q got %q", tc.want, binds)
}
})
}
}
2 changes: 2 additions & 0 deletions clab/test_data/topo1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ topology:
kind: srl
type: ixr6
license: node1.lic
binds:
- /node/src:/dst
3 changes: 3 additions & 0 deletions clab/test_data/topo2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ topology:
node1:
kind: srl
type: ixr6
binds:
- /node/src1:/dst1
- /node/src2:/dst2
2 changes: 2 additions & 0 deletions clab/test_data/topo3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: topo3
topology:
defaults:
license: default.lic
binds:
- /default/src:/dst
nodes:
node1:
kind: srl
Expand Down
8 changes: 7 additions & 1 deletion clab/test_data/topo4.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
name: topo3
name: topo4
topology:
defaults:
license: default.lic
binds:
- /default/src:/dst
kinds:
srl:
license: kind.lic
binds:
- /kind/src:/dst
nodes:
node1:
kind: srl
type: ixr6
license: node1.lic
binds:
- /node/src:/dst
11 changes: 11 additions & 0 deletions clab/test_data/topo5.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: topo5
topology:
kinds:
srl:
binds:
- /kind/src:/dst
nodes:
node1:
kind: srl
type: ixr6
license: node1.lic

0 comments on commit b748fc3

Please sign in to comment.