Skip to content
Permalink
Browse files Browse the repository at this point in the history
Fixes #14931 - TFTP class instantiating fixed
  • Loading branch information
lzap authored and dmitri-d committed May 5, 2016
1 parent 4f0fafb commit eef532a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions modules/tftp/tftp_api.rb
Expand Up @@ -8,13 +8,14 @@ class Api < ::Sinatra::Base
helpers ::Proxy::Helpers
authorize_with_trusted_hosts
authorize_with_ssl_client
VARIANTS = ["Syslinux", "Pxegrub", "Pxegrub2", "Ztp", "Poap"].freeze

helpers do
def instantiate variant, mac=nil
# Filenames must end in a hex representation of a mac address but only if mac is not empty
log_halt 403, "Invalid MAC address: #{mac}" unless valid_mac?(mac) || mac.nil?
log_halt 403, "Unrecognized pxeboot config type: #{variant}" unless defined? variant.capitalize
eval "Proxy::TFTP::#{variant.capitalize}.new"
log_halt 403, "Unrecognized pxeboot config type: #{variant}" unless VARIANTS.include?(variant.capitalize)
Object.const_get("Proxy").const_get('TFTP').const_get(variant.capitalize).new
end

def create variant, mac
Expand Down
11 changes: 11 additions & 0 deletions test/tftp/tftp_api_test.rb
Expand Up @@ -17,6 +17,17 @@ def setup
@args = { :pxeconfig => "foo" }
end

def test_instantiate_syslinux
obj = app.helpers.instantiate "syslinux", "AA:BB:CC:DD:EE:FF"
assert_equal "Proxy::TFTP::Syslinux", obj.class.name
end

def test_instantiate_nonexisting
subject = app
subject.helpers.expects(:log_halt).with(403, "Unrecognized pxeboot config type: Server").at_least(1)
subject.helpers.instantiate "Server", "AA:BB:CC:DD:EE:FF"
end

def test_api_can_fetch_boot_file
Proxy::Util::CommandTask.stubs(:new).returns(true)
FileUtils.stubs(:mkdir_p).returns(true)
Expand Down

0 comments on commit eef532a

Please sign in to comment.