Skip to content

Commit

Permalink
template rendering, single values & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerza committed May 25, 2021
1 parent 10fa1a8 commit 74c62f1
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 15 deletions.
2 changes: 1 addition & 1 deletion clab/config/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Session struct {
Session *ssh.Session
}

func NewSession(username, password string, host string) (*Session, error) {
func NewSession(username, password, host string) (*Session, error) {

sshConfig := &ssh.ClientConfig{
User: username,
Expand Down
58 changes: 46 additions & 12 deletions clab/config/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ func RenderLink(link *clab.Link) (*ConfigSnippet, *ConfigSnippet, error) {
for k, v := range link.Labels {
r := strings.Split(v, ",")
switch len(r) {
case 1:
case 2:
case 1, 2:
l[k] = r
default:
log.Warnf("%s: %s contains %d elements: %s", link, k, len(r), v)
Expand Down Expand Up @@ -190,24 +189,59 @@ var funcMap = map[string]interface{}{
return a[1], nil
},
"default": func(val interface{}, def interface{}) (interface{}, error) {
if val == nil {
return def, nil
if def == nil {
return nil, fmt.Errorf("default value expected")
}
switch val.(type) {
case string:
if val == "" {
return def, nil
}
default:
if val == nil {
return def, nil
}
}
return val, nil
},
"contains": func(str interface{}, substr interface{}) (interface{}, error) {
return strings.Contains(fmt.Sprintf("%v", str), fmt.Sprintf("%v", substr)), nil
},
"slice": func(val interface{}, start interface{}, end interface{}) (interface{}, error) {
v := fmt.Sprintf("%v", val)
s := int(start.(int))
e := int(end.(int))
if s < 0 {
s += len(v)
// Start and end values
var s, e int
switch tmp := start.(type) {
case int:
s = tmp
default:
return nil, fmt.Errorf("int expeted for 2nd parameter %v", tmp)
}
if e < 0 {
e += len(v)
switch tmp := end.(type) {
case int:
e = tmp
default:
return nil, fmt.Errorf("int expeted for 3rd parameter %v", tmp)
}

// string or array
switch v := val.(type) {
case string:
if s < 0 {
s += len(v)
}
if e < 0 {
e += len(v)
}
return v[s:e], nil
case []interface{}:
if s < 0 {
s += len(v)
}
if e < 0 {
e += len(v)
}
return v[s:e], nil
}
return v[s:e], nil
return nil, fmt.Errorf("not an array")
},
}
40 changes: 40 additions & 0 deletions clab/config/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package config

import (
"fmt"
"testing"
)

func TestFuncMapDefault(t *testing.T) {

// parameters & return
tSet := map[string][][]interface{}{
"default": {
{nil, 1, 1},
{5, 1, 5},
{"", "1", "1"},
{nil, nil, fmt.Errorf("")},
},
"contains": {
{"aa.", ".", true},
{"ss", ".", false},
}}

for name, set := range tSet {
fn := funcMap[name].(func(interface{}, interface{}) (interface{}, error))

for _, p := range set {
exp := p[len(p)-1]
var expe error
switch v := exp.(type) {
case error:
expe = v
exp = nil
}
res, err := fn(p[0], p[1])
if res != exp || (err != nil && expe == nil) {
t.Errorf("%v expected %v got %v error %v err: %v", p, exp, res, expe, err)
}
}
}
}
5 changes: 3 additions & 2 deletions lab-examples/vr05/conf1.clab.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ topology:
labels:
port: 1/1/c1/1, 1/1/c2/1
ip: 1.1.1.2/30
vlan: 99
vlan: "99, 99"
- endpoints: [sr2:eth1, sr3:eth2]
labels:
port: 1/1/c1/1, 1/1/c2/1
port: 1/1/c1/1, 1/1/c2/1ssh
vlan: 98
- endpoints: [sr3:eth1, sr4:eth2]
labels:
port: 1/1/c1/1, 1/1/c2/1
Expand Down

0 comments on commit 74c62f1

Please sign in to comment.