summaryrefslogtreecommitdiffstats
path: root/test/network/server/webrick.rb
blob: 624147b6c67810075fb47a5cd118d8e8fa89202c (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
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env ruby

require File.expand_path(File.dirname(__FILE__) + '/../../lib/puppettest')

require 'puppettest'
require 'puppet/network/http_server/webrick'
require 'mocha'

class TestWebrickServer < Test::Unit::TestCase
  include PuppetTest::ServerTest

  def setup
    Puppet::Util::SUIDManager.stubs(:asuser).yields
    super
  end

  def teardown
    super
    Puppet::Network::HttpPool.clear_http_instances
  end

  # Make sure we can create a server, and that it knows how to create its
  # certs by default.
  def test_basics
    server = nil
    assert_raise(Puppet::Error, "server succeeded with no cert") do

            server = Puppet::Network::HTTPServer::WEBrick.new(
                
        :Port => @@port,
        
        :Handlers => {
          :Status => nil
        }
      )
    end

    assert_nothing_raised("Could not create simple server") do

            server = Puppet::Network::HTTPServer::WEBrick.new(
                
        :Port => @@port,
        
        :Handlers => {
          :CA => {}, # so that certs autogenerate
          :Status => nil
        }
      )
    end

    assert(server, "did not create server")

    assert(server.cert, "did not retrieve cert")
  end

  # test that we can connect to the server
  # we have to use fork here, because we apparently can't use threads
  # to talk to other threads
  def test_connect_with_fork
    Puppet[:autosign] = true
    serverpid, server = mk_status_server

    # create a status client, and verify it can talk
    client = mk_status_client

    assert(client.cert, "did not get cert for client")

    retval = nil
    assert_nothing_raised("Could not connect to server") {
      retval = client.status
    }
    assert_equal(1, retval)
  end

  def mk_status_client
    client = nil

    assert_nothing_raised {

            client = Puppet::Network::Client.status.new(
                
        :Server => "localhost",
        
        :Port => @@port
      )
    }
    client
  end

  def mk_status_server
    server = nil
    Puppet[:certdnsnames] = "localhost"
    assert_nothing_raised {

            server = Puppet::Network::HTTPServer::WEBrick.new(
                
        :Port => @@port,
        
        :Handlers => {
          :CA => {}, # so that certs autogenerate
          :Status => nil
        }
      )

    }

    pid = fork {
      Puppet.run_mode.stubs(:master?).returns true
      assert_nothing_raised {
        trap(:INT) { server.shutdown }
        server.start
      }
    }
    @@tmppids << pid
    [pid, server]
  end

  def kill_and_wait(pid, file)
    %x{kill -INT #{pid} 2>/dev/null}
    count = 0
    while count < 30 && File::exist?(file)
      count += 1
      sleep(1)
    end
    assert(count < 30, "Killing server #{pid} failed")
  end
end