helpers_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package libdocker
  14. import (
  15. "fmt"
  16. "testing"
  17. dockertypes "github.com/docker/docker/api/types"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. func TestMatchImageTagOrSHA(t *testing.T) {
  21. for i, testCase := range []struct {
  22. Inspected dockertypes.ImageInspect
  23. Image string
  24. Output bool
  25. }{
  26. {
  27. Inspected: dockertypes.ImageInspect{RepoTags: []string{"ubuntu:latest"}},
  28. Image: "ubuntu",
  29. Output: true,
  30. },
  31. {
  32. Inspected: dockertypes.ImageInspect{RepoTags: []string{"ubuntu:14.04"}},
  33. Image: "ubuntu:latest",
  34. Output: false,
  35. },
  36. {
  37. Inspected: dockertypes.ImageInspect{RepoTags: []string{"colemickens/hyperkube-amd64:217.9beff63"}},
  38. Image: "colemickens/hyperkube-amd64:217.9beff63",
  39. Output: true,
  40. },
  41. {
  42. Inspected: dockertypes.ImageInspect{RepoTags: []string{"colemickens/hyperkube-amd64:217.9beff63"}},
  43. Image: "docker.io/colemickens/hyperkube-amd64:217.9beff63",
  44. Output: true,
  45. },
  46. {
  47. Inspected: dockertypes.ImageInspect{RepoTags: []string{"docker.io/kubernetes/pause:latest"}},
  48. Image: "kubernetes/pause:latest",
  49. Output: true,
  50. },
  51. {
  52. Inspected: dockertypes.ImageInspect{
  53. ID: "sha256:2208f7a29005d226d1ee33a63e33af1f47af6156c740d7d23c7948e8d282d53d",
  54. },
  55. Image: "myimage@sha256:2208f7a29005d226d1ee33a63e33af1f47af6156c740d7d23c7948e8d282d53d",
  56. Output: true,
  57. },
  58. {
  59. Inspected: dockertypes.ImageInspect{
  60. ID: "sha256:2208f7a29005d226d1ee33a63e33af1f47af6156c740d7d23c7948e8d282d53d",
  61. },
  62. Image: "myimage@sha256:2208f7a29005",
  63. Output: false,
  64. },
  65. {
  66. Inspected: dockertypes.ImageInspect{
  67. ID: "sha256:2208f7a29005d226d1ee33a63e33af1f47af6156c740d7d23c7948e8d282d53d",
  68. },
  69. Image: "myimage@sha256:2208",
  70. Output: false,
  71. },
  72. {
  73. // mismatched ID is ignored
  74. Inspected: dockertypes.ImageInspect{
  75. ID: "sha256:2208f7a29005d226d1ee33a63e33af1f47af6156c740d7d23c7948e8d282d53d",
  76. },
  77. Image: "myimage@sha256:0000f7a29005d226d1ee33a63e33af1f47af6156c740d7d23c7948e8d282d53d",
  78. Output: false,
  79. },
  80. {
  81. // invalid digest is ignored
  82. Inspected: dockertypes.ImageInspect{
  83. ID: "sha256:unparseable",
  84. },
  85. Image: "myimage@sha256:unparseable",
  86. Output: false,
  87. },
  88. {
  89. // v1 schema images can be pulled in one format and returned in another
  90. Inspected: dockertypes.ImageInspect{
  91. ID: "sha256:9bbdf247c91345f0789c10f50a57e36a667af1189687ad1de88a6243d05a2227",
  92. RepoDigests: []string{"centos/ruby-23-centos7@sha256:940584acbbfb0347272112d2eb95574625c0c60b4e2fdadb139de5859cf754bf"},
  93. },
  94. Image: "centos/ruby-23-centos7@sha256:940584acbbfb0347272112d2eb95574625c0c60b4e2fdadb139de5859cf754bf",
  95. Output: true,
  96. },
  97. {
  98. Inspected: dockertypes.ImageInspect{
  99. ID: "sha256:9bbdf247c91345f0789c10f50a57e36a667af1189687ad1de88a6243d05a2227",
  100. RepoTags: []string{"docker.io/busybox:latest"},
  101. },
  102. Image: "docker.io/library/busybox:latest",
  103. Output: true,
  104. },
  105. {
  106. // RepoDigest match is required
  107. Inspected: dockertypes.ImageInspect{
  108. ID: "",
  109. RepoDigests: []string{"docker.io/centos/ruby-23-centos7@sha256:000084acbbfb0347272112d2eb95574625c0c60b4e2fdadb139de5859cf754bf"},
  110. },
  111. Image: "centos/ruby-23-centos7@sha256:940584acbbfb0347272112d2eb95574625c0c60b4e2fdadb139de5859cf754bf",
  112. Output: false,
  113. },
  114. {
  115. // RepoDigest match is allowed
  116. Inspected: dockertypes.ImageInspect{
  117. ID: "sha256:9bbdf247c91345f0789c10f50a57e36a667af1189687ad1de88a6243d05a2227",
  118. RepoDigests: []string{"docker.io/centos/ruby-23-centos7@sha256:940584acbbfb0347272112d2eb95574625c0c60b4e2fdadb139de5859cf754bf"},
  119. },
  120. Image: "centos/ruby-23-centos7@sha256:940584acbbfb0347272112d2eb95574625c0c60b4e2fdadb139de5859cf754bf",
  121. Output: true,
  122. },
  123. {
  124. // RepoDigest and ID are checked
  125. Inspected: dockertypes.ImageInspect{
  126. ID: "sha256:940584acbbfb0347272112d2eb95574625c0c60b4e2fdadb139de5859cf754bf",
  127. RepoDigests: []string{"docker.io/centos/ruby-23-centos7@sha256:9bbdf247c91345f0789c10f50a57e36a667af1189687ad1de88a6243d05a2227"},
  128. },
  129. Image: "centos/ruby-23-centos7@sha256:940584acbbfb0347272112d2eb95574625c0c60b4e2fdadb139de5859cf754bf",
  130. Output: true,
  131. },
  132. {
  133. // unparseable RepoDigests are skipped
  134. Inspected: dockertypes.ImageInspect{
  135. ID: "sha256:9bbdf247c91345f0789c10f50a57e36a667af1189687ad1de88a6243d05a2227",
  136. RepoDigests: []string{
  137. "centos/ruby-23-centos7@sha256:unparseable",
  138. "docker.io/centos/ruby-23-centos7@sha256:940584acbbfb0347272112d2eb95574625c0c60b4e2fdadb139de5859cf754bf",
  139. },
  140. },
  141. Image: "centos/ruby-23-centos7@sha256:940584acbbfb0347272112d2eb95574625c0c60b4e2fdadb139de5859cf754bf",
  142. Output: true,
  143. },
  144. {
  145. // unparseable RepoDigest is ignored
  146. Inspected: dockertypes.ImageInspect{
  147. ID: "sha256:9bbdf247c91345f0789c10f50a57e36a667af1189687ad1de88a6243d05a2227",
  148. RepoDigests: []string{"docker.io/centos/ruby-23-centos7@sha256:unparseable"},
  149. },
  150. Image: "centos/ruby-23-centos7@sha256:940584acbbfb0347272112d2eb95574625c0c60b4e2fdadb139de5859cf754bf",
  151. Output: false,
  152. },
  153. {
  154. // unparseable image digest is ignored
  155. Inspected: dockertypes.ImageInspect{
  156. ID: "sha256:9bbdf247c91345f0789c10f50a57e36a667af1189687ad1de88a6243d05a2227",
  157. RepoDigests: []string{"docker.io/centos/ruby-23-centos7@sha256:unparseable"},
  158. },
  159. Image: "centos/ruby-23-centos7@sha256:unparseable",
  160. Output: false,
  161. },
  162. {
  163. // prefix match is rejected for ID and RepoDigest
  164. Inspected: dockertypes.ImageInspect{
  165. ID: "sha256:unparseable",
  166. RepoDigests: []string{"docker.io/centos/ruby-23-centos7@sha256:unparseable"},
  167. },
  168. Image: "sha256:unparseable",
  169. Output: false,
  170. },
  171. {
  172. // possible SHA prefix match is rejected for ID and RepoDigest because it is not in the named format
  173. Inspected: dockertypes.ImageInspect{
  174. ID: "sha256:0000f247c91345f0789c10f50a57e36a667af1189687ad1de88a6243d05a2227",
  175. RepoDigests: []string{"docker.io/centos/ruby-23-centos7@sha256:0000f247c91345f0789c10f50a57e36a667af1189687ad1de88a6243d05a2227"},
  176. },
  177. Image: "sha256:0000",
  178. Output: false,
  179. },
  180. } {
  181. match := matchImageTagOrSHA(testCase.Inspected, testCase.Image)
  182. assert.Equal(t, testCase.Output, match, testCase.Image+fmt.Sprintf(" is not a match (%d)", i))
  183. }
  184. }
  185. func TestMatchImageIDOnly(t *testing.T) {
  186. for i, testCase := range []struct {
  187. Inspected dockertypes.ImageInspect
  188. Image string
  189. Output bool
  190. }{
  191. // shouldn't match names or tagged names
  192. {
  193. Inspected: dockertypes.ImageInspect{RepoTags: []string{"ubuntu:latest"}},
  194. Image: "ubuntu",
  195. Output: false,
  196. },
  197. {
  198. Inspected: dockertypes.ImageInspect{RepoTags: []string{"colemickens/hyperkube-amd64:217.9beff63"}},
  199. Image: "colemickens/hyperkube-amd64:217.9beff63",
  200. Output: false,
  201. },
  202. // should match name@digest refs if they refer to the image ID (but only the full ID)
  203. {
  204. Inspected: dockertypes.ImageInspect{
  205. ID: "sha256:2208f7a29005d226d1ee33a63e33af1f47af6156c740d7d23c7948e8d282d53d",
  206. },
  207. Image: "myimage@sha256:2208f7a29005d226d1ee33a63e33af1f47af6156c740d7d23c7948e8d282d53d",
  208. Output: true,
  209. },
  210. {
  211. Inspected: dockertypes.ImageInspect{
  212. ID: "sha256:2208f7a29005d226d1ee33a63e33af1f47af6156c740d7d23c7948e8d282d53d",
  213. },
  214. Image: "myimage@sha256:2208f7a29005",
  215. Output: false,
  216. },
  217. {
  218. Inspected: dockertypes.ImageInspect{
  219. ID: "sha256:2208f7a29005d226d1ee33a63e33af1f47af6156c740d7d23c7948e8d282d53d",
  220. },
  221. Image: "myimage@sha256:2208",
  222. Output: false,
  223. },
  224. // should match when the IDs are literally the same
  225. {
  226. Inspected: dockertypes.ImageInspect{
  227. ID: "foobar",
  228. },
  229. Image: "foobar",
  230. Output: true,
  231. },
  232. // shouldn't match mismatched IDs
  233. {
  234. Inspected: dockertypes.ImageInspect{
  235. ID: "sha256:2208f7a29005d226d1ee33a63e33af1f47af6156c740d7d23c7948e8d282d53d",
  236. },
  237. Image: "myimage@sha256:0000f7a29005d226d1ee33a63e33af1f47af6156c740d7d23c7948e8d282d53d",
  238. Output: false,
  239. },
  240. // shouldn't match invalid IDs or refs
  241. {
  242. Inspected: dockertypes.ImageInspect{
  243. ID: "sha256:unparseable",
  244. },
  245. Image: "myimage@sha256:unparseable",
  246. Output: false,
  247. },
  248. // shouldn't match against repo digests
  249. {
  250. Inspected: dockertypes.ImageInspect{
  251. ID: "sha256:9bbdf247c91345f0789c10f50a57e36a667af1189687ad1de88a6243d05a2227",
  252. RepoDigests: []string{"centos/ruby-23-centos7@sha256:940584acbbfb0347272112d2eb95574625c0c60b4e2fdadb139de5859cf754bf"},
  253. },
  254. Image: "centos/ruby-23-centos7@sha256:940584acbbfb0347272112d2eb95574625c0c60b4e2fdadb139de5859cf754bf",
  255. Output: false,
  256. },
  257. } {
  258. match := matchImageIDOnly(testCase.Inspected, testCase.Image)
  259. assert.Equal(t, testCase.Output, match, fmt.Sprintf("%s is not a match (%d)", testCase.Image, i))
  260. }
  261. }