|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'ona' |
| 4 | +require 'net/http' |
| 5 | + |
| 6 | +# Generate new port number each time asked |
| 7 | +class PortManager |
| 8 | + @next_port = 11_080 |
| 9 | + |
| 10 | + def self.next_port |
| 11 | + port = @next_port |
| 12 | + @next_port += 1 |
| 13 | + port |
| 14 | + end |
| 15 | +end |
| 16 | + |
| 17 | +def with_retries(max_retries = 30) |
| 18 | + (1..max_retries).each do |
| 19 | + begin |
| 20 | + yield |
| 21 | + return |
| 22 | + rescue StandardError |
| 23 | + # do nothing |
| 24 | + end |
| 25 | + sleep(0.2) |
| 26 | + end |
| 27 | + raise "max retries #{max_retries} reached, aborting" |
| 28 | +end |
| 29 | + |
| 30 | +def ephemeral_onadev_container |
| 31 | + host = 'localhost' |
| 32 | + port = PortManager.next_port |
| 33 | + container_prefix = 'onadev_' |
| 34 | + dcm_path = '/ona/dcm.php' |
| 35 | + @dcm_endpoint = "http://#{host}:#{port}#{dcm_path}" |
| 36 | + name = "#{container_prefix}#{port}" |
| 37 | + |
| 38 | + # define and start ephemeral (--rm) container |
| 39 | + `podman container create -p #{port}:80 --name #{name} --rm docker://ghcr.io/gsi-hpc/ona-dev:latest` |
| 40 | + `podman container start #{name}` |
| 41 | + |
| 42 | + # wait for webserver |
| 43 | + with_retries do |
| 44 | + res = Net::HTTP.start(host, port).get(dcm_path) |
| 45 | + raise unless res.is_a?(Net::HTTPSuccess) |
| 46 | + end |
| 47 | + |
| 48 | + yield |
| 49 | +ensure |
| 50 | + `podman container kill #{name}` |
| 51 | +end |
| 52 | + |
| 53 | +describe ONA do |
| 54 | + subject(:ona) { described_class.new(@dcm_endpoint, 'admin', 'admin') } # rubocop:disable RSpec/InstanceVariable |
| 55 | + |
| 56 | + around { |ex| ephemeral_onadev_container(&ex) } |
| 57 | + |
| 58 | + describe 'get_module_list' do |
| 59 | + subject(:result) { ona.query('get_module_list', { type: 'array' }) } |
| 60 | + |
| 61 | + it 'returns the module list' do |
| 62 | + expect(result).to be_an_instance_of(Hash).and \ |
| 63 | + include('domain_display') |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + describe 'domain_display' do |
| 68 | + subject(:result) { ona.query('domain_display', { domain: 'example.com' }) } |
| 69 | + |
| 70 | + it { is_expected.to include('fqdn' => 'example.com') } |
| 71 | + end |
| 72 | +end |
0 commit comments