Skip to content

Commit

Permalink
added homedir resolve process for license paths (#182)
Browse files Browse the repository at this point in the history
* added homedir resolve process for license paths

* added homedir module

* remove excessive comments

* improved license init logic

* moved path resolving logic to a separate path

* added tests
  • Loading branch information
hellt authored Dec 1, 2020
1 parent 5523e7a commit e200e78
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 23 deletions.
59 changes: 36 additions & 23 deletions clab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/docker/go-connections/nat"
"github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
)
Expand Down Expand Up @@ -252,29 +253,17 @@ func (c *cLab) imageInitialization(nodeCfg *NodeConfig, kind string) string {
return c.Config.Topology.Defaults.Image
}

func (c *cLab) licenseInit(nodeCfg *NodeConfig, kind string) (string, error) {
if nodeCfg.License != "" {
lp, err := filepath.Abs(nodeCfg.License)
if err != nil {
return "", err
}
return lp, nil
}
if c.Config.Topology.Kinds[kind].License != "" {
lp, err := filepath.Abs(c.Config.Topology.Kinds[kind].License)
if err != nil {
return "", err
}
return lp, nil
}
if c.Config.Topology.Defaults.License != "" {
lp, err := filepath.Abs(c.Config.Topology.Defaults.License)
if err != nil {
return "", err
}
return lp, nil
func (c *cLab) licenseInit(nodeCfg *NodeConfig, node *Node) (string, error) {
switch {
case nodeCfg.License != "":
return nodeCfg.License, nil
case c.Config.Topology.Kinds[node.Kind].License != "":
return c.Config.Topology.Kinds[node.Kind].License, nil
case c.Config.Topology.Defaults.License != "":
return c.Config.Topology.Defaults.License, nil
default:
return "", fmt.Errorf("no license found for node '%s' of kind '%s'", node.ShortName, node.Kind)
}
return "", fmt.Errorf("no license found for node(s) of kind %s", nodeCfg.Kind)
}

func (c *cLab) cmdInitialization(nodeCfg *NodeConfig, kind string, defCmd string) string {
Expand Down Expand Up @@ -358,10 +347,15 @@ func (c *cLab) NewNode(nodeName string, nodeCfg NodeConfig, idx int) error {
// initialize the global parameters with defaults, can be overwritten later
node.Config = c.configInitialization(&nodeCfg, node.Kind)

lp, err := c.licenseInit(&nodeCfg, node.Kind)
lp, err := c.licenseInit(&nodeCfg, node)
if err != nil {
return err
}
lp, err = resolvePath(lp)
if err != nil {
return err
}

node.License = lp

node.Image = c.imageInitialization(&nodeCfg, node.Kind)
Expand Down Expand Up @@ -495,3 +489,22 @@ func (c *cLab) VerifyBridgesExist() error {
}
return nil
}

//resolvePath resolves a string path by expanding `~` to home dir or getting Abs path for the given path
func resolvePath(p string) (string, error) {
var err error
switch {
// resolve ~/ path
case p[0] == '~':
p, err = homedir.Expand(p)
if err != nil {
return "", err
}
default:
p, err = filepath.Abs(p)
if err != nil {
return "", err
}
}
return p, nil
}
60 changes: 60 additions & 0 deletions clab/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package clab

import (
"path/filepath"
"strings"
"testing"
)

func TestLicenseInit(t *testing.T) {
tests := map[string]struct {
got string
want string
}{
"node_license": {
got: "test_data/topo1.yml",
want: "node1.lic",
},
"kind_license": {
got: "test_data/topo2.yml",
want: "kind.lic",
},
"default_license": {
got: "test_data/topo3.yml",
want: "default.lic",
},
"kind_overwrite": {
got: "test_data/topo4.yml",
want: "node1.lic",
},
}

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))

lic, err := c.licenseInit(&nodeCfg, &node)
if err != nil {
t.Fatal(err)
}
if lic != tc.want {
t.Fatalf("wanted '%s' got '%s'", tc.want, lic)
}
})
}
}

func abspath(s string) string {
p, _ := filepath.Abs(s)
return p
}
7 changes: 7 additions & 0 deletions clab/test_data/topo1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: topo1
topology:
nodes:
node1:
kind: srl
type: ixr6
license: node1.lic
9 changes: 9 additions & 0 deletions clab/test_data/topo2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: topo2
topology:
kinds:
srl:
license: kind.lic
nodes:
node1:
kind: srl
type: ixr6
8 changes: 8 additions & 0 deletions clab/test_data/topo3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: topo3
topology:
defaults:
license: default.lic
nodes:
node1:
kind: srl
type: ixr6
12 changes: 12 additions & 0 deletions clab/test_data/topo4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: topo3
topology:
defaults:
license: default.lic
kinds:
srl:
license: kind.lic
nodes:
node1:
kind: srl
type: ixr6
license: node1.lic
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/docker/go-units v0.4.0 // indirect
github.com/google/go-cmp v0.2.0
github.com/google/uuid v1.1.2
github.com/mitchellh/go-homedir v1.1.0
github.com/olekukonko/tablewriter v0.0.4
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mreiferson/go-httpclient v0.0.0-20160630210159-31f0106b4474/go.mod h1:OQA4XLvDbMgS8P0CevmM4m9Q3Jq4phKUzcocxuGJ5m8=
Expand Down

0 comments on commit e200e78

Please sign in to comment.