common_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. package api
  2. import (
  3. "io/ioutil"
  4. "path/filepath"
  5. "testing"
  6. "os"
  7. "github.com/docker/engine-api/types"
  8. )
  9. type ports struct {
  10. ports []types.Port
  11. expected string
  12. }
  13. // DisplayablePorts
  14. func TestDisplayablePorts(t *testing.T) {
  15. cases := []ports{
  16. {
  17. []types.Port{
  18. {
  19. PrivatePort: 9988,
  20. Type: "tcp",
  21. },
  22. },
  23. "9988/tcp"},
  24. {
  25. []types.Port{
  26. {
  27. PrivatePort: 9988,
  28. Type: "udp",
  29. },
  30. },
  31. "9988/udp",
  32. },
  33. {
  34. []types.Port{
  35. {
  36. IP: "0.0.0.0",
  37. PrivatePort: 9988,
  38. Type: "tcp",
  39. },
  40. },
  41. "0.0.0.0:0->9988/tcp",
  42. },
  43. {
  44. []types.Port{
  45. {
  46. PrivatePort: 9988,
  47. PublicPort: 8899,
  48. Type: "tcp",
  49. },
  50. },
  51. "9988/tcp",
  52. },
  53. {
  54. []types.Port{
  55. {
  56. IP: "4.3.2.1",
  57. PrivatePort: 9988,
  58. PublicPort: 8899,
  59. Type: "tcp",
  60. },
  61. },
  62. "4.3.2.1:8899->9988/tcp",
  63. },
  64. {
  65. []types.Port{
  66. {
  67. IP: "4.3.2.1",
  68. PrivatePort: 9988,
  69. PublicPort: 9988,
  70. Type: "tcp",
  71. },
  72. },
  73. "4.3.2.1:9988->9988/tcp",
  74. },
  75. {
  76. []types.Port{
  77. {
  78. PrivatePort: 9988,
  79. Type: "udp",
  80. }, {
  81. PrivatePort: 9988,
  82. Type: "udp",
  83. },
  84. },
  85. "9988/udp, 9988/udp",
  86. },
  87. {
  88. []types.Port{
  89. {
  90. IP: "1.2.3.4",
  91. PublicPort: 9998,
  92. PrivatePort: 9998,
  93. Type: "udp",
  94. }, {
  95. IP: "1.2.3.4",
  96. PublicPort: 9999,
  97. PrivatePort: 9999,
  98. Type: "udp",
  99. },
  100. },
  101. "1.2.3.4:9998-9999->9998-9999/udp",
  102. },
  103. {
  104. []types.Port{
  105. {
  106. IP: "1.2.3.4",
  107. PublicPort: 8887,
  108. PrivatePort: 9998,
  109. Type: "udp",
  110. }, {
  111. IP: "1.2.3.4",
  112. PublicPort: 8888,
  113. PrivatePort: 9999,
  114. Type: "udp",
  115. },
  116. },
  117. "1.2.3.4:8887->9998/udp, 1.2.3.4:8888->9999/udp",
  118. },
  119. {
  120. []types.Port{
  121. {
  122. PrivatePort: 9998,
  123. Type: "udp",
  124. }, {
  125. PrivatePort: 9999,
  126. Type: "udp",
  127. },
  128. },
  129. "9998-9999/udp",
  130. },
  131. {
  132. []types.Port{
  133. {
  134. IP: "1.2.3.4",
  135. PrivatePort: 6677,
  136. PublicPort: 7766,
  137. Type: "tcp",
  138. }, {
  139. PrivatePort: 9988,
  140. PublicPort: 8899,
  141. Type: "udp",
  142. },
  143. },
  144. "9988/udp, 1.2.3.4:7766->6677/tcp",
  145. },
  146. {
  147. []types.Port{
  148. {
  149. IP: "1.2.3.4",
  150. PrivatePort: 9988,
  151. PublicPort: 8899,
  152. Type: "udp",
  153. }, {
  154. IP: "1.2.3.4",
  155. PrivatePort: 9988,
  156. PublicPort: 8899,
  157. Type: "tcp",
  158. }, {
  159. IP: "4.3.2.1",
  160. PrivatePort: 2233,
  161. PublicPort: 3322,
  162. Type: "tcp",
  163. },
  164. },
  165. "4.3.2.1:3322->2233/tcp, 1.2.3.4:8899->9988/tcp, 1.2.3.4:8899->9988/udp",
  166. },
  167. {
  168. []types.Port{
  169. {
  170. PrivatePort: 9988,
  171. PublicPort: 8899,
  172. Type: "udp",
  173. }, {
  174. IP: "1.2.3.4",
  175. PrivatePort: 6677,
  176. PublicPort: 7766,
  177. Type: "tcp",
  178. }, {
  179. IP: "4.3.2.1",
  180. PrivatePort: 2233,
  181. PublicPort: 3322,
  182. Type: "tcp",
  183. },
  184. },
  185. "9988/udp, 4.3.2.1:3322->2233/tcp, 1.2.3.4:7766->6677/tcp",
  186. },
  187. {
  188. []types.Port{
  189. {
  190. PrivatePort: 80,
  191. Type: "tcp",
  192. }, {
  193. PrivatePort: 1024,
  194. Type: "tcp",
  195. }, {
  196. PrivatePort: 80,
  197. Type: "udp",
  198. }, {
  199. PrivatePort: 1024,
  200. Type: "udp",
  201. }, {
  202. IP: "1.1.1.1",
  203. PublicPort: 80,
  204. PrivatePort: 1024,
  205. Type: "tcp",
  206. }, {
  207. IP: "1.1.1.1",
  208. PublicPort: 80,
  209. PrivatePort: 1024,
  210. Type: "udp",
  211. }, {
  212. IP: "1.1.1.1",
  213. PublicPort: 1024,
  214. PrivatePort: 80,
  215. Type: "tcp",
  216. }, {
  217. IP: "1.1.1.1",
  218. PublicPort: 1024,
  219. PrivatePort: 80,
  220. Type: "udp",
  221. }, {
  222. IP: "2.1.1.1",
  223. PublicPort: 80,
  224. PrivatePort: 1024,
  225. Type: "tcp",
  226. }, {
  227. IP: "2.1.1.1",
  228. PublicPort: 80,
  229. PrivatePort: 1024,
  230. Type: "udp",
  231. }, {
  232. IP: "2.1.1.1",
  233. PublicPort: 1024,
  234. PrivatePort: 80,
  235. Type: "tcp",
  236. }, {
  237. IP: "2.1.1.1",
  238. PublicPort: 1024,
  239. PrivatePort: 80,
  240. Type: "udp",
  241. },
  242. },
  243. "80/tcp, 80/udp, 1024/tcp, 1024/udp, 1.1.1.1:1024->80/tcp, 1.1.1.1:1024->80/udp, 2.1.1.1:1024->80/tcp, 2.1.1.1:1024->80/udp, 1.1.1.1:80->1024/tcp, 1.1.1.1:80->1024/udp, 2.1.1.1:80->1024/tcp, 2.1.1.1:80->1024/udp",
  244. },
  245. }
  246. for _, port := range cases {
  247. actual := DisplayablePorts(port.ports)
  248. if port.expected != actual {
  249. t.Fatalf("Expected %s, got %s.", port.expected, actual)
  250. }
  251. }
  252. }
  253. // MatchesContentType
  254. func TestJsonContentType(t *testing.T) {
  255. if !MatchesContentType("application/json", "application/json") {
  256. t.Fail()
  257. }
  258. if !MatchesContentType("application/json; charset=utf-8", "application/json") {
  259. t.Fail()
  260. }
  261. if MatchesContentType("dockerapplication/json", "application/json") {
  262. t.Fail()
  263. }
  264. }
  265. // LoadOrCreateTrustKey
  266. func TestLoadOrCreateTrustKeyInvalidKeyFile(t *testing.T) {
  267. tmpKeyFolderPath, err := ioutil.TempDir("", "api-trustkey-test")
  268. if err != nil {
  269. t.Fatal(err)
  270. }
  271. defer os.RemoveAll(tmpKeyFolderPath)
  272. tmpKeyFile, err := ioutil.TempFile(tmpKeyFolderPath, "keyfile")
  273. if err != nil {
  274. t.Fatal(err)
  275. }
  276. if _, err := LoadOrCreateTrustKey(tmpKeyFile.Name()); err == nil {
  277. t.Fatalf("expected an error, got nothing.")
  278. }
  279. }
  280. func TestLoadOrCreateTrustKeyCreateKey(t *testing.T) {
  281. tmpKeyFolderPath, err := ioutil.TempDir("", "api-trustkey-test")
  282. if err != nil {
  283. t.Fatal(err)
  284. }
  285. defer os.RemoveAll(tmpKeyFolderPath)
  286. // Without the need to create the folder hierarchy
  287. tmpKeyFile := filepath.Join(tmpKeyFolderPath, "keyfile")
  288. if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil {
  289. t.Fatalf("expected a new key file, got : %v and %v", err, key)
  290. }
  291. if _, err := os.Stat(tmpKeyFile); err != nil {
  292. t.Fatalf("Expected to find a file %s, got %v", tmpKeyFile, err)
  293. }
  294. // With the need to create the folder hierarchy as tmpKeyFie is in a path
  295. // where some folders do not exist.
  296. tmpKeyFile = filepath.Join(tmpKeyFolderPath, "folder/hierarchy/keyfile")
  297. if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil {
  298. t.Fatalf("expected a new key file, got : %v and %v", err, key)
  299. }
  300. if _, err := os.Stat(tmpKeyFile); err != nil {
  301. t.Fatalf("Expected to find a file %s, got %v", tmpKeyFile, err)
  302. }
  303. // With no path at all
  304. defer os.Remove("keyfile")
  305. if key, err := LoadOrCreateTrustKey("keyfile"); err != nil || key == nil {
  306. t.Fatalf("expected a new key file, got : %v and %v", err, key)
  307. }
  308. if _, err := os.Stat("keyfile"); err != nil {
  309. t.Fatalf("Expected to find a file keyfile, got %v", err)
  310. }
  311. }
  312. func TestLoadOrCreateTrustKeyLoadValidKey(t *testing.T) {
  313. tmpKeyFile := filepath.Join("fixtures", "keyfile")
  314. if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil {
  315. t.Fatalf("expected a key file, got : %v and %v", err, key)
  316. }
  317. }