Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Commit 87c398b

Browse files
committed
Add tests for cid2civ
1 parent 281df4e commit 87c398b

25 files changed

Lines changed: 283 additions & 8 deletions

File tree

poetry.lock

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ ipython = "^8.14.0"
4949
pytest-sugar = "^0.9.7"
5050
rich = "^13.4.1"
5151
docopt = "^0.6.2"
52+
requests-file = "^1.5.1"
5253

5354
[tool.poetry.scripts]
5455
cloudimagedirectory-transformer = "cloudimagedirectory.transformer:run"

scripts/cid2civ

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ Arguments:
2323
FILE a local file with downloaded image data from CID
2424
2525
Options:
26-
-h, --help Show this help message and exit.
27-
--verbose Show debug messages and unlimited data sets.
26+
-h, --help Show this help message and exit.
27+
--verbose Show debug messages and unlimited data sets.
28+
--website=URL Specify the URL of Cloud Image Directory (CID)
29+
[default: https://imagedirectory.cloud].
2830
"""
2931
import collections
3032
import json
@@ -34,11 +36,9 @@ import textwrap
3436

3537
import docopt
3638
import requests
39+
import requests_file
3740

3841

39-
# A URL of the Cloud Image Directory to test.
40-
CLOUD_IMAGE_DIRECTORY = "https://imagedirectory.cloud"
41-
4242
# Cloud providers.
4343
CLOUD_PROVIDER_AWS = "aws"
4444
CLOUD_PROVIDER_AZURE = "azure"
@@ -168,6 +168,9 @@ def download_cid_data(website: str, provider: str) -> dict:
168168
logging.info(f"Downloading image data from {website}")
169169

170170
with requests.Session() as session:
171+
# Support local files for testing purposes.
172+
session.mount("file://", requests_file.FileAdapter())
173+
171174
# Get references to images.
172175
image_refs = fetch_image_refs(
173176
session=session,
@@ -385,14 +388,14 @@ def _print_list(description, values, limit=None, indent=2):
385388
limit = len(values) if limit is None else limit
386389
reduced = sorted(values)[: min(limit, len(values))]
387390
indented = textwrap.indent("\n".join(reduced), prefix=" " * indent)
388-
print(f"{description} ({len(reduced)} out of {len(values)}): \n{indented}\n")
391+
print(f"{description} ({len(reduced)} out of {len(values)}):\n{indented}\n")
389392

390393

391394
def main():
392395
"""Run the cid2civ script."""
393396
# Process the cmdline options.
394397
options = docopt.docopt(__doc__)
395-
website = CLOUD_IMAGE_DIRECTORY
398+
website = options["--website"]
396399
verbose = options["--verbose"]
397400

398401
# Set up logging to stderr.

tests/scripts/test_cid2civ.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""Tests for the cid2civ script."""
2+
import os
3+
import subprocess
4+
import sys
5+
6+
7+
def cid2civ(*args, in_file=None, out_file=None):
8+
"""Run the cid2civ script and check its output."""
9+
pwd = os.getcwd()
10+
11+
script = f"{pwd}/scripts/cid2civ"
12+
data = f"{pwd}/tests/scripts/testdata/cid2civ"
13+
website = f"{pwd}/tests/scripts/testdata/website"
14+
15+
if in_file:
16+
args += (f"{data}/{in_file}",)
17+
18+
if "download" in args:
19+
args += ("--website", f"file://{website}")
20+
21+
result = subprocess.run(
22+
[script, "--verbose", *args], capture_output=True, text=True
23+
)
24+
25+
print(result.stderr, file=sys.stderr)
26+
print(result.stdout, file=sys.stdout)
27+
result.check_returncode()
28+
29+
if out_file:
30+
with open(f"{data}/{out_file}") as f:
31+
assert result.stdout.strip() == f.read().strip()
32+
33+
return result
34+
35+
36+
def test_cid2civ_help():
37+
"""Test the --help option of the cid2civ script."""
38+
result = cid2civ("--help")
39+
assert "Usage:" in result.stdout
40+
assert "Commands:" in result.stdout
41+
assert "Arguments:" in result.stdout
42+
43+
44+
def test_cid2civ_download():
45+
"""Test the download command of the cid2civ script."""
46+
cid2civ("download", "aws", out_file="aws.cid")
47+
cid2civ("download", "azure", out_file="azure.cid")
48+
cid2civ("download", "google", out_file="google.cid")
49+
50+
51+
def test_cid2civ_analyze():
52+
"""Test the analyze command of the cid2civ script."""
53+
cid2civ("analyze", in_file="aws.cid", out_file="aws.txt")
54+
cid2civ("analyze", in_file="azure.cid", out_file="azure.txt")
55+
cid2civ("analyze", in_file="google.cid", out_file="google.txt")
56+
57+
58+
def test_cid2civ_convert():
59+
"""Test the convert command of the cid2civ script."""
60+
cid2civ("convert", in_file="aws.cid", out_file="aws.civ")
61+
cid2civ("convert", in_file="azure.cid", out_file="azure.civ")
62+
cid2civ("convert", in_file="google.cid", out_file="google.civ")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"provider": "aws",
3+
"images": [
4+
{
5+
"name": "RHEL 6.10 hvm x86_64 Hourly2",
6+
"arch": "x86_64",
7+
"version": "6.10",
8+
"imageId": "ami-0c22ca1423e1721e7",
9+
"date": "2021-03-18T15:22:40.000Z",
10+
"virt": "hvm",
11+
"selflink": "https://console.aws.amazon.com/ec2/home?region=af-south-1#launchAmi=ami-0c22ca1423e1721e7",
12+
"region": "af-south-1"
13+
}
14+
],
15+
"total": 1
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"provider": "aws",
3+
"instances": [
4+
{
5+
"name": "RHEL-6.10-hvm-x86_64-Hourly2",
6+
"ami": "ami-0c22ca1423e1721e7",
7+
"region": "af-south-1",
8+
"username": "ec2-user",
9+
"instance_type": "t3.micro",
10+
"arch": "x86_64"
11+
}
12+
]
13+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Number of AWS images: 1
2+
Number of unique images (case-sensitive): 1
3+
Number of unique images (case-insensitive: 1
4+
5+
Names of attributes (8 out of 8):
6+
arch
7+
date
8+
imageId
9+
name
10+
region
11+
selflink
12+
version
13+
virt
14+
15+
Values of the 'arch' attribute (1 out of 1):
16+
x86_64
17+
18+
Values of the 'date' attribute (1 out of 1):
19+
2021-03-18T15:22:40.000Z
20+
21+
Values of the 'imageId' attribute (1 out of 1):
22+
ami-0c22ca1423e1721e7
23+
24+
Values of the 'name' attribute (1 out of 1):
25+
RHEL 6.10 hvm x86_64 Hourly2
26+
27+
Values of the 'region' attribute (1 out of 1):
28+
af-south-1
29+
30+
Values of the 'selflink' attribute (1 out of 1):
31+
https://console.aws.amazon.com/ec2/home?region=af-south-1#launchAmi=ami-0c22ca1423e1721e7
32+
33+
Values of the 'version' attribute (1 out of 1):
34+
6.10
35+
36+
Values of the 'virt' attribute (1 out of 1):
37+
hvm
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"provider": "azure",
3+
"images": [
4+
{
5+
"name": "osa osa_311 x64",
6+
"arch": "x64",
7+
"version": "311.161",
8+
"imageId": "RedHat:osa:osa_311:311.161.20200115",
9+
"date": "2020-01-15",
10+
"virt": "unknown"
11+
}
12+
],
13+
"total": 1
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"provider": "azure",
3+
"instances": [
4+
{
5+
"name": "osa-osa_311-x64",
6+
"image_definition": {
7+
"publisher": "RedHat",
8+
"offer": "osa",
9+
"sku": "osa_311",
10+
"version": "311.161.20200115"
11+
},
12+
"arch": "x64"
13+
}
14+
]
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Number of AZURE images: 1
2+
Number of unique images (case-sensitive): 1
3+
Number of unique images (case-insensitive: 1
4+
5+
Names of attributes (6 out of 6):
6+
arch
7+
date
8+
imageId
9+
name
10+
version
11+
virt
12+
13+
Values of the 'arch' attribute (1 out of 1):
14+
x64
15+
16+
Values of the 'date' attribute (1 out of 1):
17+
2020-01-15
18+
19+
Values of the 'imageId' attribute (1 out of 1):
20+
RedHat:osa:osa_311:311.161.20200115
21+
22+
Values of the 'name' attribute (1 out of 1):
23+
osa osa_311 x64
24+
25+
Values of the 'version' attribute (1 out of 1):
26+
311.161
27+
28+
Values of the 'virt' attribute (1 out of 1):
29+
unknown

0 commit comments

Comments
 (0)