common_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package api
  2. import (
  3. "io/ioutil"
  4. "path/filepath"
  5. "testing"
  6. "github.com/docker/docker/api/types"
  7. "os"
  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/udp, 1.2.3.4:8899->9988/tcp",
  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. for _, port := range cases {
  189. actual := DisplayablePorts(port.ports)
  190. if port.expected != actual {
  191. t.Fatalf("Expected %s, got %s.", port.expected, actual)
  192. }
  193. }
  194. }
  195. // MatchesContentType
  196. func TestJsonContentType(t *testing.T) {
  197. if !MatchesContentType("application/json", "application/json") {
  198. t.Fail()
  199. }
  200. if !MatchesContentType("application/json; charset=utf-8", "application/json") {
  201. t.Fail()
  202. }
  203. if MatchesContentType("dockerapplication/json", "application/json") {
  204. t.Fail()
  205. }
  206. }
  207. // LoadOrCreateTrustKey
  208. func TestLoadOrCreateTrustKeyInvalidKeyFile(t *testing.T) {
  209. tmpKeyFolderPath, err := ioutil.TempDir("", "api-trustkey-test")
  210. if err != nil {
  211. t.Fatal(err)
  212. }
  213. defer os.RemoveAll(tmpKeyFolderPath)
  214. tmpKeyFile, err := ioutil.TempFile(tmpKeyFolderPath, "keyfile")
  215. if err != nil {
  216. t.Fatal(err)
  217. }
  218. if _, err := LoadOrCreateTrustKey(tmpKeyFile.Name()); err == nil {
  219. t.Fatalf("expected an error, got nothing.")
  220. }
  221. }
  222. func TestLoadOrCreateTrustKeyCreateKey(t *testing.T) {
  223. tmpKeyFolderPath, err := ioutil.TempDir("", "api-trustkey-test")
  224. if err != nil {
  225. t.Fatal(err)
  226. }
  227. defer os.RemoveAll(tmpKeyFolderPath)
  228. // Without the need to create the folder hierarchy
  229. tmpKeyFile := filepath.Join(tmpKeyFolderPath, "keyfile")
  230. if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil {
  231. t.Fatalf("expected a new key file, got : %v and %v", err, key)
  232. }
  233. if _, err := os.Stat(tmpKeyFile); err != nil {
  234. t.Fatalf("Expected to find a file %s, got %v", tmpKeyFile, err)
  235. }
  236. // With the need to create the folder hierarchy as tmpKeyFie is in a path
  237. // where some folder do not exists.
  238. tmpKeyFile = filepath.Join(tmpKeyFolderPath, "folder/hierarchy/keyfile")
  239. if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil {
  240. t.Fatalf("expected a new key file, got : %v and %v", err, key)
  241. }
  242. if _, err := os.Stat(tmpKeyFile); err != nil {
  243. t.Fatalf("Expected to find a file %s, got %v", tmpKeyFile, err)
  244. }
  245. // With no path at all
  246. defer os.Remove("keyfile")
  247. if key, err := LoadOrCreateTrustKey("keyfile"); err != nil || key == nil {
  248. t.Fatalf("expected a new key file, got : %v and %v", err, key)
  249. }
  250. if _, err := os.Stat("keyfile"); err != nil {
  251. t.Fatalf("Expected to find a file keyfile, got %v", err)
  252. }
  253. }
  254. func TestLoadOrCreateTrustKeyLoadValidKey(t *testing.T) {
  255. tmpKeyFile := filepath.Join("fixtures", "keyfile")
  256. if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil {
  257. t.Fatalf("expected a key file, got : %v and %v", err, key)
  258. }
  259. }