|
1 | 1 | import { AnalyzedPackageWithVersion } from "../../../lib/analyzer/types"; |
2 | | -import { buildTree } from "../../../lib/dependency-tree"; |
| 2 | +import { |
| 3 | + buildTree, |
| 4 | + nameAndVersionFromTargetImage, |
| 5 | +} from "../../../lib/dependency-tree"; |
3 | 6 |
|
4 | 7 | const targetImage = "snyk/deptree-test@1.0.0"; |
5 | 8 | const targetOS = { |
@@ -76,3 +79,161 @@ describe("dependency-tree", () => { |
76 | 79 | }); |
77 | 80 | }); |
78 | 81 | }); |
| 82 | + |
| 83 | +const validSha256 = |
| 84 | + "sha256:56ea7092e72db3e9f84d58d583370d59b842de02ea9e1f836c3f3afc7ce408c1"; |
| 85 | + |
| 86 | +describe("nameAndVersionFromTargetImage", () => { |
| 87 | + describe("handles valid image references", () => { |
| 88 | + it("with repository only", () => { |
| 89 | + expect(nameAndVersionFromTargetImage("nginx")).toEqual({ |
| 90 | + name: "nginx", |
| 91 | + version: "latest", |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it("with tag", () => { |
| 96 | + expect(nameAndVersionFromTargetImage("nginx:1.23")).toEqual({ |
| 97 | + name: "nginx", |
| 98 | + version: "1.23", |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + it("with digest", () => { |
| 103 | + expect(nameAndVersionFromTargetImage(`nginx@${validSha256}`)).toEqual({ |
| 104 | + name: "nginx", |
| 105 | + version: "", |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + it("with tag and digest", () => { |
| 110 | + expect( |
| 111 | + nameAndVersionFromTargetImage(`nginx:1.23@${validSha256}`), |
| 112 | + ).toEqual({ |
| 113 | + name: "nginx", |
| 114 | + version: "1.23", |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it("with registry", () => { |
| 119 | + expect(nameAndVersionFromTargetImage("gcr.io/project/nginx")).toEqual({ |
| 120 | + name: "gcr.io/project/nginx", |
| 121 | + version: "latest", |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + it("with registry and port", () => { |
| 126 | + expect(nameAndVersionFromTargetImage("localhost:5000/foo/bar")).toEqual({ |
| 127 | + name: "localhost:5000/foo/bar", |
| 128 | + version: "latest", |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + it("with registry and port and tag", () => { |
| 133 | + expect( |
| 134 | + nameAndVersionFromTargetImage("localhost:5000/foo/bar:tag"), |
| 135 | + ).toEqual({ |
| 136 | + name: "localhost:5000/foo/bar", |
| 137 | + version: "tag", |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + it("with registry and port and digest", () => { |
| 142 | + expect( |
| 143 | + nameAndVersionFromTargetImage(`localhost:5000/foo/bar@${validSha256}`), |
| 144 | + ).toEqual({ |
| 145 | + name: "localhost:5000/foo/bar", |
| 146 | + version: "", |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + it("with registry, port, digest and tag", () => { |
| 151 | + expect( |
| 152 | + nameAndVersionFromTargetImage( |
| 153 | + `localhost:5000/foo/bar:tag@${validSha256}`, |
| 154 | + ), |
| 155 | + ).toEqual({ |
| 156 | + name: "localhost:5000/foo/bar", |
| 157 | + version: "tag", |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + it("with library/ namespace", () => { |
| 162 | + expect(nameAndVersionFromTargetImage("library/nginx:latest")).toEqual({ |
| 163 | + name: "library/nginx", |
| 164 | + version: "latest", |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + it("with dots and dashes in the tag", () => { |
| 169 | + expect(nameAndVersionFromTargetImage("nginx:1.23.0-alpha")).toEqual({ |
| 170 | + name: "nginx", |
| 171 | + version: "1.23.0-alpha", |
| 172 | + }); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + // These tests are to verify that the previous logic is still working as expected for |
| 177 | + // references that cannot be parsed by the new parseImageReference function. |
| 178 | + // They are not necessarily asserting that this is the correct parsing logic. |
| 179 | + describe("handles file-based reference strings", () => { |
| 180 | + it("with a simple image name", () => { |
| 181 | + expect(nameAndVersionFromTargetImage("image.tar")).toEqual({ |
| 182 | + name: "image.tar", |
| 183 | + version: "", |
| 184 | + }); |
| 185 | + }); |
| 186 | + |
| 187 | + it("with a longer path", () => { |
| 188 | + expect(nameAndVersionFromTargetImage("path/to/archive.tar")).toEqual({ |
| 189 | + name: "path/to/archive.tar", |
| 190 | + version: "", |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + it("with a tag", () => { |
| 195 | + expect(nameAndVersionFromTargetImage("path/to/archive.tar:tag")).toEqual({ |
| 196 | + name: "path/to/archive.tar", |
| 197 | + version: "tag", |
| 198 | + }); |
| 199 | + }); |
| 200 | + |
| 201 | + it("with a digest", () => { |
| 202 | + expect( |
| 203 | + nameAndVersionFromTargetImage(`archive.tar@${validSha256}`), |
| 204 | + ).toEqual({ |
| 205 | + name: "archive.tar", |
| 206 | + version: "", |
| 207 | + }); |
| 208 | + }); |
| 209 | + |
| 210 | + it("with a tag and digest", () => { |
| 211 | + expect( |
| 212 | + nameAndVersionFromTargetImage(`path/to/archive.tar:tag@${validSha256}`), |
| 213 | + ).toEqual({ |
| 214 | + name: "path/to/archive.tar", |
| 215 | + version: "tag", |
| 216 | + }); |
| 217 | + }); |
| 218 | + |
| 219 | + it("with a tag and specific image name", () => { |
| 220 | + expect( |
| 221 | + nameAndVersionFromTargetImage("path/to/archive.tar:image:tag"), |
| 222 | + ).toEqual({ |
| 223 | + name: "path/to/archive.tar:image", |
| 224 | + version: "tag", |
| 225 | + }); |
| 226 | + }); |
| 227 | + |
| 228 | + it("with a tag and specific image name and digest", () => { |
| 229 | + expect( |
| 230 | + nameAndVersionFromTargetImage( |
| 231 | + `path/to/archive.tar:image:tag@${validSha256}`, |
| 232 | + ), |
| 233 | + ).toEqual({ |
| 234 | + name: "path/to/archive.tar:image:tag", |
| 235 | + version: "", |
| 236 | + }); |
| 237 | + }); |
| 238 | + }); |
| 239 | +}); |
0 commit comments