-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathci.py
executable file
·878 lines (777 loc) · 40.6 KB
/
ci.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
#!/usr/bin/env python3
from multiprocessing.pool import ThreadPool
from threading import current_thread
from concurrent.futures import Future, ThreadPoolExecutor
import os
import shutil
import time
import logging
from logging import Logger
import mimetypes
import json
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from functools import wraps
from typing import Callable, Any, Literal
from textwrap import dedent
import boto3
import requests
from boto3.exceptions import S3UploadFailedError
from botocore.exceptions import ClientError
import docker
from docker.errors import APIError,ContainerError,ImageNotFound
from docker.models.containers import Container, ExecResult
from docker import DockerClient
import anybadge
from ansi2html import Ansi2HTMLConverter
from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.common.exceptions import TimeoutException, WebDriverException
from jinja2 import Environment, FileSystemLoader, select_autoescape, Template
from pyvirtualdisplay import Display
logger: Logger = logging.getLogger(__name__)
def testing(func: Callable):
"""If the DRY_RUN env is set and this decorator is used on a function it will return None
Args:
func (function): A function
"""
@wraps(func)
def wrapper(*args, **kwargs) -> Any | None:
if os.environ.get("DRY_RUN") == "true":
logger.warning("Dry run enabled, skipping execution of %s", func.__name__)
return
return func(*args,**kwargs)
return wrapper
def deprecated(reason: str):
"""Decorator to mark a function as deprecated. Will log a warning when the function is called.
Args:
reason (str): The reason it is deprecated.
"""
def deprecated_decorator(func: Callable):
@wraps(func)
def wrapper(*args, **kwargs) -> Any:
logger.warning("Function %s is deprecated. Reason: %s", func.__name__, reason)
return func(*args,**kwargs)
return wrapper
return deprecated_decorator
class SetEnvs():
"""Simple helper class that sets up the ENVs"""
def __init__(self) -> None:
self.logger: Logger = logging.getLogger("SetEnvs")
os.environ["S6_VERBOSITY"] = os.environ.get("CI_S6_VERBOSITY","2")
# Set the optional parameters
self.dockerenv: dict[str, str] = self.convert_env(os.environ.get("DOCKER_ENV", ""))
# self.docker_volumes: list[str] = self.convert_volumes(os.environ.get("DOCKER_VOLUMES", "")) # For future use
# self.docker_privileged: bool = os.environ.get("DOCKER_PRIVILEGED", "false").lower() == "true" # For future use
self.webauth: str = os.environ.get("WEB_AUTH", "user:password")
self.webpath: str = os.environ.get("WEB_PATH", "")
self.screenshot: bool = os.environ.get("WEB_SCREENSHOT", "false").lower() == "true"
# Make sure the numeric values are set even if they are set to empty strings in the environment
self.screenshot_timeout: int = (os.environ.get("WEB_SCREENSHOT_TIMEOUT", "120") or "120")
self.screenshot_delay: int = (os.environ.get("WEB_SCREENSHOT_DELAY", "10") or "10")
self.logs_timeout: int = (os.environ.get("DOCKER_LOGS_TIMEOUT", "120") or "120")
self.sbom_timeout: int = (os.environ.get("SBOM_TIMEOUT", "900") or "900")
self.port: int = (os.environ.get("PORT", "80") or "80")
self.builder: str = os.environ.get("NODE_NAME", "-")
self.ssl: str = os.environ.get("SSL", "false")
self.region: str = os.environ.get("S3_REGION", "us-east-1")
self.bucket: str = os.environ.get("S3_BUCKET", "ci-tests.linuxserver.io")
if os.environ.get("DELAY_START"):
self.logger.warning("DELAY_START env is obsolete, and not in use anymore")
if os.environ.get("DOCKER_VOLUMES"):
self.logger.warning("DOCKER_VOLUMES env is not in use")
if os.environ.get("DOCKER_PRIVILEGED"):
self.logger.warning("DOCKER_PRIVILEGED env is not in use")
self.check_env()
self.validate_attrs()
env_data = dedent(f"""
ENVIRONMENT DATA:
NODE_NAME: '{os.environ.get("NODE_NAME")}'
IMAGE: '{os.environ.get("IMAGE")}'
BASE: '{os.environ.get("BASE")}'
META_TAG: '{os.environ.get("META_TAG")}'
TAGS: '{os.environ.get("TAGS")}'
S6_VERBOSITY: '{os.environ.get("S6_VERBOSITY")}'
CI_S6_VERBOSITY '{os.environ.get("CI_S6_VERBOSITY")}'
CI_LOG_LEVEL '{os.environ.get("CI_LOG_LEVEL")}'
DOCKER_ENV: '{os.environ.get("DOCKER_ENV")}'
DOCKER_VOLUMES: '{os.environ.get("DOCKER_VOLUMES")}' (Not in use)
DOCKER_PRIVILEGED: '{os.environ.get("DOCKER_PRIVILEGED")}' (Not in use)
WEB_AUTH: '{os.environ.get("WEB_AUTH")}'
WEB_PATH: '{os.environ.get("WEB_PATH")}'
WEB_SCREENSHOT: '{os.environ.get("WEB_SCREENSHOT")}'
WEB_SCREENSHOT_TIMEOUT: '{os.environ.get("WEB_SCREENSHOT_TIMEOUT")}'
WEB_SCREENSHOT_DELAY: '{os.environ.get("WEB_SCREENSHOT_DELAY")}'
DOCKER_LOGS_TIMEOUT: '{os.environ.get("DOCKER_LOGS_TIMEOUT")}'
SBOM_TIMEOUT: '{os.environ.get("SBOM_TIMEOUT")}'
DELAY_START: '{os.environ.get("DELAY_START")}' (Not in use)
PORT: '{os.environ.get("PORT")}'
SSL: '{os.environ.get("SSL")}'
S3_REGION: '{os.environ.get("S3_REGION")}'
S3_BUCKET: '{os.environ.get("S3_BUCKET")}'
Docker Engine Version: '{self.get_docker_engine_version()}'
""")
self.logger.info(env_data)
def get_docker_engine_version(self) -> str:
"""Get the Docker Engine version
Returns:
str: The Docker Engine version
"""
try:
return docker.from_env().version().get("Version")
except Exception:
logger.error("Failed to get Docker Engine version!")
return "Unknown"
def validate_attrs(self) -> None:
"""Validate the numeric environment variables"""
try:
self.screenshot_timeout = int(self.screenshot_timeout)
self.screenshot_delay = int(self.screenshot_delay)
self.logs_timeout = int(self.logs_timeout)
self.sbom_timeout = int(self.sbom_timeout)
self.port = int(self.port)
except (ValueError,TypeError) as error:
self.logger.exception("Failed to convert numeric envs to int!")
raise CIError("Failed to convert numeric envs to int!") from error
def _split_key_value_string(self, kv:str, make_list:bool = False) -> dict[str,str] | list[str]:
"""Split a key value string into a dictionary or list.
Args:
kv (str): A string with key values separated by the pipe symbol. e.g `key1=val1|key2=val2`.
make_list (bool, optional): If the return value should be a list of strings where the key and value is separated by :. Defaults to False.
Returns:
dict[str,str]: Returns a dictionary with our keys and values.
"""
if make_list:
return [f"{k}:{v}" for k,v in (item.split("=") for item in kv.split("|") if item and "=" in item and item.split('=')[1])]
return dict((item.split('=') for item in kv.split('|') if item and "=" in item and item.split('=')[1]))
def convert_env(self, envs:str = None) -> dict[str,str]:
"""Convert env DOCKER_ENV to dictionary
Args:
envs (str, optional): A string with key values separated by the pipe symbol. e.g `key1=val1|key2=val2`. Defaults to None.
Raises:
CIError: Raises a CIError Exception if it failes to parse the string
Returns:
dict[str,str]: Returns a dictionary with our keys and values.
"""
env_dict: dict = {}
if envs:
self.logger.info("Converting envs '%s' to dictionary", envs)
try:
env_dict = self._split_key_value_string(envs)
env_dict["S6_VERBOSITY"] = os.environ.get("S6_VERBOSITY")
except Exception as error:
self.logger.exception("Failed to convert DOCKER_ENV: %s to dictionary!", envs)
raise CIError(f"Failed converting DOCKER_ENV: {envs} to dictionary") from error
return env_dict
def convert_volumes(self, volumes:str = None) -> list[str]:
"""Convert env DOCKER_VOLUMES to list
Args:
volumes (str, optional): A string with key values separated by the pipe symbol. e.g `key1=val1|key2=val2`. Defaults to None.
Raises:
CIError: Raises a CIError Exception if it fails to parse the string
Returns:
list[str]: Returns a list with our keys and values.
"""
volume_list: list = []
if volumes:
self.logger.info("Converting volumes '%s' to list", volumes)
try:
volume_list = self._split_key_value_string(volumes, make_list=True)
except Exception as error:
self.logger.exception("Failed to convert DOCKER_VOLUME: %s to list!", volumes)
raise CIError(f"Failed converting DOCKER_VOLUME: {volumes} to list") from error
return volume_list
def check_env(self) -> None:
"""Make sure all needed ENVs are set
Raises:
CIError: Raises a CIError exception if one of the environment values is not set.
"""
try:
self.image: str = os.environ["IMAGE"]
self.base: str = os.environ["BASE"]
self.s3_key: str = os.environ["ACCESS_KEY"]
self.s3_secret: str = os.environ["SECRET_KEY"]
self.meta_tag: str = os.environ["META_TAG"]
self.tags_env: str = os.environ["TAGS"]
except KeyError as error:
self.logger.exception("Key is not set in ENV!")
raise CIError(f"Key {error} is not set in ENV!") from error
class CI(SetEnvs):
"""CI object to use for testing image tags.
Attributes:
client (DockerClient): Docker client object
tags (list): List of tags to test
tag_report_tests (dict): Dictionary to hold the test results for each tag
report_containers (dict): Dictionary to hold the report information for each tag
report_status (str): The status of the report
outdir (str): The output directory
s3_client (boto3.client): S3 client object
Args:
SetEnvs (Object): Helper class that initializes and checks that all the necessary environment variables exists. Object is initialized upon init of CI.
"""
def __init__(self) -> None:
super().__init__() # Init the SetEnvs object.
self.logger = logging.getLogger("LSIO CI")
self.start_time: float = 0.0
self.total_runtime: float = 0.0
logging.getLogger("botocore.auth").setLevel(logging.INFO) # Don't log the S3 authentication steps.
self.client: DockerClient = self.create_docker_client()
self.tags = list(self.tags_env.split("|"))
self.tag_report_tests:dict[str,dict[str,dict]] = {tag: {"test":{}} for tag in self.tags} # Adds all the tags as keys with an empty dict as value to the dict
self.report_containers: dict[str,dict[str,dict]] = {}
self.report_status = "PASS"
self.outdir: str = f"{os.path.dirname(os.path.realpath(__file__))}/output/{self.image}/{self.meta_tag}"
os.makedirs(self.outdir, exist_ok=True)
self.s3_client = self.create_s3_client()
def run(self,tags: list) -> None:
"""Will iterate over all the tags running container_test() on each tag, multithreaded.
Args:
`tags` (list): All the tags we will test on the image.
"""
self.start_time = time.time()
thread_pool = ThreadPool(processes=10)
thread_pool.map(self.container_test,tags)
display = Display(size=(1920, 1080)) # Setup an x virtual frame buffer (Xvfb) that Selenium can use during the tests.
display.start()
thread_pool.close()
thread_pool.join()
display.stop()
self.total_runtime = time.time() - self.start_time
def container_test(self, tag: str) -> None:
"""Main container test logic.
Args:
`tag` (str): The container tag
1. Spins up the container tag
Checks the container logs for either `[services.d] done.` or `[ls.io-init] done.`
2. Export the build version from the Container object.
3. Export the package info (SBOM) from the Container object.
4. Take a screenshot for the report if the screenshot env is true.
5. Add report information to report.json.
"""
start_time = time.time()
# Name the thread for easier debugging.
thread_name: str = f"{self.get_platform(tag).upper()}Thread"
current_thread().name = thread_name
# Start the container
self.logger.info("Starting test of: %s", tag)
container: Container = self.client.containers.run(f"{self.image}:{tag}",
detach=True,
environment=self.dockerenv)
container_config: list[str] = container.attrs["Config"]["Env"]
self.logger.info("Container config of tag %s: %s",tag,container_config)
# Run these tests in parallel so the runtime data is more accurate.
with ThreadPoolExecutor(max_workers=2,thread_name_prefix=thread_name) as executor:
future_sbom: Future[str] = executor.submit(self.generate_sbom, tag)
future_logs: Future[bool] = executor.submit(self.watch_container_logs, container, tag)
sbom: str = future_sbom.result(self.sbom_timeout + 5) # Set a thread timeout if the function for some reason hangs
logsfound: bool = future_logs.result(self.logs_timeout + 5) # Set a thread timeout if the function for some reason hangs
build_info: dict = self.get_build_info(container,tag) # Get the image build info
if not logsfound:
self.logger.error("Test of %s FAILED after %.2f seconds", tag, time.time() - start_time)
self._endtest(container, tag, build_info, sbom, False, start_time)
return
if build_info["version"] == "ERROR":
self.logger.error("Test of %s FAILED after %.2f seconds", tag, time.time() - start_time)
self._endtest(container, tag, build_info, sbom, False, start_time)
return
if sbom == "ERROR":
self.logger.error("Test of %s FAILED after %.2f seconds", tag, time.time() - start_time)
self._endtest(container, tag, build_info, sbom, False, start_time)
return
# Screenshot the web interface and check connectivity
self.take_screenshot(container, tag)
self._endtest(container, tag, build_info, sbom, True, start_time)
self.logger.success("Test of %s PASSED after %.2f seconds", tag, time.time() - start_time)
return
def _endtest(self, container:Container, tag:str, build_info:dict[str,str], packages:str, test_success: bool, start_time:float|int = 0.0) -> None:
"""End the test with as much info as we have and append to the report.
Args:
`container` (Container): Container object
`tag` (str): The container tag
`build_info` (str): Information about the build (version, size etc)
`packages` (str): SBOM dump from the container
`test_success` (bool): If the testing of the container failed or not
`start_time` (float, optional): The start time of the test. Defaults to 0.0. Used to calculate the runtime of the test.
"""
if not start_time:
runtime = "-"
if isinstance(start_time,(float, int)):
runtime = f"{time.time() - start_time:.2f}s"
logblob: str = container.logs(timestamps=True).decode("utf-8")
self.create_html_ansi_file(logblob, tag, "log") # Generate an html container log file based on the latest logs
try:
container.remove(force="true")
except APIError:
self.logger.exception("Failed to remove container %s",tag)
warning_texts: dict[str, str] = {
"dotnet": "May be a .NET app. Service might not start on ARM32 with QEMU",
"uwsgi": "This image uses uWSGI and might not start on ARM/QEMU"
}
# Add the info to the report
self.report_containers[tag] = {
"logs": logblob,
"sysinfo": packages,
"warnings": {
"dotnet": warning_texts["dotnet"] if "icu-libs" in packages and "arm32" in tag else "",
"uwsgi": warning_texts["uwsgi"] if "uwsgi" in packages and "arm" in tag else ""
},
"build_info": build_info,
"test_results": self.tag_report_tests[tag]["test"],
"test_success": test_success,
"runtime": runtime
}
self.report_containers[tag]["has_warnings"] = any(warning[1] for warning in self.report_containers[tag]["warnings"].items())
def get_platform(self, tag: str) -> str:
"""Check the 5 first characters of the tag and return the platform.
If no match is found return amd64.
Returns:
str: The platform
"""
platform: str = tag[:5]
match platform:
case "amd64":
return "amd64"
case "arm64":
return "arm64"
case "arm32":
return "arm"
case _:
return "amd64"
@deprecated(reason="Use generate_sbom instead")
def export_package_info(self, container:Container, tag:str) -> str:
"""Dump the package info into a string for the report
Args:
container (Container): The container we are testing
tag (str): The tag we are testing
Returns:
str: Return the output of the dump command or "ERROR"
"""
# Dump package information
dump_commands: dict[str, str] = {
"alpine": "apk info -v",
"debian": "apt list",
"ubuntu": "apt list",
"fedora": "rpm -qa",
"arch": "pacman -Q"
}
try:
self.logger.info("Dumping package info for %s",tag)
info: ExecResult = container.exec_run(dump_commands[self.base])
packages: str = info[1].decode("utf-8")
if info[0] != 0:
raise CIError(f"Failed to dump packages. Output: {packages}")
self.tag_report_tests[tag]["test"]["Dump package info"] = (dict(sorted({
"status":"PASS",
"message":"-"}.items())))
self.logger.info("Dump package info %s: PASS", tag)
except (APIError, IndexError,CIError) as error:
packages = "ERROR"
self.logger.exception("Dumping package info on %s: FAIL", tag)
self.tag_report_tests[tag]["test"]["Dump package info"] = (dict(sorted({
"Dump package info":"FAIL",
"message":str(error)}.items())))
self.report_status = "FAIL"
return packages
def generate_sbom(self, tag:str) -> str:
"""Generate the SBOM for the image tag.
Creates the output file in `{self.outdir}/{tag}.sbom.html`
Args:
tag (str): The tag we are testing
Returns:
bool: Return the output if successful otherwise "ERROR".
"""
start_time = time.time()
platform: str = self.get_platform(tag)
syft:Container = self.client.containers.run(image="ghcr.io/anchore/syft:latest",command=f"{self.image}:{tag} --platform=linux/{platform}",
detach=True, volumes={"/var/run/docker.sock": {"bind": "/var/run/docker.sock", "mode": "rw"}})
self.logger.info("Creating SBOM package list on %s",tag)
test = "Create SBOM"
t_end: float = time.time() + self.sbom_timeout
self.logger.info("Tailing the syft container logs for %s seconds looking the 'VERSION' message on tag: %s",self.sbom_timeout,tag)
error_message = "Did not find the 'VERSION' keyword in the syft container logs"
while time.time() < t_end:
time.sleep(5)
try:
logblob: str = syft.logs().decode("utf-8")
if "VERSION" in logblob:
self.logger.info("Get package versions for %s completed", tag)
self._add_test_result(tag, test, "PASS", "-", start_time)
self.logger.success("%s package list %s: PASSED after %.2f seconds", test, tag, time.time() - start_time)
self.create_html_ansi_file(str(logblob),tag,"sbom")
try:
syft.remove(force=True)
except Exception:
self.logger.exception("Failed to remove the syft container, %s",tag)
return logblob
except (APIError,ContainerError,ImageNotFound) as error:
error_message: APIError | ContainerError | ImageNotFound = error
self.logger.exception("Creating SBOM package list on %s: FAIL", tag)
self.logger.error("Failed to generate SBOM output on tag %s. SBOM output:\n%s",tag, logblob)
self.report_status = "FAIL"
self._add_test_result(tag, test, "FAIL", str(error_message), start_time)
try:
syft.remove(force=True)
except Exception:
self.logger.exception("Failed to remove the syft container, %s",tag)
return "ERROR"
@deprecated(reason="Use get_build_info instead")
def get_build_version(self,container:Container,tag:str) -> str:
"""Fetch the build version from the container object attributes.
Args:
container (Container): The container we are testing
tag (str): The current tag we are testing
Returns:
str: Returns the build version or "ERROR"
"""
try:
self.logger.info("Fetching build version on tag: %s",tag)
build_version: str = container.attrs["Config"]["Labels"]["build_version"]
self.tag_report_tests[tag]["test"]["Get build version"] = (dict(sorted({
"status":"PASS",
"message":"-"}.items())))
self.logger.info("Get build version on tag '%s': PASS", tag)
except (APIError,KeyError) as error:
self.logger.exception("Get build version on tag '%s': FAIL", tag)
build_version = "ERROR"
if isinstance(error,KeyError):
error: str = f"KeyError: {error}"
self.tag_report_tests[tag]["test"]["Get build version"] = (dict(sorted({
"status":"FAIL",
"message":str(error)}.items())))
self.report_status = "FAIL"
return build_version
def get_build_info(self,container:Container,tag:str) -> dict[str,str]:
"""Get the build information from the container object.
Args:
container (Container): The container we are testing
tag (str): The tag we are testing
Returns:
dict[str,str]: Returns a dictionary with the build information
```
{
"version": "1.0.0",
"created": "xxxx-xx-xx",
"size": "100MB",
"maintainer": "user"
"builder": "node"
}
```
"""
test = "Get build info"
start_time = time.time()
try:
self.logger.info("Fetching build info on tag: %s",tag)
build_info: dict[str,str] = {
"version": container.attrs["Config"]["Labels"]["org.opencontainers.image.version"],
"created": container.attrs["Config"]["Labels"]["org.opencontainers.image.created"],
"size": "%.2f" % float(int(container.image.attrs["Size"])/1000000) + "MB",
"maintainer": container.attrs["Config"]["Labels"]["maintainer"],
"builder": self.builder
}
self._add_test_result(tag, test, "PASS", "-", start_time)
self.logger.success("Get build info on tag '%s': PASS", tag)
except (APIError,KeyError) as error:
self.logger.exception("Get build info on tag '%s': FAIL", tag)
build_info = {"version": "ERROR", "created": "ERROR", "size": "ERROR", "maintainer": "ERROR"}
if isinstance(error,KeyError):
error: str = f"KeyError: {error}"
self._add_test_result(tag, test, "FAIL", str(error), start_time)
self.report_status = "FAIL"
return build_info
def watch_container_logs(self, container:Container, tag:str) -> bool:
"""Tail the container logs for n seconds and look for the init done message that tells us the container started up
successfully.
Args:
container (Container): The container we are testing
tag (str): The tag we are testing
Returns:
bool: Return True if the "done" message is found, otherwise False.
"""
test = "Container startup"
start_time = time.time()
t_end: float = time.time() + self.logs_timeout
self.logger.info("Tailing the %s logs for %s seconds looking for the 'done' message", tag, self.logs_timeout)
while time.time() < t_end:
try:
logblob: str = container.logs().decode("utf-8")
if "[services.d] done." in logblob or "[ls.io-init] done." in logblob:
self.logger.info("%s completed for %s",test, tag)
self._add_test_result(tag, test, "PASS", "-", start_time)
self.logger.success("%s %s: PASSED after %.2f seconds", test, tag, time.time() - start_time)
return True
time.sleep(1)
except APIError as error:
self.logger.exception("%s %s: FAIL - INIT NOT FINISHED", test, tag)
self._add_test_result(tag, test, "FAIL", f"INIT NOT FINISHED: {str(error)}", start_time)
self.report_status = "FAIL"
return False
self.logger.error("%s failed for %s", test, tag)
self._add_test_result(tag, test, "FAIL", "INIT NOT FINISHED", start_time)
self.logger.error("%s %s: FAIL - INIT NOT FINISHED", test, tag)
self.report_status = "FAIL"
return False
def report_render(self) -> None:
"""Render the index file for upload"""
self.logger.info("Rendering Report")
env = Environment(autoescape=select_autoescape(enabled_extensions=("html", "xml"),default_for_string=True),
loader = FileSystemLoader(os.path.dirname(os.path.realpath(__file__))) )
template: Template = env.get_template("template.html")
self.report_containers = json.loads(json.dumps(self.report_containers,sort_keys=True))
with open(f"{self.outdir}/index.html", mode="w", encoding="utf-8") as file_:
file_.write(template.render(
report_containers=self.report_containers,
report_status=self.report_status,
meta_tag=self.meta_tag,
image=self.image,
bucket=self.bucket,
region=self.region,
screenshot=self.screenshot,
total_runtime=f"{self.total_runtime:.2f}s",
))
def badge_render(self) -> None:
"""Render the badge file for upload"""
self.logger.info("Creating badge")
try:
badge = anybadge.Badge("CI", self.report_status, thresholds={
"PASS": "green", "FAIL": "red"})
badge.write_badge(f"{self.outdir}/badge.svg", overwrite=True)
with open(f"{self.outdir}/ci-status.yml", "w", encoding="utf-8") as file:
file.write(f"CI: '{self.report_status}'")
except (ValueError,RuntimeError,FileNotFoundError,OSError):
self.logger.exception("Failed to render badge file!")
def json_render(self) -> None:
"""Create a JSON file of the report data."""
self.logger.info("Creating report.json file")
try:
with open(f"{self.outdir}/report.json", mode="w", encoding="utf-8") as file:
json.dump(self.report_containers, file, indent=2, sort_keys=True)
except (OSError,FileNotFoundError,TypeError,Exception):
self.logger.exception("Failed to render JSON file!")
def report_upload(self) -> None:
"""Upload report files to S3
Raises:
Exception: S3UploadFailedError
Exception: ValueError
Exception: ClientError
"""
self.logger.info("Uploading report files")
try:
shutil.copyfile(f"{os.path.dirname(os.path.realpath(__file__))}/404.jpg", f"{self.outdir}/404.jpg")
shutil.copyfile(f"{os.path.dirname(os.path.realpath(__file__))}/logo.jpg", f"{self.outdir}/logo.jpg")
shutil.copyfile(f"{os.path.dirname(os.path.realpath(__file__))}/favicon.ico", f"{self.outdir}/favicon.ico")
except Exception:
self.logger.exception("Failed to copy 404/favicon/logo file!")
# Loop through files in outdir and upload
for filename in os.listdir(self.outdir):
time.sleep(0.5)
ctype: tuple[str | None, str | None] = mimetypes.guess_type(filename.lower(), strict=False)
ctype = {"ContentType": ctype[0] if ctype[0] else "text/plain", "ACL": "public-read", "CacheControl": "no-cache"} # Set content types for files
try:
self.upload_file(f"{self.outdir}/{filename}", filename, ctype)
except (S3UploadFailedError, ValueError, ClientError) as error:
self.logger.exception("Upload Error!")
self.log_upload()
raise CIError(f"Upload Error: {error}") from error
self.logger.info("Report available on https://%s/%s/%s/index.html",self.bucket, self.image, self.meta_tag)
def create_html_ansi_file(self, blob:str, tag:str, name:str, full:bool = True) -> None:
"""Creates an HTML file in the "self.outdir" directory that we upload to S3
Args:
blob (str): The blob you want to convert
tag (str): The tag we are testing
name (str): The name of the file. File name will be `{tag}.{name}.html`
full (bool): Whether to include the full HTML document or only the body.
"""
try:
self.logger.info("Creating %s.%s.html", tag, name)
converter = Ansi2HTMLConverter(title=f"{tag}-{name}")
html_logs: str = converter.convert(blob,full=full)
with open(f"{self.outdir}/{tag}.{name}.html", "w", encoding="utf-8") as file:
file.write(html_logs)
except Exception:
self.logger.exception("Failed to create %s.%s.html", tag,name)
@testing
def upload_file(self, file_path:str, object_name:str, content_type:dict) -> None:
"""Upload a file to an S3 bucket
Args:
file_path (str): File to upload
object_name (str): S3 object name.
content_type (dict): Content type for the file
"""
self.logger.info("Uploading %s to %s bucket",file_path, self.bucket)
destination_dir: str = f"{self.image}/{self.meta_tag}"
latest_dir: str = f"{self.image}/latest"
self.s3_client.upload_file(file_path, self.bucket, f"{destination_dir}/{object_name}", ExtraArgs=content_type)
self.s3_client.upload_file(file_path, self.bucket, f"{latest_dir}/{object_name}", ExtraArgs=content_type)
def log_upload(self) -> None:
"""Upload the ci.log to S3
Raises:
Exception: S3UploadFailedError
Exception: ClientError
"""
self.logger.info("Uploading logs")
try:
self.upload_file(f"{self.outdir}/ci.log", "ci.log", {"ContentType": "text/plain", "ACL": "public-read"})
with open(f"{self.outdir}/ci.log","r", encoding="utf-8") as logs:
blob: str = logs.read()
self.create_html_ansi_file(blob,"python","log")
self.upload_file(f"{self.outdir}/python.log.html", "python.log.html", {"ContentType": "text/html", "ACL": "public-read"})
except (S3UploadFailedError, ClientError):
self.logger.exception("Failed to upload the CI logs!")
def _add_test_result(self, tag:str, test:str, status:str, message:str, start_time:float|int = 0.0) -> None:
"""Add a test result to the report
Args:
tag (str): The tag we are testing
test (str): The test we are running
status (str): The status of the test
message (str): The message of the test
start_time (str, optional): The start time of the test. Defaults to 0.0. Used to calculate the runtime of the test.
"""
if status not in ["PASS","FAIL"]:
raise ValueError("Status must be either PASS or FAIL")
if tag not in self.tags:
raise ValueError("Tag not in the list of tags")
if not start_time:
runtime = "-"
if isinstance(start_time,(float, int)):
runtime: str = f"{time.time() - start_time:.2f}s"
self.tag_report_tests[tag]["test"][test] = (dict(sorted({
"status":status,
"message":message,
"runtime": runtime}.items())))
def take_screenshot(self, container: Container, tag:str) -> None:
"""Take a screenshot and save it to self.outdir if self.screenshot is True
Takes a screenshot using a ChromiumDriver instance.
Args:
container (Container): Container object
tag (str): The container tag we are testing.
"""
if not self.screenshot:
return
proto: Literal["https", "http"] = "https" if self.ssl.upper() == "TRUE" else "http"
screenshot_timeout = time.time() + self.screenshot_timeout
test = "Get screenshot"
start_time = time.time()
try:
driver: WebDriver = self.setup_driver()
container.reload()
ip_adr:str = container.attrs.get("NetworkSettings",{}).get("Networks",{}).get("bridge",{}).get("IPAddress","")
webauth: str = f"{self.webauth}@" if self.webauth else ""
endpoint: str = f"{proto}://{webauth}{ip_adr}:{self.port}{self.webpath}"
self.logger.info("Trying for %s seconds to take a screenshot of %s ",self.screenshot_timeout, tag)
while time.time() < screenshot_timeout:
try:
if not self._check_response(endpoint):
raise requests.ConnectionError("Bad response")
driver.get(endpoint)
time.sleep(self.screenshot_delay) # A grace period for the page to load
self.logger.debug("Trying to take screenshot of %s at %s", tag, endpoint)
driver.get_screenshot_as_file(f"{self.outdir}/{tag}.png")
if not os.path.isfile(f"{self.outdir}/{tag}.png"):
raise FileNotFoundError(f"Screenshot '{self.outdir}/{tag}.png' not found")
self._add_test_result(tag, test, "PASS", "-", start_time)
self.logger.success("Screenshot %s: PASSED after %.2f seconds", tag, time.time() - start_time)
return
except Exception as error:
logger.debug("Failed to take screenshot of %s at %s, trying again in 3 seconds", tag, endpoint, exc_info=error)
time.sleep(3)
if time.time() >= screenshot_timeout:
self.logger.error("Failed to take screenshot of %s at %s", tag, endpoint)
raise error
raise TimeoutException("Timeout taking screenshot")
except (requests.Timeout, requests.ConnectionError, KeyError) as error:
self._add_test_result(tag, test, "FAIL", f"CONNECTION ERROR: {str(error)}", start_time)
self.logger.exception("Screenshot %s FAIL CONNECTION ERROR", tag)
except TimeoutException as error:
self._add_test_result(tag, test, "FAIL", f"TIMEOUT: {str(error)}", start_time)
self.logger.exception("Screenshot %s FAIL TIMEOUT", tag)
except (WebDriverException, Exception) as error:
self._add_test_result(tag, test, "FAIL", f"UNKNOWN: {str(error)}", start_time)
self.logger.exception("Screenshot %s FAIL UNKNOWN", tag)
finally:
try:
driver.quit()
except Exception:
self.logger.exception("Failed to quit the driver")
def _check_response(self, endpoint:str) -> bool:
"""Check if we can get a good response from the endpoint
Args:
endpoint (str): The endpoint we are testing
Returns:
bool: Return True if we get a good response, otherwise False.
"""
try:
self.logger.debug("Checking response on %s", endpoint)
response = requests.get(endpoint, timeout=10, verify=False)
response.raise_for_status()
return True
except (requests.ConnectionError, requests.Timeout, requests.HTTPError, requests.RequestException) as exc:
self.logger.warning("Failed to get a good response on %s", endpoint, exc_info=exc)
return False
@deprecated(reason="Use the chrome driver directly instead")
def start_tester(self, proto:str, endpoint:str, tag:str) -> tuple[Container,str]:
"""Spin up an RDP test container to load the container web ui.
Args:
`proto` (str): The protocol to use for the endpoint.
`endpoint` (str): The container endpoint to use with the tester container.
`tag` (str): The container tag
Returns:
Container/str: Returns the tester Container object and the tester endpoint
"""
self.logger.info("Starting tester container for tag: %s", tag)
testercontainer: Container = self.client.containers.run("ghcr.io/linuxserver/tester:latest",
shm_size="1G",
security_opt=["seccomp=unconfined"],
detach=True,
environment={"URL": endpoint})
#Sleep for the user specified amount of time
self.logger.info("Sleeping for %s seconds before reloading %s and refreshing container attrs on %s run", self.test_container_delay, testercontainer.image, tag)
time.sleep(int(self.test_container_delay))
testercontainer.reload()
testerip: str = testercontainer.attrs["NetworkSettings"]["Networks"]["bridge"]["IPAddress"]
testerendpoint: str = f"http://{testerip}:3000"
session = requests.Session()
retries = Retry(total=10, backoff_factor=2,status_forcelist=[502, 503, 504])
session.mount(proto, HTTPAdapter(max_retries=retries))
session.get(testerendpoint)
return testercontainer, testerendpoint
def setup_driver(self) -> WebDriver:
"""Return a single ChromiumDriver object the class can use
Returns:
Webdriver: Returns a Chromedriver object
"""
self.logger.info("Init Chromedriver")
# Selenium webdriver options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--ignore-certificate-errors")
chrome_options.add_argument("--disable-dev-shm-usage") # https://developers.google.com/web/tools/puppeteer/troubleshooting#tips
driver = webdriver.Chrome(options=chrome_options)
driver.set_page_load_timeout(60)
driver.set_window_size(1920,1080)
return driver
@testing
def create_s3_client(self) -> boto3.client:
"""Create and return an s3 client object
Returns:
Session.client: An S3 client.
"""
s3_client = boto3.Session().client(
"s3",
region_name=self.region,
aws_access_key_id=self.s3_key,
aws_secret_access_key=self.s3_secret)
return s3_client
def create_docker_client(self) -> DockerClient|None:
"""Create and return a docker client object
Returns:
DockerClient: A docker client object
"""
try:
return docker.from_env()
except Exception:
self.logger.error("Failed to create Docker client!")
class CIError(Exception):
pass