#!/usr/bin/python3 # Copyright 2021 Red Hat, Inc. # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. # This script is to mock podman command when testing containers # created by the ansible collection containers.podman. from json import dumps from yaml import safe_load as yaml_safe_load import sys sample = { "Id": "21d8b432eaec1b4eac2a21a78de524bdbb2f074d4ea43d3605b2b072ffe21878", "State": { "Status": "running", "Running": True, }, "HostConfig": { "Binds": [], "CgroupManager": "systemd", "CgroupMode": "private", "ContainerIDFile": "", "LogConfig": { "Type": "k8s-file", "Config": None, "Path": "/ctr.log", "Tag": "", "Size": "0B" }, "NetworkMode": "slirp4netns", "RestartPolicy": { "Name": "", "MaximumRetryCount": 0 }, "AutoRemove": False, "VolumeDriver": "", "VolumesFrom": None, "CapAdd": [], "CapDrop": [ "CAP_AUDIT_WRITE", "CAP_MKNOD", "CAP_NET_RAW" ], "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": [], "GroupAdd": [], "IpcMode": "private", "Cgroup": "", "Cgroups": "default", "Links": None, "OomScoreAdj": 0, "PidMode": "private", "Privileged": False, "PublishAllPorts": False, "ReadonlyRootfs": False, "SecurityOpt": [], "Tmpfs": {}, "UTSMode": "private", "UsernsMode": "", "ShmSize": 65536000, "Runtime": "oci", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "user.slice", "BlkioWeight": 0, "BlkioWeightDevice": None, "BlkioDeviceReadBps": None, "BlkioDeviceWriteBps": None, "BlkioDeviceReadIOps": None, "BlkioDeviceWriteIOps": None, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DiskQuota": 0, "KernelMemory": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": 0, "OomKillDisable": False, "PidsLimit": 2048, "Ulimits": [], "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "CgroupConf": None }, "Config": { "Hostname": "9d8048113074", "Domainname": "", "User": "1001", "AttachStdin": False, "AttachStdout": False, "AttachStderr": False, "Tty": False, "OpenStdin": False, "StdinOnce": False, "Env": [ "PATH=/opt/app-root/src/bin:/opt/app-root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "TERM=xterm", "container=oci", "STI_SCRIPTS_URL=image:///usr/libexec/s2i", "STI_SCRIPTS_PATH=/usr/libexec/s2i", "HOME=/var/lib/redis", "REDIS_VERSION=5", "REDIS_PREFIX=/usr", "CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/redis", "APP_ROOT=/opt/app-root", "PLATFORM=el8", "HOSTNAME=9d8048113074" ], "Cmd": [ "run-redis" ], "Image": "registry.redhat.io/rhel8/redis-5:latest", "Volumes": [], "WorkingDir": "/opt/app-root/src", "Entrypoint": "container-entrypoint", "OnBuild": None, "Labels": { "architecture": "x86_64", "build-date": "2021-05-05T06:23:07.897115", "vcs-ref": "ea375e008017960b0b749c1aae4dcd386ee68205", "vcs-type": "git", }, "Annotations": { "io.container.manager": "libpod", "io.kubernetes.cri-o.Created": "2021-05-22T10:08:18.243648647-04:00", "io.kubernetes.cri-o.TTY": "false", "io.podman.annotations.autoremove": "FALSE", "io.podman.annotations.init": "FALSE", "io.podman.annotations.privileged": "FALSE", "io.podman.annotations.publish-all": "FALSE", "org.opencontainers.image.stopSignal": "15" }, "StopSignal": 15, "Umask": "0022", "Timeout": 0, "StopTimeout": 10 }, "Image": "0ece6dfb3015c221c8ad6d364dea7884ae3e24becd60e94b80d5361f4ed78f47", "ImageName": "undercloud-0.ctlplane.redhat.local:8787/rh-osbs/rhosp16-openstack-nova-compute:16.1_20210430.1", "Name": "nova_compute", "Mounts": [], "OCIRuntime": "crun", "ConmonPidFile": "/run/user/1000/containers/overlay-containers/9d8048113074bdd2c25ba3b0e0606608fbb6e82173afe61696f7bd48f61d7aa4/userdata/conmon.pid", "PidFile": "", "RestartCount": 0, "MountLabel": "system_u:object_r:container_file_t:s0:c738,c1002", "ProcessLabel": "system_u:system_r:container_t:s0:c738,c1002", "AppArmorProfile": "", "EffectiveCaps": None, "BoundingCaps": [ "CAP_CHOWN", "CAP_DAC_OVERRIDE", "CAP_FOWNER", "CAP_FSETID", "CAP_KILL", "CAP_NET_BIND_SERVICE", "CAP_SETFCAP", "CAP_SETGID", "CAP_SETPCAP", "CAP_SETUID", "CAP_SYS_CHROOT" ], "ExecIDs": [], } image_get = { "Id": "1f202f9b76988ef7cd962db56a801b89539e0dcf1bc03953883faaaf83f4c654", "Digest": "sha256:ab901ece87a1bad3bbf7581356f18c1f79527124ed4792158c2b0b43a4896994", "RepoTags": [ "something/something:latest" ], "RepoDigests": [ "something/something@sha256:ab901ece87a1bad3bbf7581356f18c1f79527124ed4792158c2b0b43a4896994" ], "Parent": "5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6", "Comment": "", "Created": "2021-09-27T21:49:36.486090789Z", "Config": { "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "sleep", "infinity" ], "Labels": { "build_date": "2021-09-27", "description": "something", "io.buildah.version": "1.23.0", "maintainer": "David Vallee Delisle \u003cdvd@redhat.com\u003e", "name": "something", "org.label-schema.build-date": "20210915", "org.label-schema.license": "GPLv2", "vendor": "Red Hat" } }, "Version": "", "Author": "David Vallee Delisle", "Architecture": "amd64", "Os": "linux", "Size": 1004968286, "VirtualSize": 1004968286, "GraphDriver": { "Name": "overlay", "Data": { "LowerDir": "/home/dvd/.local/share/containers/storage/overlay/74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59/diff", "UpperDir": "/home/dvd/.local/share/containers/storage/overlay/1e79d79d8ad3c6eb4446e0c429275c70f0e2918f2e0038441cd11e6f3e0b2aaf/diff", "WorkDir": "/home/dvd/.local/share/containers/storage/overlay/1e79d79d8ad3c6eb4446e0c429275c70f0e2918f2e0038441cd11e6f3e0b2aaf/work" } }, "RootFS": { "Type": "layers", "Layers": [ "sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59", "sha256:47bd6062c936b8eead8f0c88a7023a4e2f8c6e53086d201c9da048dde7e3b9a3" ] }, "Labels": { "build_date": "2021-09-27", "description": "something", "io.buildah.version": "1.23.0", "maintainer": "David Vallee Delisle \u003cdvd@redhat.com\u003e", "name": "something", "org.label-schema.build-date": "20210915", "org.label-schema.license": "GPLv2", "vendor": "Red Hat" }, "Annotations": { "org.opencontainers.image.base.digest": "sha256:a1801b843b1bfaf77c501e7a6d3f709401a1e0c83863037fa3aab063a7fdb9dc", "org.opencontainers.image.base.name": "quay.io/centos/centos:8" }, "ManifestType": "application/vnd.oci.image.manifest.v1+json", "User": "", } def read_config(config='/test.config.yml'): with open(config, 'r') as yaml_config: test_config = yaml_safe_load(yaml_config) return test_config def container_list(): test_config = read_config('/test.config.yml') config_folder = test_config.get('config_folder') sample['Name'] = test_config.get('service_name') sample['State']['Running'] = bool(test_config.get('service_running', True)) sample['Mounts'].append({'Type': 'bind', 'Source': config_folder}) return [sample] def container_exec(): test_config = read_config('/test.config.yml') return test_config.get('validator_out') if __name__ == '__main__': if "image" in sys.argv[1]: print(dumps([image_get])) elif "version" in sys.argv[1]: print("podman version 3.3.1") elif len(sys.argv) > 1 and sys.argv[2] == "run": print(container_exec()) elif sys.argv[1] == "container": container = container_list() container[0]['cmd'] = sys.argv print(dumps(container)) else: print(container_exec()) print(sys.argv)