Skip to content

Commit b230a5a

Browse files
authored
feat: Enhance the query_image_content method (#189)
1 parent 1acf59f commit b230a5a

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

sdcclient/_scanning.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,20 @@ def query_image_content(self, image, content_type=""):
137137
- image: Input image can be in the following formats: registry/repo:tag
138138
- content_type: The content type can be one of the following types:
139139
- os: Operating System Packages
140+
- files: Files
140141
- npm: Node.JS NPM Module
141142
- gem: Ruby GEM
142-
- files: Files
143+
- python: Python modules
144+
- java: Java packages
143145
144146
**Success Return Value**
145147
A JSON object representing the image content.
146148
'''
149+
content_type = content_type.lower()
150+
supported_types = ["os", "files", "npm", "gem", "python", "java"]
151+
if content_type not in supported_types:
152+
return False, f"unsupported type provided: {content_type}, must be one of {supported_types}"
153+
147154
return self._query_image(image, query_group='content', query_type=content_type)
148155

149156
def query_image_metadata(self, image, metadata_type=""):
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import os
2+
3+
from expects import expect, contain, have_keys, equal
4+
from mamba import before, it, context, description
5+
6+
from sdcclient import SdScanningClient
7+
from specs import be_successful_api_call
8+
9+
with description("Query Image Content", "integration") as self:
10+
with before.each:
11+
self.client = SdScanningClient(sdc_url=os.getenv("SDC_SECURE_URL", "https://secure.sysdig.com"),
12+
token=os.getenv("SDC_SECURE_TOKEN"))
13+
14+
with it("is able to retrieve the OS contents"):
15+
ok, res = self.client.query_image_content("alpine:latest", "os")
16+
17+
expect((ok, res)).to(be_successful_api_call)
18+
expect(res["content"]).to(contain(have_keys("license", "origin", "package", "size", "type", "version")))
19+
expect(res["content_type"]).to(equal("os"))
20+
21+
with it("is able to retrieve the npm contents"):
22+
ok, res = self.client.query_image_content("alpine:latest", "npm")
23+
24+
expect((ok, res)).to(be_successful_api_call)
25+
expect(res["content_type"]).to(equal("npm"))
26+
27+
with it("is able to retrieve the gem contents"):
28+
ok, res = self.client.query_image_content("alpine:latest", "gem")
29+
30+
expect((ok, res)).to(be_successful_api_call)
31+
expect(res["content_type"]).to(equal("gem"))
32+
33+
with it("is able to retrieve the python contents"):
34+
ok, res = self.client.query_image_content("alpine:latest", "python")
35+
36+
expect((ok, res)).to(be_successful_api_call)
37+
expect(res["content_type"]).to(equal("python"))
38+
39+
with it("is able to retrieve the java contents"):
40+
ok, res = self.client.query_image_content("alpine:latest", "java")
41+
42+
expect((ok, res)).to(be_successful_api_call)
43+
expect(res["content_type"]).to(equal("java"))
44+
45+
with it("is able to retrieve the files contents"):
46+
ok, res = self.client.query_image_content("alpine:latest", "files")
47+
48+
expect((ok, res)).to(be_successful_api_call)
49+
expect(res["content"]).to(
50+
contain(have_keys("filename", "gid", "linkdest", "mode", "sha256", "size", "type", "uid")))
51+
expect(res["content_type"]).to(equal("files"))
52+
53+
with context("when the type is not in the supported list"):
54+
with it("returns an error indicating the type is incorrect"):
55+
ok, res = self.client.query_image_content("alpine:latest", "Unknown")
56+
57+
expect((ok, res)).not_to(be_successful_api_call)
58+
expect(res).to(equal(
59+
"unsupported type provided: unknown, must be one of ['os', 'files', 'npm', 'gem', 'python', 'java']"))

0 commit comments

Comments
 (0)