diff --git a/lib/r10k/source/exec.rb b/lib/r10k/source/exec.rb index 0ff4f3bac..954cdd1ee 100644 --- a/lib/r10k/source/exec.rb +++ b/lib/r10k/source/exec.rb @@ -28,6 +28,11 @@ def initialize(name, basedir, options = {}) end end + unless R10K::Source::Hash.valid_environment_hash?(environments) + raise R10K::Error, _("Environment source ${name} command %{cmd} did not return valid environment data.\n" \ + 'Returned: %{dat}') % {name: name, cmd: command, dat: environments} + end + # Set the environments key for the parent class to consume options[:environments] = environments diff --git a/lib/r10k/source/hash.rb b/lib/r10k/source/hash.rb index 1e542bd18..2556662bd 100644 --- a/lib/r10k/source/hash.rb +++ b/lib/r10k/source/hash.rb @@ -122,6 +122,14 @@ class R10K::Source::Hash < R10K::Source::Base include R10K::Logging + # @param hash [Hash] A hash to validate. + # @return [Boolean] False if the hash is obviously invalid. A true return + # means _maybe_ it's valid. + def self.valid_environment_hash?(hash) + # TODO: more robust schema valiation + hash.is_a?(Hash) + end + # @param name [String] The identifier for this source. # @param basedir [String] The base directory where the generated environments will be created. # @param options [Hash] An additional set of options for this source. The diff --git a/spec/unit/source/exec_spec.rb b/spec/unit/source/exec_spec.rb index d321a3519..2cfc9af05 100644 --- a/spec/unit/source/exec_spec.rb +++ b/spec/unit/source/exec_spec.rb @@ -58,7 +58,14 @@ end context 'that produces invalid output' do - it "rejects schema invalid data" + it "rejects strings as schema invalid data" do + allow_any_instance_of(R10K::Util::Subprocess) + .to receive(:execute) + .and_return(double('result', stdout: "200 OK")) + + expect { described_class.new('execsource', '/some/nonexistent/dir', command: '/path/to/command') } + .to raise_error(/did not return valid environment data/) + end it 'raises an error for non-json, non-yaml data' do allow_any_instance_of(R10K::Util::Subprocess)