From fdc44d1f05604cb8f7cdda4a85f9c8cb0390b249 Mon Sep 17 00:00:00 2001 From: Duncan Mac-Vicar P Date: Tue, 19 Jun 2018 12:05:50 +0200 Subject: [PATCH] Implement service retrieval --- lib/suse/connect/client.rb | 2 ++ lib/suse/connect/dnf.rb | 19 +++++++++++++++++++ spec/connect/dnf_spec.rb | 29 +++++++++++++++++++++++++++++ spec/fixtures/service_response.xml | 7 +++++++ 4 files changed, 57 insertions(+) create mode 100644 spec/fixtures/service_response.xml diff --git a/lib/suse/connect/client.rb b/lib/suse/connect/client.rb index adfa05a2..5ca61371 100644 --- a/lib/suse/connect/client.rb +++ b/lib/suse/connect/client.rb @@ -19,6 +19,8 @@ def initialize(config) @config = config @api = Api.new(@config) log.debug "Merged options: #{@config}" + + Dnf.config = config end # Announces the system, activates the product on SCC and adds the service to the system diff --git a/lib/suse/connect/dnf.rb b/lib/suse/connect/dnf.rb index 490ab39b..a348da98 100644 --- a/lib/suse/connect/dnf.rb +++ b/lib/suse/connect/dnf.rb @@ -51,6 +51,25 @@ def repositories JSON.parse(helper_out, symbolize_names: true) end + # Internal method that implements service refresh logic missing in dnf + def read_service(service_url) + connection = Connection.new( + service_url, + language: Dnf.config.language, + insecure: Dnf.config.insecure, + verify_callback: Dnf.config.verify_callback, + debug: Dnf.config.debug + ) + auth = Class.new.extend(SUSE::Toolkit::Utilities).system_auth + + uri = URI.parse(service_url) + request = Net::HTTP::Get.new(uri.path) + request['Authorization'] = auth + service_index = connection.http.request(request) + xml_doc = REXML::Document.new(service_index.body, compress_whitespace: []) + xml_doc.root.elements.map(&:to_hash) + end + def root_arg "--installroot '#{SUSE::Connect::System.filesystem_root}' " if SUSE::Connect::System.filesystem_root end diff --git a/spec/connect/dnf_spec.rb b/spec/connect/dnf_spec.rb index 4d449d0b..52aeb4dd 100644 --- a/spec/connect/dnf_spec.rb +++ b/spec/connect/dnf_spec.rb @@ -105,4 +105,33 @@ expect(subject.repositories.size).to eq 0 end end + + describe '.read_service' do + let(:service_name) { 'valid_service' } + let(:service_url) { 'http://example.com/access/service/666' } + let(:login) { 'login' } + let(:password) { 'password' } + let(:system_credentials) { Credentials.new(login, password, Credentials::GLOBAL_CREDENTIALS_FILE) } + let(:repo) do + { alias: 'RES7', + autorefresh: 'true', + enabled: 'true', + name: 'RES7', + url: 'https://updates.suse.com/repo/$RCE/RES7/src?token' } + end + + before do + allow(Dnf).to receive(:config).at_least(1).and_return(Config.new) + allow(Credentials).to receive(:read).with(Credentials::GLOBAL_CREDENTIALS_FILE).and_return(system_credentials) + stub_request(:get, "http://#{login}:#{password}@example.com/access/service/666") + .to_return(status: 200, body: File.read('spec/fixtures/service_response.xml')) + end + + it 'returns a list of repositories' do + service = subject.read_service(service_url) + expect(service.size).to eq 2 + expect(service.first).to eq repo + end + end + end diff --git a/spec/fixtures/service_response.xml b/spec/fixtures/service_response.xml new file mode 100644 index 00000000..e523560a --- /dev/null +++ b/spec/fixtures/service_response.xml @@ -0,0 +1,7 @@ + + + +