summaryrefslogtreecommitdiff
path: root/t/test-custodian-util-tftp.rb
blob: a4712260a7ec58289a5e7dc78c62a8d746938a02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/ruby -I./lib/ -I../lib/

require 'custodian/util/tftp'
require 'test/unit'

#
# Unit test for our tftp utility class.
#
class TestTftpUtil < Test::Unit::TestCase


  #
  # Create the test suite environment: NOP.
  #
  def setup
  end

  #
  # Destroy the test suite environment: NOP.
  #
  def teardown
  end


  #
  # Test we can construct new objects.
  #
  def test_init

    #
    # Normal construction works.
    #
    assert_nothing_raised do
        Custodian::Util::Tftp.new('foo')
    end
    assert_nothing_raised do
        Custodian::Util::Tftp.new('foo', 123)
    end
    assert_nothing_raised do
        Custodian::Util::Tftp.new('foo', '123')
    end


    #
    # A hostname must be supplied
    #
    assert_raise ArgumentError do
        Custodian::Util::Tftp.new(nil)
    end

    #
    # A hostname is a string, not an array, hash, or similar.
    #
    assert_raise ArgumentError do
      Custodian::Util::Tftp.new({})
    end
    assert_raise ArgumentError do
      Custodian::Util::Tftp.new([])
    end

    #
    # A port, if supplied, must be a number
    #
    assert_raise ArgumentError do
      Custodian::Util::Tftp.new('foo', 'bar')
    end

    #
    # The default port is 69
    #
    assert_equal(Custodian::Util::Tftp.new('foo').port, 69)


  end

  #
  # Test a tftp successful fetch
  #
  def test_tftp_suceeds
    helper = Custodian::Util::Tftp.new('foo')
    def helper.tftp(args)
      filename = args.split(' ').last
      File.open(filename, 'w') { |w| w.puts 'stuff' }
      return true
    end

    assert(helper.test('file'))
  end

  #
  # Test a tftp failed fetch
  #
  def test_tftp_failed_fetch
    helper = Custodian::Util::Tftp.new('foo')
    def helper.tftp(_args)
      return false
    end

    assert(!helper.test('file'))
  end

  #
  # Test a tftp fetch of empty file
  #
  def test_tftp_empty_file
    helper = Custodian::Util::Tftp.new('foo')
    def helper.tftp(args)
      filename = args.split(' ').last
      File.open(filename, 'w') { |w| }
      return true
    end

    assert(!helper.test('file'))
  end

end