Skip to content

feat: Enhance and test the query_image_content method #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion sdcclient/_scanning.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,20 @@ def query_image_content(self, image, content_type=""):
- image: Input image can be in the following formats: registry/repo:tag
- content_type: The content type can be one of the following types:
- os: Operating System Packages
- files: Files
- npm: Node.JS NPM Module
- gem: Ruby GEM
- files: Files
- python: Python modules
- java: Java packages

**Success Return Value**
A JSON object representing the image content.
'''
content_type = content_type.lower()
supported_types = ["os", "files", "npm", "gem", "python", "java"]
if content_type not in supported_types:
return False, f"unsupported type provided: {content_type}, must be one of {supported_types}"

return self._query_image(image, query_group='content', query_type=content_type)

def query_image_metadata(self, image, metadata_type=""):
Expand Down
59 changes: 59 additions & 0 deletions specs/secure/scanning/query_image_content_spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os

from expects import expect, contain, have_keys, equal
from mamba import before, it, context, description

from sdcclient import SdScanningClient
from specs import be_successful_api_call

with description("Query Image Content", "integration") as self:
with before.each:
self.client = SdScanningClient(sdc_url=os.getenv("SDC_SECURE_URL", "https://secure.sysdig.com"),
token=os.getenv("SDC_SECURE_TOKEN"))

with it("is able to retrieve the OS contents"):
ok, res = self.client.query_image_content("alpine:latest", "os")

expect((ok, res)).to(be_successful_api_call)
expect(res["content"]).to(contain(have_keys("license", "origin", "package", "size", "type", "version")))
expect(res["content_type"]).to(equal("os"))

with it("is able to retrieve the npm contents"):
ok, res = self.client.query_image_content("alpine:latest", "npm")

expect((ok, res)).to(be_successful_api_call)
expect(res["content_type"]).to(equal("npm"))

with it("is able to retrieve the gem contents"):
ok, res = self.client.query_image_content("alpine:latest", "gem")

expect((ok, res)).to(be_successful_api_call)
expect(res["content_type"]).to(equal("gem"))

with it("is able to retrieve the python contents"):
ok, res = self.client.query_image_content("alpine:latest", "python")

expect((ok, res)).to(be_successful_api_call)
expect(res["content_type"]).to(equal("python"))

with it("is able to retrieve the java contents"):
ok, res = self.client.query_image_content("alpine:latest", "java")

expect((ok, res)).to(be_successful_api_call)
expect(res["content_type"]).to(equal("java"))

with it("is able to retrieve the files contents"):
ok, res = self.client.query_image_content("alpine:latest", "files")

expect((ok, res)).to(be_successful_api_call)
expect(res["content"]).to(
contain(have_keys("filename", "gid", "linkdest", "mode", "sha256", "size", "type", "uid")))
expect(res["content_type"]).to(equal("files"))

with context("when the type is not in the supported list"):
with it("returns an error indicating the type is incorrect"):
ok, res = self.client.query_image_content("alpine:latest", "Unknown")

expect((ok, res)).not_to(be_successful_api_call)
expect(res).to(equal(
"unsupported type provided: unknown, must be one of ['os', 'files', 'npm', 'gem', 'python', 'java']"))