Skip to content

Commit 275d306

Browse files
author
Vipin Jain
committed
add container runtime unit tests
1 parent c8cad8b commit 275d306

File tree

5 files changed

+86
-54
lines changed

5 files changed

+86
-54
lines changed

crt/crt.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package crt
1818
import (
1919
"encoding/json"
2020
"errors"
21-
"log"
2221
"reflect"
2322

2423
"github.com/contiv/netplugin/core"
@@ -72,6 +71,10 @@ func (c *Crt) GetContainerName(contId string) (string, error) {
7271
return c.ContainerIf.GetContainerName(contId)
7372
}
7473

74+
func (c *Crt) Deinit() {
75+
c.ContainerIf.Deinit()
76+
}
77+
7578
func (c *Crt) Init(configStr string) error {
7679

7780
cfg := &CrtConfig{}
@@ -80,8 +83,6 @@ func (c *Crt) Init(configStr string) error {
8083
return err
8184
}
8285

83-
log.Printf("container runtime %s \n", cfg.Crt.Type)
84-
8586
if _, ok := ContainerIfRegistry[cfg.Crt.Type]; !ok {
8687
return errors.New("unregistered container run time")
8788
}
@@ -92,7 +93,6 @@ func (c *Crt) Init(configStr string) error {
9293
if err != nil {
9394
return err
9495
}
95-
log.Printf("got crt config %v \n", crtConfig)
9696

9797
config := &core.Config{V: crtConfig}
9898
crtType := ContainerIfRegistry[cfg.Crt.Type].CrtType

crt/crt_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/***
2+
Copyright 2014 Cisco Systems Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package crt
17+
18+
import (
19+
"testing"
20+
)
21+
22+
func TestCrtInit(t *testing.T) {
23+
configStr := `{
24+
"docker" : {
25+
"socket" : "unix:///var/run/docker.sock"
26+
},
27+
"crt" : {
28+
"type": "docker"
29+
}
30+
}`
31+
crt := Crt{}
32+
err := crt.Init(configStr)
33+
if err != nil {
34+
t.Fatalf("crt init failed: Error: %s", err)
35+
}
36+
defer func() { crt.Deinit() }()
37+
}
38+
39+
func TestCrtInitInvalidConfigMissingCrt(t *testing.T) {
40+
configStr := `{
41+
"docker" : {
42+
"socket" : "unix:///var/run/docker.sock"
43+
}
44+
}`
45+
crt := Crt{}
46+
err := crt.Init(configStr)
47+
if err == nil {
48+
t.Fatalf("crt init succeeded!")
49+
}
50+
}
51+
52+
func TestCrtInitInvalidConfigMissingCrtIf(t *testing.T) {
53+
configStr := `{
54+
"crt" : {
55+
"type": "docker"
56+
}
57+
}`
58+
crt := Crt{}
59+
err := crt.Init(configStr)
60+
if err == nil {
61+
t.Fatalf("crt init succeeded!")
62+
}
63+
}
64+
65+
func TestCrtInitInvalidConfigInvalidCrt(t *testing.T) {
66+
configStr := `{
67+
"crt" : {
68+
"type": "rocket"
69+
}
70+
}`
71+
crt := Crt{}
72+
err := crt.Init(configStr)
73+
if err == nil {
74+
t.Fatalf("crt init succeeded!")
75+
}
76+
}

crt/docker/docker.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type Docker struct {
4242

4343
func (d *Docker) Init(config *core.Config) error {
4444
if config == nil {
45-
return &core.Error{Desc: fmt.Sprintf("Invalid arguments. cfg: %v", config)}
45+
return errors.New("null config!")
4646
}
4747

4848
cfg, ok := config.V.(*DockerConfig)
@@ -51,6 +51,10 @@ func (d *Docker) Init(config *core.Config) error {
5151
return &core.Error{Desc: "Invalid config type passed!"}
5252
}
5353

54+
if cfg.Docker.Socket == "" {
55+
return errors.New(fmt.Sprintf("Invalid arguments. cfg: %v", config))
56+
}
57+
5458
// TODO: ADD TLS support
5559
d.Client, _ = dockerclient.NewDockerClient(cfg.Docker.Socket, nil)
5660

plugin/netplugin_test.go

-49
Original file line numberDiff line numberDiff line change
@@ -177,52 +177,3 @@ func TestNetPluginInitInvalidConfigMissingNetworkDriver(t *testing.T) {
177177
}
178178
defer func() { plugin.Deinit() }()
179179
}
180-
181-
func TestNetPluginInitInvalidConfigMissingContainerDriverName(t *testing.T) {
182-
configStr := `{
183-
"drivers" : {
184-
"network": "ovs",
185-
"endpoint": "ovs",
186-
"state": "etcd"
187-
},
188-
"ovs" : {
189-
"dbip": "127.0.0.1",
190-
"dbport": 6640
191-
},
192-
"etcd" : {
193-
"machines": ["http://1.0.0.1:4001"]
194-
},
195-
"docker" : {
196-
"socket" : "unix:///var/run/docker.sock"
197-
}
198-
}`
199-
plugin := NetPlugin{}
200-
err := plugin.Init(configStr)
201-
if err == nil {
202-
t.Fatalf("plugin init failed: Error: %s", err)
203-
}
204-
}
205-
206-
func TestNetPluginInitInvalidConfigMissingContainerDriver(t *testing.T) {
207-
configStr := `{
208-
"drivers" : {
209-
"network": "ovs",
210-
"endpoint": "ovs",
211-
"state": "etcd",
212-
"container": "docker"
213-
},
214-
"ovs" : {
215-
"dbip": "127.0.0.1",
216-
"dbport": 6640
217-
},
218-
"etcd" : {
219-
"machines": ["http://1.0.0.1:4001"]
220-
}
221-
}`
222-
plugin := NetPlugin{}
223-
err := plugin.Init(configStr)
224-
if err != nil {
225-
t.Fatalf("plugin init failed: Error: %s", err)
226-
}
227-
defer func() { plugin.Deinit() }()
228-
}

scripts/unittests

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ run_in_vagrant=false
1111
test_packages="github.com/contiv/netplugin/drivers \
1212
github.com/contiv/netplugin/plugin \
1313
github.com/contiv/netplugin/netutils \
14+
github.com/contiv/netplugin/crt \
1415
github.com/contiv/netplugin/gstate"
1516

1617
while [ "${#}" -gt 0 ]

0 commit comments

Comments
 (0)