forked from kata-containers/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel_test.go
142 lines (113 loc) · 3.04 KB
/
channel_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// Copyright (c) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestSetupVSockChannel(t *testing.T) {
c := &vSockChannel{}
err := c.setup()
assert.Nil(t, err, "%v", err)
}
func TestTeardownVSockChannel(t *testing.T) {
c := &vSockChannel{}
err := c.teardown()
assert.Nil(t, err, "%v", err)
}
func TestWaitVSockChannel(t *testing.T) {
c := &vSockChannel{}
err := c.wait()
assert.Nil(t, err, "%v", err)
}
func TestWaitSerialChannel(t *testing.T) {
_, f, err := os.Pipe()
assert.Nil(t, err, "%v", err)
defer f.Close()
c := &serialChannel{serialConn: f}
err = c.wait()
assert.Nil(t, err, "%v", err)
}
func TestListenSerialChannel(t *testing.T) {
f, _, err := os.Pipe()
assert.Nil(t, err, "%v", err)
c := &serialChannel{serialConn: f}
l, err := c.listen()
assert.Nil(t, err, "%v", err)
assert.NotNil(t, l, "listen should not return nil listener")
err = l.Close()
assert.Nil(t, err, "%v", err)
err = c.teardown()
assert.Error(t, err, "connection should be already closed")
}
func TestTeardownSerialChannel(t *testing.T) {
_, f, err := os.Pipe()
assert.Nil(t, err, "%v", err)
c := &serialChannel{serialConn: f}
err = c.teardown()
assert.Nil(t, err, "%v", err)
}
func TestTeardownSerialChannelTimeout(t *testing.T) {
_, f, err := os.Pipe()
assert.Nil(t, err, "%v", err)
channelCloseTimeout = 1 * time.Microsecond
c := &serialChannel{
serialConn: f,
waitCh: make(chan struct{}),
}
err = c.teardown()
assert.NotNil(t, err, "channel close should timeout")
}
func TestNewChannel(t *testing.T) {
assert := assert.New(t)
orgChannelExistMaxTries := channelExistMaxTries
orgChannelExistWaitTime := channelExistWaitTime
orgVSockDevPath := vSockDevPath
orgVirtIOPath := virtIOPath
orgIsAFVSockSupportedFunc := isAFVSockSupportedFunc
channelExistMaxTries = 1
channelExistWaitTime = 0
vSockDevPath = "/abc/xyz/123"
virtIOPath = "/abc/xyz/123"
isAFVSockSupportedFunc = func() (bool, error) { return false, errors.New("vsock") }
defer func() {
channelExistMaxTries = orgChannelExistMaxTries
channelExistWaitTime = orgChannelExistWaitTime
vSockDevPath = orgVSockDevPath
virtIOPath = orgVirtIOPath
isAFVSockSupportedFunc = orgIsAFVSockSupportedFunc
}()
c, err := newChannel()
assert.Error(err)
assert.Nil(c)
vSockDevPath = "/dev/null"
c, err = newChannel()
assert.Error(err)
assert.Nil(c)
isAFVSockSupportedFunc = func() (bool, error) { return true, nil }
c, err = newChannel()
assert.NoError(err)
_, ok := c.(*vSockChannel)
assert.True(ok)
vSockDevPath = "/abc/xyz/123"
virtIOPath, err = ioutil.TempDir("", "virtio")
assert.NoError(err)
portPath := filepath.Join(virtIOPath, "port")
err = os.Mkdir(portPath, 0777)
assert.NoError(err)
defer os.Remove(portPath)
err = ioutil.WriteFile(filepath.Join(portPath, "name"), []byte(serialChannelName), 0777)
assert.NoError(err)
c, err = newChannel()
assert.NoError(err)
_, ok = c.(*serialChannel)
assert.True(ok)
}