PageRenderTime 78ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/Godeps/_workspace/src/github.com/milosgajdos83/tenus/helpers_linux_test.go

https://gitlab.com/Lin0x/oz
Go | 264 lines | 224 code | 39 blank | 1 comment | 82 complexity | 61f5f11a61dabc0d41a0bb840bdbea9f MD5 | raw file
  1. package tenus
  2. import (
  3. "bytes"
  4. "net"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  8. "testing"
  9. "time"
  10. )
  11. type ifcNameTest struct {
  12. ifcName string
  13. expected bool
  14. }
  15. var ifcNameTests = []ifcNameTest{
  16. {"link1", true},
  17. {"", false},
  18. {"a", false},
  19. {"abcdefghijklmnopqr", false},
  20. {"link\uF021", false},
  21. {"eth0.123", true},
  22. }
  23. func Test_NetInterfaceNameValid(t *testing.T) {
  24. for _, tt := range ifcNameTests {
  25. ret, _ := NetInterfaceNameValid(tt.ifcName)
  26. if ret != tt.expected {
  27. t.Errorf("NetInterfaceNameValid(%s): expected %v, returned %v", tt.ifcName, tt.expected, ret)
  28. }
  29. }
  30. }
  31. type ifcMacTest struct {
  32. testLink
  33. opts LinkOptions
  34. testVal string
  35. expected *net.Interface
  36. }
  37. // correct MAC Address will always parse into HardwareAddr
  38. var hw, _ = net.ParseMAC("22:ce:e0:99:63:6f")
  39. var ifcMacTests = []ifcMacTest{
  40. {testLink{name: "ifc01", linkType: "dummy"},
  41. LinkOptions{MacAddr: "22:ce:e0:99:63:6f"}, "22:ce:e0:99:63:6f",
  42. &net.Interface{Name: "ifc01", HardwareAddr: hw}},
  43. {testLink{name: "ifc02", linkType: "dummy"},
  44. LinkOptions{MacAddr: "26:2e:71:98:60:8f"}, "",
  45. nil},
  46. {testLink{name: "ifc03", linkType: "dummy"},
  47. LinkOptions{MacAddr: "fa:de:b0:99:52:1c"}, "randomstring",
  48. nil},
  49. }
  50. func Test_FindInterfaceByMacAddress(t *testing.T) {
  51. for _, tt := range ifcMacTests {
  52. tl := &testLink{}
  53. if err := tl.prepTestLink(tt.name, tt.linkType); err != nil {
  54. t.Skipf("InterfaceByMacAddress test requries external command: %v", err)
  55. }
  56. if err := tl.prepLinkOpts(LinkOptions{MacAddr: tt.opts.MacAddr}); err != nil {
  57. t.Skipf("InterfaceByMacAddress test requries external command: %v", err)
  58. }
  59. if err := tl.create(); err != nil {
  60. t.Fatalf("testLink.create failed: %v", err)
  61. } else {
  62. time.Sleep(10 * time.Millisecond)
  63. }
  64. if err := tl.setup(); err != nil {
  65. t.Fatalf("testLink.setup failed: %v", err)
  66. } else {
  67. time.Sleep(10 * time.Millisecond)
  68. }
  69. ifc, err := FindInterfaceByMacAddress(tt.testVal)
  70. if ifc != nil {
  71. if tt.expected != nil {
  72. if ifc.Name != tt.expected.Name || !bytes.Equal(ifc.HardwareAddr, tt.expected.HardwareAddr) {
  73. tl.teardown()
  74. t.Fatalf("FindInterfaceByMacAddress(%s): expected %v, returned %v",
  75. tt.testVal, tt.expected, ifc)
  76. }
  77. }
  78. if tt.expected == nil {
  79. tl.teardown()
  80. t.Fatalf("FindInterfaceByMacAddress(%s): expected %v, returned %v ",
  81. tt.testVal, tt.expected, ifc)
  82. }
  83. }
  84. if ifc == nil {
  85. if tt.expected != nil {
  86. tl.teardown()
  87. t.Fatalf("FindInterfaceByMacAddress(%s): expected %v, returned %v, error: %s",
  88. tt.testVal, tt.expected, ifc, err)
  89. }
  90. }
  91. if err := tl.teardown(); err != nil {
  92. t.Fatalf("testLink.teardown failed: %v", err)
  93. } else {
  94. time.Sleep(10 * time.Millisecond)
  95. }
  96. }
  97. }
  98. type dockerPidTest struct {
  99. testDocker
  100. host string
  101. expected int
  102. }
  103. var dockerPidTests = []dockerPidTest{
  104. {testDocker{name: "topper1", command: "/usr/bin/top"}, "/var/run/docker.sock", 1234},
  105. {testDocker{name: "topper2", command: "/usr/bin/top"}, "somehost.com:9011", 0},
  106. {testDocker{name: "topper3", command: "/usr/bin/top"}, "", 0},
  107. }
  108. func Test_DockerPidByName(t *testing.T) {
  109. for _, tt := range dockerPidTests {
  110. td := &testDocker{}
  111. if err := td.prepTestDocker(tt.name, tt.command); err != nil {
  112. t.Skipf("DockerPidByName test requries external command: %v", err)
  113. }
  114. if err := td.create(); err != nil {
  115. t.Fatalf("prepTestDocker.create failed: %v", err)
  116. } else {
  117. time.Sleep(10 * time.Millisecond)
  118. }
  119. retPid, err := DockerPidByName(tt.name, tt.host)
  120. if retPid != 0 {
  121. if tt.expected != 0 {
  122. dockerPath, err := exec.LookPath("docker")
  123. if err != nil {
  124. t.Errorf("Unable to find docker in PATH: %s", err)
  125. }
  126. out, err := exec.Command(dockerPath, "inspect", "-f", "{{.State.Pid }}", tt.name).Output()
  127. if err != nil {
  128. td.teardown()
  129. t.Fatalf("Failed to run tt.testCmd.Output(): %s", err)
  130. }
  131. actualPid, err := strconv.Atoi(strings.TrimSpace(string(out)))
  132. if err != nil {
  133. td.teardown()
  134. t.Fatalf("Failed to run strconv.Atoi(strings.TrimSpace(string(%v))): %s",
  135. out, err)
  136. }
  137. if retPid != actualPid {
  138. td.teardown()
  139. t.Errorf("DockerPidByName(%s, %s): expected: %v, returned: %v",
  140. tt.name, tt.host, out, retPid)
  141. }
  142. }
  143. if tt.expected == 0 {
  144. td.teardown()
  145. t.Errorf("DockerPidByName(%s, %s): expected: %v, returned: %v",
  146. tt.name, tt.host, tt.expected, retPid)
  147. }
  148. }
  149. if retPid == 0 {
  150. if tt.expected != 0 {
  151. td.teardown()
  152. t.Errorf("DockerPidByName(%s, %s): expected: %v, returned: %v, error: %s",
  153. tt.name, tt.host, tt.expected, retPid, err)
  154. }
  155. }
  156. if err := td.teardown(); err != nil {
  157. t.Fatalf("testDocker.teardown failed: %v", err)
  158. } else {
  159. time.Sleep(10 * time.Millisecond)
  160. }
  161. }
  162. }
  163. type netNsTest struct {
  164. pid int
  165. testCmd *exec.Cmd
  166. expected int
  167. }
  168. var netNsTests = []netNsTest{
  169. {1234, &exec.Cmd{
  170. Path: "",
  171. Args: []string{"docker", "inspect", "-f", "{{.State.Pid }}", "testdckr"}},
  172. 1234},
  173. {0, &exec.Cmd{}, 0},
  174. }
  175. func Test_NetNsHandle(t *testing.T) {
  176. for _, tt := range netNsTests {
  177. td := &testDocker{}
  178. if err := td.prepTestDocker("testdckr", "/usr/bin/top"); err != nil {
  179. t.Skipf("NetNsHandle test requries external command: %v", err)
  180. }
  181. if err := td.create(); err != nil {
  182. t.Fatalf("prepTestDocker.create failed: %v", err)
  183. } else {
  184. time.Sleep(10 * time.Millisecond)
  185. }
  186. if tt.pid != 0 {
  187. dockerPath, err := exec.LookPath("docker")
  188. if err != nil {
  189. t.Errorf("Unable to find docker in PATH: %s", err)
  190. }
  191. out, err := exec.Command(dockerPath, "inspect", "-f", "{{.State.Pid }}", "testdckr").Output()
  192. if err != nil {
  193. td.teardown()
  194. t.Fatalf("Failed to run tt.testCmd.Output(): %s", err)
  195. }
  196. actualPid, err := strconv.Atoi(strings.TrimSpace(string(out)))
  197. if err != nil {
  198. td.teardown()
  199. t.Fatalf("Failed to run strconv.Atoi(strings.TrimSpace(string(%v))): %s",
  200. out, err)
  201. }
  202. tt.pid = actualPid
  203. }
  204. nsFd, err := NetNsHandle(tt.pid)
  205. if nsFd == 0 {
  206. if tt.expected != 0 {
  207. td.teardown()
  208. t.Fatalf("NetNsHandle(%d): expected: non-zero, returned: %v, error: %s",
  209. tt.pid, nsFd, err)
  210. }
  211. }
  212. if nsFd != 0 {
  213. if tt.expected == 0 {
  214. td.teardown()
  215. t.Fatalf("NetNsHandle(%d): expected: %d, returned: %v",
  216. tt.pid, tt.expected, nsFd)
  217. }
  218. }
  219. if err := td.teardown(); err != nil {
  220. t.Fatalf("testDocker.teardown failed: %v", err)
  221. } else {
  222. time.Sleep(10 * time.Millisecond)
  223. }
  224. }
  225. }