diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 438463f..04e6a23 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,11 +1,3 @@ -FROM registry.access.redhat.com/ubi7 +FROM registry.access.redhat.com/ubi7/go-toolset:1.19 -RUN yum install go-toolset-1.19 git make curl gzip tar --enablerepo=rhel-7-server-devtools-rpms -y - -COPY ./entrypoint.sh /usr/bin/ -RUN chmod +x /usr/bin/entrypoint.sh - -COPY ./enable_go_scl.sh /etc/profile.d/ -RUN chmod +x /etc/profile.d/enable_go_scl.sh - -ENTRYPOINT ["entrypoint.sh"] +ENV IS_IN_CONTAINER=true \ No newline at end of file diff --git a/.devcontainer/commands/prepare_containers.sh b/.devcontainer/commands/prepare_containers.sh new file mode 100755 index 0000000..9f85e72 --- /dev/null +++ b/.devcontainer/commands/prepare_containers.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +pushd $(dirname "$0")/.. +DEVCONTAINER_PATH=$(pwd) +popd + +LOCAL_COMPOSE_FILE=$DEVCONTAINER_PATH/docker-compose.local.yml + +if [ ! -f "$LOCAL_COMPOSE_FILE" ]; then +cat >"$LOCAL_COMPOSE_FILE" < `Preferences: Open user settings (JSON)`). This will set up the dev containers plugin to work with `podman` and `podman-compose` +``` + "dev.containers.dockerComposePath": "podman-compose", + "dev.containers.dockerPath": "podman" +``` +3. Execute `.devcontainer/commands/prepare_containers.sh`. It will create `docker-compose.local.yml` file that will be used to run the container properly. -You have to have a system with `subscription-manager` installed and registered correctly to use the UBI image (which is the base for this Dockerfile) and to have access to the relevan Red Hat repositories. +### Running make commands in a container +There is an option to run make commands inside the `docker-compose` generated environment. Just prefix a make command you would like to run with `podman-`. e.g. to run `make test` in a container, use `make podman-test`. ## Mocking subscription-manager commands -`mocked_run.sh` is a wrapper of `go run main.go` to execute the command in mocked context. +`mocked_run.sh` is a shortcut to running `go run main.go` with mocked context. ### Preparing the mocks -Inside the `mocks` folder run `./prepare_mock.sh` to generate outputs that will be used as mocks. +Inside the `mocks` folder run `./mock_from_host.sh` to generate outputs that will be used as mocks. You have to have a system with `subscription-manager` installed and registered correctly to generate the mocks. + diff --git a/hack/create-cert.sh b/mocks/create-cert.sh similarity index 55% rename from hack/create-cert.sh rename to mocks/create-cert.sh index 2575081..ddeb6fa 100755 --- a/hack/create-cert.sh +++ b/mocks/create-cert.sh @@ -2,13 +2,19 @@ # Execute this script in the directory with test-cert.cnf # Purpose: Create a self-signed certificate for testing -openssl genrsa -out test-cert.key +if [ -d consumer ]; then + rm -rf consumer +fi + +mkdir consumer + +openssl genrsa -out consumer/key.pem # To create a CSR for submitting to CA # openssl req -new -key test-cert.key -out test-cert.csr -config test-cert.cnf # Create a self-signed certificate -openssl req -x509 -new -key test-cert.key -out test-cert.crt -config test-cert.cnf +openssl req -x509 -new -key consumer/key.pem -out consumer/cert.pem -config mock-cert.cnf # Show the certificate -openssl x509 -in test-cert.crt -text -noout \ No newline at end of file +openssl x509 -in consumer/cert.pem -text -noout \ No newline at end of file diff --git a/hack/test-cert.cnf b/mocks/mock-cert.cnf similarity index 100% rename from hack/test-cert.cnf rename to mocks/mock-cert.cnf diff --git a/mocks/prepare_mock.sh b/mocks/mock_from_host.sh similarity index 100% rename from mocks/prepare_mock.sh rename to mocks/mock_from_host.sh diff --git a/mocks/subscription-manager b/mocks/subscription-manager index d6eed65..2d6add2 100755 --- a/mocks/subscription-manager +++ b/mocks/subscription-manager @@ -1,4 +1,203 @@ #!/bin/bash -echo "#######Mocked output#######" -cat $(dirname "$0")/subscription-manager-$@ \ No newline at end of file +# Tries to look for a predefined file, if the file does not exist, it will use hard-coded version + +IDENTITY=\ +"system identity: 01234567-89ab-cdef-0123-456789abcdef +name: hostname +org name: 12345678 +org ID: 12345678" + +USAGE=\ +"Current Usage: Production" + +SERVICE_LEVEL=\ +"Current service level: Premium" + +FACTS_GENERAL=\ +"cpu.core(s)_per_socket: 4 +cpu.cpu(s): 8 +cpu.cpu_socket(s): 3 +cpu.thread(s)_per_core: 2 +cpu.topology_source: kernel /sys cpu sibling lists +conversions.activity: conversion +conversions.activity_ended: 2023-10-06T14:32:44.223365Z +conversions.activity_started: 2023-10-06T14:16:45.898768Z +conversions.run_id: null +conversions.source_os.id: Core +conversions.source_os.name: CentOS Linux +conversions.source_os.version: 7.9 +conversions.success: True +conversions.target_os.id: Maipo +conversions.target_os.name: Red Hat Enterprise Linux Server +conversions.target_os.version: 7.9 +conversions.version: 1 +distribution.id: Maipo +distribution.name: Red Hat Enterprise Linux Server +distribution.version: 7.9 +distribution.version.modifier: ga +last_boot: 2021-09-05 09:58:46 +lscpu.architecture: x86_64 +lscpu.bogomips: 5990.40 +lscpu.byte_order: Little Endian +lscpu.core(s)_per_socket: 4 +lscpu.cpu(s): 8 +lscpu.cpu_family: 6 +lscpu.cpu_max_mhz: 4800.0000 +lscpu.cpu_mhz: 400.000 +lscpu.cpu_min_mhz: 400.0000 +lscpu.cpu_op-mode(s): 32-bit, 64-bit +lscpu.flags: fpu vme +lscpu.l1d_cache: 48K +lscpu.l1i_cache: 32K +lscpu.l2_cache: 1280K +lscpu.l3_cache: 12288K +lscpu.model: 140 +lscpu.model_name: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz +lscpu.numa_node(s): 1 +lscpu.numa_node0_cpu(s): 0-7 +lscpu.on-line_cpu(s)_list: 0-7 +lscpu.socket(s): 1 +lscpu.stepping: 1 +lscpu.thread(s)_per_core: 2 +lscpu.vendor_id: GenuineIntel +lscpu.virtualization: VT-x +memory.memtotal: 32550092 +memory.swaptotal: 8388604 +net.interface.lo.ipv4_address: 127.0.0.1 +net.interface.lo.ipv4_address_list: 127.0.0.1 +net.interface.lo.ipv4_broadcast: Unknown +net.interface.lo.ipv4_broadcast_list: Unknown +net.interface.lo.ipv4_netmask: 8 +net.interface.lo.ipv4_netmask_list: 8 +net.interface.lo.ipv6_address.host: ::1 +net.interface.lo.ipv6_address.host_list: ::1 +net.interface.lo.ipv6_netmask.host: 128 +net.interface.lo.ipv6_netmask.host_list: 128 +net.interface.tap0.ipv4_address: 10.0.2.100 +net.interface.tap0.ipv4_address_list: 10.0.2.100 +net.interface.tap0.ipv4_broadcast: 10.0.2.255 +net.interface.tap0.ipv4_broadcast_list: 10.0.2.255 +net.interface.tap0.ipv4_netmask: 24 +net.interface.tap0.ipv4_netmask_list: 24 +net.interface.tap0.ipv6_address.global: fd00::b08b:92ff:fe3f:fc3a +net.interface.tap0.ipv6_address.global_list: fd00::b08b:92ff:fe3f:fc3a +net.interface.tap0.ipv6_address.link: fe80::b08b:92ff:fe3f:fc3a +net.interface.tap0.ipv6_address.link_list: fe80::b08b:92ff:fe3f:fc3a +net.interface.tap0.ipv6_netmask.global: 64 +net.interface.tap0.ipv6_netmask.global_list: 64 +net.interface.tap0.ipv6_netmask.link: 64 +net.interface.tap0.ipv6_netmask.link_list: 64 +net.interface.tap0.mac_address: B2:8B:92:3F:FC:3A +network.fqdn: hostname +network.hostname: hostname +network.ipv4_address: 10.0.2.100 +network.ipv6_address: fd00::b08b:92ff:fe3f:fc3a, fe80::b08b:92ff:fe3f:fc3a +proc_cpuinfo.common.address_sizes: 39 bits physical, 48 bits virtual +proc_cpuinfo.common.bogomips: 5990.40 +proc_cpuinfo.common.bugs: spectre_v1 spectre_v2 spec_store_bypass swapgs eibrs_pbrsb gds +proc_cpuinfo.common.cache_alignment: 64 +proc_cpuinfo.common.cache_size: 12288 KB +proc_cpuinfo.common.clflush_size: 64 +proc_cpuinfo.common.cpu_cores: 4 +proc_cpuinfo.common.cpu_family: 6 +proc_cpuinfo.common.cpuid_level: 27 +proc_cpuinfo.common.flags: fpu +proc_cpuinfo.common.fpu: yes +proc_cpuinfo.common.fpu_exception: yes +proc_cpuinfo.common.microcode: 0xac +proc_cpuinfo.common.model: 140 +proc_cpuinfo.common.model_name: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz +proc_cpuinfo.common.physical_id: 0 +proc_cpuinfo.common.power_management: Unknown +proc_cpuinfo.common.siblings: 8 +proc_cpuinfo.common.stepping: 1 +proc_cpuinfo.common.vendor_id: GenuineIntel +proc_cpuinfo.common.vmx_flags: vnmi +proc_cpuinfo.common.wp: yes +proc_stat.btime: 1693907926 +system.certificate_version: 3.2 +system.default_locale: Unknown +uname.machine: x86_64 +uname.nodename: hostname +uname.release: 6.4.10-100.fc11.x86_64 +uname.sysname: Linux +uname.version: #1 SMP PREEMPT_DYNAMIC Fri Aug 11 15:18:39 UTC 2021" + +FACTS_AWS=\ +"aws_account_id: 000000000000 +aws_instance_id: 1-11111111111111111" + +FACTS_AZURE=\ +"azure_subscription_id: 00000000-0000-0000-0000-000000000000 +azure_instance_id: 11111111-1111-1111-1111-111111111111" + +FACTS_GCP=\ +"gcp_project_number: 000000000000 +gcp_instance_id: 1111111111111111111" + +show_identity() { + # Show information about the host identity. + echo "${IDENTITY}" +} + +show_service_level() { + # Show the current service level. + echo "${SERVICE_LEVEL}" +} + +show_usage() { + # Show the current usage. + echo "${USAGE}" +} + +show_facts() { + # Print general facts about the host. + echo "${FACTS_GENERAL}" + + # Print cloud facts. Use the CLOUD_PROVIDER environment variable + # to choose a cloud provider to mock or select aws by default. + case "${CLOUD_PROVIDER}" in + azure|Azure|AZURE) + echo "${FACTS_AZURE}" + ;; + gcp|GCP) + echo "${FACTS_GCP}" + ;; + *) + echo "${FACTS_AWS}" + ;; + esac +} + +hard_coded() { + # Handle the specified subscription-manager command. + case "${command:=${1}}" in + identity) + show_identity + ;; + usage) + show_usage + ;; + service-level) + show_service_level + ;; + facts) + show_facts + ;; + *) + echo "This command is not supported." >&2 + exit 1 + ;; + esac +} + +MOCK_FILE=$(dirname "$0")/subscription-manager-$@ + +if [ -f "$MOCK_FILE" ]; then + echo "####### Mocked output from $MOCK_FILE #######" + cat $MOCK_FILE +else + echo "####### Mocked output from hard coded value #######" + hard_coded "${@}" +fi \ No newline at end of file diff --git a/test/bin/subscription-manager b/test/bin/subscription-manager deleted file mode 100755 index a794c24..0000000 --- a/test/bin/subscription-manager +++ /dev/null @@ -1,203 +0,0 @@ -#!/bin/bash -# -# Mocked subscription manager -# -# Use the mocked version of the subscription manager for testing and debugging: -# subscription-manager (identity|service-level|usage|facts) -# -# Specify the CLOUD_PROVIDER environment variable to change the output of the facts: -# CLOUD_PROVIDER=(aws|azure|gcp) -# - -IDENTITY=\ -"system identity: 01234567-89ab-cdef-0123-456789abcdef -name: hostname -org name: 12345678 -org ID: 12345678" - -USAGE=\ -"Current Usage: Production" - -SERVICE_LEVEL=\ -"Current service level: Premium" - -FACTS_GENERAL=\ -"cpu.core(s)_per_socket: 4 -cpu.cpu(s): 8 -cpu.cpu_socket(s): 3 -cpu.thread(s)_per_core: 2 -cpu.topology_source: kernel /sys cpu sibling lists -conversions.activity: conversion -conversions.activity_ended: 2023-10-06T14:32:44.223365Z -conversions.activity_started: 2023-10-06T14:16:45.898768Z -conversions.run_id: null -conversions.source_os.id: Core -conversions.source_os.name: CentOS Linux -conversions.source_os.version: 7.9 -conversions.success: True -conversions.target_os.id: Maipo -conversions.target_os.name: Red Hat Enterprise Linux Server -conversions.target_os.version: 7.9 -conversions.version: 1 -distribution.id: Maipo -distribution.name: Red Hat Enterprise Linux Server -distribution.version: 7.9 -distribution.version.modifier: ga -last_boot: 2021-09-05 09:58:46 -lscpu.architecture: x86_64 -lscpu.bogomips: 5990.40 -lscpu.byte_order: Little Endian -lscpu.core(s)_per_socket: 4 -lscpu.cpu(s): 8 -lscpu.cpu_family: 6 -lscpu.cpu_max_mhz: 4800.0000 -lscpu.cpu_mhz: 400.000 -lscpu.cpu_min_mhz: 400.0000 -lscpu.cpu_op-mode(s): 32-bit, 64-bit -lscpu.flags: fpu vme -lscpu.l1d_cache: 48K -lscpu.l1i_cache: 32K -lscpu.l2_cache: 1280K -lscpu.l3_cache: 12288K -lscpu.model: 140 -lscpu.model_name: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz -lscpu.numa_node(s): 1 -lscpu.numa_node0_cpu(s): 0-7 -lscpu.on-line_cpu(s)_list: 0-7 -lscpu.socket(s): 1 -lscpu.stepping: 1 -lscpu.thread(s)_per_core: 2 -lscpu.vendor_id: GenuineIntel -lscpu.virtualization: VT-x -memory.memtotal: 32550092 -memory.swaptotal: 8388604 -net.interface.lo.ipv4_address: 127.0.0.1 -net.interface.lo.ipv4_address_list: 127.0.0.1 -net.interface.lo.ipv4_broadcast: Unknown -net.interface.lo.ipv4_broadcast_list: Unknown -net.interface.lo.ipv4_netmask: 8 -net.interface.lo.ipv4_netmask_list: 8 -net.interface.lo.ipv6_address.host: ::1 -net.interface.lo.ipv6_address.host_list: ::1 -net.interface.lo.ipv6_netmask.host: 128 -net.interface.lo.ipv6_netmask.host_list: 128 -net.interface.tap0.ipv4_address: 10.0.2.100 -net.interface.tap0.ipv4_address_list: 10.0.2.100 -net.interface.tap0.ipv4_broadcast: 10.0.2.255 -net.interface.tap0.ipv4_broadcast_list: 10.0.2.255 -net.interface.tap0.ipv4_netmask: 24 -net.interface.tap0.ipv4_netmask_list: 24 -net.interface.tap0.ipv6_address.global: fd00::b08b:92ff:fe3f:fc3a -net.interface.tap0.ipv6_address.global_list: fd00::b08b:92ff:fe3f:fc3a -net.interface.tap0.ipv6_address.link: fe80::b08b:92ff:fe3f:fc3a -net.interface.tap0.ipv6_address.link_list: fe80::b08b:92ff:fe3f:fc3a -net.interface.tap0.ipv6_netmask.global: 64 -net.interface.tap0.ipv6_netmask.global_list: 64 -net.interface.tap0.ipv6_netmask.link: 64 -net.interface.tap0.ipv6_netmask.link_list: 64 -net.interface.tap0.mac_address: B2:8B:92:3F:FC:3A -network.fqdn: hostname -network.hostname: hostname -network.ipv4_address: 10.0.2.100 -network.ipv6_address: fd00::b08b:92ff:fe3f:fc3a, fe80::b08b:92ff:fe3f:fc3a -proc_cpuinfo.common.address_sizes: 39 bits physical, 48 bits virtual -proc_cpuinfo.common.bogomips: 5990.40 -proc_cpuinfo.common.bugs: spectre_v1 spectre_v2 spec_store_bypass swapgs eibrs_pbrsb gds -proc_cpuinfo.common.cache_alignment: 64 -proc_cpuinfo.common.cache_size: 12288 KB -proc_cpuinfo.common.clflush_size: 64 -proc_cpuinfo.common.cpu_cores: 4 -proc_cpuinfo.common.cpu_family: 6 -proc_cpuinfo.common.cpuid_level: 27 -proc_cpuinfo.common.flags: fpu -proc_cpuinfo.common.fpu: yes -proc_cpuinfo.common.fpu_exception: yes -proc_cpuinfo.common.microcode: 0xac -proc_cpuinfo.common.model: 140 -proc_cpuinfo.common.model_name: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz -proc_cpuinfo.common.physical_id: 0 -proc_cpuinfo.common.power_management: Unknown -proc_cpuinfo.common.siblings: 8 -proc_cpuinfo.common.stepping: 1 -proc_cpuinfo.common.vendor_id: GenuineIntel -proc_cpuinfo.common.vmx_flags: vnmi -proc_cpuinfo.common.wp: yes -proc_stat.btime: 1693907926 -system.certificate_version: 3.2 -system.default_locale: Unknown -uname.machine: x86_64 -uname.nodename: hostname -uname.release: 6.4.10-100.fc11.x86_64 -uname.sysname: Linux -uname.version: #1 SMP PREEMPT_DYNAMIC Fri Aug 11 15:18:39 UTC 2021" - -FACTS_AWS=\ -"aws_account_id: 000000000000 -aws_instance_id: 1-11111111111111111" - -FACTS_AZURE=\ -"azure_subscription_id: 00000000-0000-0000-0000-000000000000 -azure_instance_id: 11111111-1111-1111-1111-111111111111" - -FACTS_GCP=\ -"gcp_project_number: 000000000000 -gcp_instance_id: 1111111111111111111" - -show_identity() { - # Show information about the host identity. - echo "${IDENTITY}" -} - -show_service_level() { - # Show the current service level. - echo "${SERVICE_LEVEL}" -} - -show_usage() { - # Show the current usage. - echo "${USAGE}" -} - -show_facts() { - # Print general facts about the host. - echo "${FACTS_GENERAL}" - - # Print cloud facts. Use the CLOUD_PROVIDER environment variable - # to choose a cloud provider to mock or select aws by default. - case "${CLOUD_PROVIDER}" in - azure|Azure|AZURE) - echo "${FACTS_AZURE}" - ;; - gcp|GCP) - echo "${FACTS_GCP}" - ;; - *) - echo "${FACTS_AWS}" - ;; - esac -} - -main() { - # Handle the specified subscription-manager command. - case "${command:=${1}}" in - identity) - show_identity - ;; - usage) - show_usage - ;; - service-level) - show_service_level - ;; - facts) - show_facts - ;; - *) - echo "This command is not supported." >&2 - exit 1 - ;; - esac - -} - -main "${@}"