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

add spec feature: entrypoint and hostname-ignore #36

Merged
merged 2 commits into from
Feb 6, 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
29 changes: 20 additions & 9 deletions internal/pkg/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ type PostFini struct {

// Node
type Node struct {
Name string `yaml:"name" mapstructure:"name"`
Type string `yaml:"type" mapstructure:"type"`
NetBase string `yaml:"net_base" mapstructure:"net_base"`
VolumeBase string `yaml:"volume" mapstructure:"volume"`
Image string `yaml:"image" mapstructure:"image"`
Interfaces []Interface `yaml:"interfaces" mapstructure:"interfaces"`
Sysctls []Sysctl `yaml:"sysctls" mapstructure:"sysctls"`
Mounts []string `yaml:"mounts,flow" mapstructure:"mounts,flow"`
Name string `yaml:"name" mapstructure:"name"`
Type string `yaml:"type" mapstructure:"type"`
NetBase string `yaml:"net_base" mapstructure:"net_base"`
VolumeBase string `yaml:"volume" mapstructure:"volume"`
Image string `yaml:"image" mapstructure:"image"`
Interfaces []Interface `yaml:"interfaces" mapstructure:"interfaces"`
Sysctls []Sysctl `yaml:"sysctls" mapstructure:"sysctls"`
Mounts []string `yaml:"mounts,flow" mapstructure:"mounts,flow"`
HostNameIgnore bool `yaml:"hostname_ignore" mapstructure:"hostname_ignore"`
EntryPoint string `yaml:"entrypoint" mapstructure:"entrypoint"`
}

// Interface
Expand Down Expand Up @@ -397,13 +399,22 @@ func (node *Node) CreateNode() []string {
node.NetBase = "none"
}
if node.Type == "docker" || node.Type == "" {
createNodeCmd = fmt.Sprintf("docker run -td --hostname %s --net %s --name %s --rm --privileged ", node.Name, node.NetBase, node.Name)
createNodeCmd = fmt.Sprintf("docker run -td --net %s --name %s --rm --privileged ", node.NetBase, node.Name)

if !node.HostNameIgnore {
createNodeCmd += fmt.Sprintf("--hostname %s ", node.Name)
}

if len(node.Sysctls) != 0 {
for _, sysctl := range node.Sysctls {
createNodeCmd += fmt.Sprintf("--sysctl %s ", sysctl.Sysctl)
}
}

if node.EntryPoint != "" {
createNodeCmd += fmt.Sprintf("--entrypoint %s ", node.EntryPoint)
}

if node.VolumeBase == "" {
createNodeCmd += fmt.Sprintf("-v /tmp/tinet:/tinet ")
} else {
Expand Down
176 changes: 113 additions & 63 deletions internal/pkg/shell/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,15 @@ func TestNodeConfig_ExecConf(t *testing.T) {

func TestNode_DeleteNode(t *testing.T) {
type fields struct {
Name string
Type string
NetBase string
Image string
Interfaces []Interface
Sysctls []Sysctl
Mounts []string
Name string
Type string
NetBase string
Image string
Interfaces []Interface
Sysctls []Sysctl
Mounts []string
HostNameIgnore bool
EntryPoint string
}
tests := []struct {
name string
Expand Down Expand Up @@ -172,13 +174,15 @@ func TestNode_DeleteNode(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
node := &Node{
Name: tt.fields.Name,
Type: tt.fields.Type,
NetBase: tt.fields.NetBase,
Image: tt.fields.Image,
Interfaces: tt.fields.Interfaces,
Sysctls: tt.fields.Sysctls,
Mounts: tt.fields.Mounts,
Name: tt.fields.Name,
Type: tt.fields.Type,
NetBase: tt.fields.NetBase,
Image: tt.fields.Image,
Interfaces: tt.fields.Interfaces,
Sysctls: tt.fields.Sysctls,
Mounts: tt.fields.Mounts,
HostNameIgnore: tt.fields.HostNameIgnore,
EntryPoint: tt.fields.EntryPoint,
}
if got := node.DeleteNode(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Node.DeleteNode() = %v, want %v", got, tt.want)
Expand Down Expand Up @@ -474,14 +478,16 @@ func TestExecCmd(t *testing.T) {

func TestNode_CreateNode(t *testing.T) {
type fields struct {
Name string
Type string
NetBase string
VolumeBase string
Image string
Interfaces []Interface
Sysctls []Sysctl
Mounts []string
Name string
Type string
NetBase string
VolumeBase string
Image string
Interfaces []Interface
Sysctls []Sysctl
Mounts []string
HostNameIgnore bool
EntryPoint string
}
tests := []struct {
name string
Expand All @@ -501,7 +507,7 @@ func TestNode_CreateNode(t *testing.T) {
},
},
},
want: []string{"docker run -td --hostname R1 --net none --name R1 --rm --privileged -v /tmp/tinet:/tinet slankdev/frr"},
want: []string{"docker run -td --net none --name R1 --rm --privileged --hostname R1 -v /tmp/tinet:/tinet slankdev/frr"},
},
{
name: "create docker node net None with sysctls",
Expand All @@ -524,7 +530,7 @@ func TestNode_CreateNode(t *testing.T) {
},
},
},
want: []string{"docker run -td --hostname R1 --net none --name R1 --rm --privileged --sysctl net.ipv4.ip_forward=1 --sysctl net.ipv6.conf.all.forwarding=1 -v /tmp/tinet:/tinet slankdev/frr"},
want: []string{"docker run -td --net none --name R1 --rm --privileged --hostname R1 --sysctl net.ipv4.ip_forward=1 --sysctl net.ipv6.conf.all.forwarding=1 -v /tmp/tinet:/tinet slankdev/frr"},
},
{
name: "create docker node net bridge",
Expand All @@ -540,7 +546,7 @@ func TestNode_CreateNode(t *testing.T) {
},
},
},
want: []string{"docker run -td --hostname R1 --net bridge --name R1 --rm --privileged -v /tmp/tinet:/tinet slankdev/frr"},
want: []string{"docker run -td --net bridge --name R1 --rm --privileged --hostname R1 -v /tmp/tinet:/tinet slankdev/frr"},
},
{
name: "create netns node",
Expand Down Expand Up @@ -615,7 +621,7 @@ func TestNode_CreateNode(t *testing.T) {
"/usr/share/vim:/mnt/vim",
},
},
want: []string{"docker run -td --hostname T1 --net none --name T1 --rm --privileged -v /tmp/tinet:/tinet -v `pwd`:/mnt/test -v /usr/share/vim:/mnt/vim slankdev/frr"},
want: []string{"docker run -td --net none --name T1 --rm --privileged --hostname T1 -v /tmp/tinet:/tinet -v `pwd`:/mnt/test -v /usr/share/vim:/mnt/vim slankdev/frr"},
},
{
name: "create node with specify tinet volume",
Expand All @@ -631,20 +637,56 @@ func TestNode_CreateNode(t *testing.T) {
},
},
},
want: []string{"docker run -td --hostname T1 --net none --name T1 --rm --privileged -v /tmp/ak1ra24:/tinet slankdev/frr"},
want: []string{"docker run -td --net none --name T1 --rm --privileged --hostname T1 -v /tmp/ak1ra24:/tinet slankdev/frr"},
},
{
name: "create node with hostname_ignore",
fields: fields{
Name: "T1",
Image: "slankdev/frr",
VolumeBase: "/tmp/ak1ra24",
Interfaces: []Interface{
Interface{
Name: "net0",
Type: "direct",
Args: "T2#net0",
},
},
HostNameIgnore: true,
},
want: []string{"docker run -td --net none --name T1 --rm --privileged -v /tmp/ak1ra24:/tinet slankdev/frr"},
},
{
name: "create node with entrypoint",
fields: fields{
Name: "T1",
Image: "slankdev/frr",
VolumeBase: "/tmp/ak1ra24",
Interfaces: []Interface{
Interface{
Name: "net0",
Type: "direct",
Args: "T2#net0",
},
},
EntryPoint: "bash",
},
want: []string{"docker run -td --net none --name T1 --rm --privileged --hostname T1 --entrypoint bash -v /tmp/ak1ra24:/tinet slankdev/frr"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
node := &Node{
Name: tt.fields.Name,
Type: tt.fields.Type,
NetBase: tt.fields.NetBase,
VolumeBase: tt.fields.VolumeBase,
Image: tt.fields.Image,
Interfaces: tt.fields.Interfaces,
Sysctls: tt.fields.Sysctls,
Mounts: tt.fields.Mounts,
Name: tt.fields.Name,
Type: tt.fields.Type,
NetBase: tt.fields.NetBase,
VolumeBase: tt.fields.VolumeBase,
Image: tt.fields.Image,
Interfaces: tt.fields.Interfaces,
Sysctls: tt.fields.Sysctls,
Mounts: tt.fields.Mounts,
HostNameIgnore: tt.fields.HostNameIgnore,
EntryPoint: tt.fields.EntryPoint,
}
if got := node.CreateNode(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Node.CreateNode() = %v, want %v", got, tt.want)
Expand Down Expand Up @@ -941,13 +983,15 @@ func TestInterface_P2cLink(t *testing.T) {

func TestNode_Mount_docker_netns(t *testing.T) {
type fields struct {
Name string
Type string
NetBase string
Image string
Interfaces []Interface
Sysctls []Sysctl
Mounts []string
Name string
Type string
NetBase string
Image string
Interfaces []Interface
Sysctls []Sysctl
Mounts []string
HostNameIgnore bool
EntryPoint string
}
tests := []struct {
name string
Expand All @@ -974,13 +1018,15 @@ func TestNode_Mount_docker_netns(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
node := &Node{
Name: tt.fields.Name,
Type: tt.fields.Type,
NetBase: tt.fields.NetBase,
Image: tt.fields.Image,
Interfaces: tt.fields.Interfaces,
Sysctls: tt.fields.Sysctls,
Mounts: tt.fields.Mounts,
Name: tt.fields.Name,
Type: tt.fields.Type,
NetBase: tt.fields.NetBase,
Image: tt.fields.Image,
Interfaces: tt.fields.Interfaces,
Sysctls: tt.fields.Sysctls,
Mounts: tt.fields.Mounts,
HostNameIgnore: tt.fields.HostNameIgnore,
EntryPoint: tt.fields.EntryPoint,
}
if got := node.Mount_docker_netns(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Node.Mount_docker_netns() = %v, want %v", got, tt.want)
Expand Down Expand Up @@ -1017,13 +1063,15 @@ func TestGetContainerPid(t *testing.T) {

func TestNode_DelNsCmd(t *testing.T) {
type fields struct {
Name string
Type string
NetBase string
Image string
Interfaces []Interface
Sysctls []Sysctl
Mounts []string
Name string
Type string
NetBase string
Image string
Interfaces []Interface
Sysctls []Sysctl
Mounts []string
HostNameIgnore bool
EntryPoint string
}
tests := []struct {
name string
Expand All @@ -1050,13 +1098,15 @@ func TestNode_DelNsCmd(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
node := &Node{
Name: tt.fields.Name,
Type: tt.fields.Type,
NetBase: tt.fields.NetBase,
Image: tt.fields.Image,
Interfaces: tt.fields.Interfaces,
Sysctls: tt.fields.Sysctls,
Mounts: tt.fields.Mounts,
Name: tt.fields.Name,
Type: tt.fields.Type,
NetBase: tt.fields.NetBase,
Image: tt.fields.Image,
Interfaces: tt.fields.Interfaces,
Sysctls: tt.fields.Sysctls,
Mounts: tt.fields.Mounts,
HostNameIgnore: tt.fields.HostNameIgnore,
EntryPoint: tt.fields.EntryPoint,
}
if got := node.DelNsCmd(); got != tt.want {
t.Errorf("Node.DelNsCmd() = %v, want %v", got, tt.want)
Expand Down