[Ovirt-devel] [PATCH] Rewriting the identify-node piece into two managed node units

Darryl L. Pierce dpierce at redhat.com
Wed Jun 11 16:31:46 UTC 2008


Signed-off-by: Darryl L. Pierce <dpierce at redhat.com>
---
 identify-node/ovirt-awake                          |   80 ++++++++
 identify-node/ovirt-functions                      |   66 +++++++
 wui/src/host-browser/host-browser.rb               |  103 ++++++-----
 wui/src/host-browser/test-host-browser-awaken.rb   |   93 +++++++++
 wui/src/host-browser/test-host-browser-identify.rb |  204 ++++++++++++++++++++
 wui/src/host-browser/test-host-browser.rb          |  204 --------------------
 6 files changed, 501 insertions(+), 249 deletions(-)
 create mode 100755 identify-node/ovirt-awake
 create mode 100644 identify-node/ovirt-functions
 create mode 100755 wui/src/host-browser/test-host-browser-awaken.rb
 create mode 100755 wui/src/host-browser/test-host-browser-identify.rb
 delete mode 100755 wui/src/host-browser/test-host-browser.rb

diff --git a/identify-node/ovirt-awake b/identify-node/ovirt-awake
new file mode 100755
index 0000000..7f654e6
--- /dev/null
+++ b/identify-node/ovirt-awake
@@ -0,0 +1,80 @@
+#!/bin/bash
+#
+# ovirt-awake   Notifies the oVirt server that a managed node is
+#               starting up.
+#
+# Copyright (C) 2008 Red Hat, Inc.
+# Written by Darryl L. Pierce <dpierce at redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.  A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+
+# Source function library
+. /etc/init.d/ovirt-functions
+
+start () {
+    find-server
+
+    connect-to-server
+
+    receive-text
+
+    if [ $REPLY == "HELLO?" ]; then
+        echo "Starting wakeup conversation."
+
+        send-text "HELLO!"
+
+        read 0<&3
+
+        if [ $REPLY == "MODE?" ]; then
+            send-text "AWAKEN"
+
+            receive-text
+
+            KEYTAB=`echo $REPLY | awk '{ $2 }'`
+
+            if [ -n $KEYTAB ]; then
+                wget http://$SERVER/$KEYTAB
+            else
+                echo "No keytab to retrieve"
+            fi
+        else
+            echo "Did not get a mode request."
+        fi
+    else
+        echo "Did not get a proper startup marker."
+    fi
+
+    echo "Disconnecting."
+
+    disconnect-from-server
+}
+
+case "$1" in
+    start)
+        SERVER=$2
+        PORT=$3
+        PORT=$3
+        start
+        RETVAL=$?
+    ;;
+
+    *)
+        echo "Usage: $0 start"
+        RETVAL=2
+    ;;
+esac
+
+exit $RETVAL
diff --git a/identify-node/ovirt-functions b/identify-node/ovirt-functions
new file mode 100644
index 0000000..b6c9a0a
--- /dev/null
+++ b/identify-node/ovirt-functions
@@ -0,0 +1,66 @@
+#
+# Copyright (C) 2008 Red Hat, Inc.
+# Written by Darryl L. Pierce <dpierce at redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.  A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+
+# Determines the hostname and port for the oVirt server.
+#
+find-server() {
+    if [ -z $SERVER ]; then
+        dnsreply=$(dig +short -t srv _ovirt-host._tcp.$(dnsdomainname))
+        if [ $? -eq 0 ]; then
+            set _ $dnsreply; shift
+            SERVER=$4; PORT=$3
+        else
+            SERVER=; PORT=
+        fi
+    fi
+
+    if [ -z $SERVER ]; then
+        echo "No server found..."
+        exit
+    fi
+}
+
+# Establishes a TCP connection to the server.
+#
+connect-to-server () {
+    echo "Connecting to $SERVER:$PORT"...
+
+    exec 3<> /dev/tcp/$SERVER/$PORT
+}
+
+# Disconnects form the server.
+#
+disconnect-from-server () {
+    <&3-
+}
+
+# Sends text to the remote server.
+#
+send-text () {
+    echo "Sending: \"$1\""
+    echo "$1" 1>&3
+}
+
+# Receives text from the remote server.
+#
+receive-text () {
+    read 0<&3
+
+    echo "Received: \"$REPLY\""
+}
diff --git a/wui/src/host-browser/host-browser.rb b/wui/src/host-browser/host-browser.rb
index e127ddb..8337d7b 100755
--- a/wui/src/host-browser/host-browser.rb
+++ b/wui/src/host-browser/host-browser.rb
@@ -1,5 +1,5 @@
 #!/usr/bin/ruby -Wall
-# 
+#
 # Copyright (C) 2008 Red Hat, Inc.
 # Written by Darryl L. Pierce <dpierce at redhat.com>
 #
@@ -41,7 +41,7 @@ class HostBrowser
     attr_accessor :logfile
     attr_accessor :keytab_dir
     attr_accessor :keytab_filename
-  
+
     def initialize(session)
         @session = session
         @log_prefix = "[#{session.peeraddr[3]}] "
@@ -51,17 +51,28 @@ class HostBrowser
     # Ensures the conversation starts properly.
     #
     def begin_conversation
-        puts "#{@log_prefix} Begin conversation"
+        puts "#{@log_prefix} Begin conversation" unless defined?(TESTING)
         @session.write("HELLO?\n")
 
         response = @session.readline.chomp
         raise Exception.new("received #{response}, expected HELLO!") unless response == "HELLO!"
     end
 
+    # Retrieves the mode request from the remote system.
+    #
+    def get_mode
+        puts "#{@log_prefix} Determining the runtime mode." unless defined?(TESTING)
+        @session.write("MODE?\n")
+        response = @session.readline.chomp
+        puts "#{@log_prefix} MODE=#{response}" unless defined?(TESTING)
+
+        response
+    end
+
     # Requests node information from the remote system.
     #
     def get_remote_info
-        puts "#{@log_prefix} Begin remote info collection"
+        puts "#{@log_prefix} Begin remote info collection" unless defined?(TESTING)
         result = {}
         result['IPADDR'] = @session.peeraddr[3]
         @session.write("INFO?\n")
@@ -75,9 +86,9 @@ class HostBrowser
 
             key, value = info.split("=")
 
-            puts "#{@log_prefix} ::Received - #{key}:#{value}"
+            puts "#{@log_prefix} ::Received - #{key}:#{value}" unless defined?(TESTING)
             result[key] = value
-        
+
             @session.write("ACK #{key}\n")
         end
 
@@ -94,13 +105,13 @@ class HostBrowser
         ensure_present(host_info,'ARCH')
         ensure_present(host_info,'MEMSIZE')
 
-        puts "Searching for existing host record..."
+        puts "Searching for existing host record..." unless defined?(TESTING)
         host = Host.find(:first, :conditions => ["uuid = ?", host_info['UUID']])
 
         if host == nil
             begin
-                puts "Creating a new record for #{host_info['HOSTNAME']}..."
-            
+                puts "Creating a new record for #{host_info['HOSTNAME']}..." unless defined?(TESTING)
+
                 Host.new(
                     "uuid"            => host_info['UUID'],
                     "hostname"        => host_info['HOSTNAME'],
@@ -115,7 +126,7 @@ class HostBrowser
                     # successfully connects to it via libvirt.
                     "state"           => "unavailable").save
             rescue Exception => error
-                puts "Error while creating record: #{error.message}"
+                puts "Error while creating record: #{error.message}" unless defined?(TESTING)
             end
         else
             host.uuid         = host_info['UUID']
@@ -125,45 +136,43 @@ class HostBrowser
             host.arch         = host_info['ARCH']
             host.memory_in_mb = host_info['MEMSIZE']
         end
-    
+
         return host
     end
 
-    # Ends the conversation, notifying the user of the key version number.
-    #
-    def end_conversation(ktab)
-        puts "#{@log_prefix} Ending conversation"
-
-        @session.write("KTAB #{ktab}\n")
-
-        response = @session.readline.chomp
-
-        raise Exception.new("ERROR! Malformed response : expected ACK, got #{response}") unless response == "ACK"
-
-        @session.write("BYE\n");
-    end
-  
     # Creates a keytab if one is needed, returning the filename.
     #
-    def create_keytab(host_info, krb5_arg = nil)
+    def create_keytab(hostname, ipaddress, krb5_arg = nil)
         krb5 = krb5_arg || Krb5.new
-  
+
         default_realm = krb5.get_default_realm
-        libvirt_princ = 'libvirt/' + host_info['HOSTNAME'] + '@' + default_realm
-        outfile = host_info['IPADDR'] + '-libvirt.tab'
+        libvirt_princ = 'libvirt/' + hostname + '@' + default_realm
+        outfile = ipaddress + '-libvirt.tab'
         @keytab_filename = @keytab_dir + outfile
 
         # TODO need a way to test this portion
         unless defined? TESTING || File.exists?(@keytab_filename)
             # TODO replace with Kr5Auth when it supports admin actions
-            puts "Writing keytab file: #{@keytab_filename}"
+            puts "Writing keytab file: #{@keytab_filename}" unless defined?(TESTING)
             kadmin_local('addprinc -randkey ' + libvirt_princ)
             kadmin_local('ktadd -k ' + @keytab_filename + ' ' + libvirt_princ)
 
             File.chmod(0644, at keytab_filename)
         end
 
-        return outfile
+        @session.write("KTAB #{outfile}\n")
+
+        response = @session.readline.chomp
+
+        raise Exception.new("ERRINFO! No keytab acknowledgement") unless response == "ACK"
+    end
+
+    # Ends the conversation, notifying the user of the key version number.
+    #
+    def end_conversation
+        puts "#{@log_prefix} Ending conversation" unless defined?(TESTING)
+
+        @session.write("BYE\n");
     end
 
     private
@@ -185,35 +194,39 @@ def entry_point(server)
     while(session = server.accept)
         child = fork do
             remote = session.peeraddr[3]
-            
-            puts "Connected to #{remote}"
+
+            puts "Connected to #{remote}" unless defined?(TESTING)
 
             # This is needed because we just forked a new process
             # which now needs its own connection to the database.
             database_connect
-      
+
             begin
                 browser = HostBrowser.new(session)
 
                 browser.begin_conversation
-                host_info = browser.get_remote_info
-                browser.write_host_info(host_info)
-                keytab = browser.create_keytab(host_info)
-                browser.end_conversation(keytab)
+                case browser.get_mode
+                    when "AWAKEN": browser.create_keytab(remote,session.peeraddr[3])
+                end
+                #host_info = browser.get_remote_info
+                #browser.write_host_info(host_info)
+                #keytab = browser.create_keytab(host_info)
+
+                browser.end_conversation
             rescue Exception => error
                 session.write("ERROR #{error.message}\n")
-                puts "ERROR #{error.message}"
+                puts "ERROR #{error.message}" unless defined?(TESTING)
             end
-      
-            puts "Disconnected from #{remote}"
+
+            puts "Disconnected from #{remote}" unless defined?(TESTING)
         end
-    
-        Process.detach(child)        
-    end      
+
+        Process.detach(child)
+    end
 end
 
 unless defined?(TESTING)
-  
+
     # The main entry point.
     #
     unless ARGV[0] == "-n"
diff --git a/wui/src/host-browser/test-host-browser-awaken.rb b/wui/src/host-browser/test-host-browser-awaken.rb
new file mode 100755
index 0000000..a5a1bcf
--- /dev/null
+++ b/wui/src/host-browser/test-host-browser-awaken.rb
@@ -0,0 +1,93 @@
+#!/usr/bin/ruby -Wall
+#
+# Copyright (C) 2008 Red Hat, Inc.
+# Written by Darryl L. Pierce <dpierce at redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.  A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+
+require File.dirname(__FILE__) + '/../test/test_helper'
+require 'test/unit'
+require 'flexmock/test_unit'
+
+TESTING=true
+
+require 'host-browser'
+
+# +TestHostBrowserAwaken+
+class TestHostBrowserAwaken < Test::Unit::TestCase
+
+  def setup
+    @session = flexmock('session')
+    @session.should_receive(:peeraddr).at_least.once.returns { [nil,nil,nil,"192.168.2.255"] }
+
+    @krb5 = flexmock('krb5')
+
+    @browser = HostBrowser.new(@session)
+    @browser.logfile = './unit-test.log'
+    @browser.keytab_dir = '/var/temp/'
+  end
+
+  # Ensures that the server raises an exception when it receives an
+  # improper handshake response.
+  #
+  def test_begin_conversation_with_improper_response_to_greeting
+    @session.should_receive(:write).with("HELLO?\n").once().returns { |greeting| greeting.length }
+    @session.should_receive(:readline).once().returns { "SUP?" }
+
+    assert_raise(Exception) { @browser.begin_conversation }
+  end
+
+  # Ensures the server accepts a proper response from the remote system.
+  #
+  def test_begin_conversation
+    @session.should_receive(:write).with("HELLO?\n").once().returns { |greeting| greeting.length }
+    @session.should_receive(:readline).once().returns { "HELLO!\n" }
+
+    assert_nothing_raised(Exception) { @browser.begin_conversation }
+  end
+
+  # Ensures that the server is satisfied if the remote system is
+  # making a wakeup call.
+  #
+  def test_get_mode_with_awaken_request
+    @session.should_receive(:write).with("MODE?\n").once().returns { |request| request.length }
+    @session.should_receive(:readline).once().returns { "AWAKEN\n" }
+
+    result = @browser.get_mode()
+
+    assert_equal "AWAKEN", result, "method did not return the right value"
+  end
+
+  # Ensures the host browser generates a keytab as expected.
+  #
+  def test_create_keytab
+    @krb5.should_receive(:get_default_realm).once().returns { "ovirt-test-realm" }
+    @session.should_receive(:write).with("KTAB 127.0.0.1-libvirt.tab\n").once().returns { |request| request.length }
+    @session.should_receive(:readline).once().returns { "ACK\n" }
+
+    assert_nothing_raised(Exception) { @browser.create_keytab('localhost','127.0.0.1', at krb5) }
+  end
+
+  # Ensures that, if a keytab is present and a key version number available,
+  # the server ends the conversation by returning the key version number.
+  #
+  def test_end_conversation
+    @session.should_receive(:write).with("BYE\n").once().returns { |request| request.length }
+
+    assert_nothing_raised(Exception) { @browser.end_conversation }
+  end
+
+end
diff --git a/wui/src/host-browser/test-host-browser-identify.rb b/wui/src/host-browser/test-host-browser-identify.rb
new file mode 100755
index 0000000..6f4c660
--- /dev/null
+++ b/wui/src/host-browser/test-host-browser-identify.rb
@@ -0,0 +1,204 @@
+#!/usr/bin/ruby -Wall
+#
+# Copyright (C) 2008 Red Hat, Inc.
+# Written by Darryl L. Pierce <dpierce at redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.  A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+
+require File.dirname(__FILE__) + '/../test/test_helper'
+require 'test/unit'
+require 'flexmock/test_unit'
+
+TESTING=true
+
+require 'host-browser'
+
+class TestHostBrowser < Test::Unit::TestCase
+
+  def setup
+    @session = flexmock('session')
+    @session.should_receive(:peeraddr).at_least.once.returns { [nil,nil,nil,"192.168.2.255"] }
+
+    @krb5 = flexmock('krb5')
+
+    @browser = HostBrowser.new(@session)
+    @browser.logfile = './unit-test.log'
+    @browser.keytab_dir = '/var/temp/'
+
+    # default host info
+    @host_info = {}
+    @host_info['UUID']     = 'node1'
+    @host_info['IPADDR']   = '192.168.2.2'
+    @host_info['HOSTNAME'] = 'node1.ovirt.redhat.com'
+    @host_info['NUMCPUS']  = '3'
+    @host_info['CPUSPEED'] = '3'
+    @host_info['ARCH']     = 'x86_64'
+    @host_info['MEMSIZE']  = '16384'
+    @host_info['DISABLED'] = '0'
+  end
+
+  # Ensures that the server raises an exception when it receives an
+  # improper handshake response.
+  #
+  def test_begin_conversation_with_improper_response_to_greeting
+    @session.should_receive(:write).with("HELLO?\n").once().returns { |greeting| greeting.length }
+    @session.should_receive(:readline).once().returns { "SUP?" }
+
+    assert_raise(Exception) { @browser.begin_conversation }
+  end
+
+  # Ensures the server accepts a proper response from the remote system.
+  #
+  def test_begin_conversation
+    @session.should_receive(:write).with("HELLO?\n").once().returns { |greeting| greeting.length }
+    @session.should_receive(:readline).once().returns { "HELLO!\n" }
+
+    assert_nothing_raised(Exception) { @browser.begin_conversation }
+  end
+
+  # Ensures that the server raises an exception when it receives
+  # poorly formed data while exchanging system information.
+  #
+  def test_get_info_with_bad_handshake
+    @session.should_receive(:write).with("INFO?\n").once().returns { |request| request.length }
+    @session.should_receive(:readline).once().returns { "key1=value1\n" }
+    @session.should_receive(:write).with("ACK key1\n").once().returns { |request| request.length }
+    @session.should_receive(:readline).once().returns { "farkledina\n" }
+
+    assert_raise(Exception) { @browser.get_remote_info }
+  end
+
+  # Ensures that, if an info field is missing a key, the server raises
+  # an exception.
+  #
+  def test_get_info_with_missing_key
+    @session.should_receive(:write).with("INFO?\n").once().returns { |request| request.length }
+    @session.should_receive(:readline).once().returns { "=value1\n" }
+
+    assert_raise(Exception) { @browser.get_remote_info }
+  end
+
+  # Ensures that, if an info field is missing a value, the server raises
+  # an exception.
+  #
+  def test_get_info_with_missing_value
+    @session.should_receive(:write).with("INFO?\n").once().returns { |request| request.length }
+    @session.should_receive(:readline).once().returns { "key1=\n" }
+
+    assert_raise(Exception) { @browser.get_remote_info }
+  end
+
+  # Ensures that, if the server gets a poorly formed ending statement, it
+  # raises an exception.
+  #
+  def test_get_info_with_invalid_end
+    @session.should_receive(:write).with("INFO?\n").once().returns { |request| request.length }
+    @session.should_receive(:readline).once().returns { "key1=value1\n" }
+    @session.should_receive(:write).with("ACK key1\n").once().returns { |request| request.length }
+    @session.should_receive(:readline).once().returns { "ENDIFNO\n" }
+
+    assert_raise(Exception) { @browser.get_remote_info }
+  end
+
+  # Ensures that a well-formed transaction works as expected.
+  #
+  def test_get_info
+    @session.should_receive(:write).with("INFO?\n").once().returns { |request| request.length }
+    @session.should_receive(:readline).once().returns { "key1=value1\n" }
+    @session.should_receive(:write).with("ACK key1\n").once().returns { |request| request.length }
+    @session.should_receive(:readline).once().returns { "key2=value2\n" }
+    @session.should_receive(:write).with("ACK key2\n").once().returns { |request| request.length }
+    @session.should_receive(:readline).once().returns { "ENDINFO\n" }
+
+    info = @browser.get_remote_info
+
+    assert_equal 3,info.keys.size, "Should contain two keys"
+    assert info.include?("IPADDR")
+    assert info.include?("key1")
+    assert info.include?("key2")
+  end
+
+  # Ensures the host browser generates a keytab as expected.
+  #
+  def test_create_keytab
+    @krb5.should_receive(:get_default_realm).once().returns { "ovirt-test-realm" }
+
+    result = @browser.create_keytab(@host_info, at krb5)
+
+    assert_equal @browser.keytab_filename, result, "Should have returned the keytab filename"
+  end
+
+  # Ensures that, if no UUID is present, the server raises an exception.
+  #
+  def test_write_host_info_with_missing_uuid
+    @host_info['UUID'] = nil
+
+    assert_raise(Exception) { @browser.write_host_info(@host_info) }
+  end
+
+  # Ensures that, if the hostname is missing, the server
+  # raises an exception.
+  #
+  def test_write_host_info_with_missing_hostname
+    @host_info['HOSTNAME'] = nil
+
+    assert_raise(Exception) { @browser.write_host_info(@host_info) }
+  end
+
+  # Ensures that, if the number of CPUs is missing, the server raises an exception.
+  #
+  def test_write_host_info_with_missing_numcpus
+    @host_info['NUMCPUS'] = nil
+
+    assert_raise(Exception) { @browser.write_host_info(@host_info) }
+  end
+
+  # Ensures that, if the CPU speed is missing, the server raises an exception.
+  #
+  def test_write_host_info_with_missing_cpuspeed
+    @host_info['CPUSPEED'] = nil
+
+    assert_raise(Exception) { @browser.write_host_info(@host_info) }
+  end
+
+  # Ensures that, if the architecture is missing, the server raises an exception.
+  #
+  def test_write_host_info_with_missing_arch
+    @host_info['ARCH'] = nil
+
+    assert_raise(Exception) { @browser.write_host_info(@host_info) }
+  end
+
+  # Ensures that, if the memory size is missing, the server raises an exception.
+  #
+  def test_write_host_info_info_with_missing_memsize
+    @host_info['MEMSIZE'] = nil
+
+    assert_raise(Exception) { @browser.write_host_info(@host_info) }
+  end
+
+  # Ensures that, if a keytab is present and a key version number available,
+  # the server ends the conversation by returning the key version number.
+  #
+  def test_end_conversation
+    @session.should_receive(:write).with("KTAB 12345\n").once().returns { |request| request.length }
+    @session.should_receive(:readline).once().returns { "ACK\n" }
+    @session.should_receive(:write).with("BYE\n").once().returns { |request| request.length }
+
+    assert_nothing_raised(Exception) { @browser.end_conversation(12345) }
+  end
+
+end
diff --git a/wui/src/host-browser/test-host-browser.rb b/wui/src/host-browser/test-host-browser.rb
deleted file mode 100755
index 6f4c660..0000000
--- a/wui/src/host-browser/test-host-browser.rb
+++ /dev/null
@@ -1,204 +0,0 @@
-#!/usr/bin/ruby -Wall
-#
-# Copyright (C) 2008 Red Hat, Inc.
-# Written by Darryl L. Pierce <dpierce at redhat.com>
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; version 2 of the License.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-# MA  02110-1301, USA.  A copy of the GNU General Public License is
-# also available at http://www.gnu.org/copyleft/gpl.html.
-
-require File.dirname(__FILE__) + '/../test/test_helper'
-require 'test/unit'
-require 'flexmock/test_unit'
-
-TESTING=true
-
-require 'host-browser'
-
-class TestHostBrowser < Test::Unit::TestCase
-
-  def setup
-    @session = flexmock('session')
-    @session.should_receive(:peeraddr).at_least.once.returns { [nil,nil,nil,"192.168.2.255"] }
-
-    @krb5 = flexmock('krb5')
-
-    @browser = HostBrowser.new(@session)
-    @browser.logfile = './unit-test.log'
-    @browser.keytab_dir = '/var/temp/'
-
-    # default host info
-    @host_info = {}
-    @host_info['UUID']     = 'node1'
-    @host_info['IPADDR']   = '192.168.2.2'
-    @host_info['HOSTNAME'] = 'node1.ovirt.redhat.com'
-    @host_info['NUMCPUS']  = '3'
-    @host_info['CPUSPEED'] = '3'
-    @host_info['ARCH']     = 'x86_64'
-    @host_info['MEMSIZE']  = '16384'
-    @host_info['DISABLED'] = '0'
-  end
-
-  # Ensures that the server raises an exception when it receives an
-  # improper handshake response.
-  #
-  def test_begin_conversation_with_improper_response_to_greeting
-    @session.should_receive(:write).with("HELLO?\n").once().returns { |greeting| greeting.length }
-    @session.should_receive(:readline).once().returns { "SUP?" }
-
-    assert_raise(Exception) { @browser.begin_conversation }
-  end
-
-  # Ensures the server accepts a proper response from the remote system.
-  #
-  def test_begin_conversation
-    @session.should_receive(:write).with("HELLO?\n").once().returns { |greeting| greeting.length }
-    @session.should_receive(:readline).once().returns { "HELLO!\n" }
-
-    assert_nothing_raised(Exception) { @browser.begin_conversation }
-  end
-
-  # Ensures that the server raises an exception when it receives
-  # poorly formed data while exchanging system information.
-  #
-  def test_get_info_with_bad_handshake
-    @session.should_receive(:write).with("INFO?\n").once().returns { |request| request.length }
-    @session.should_receive(:readline).once().returns { "key1=value1\n" }
-    @session.should_receive(:write).with("ACK key1\n").once().returns { |request| request.length }
-    @session.should_receive(:readline).once().returns { "farkledina\n" }
-
-    assert_raise(Exception) { @browser.get_remote_info }
-  end
-
-  # Ensures that, if an info field is missing a key, the server raises
-  # an exception.
-  #
-  def test_get_info_with_missing_key
-    @session.should_receive(:write).with("INFO?\n").once().returns { |request| request.length }
-    @session.should_receive(:readline).once().returns { "=value1\n" }
-
-    assert_raise(Exception) { @browser.get_remote_info }
-  end
-
-  # Ensures that, if an info field is missing a value, the server raises
-  # an exception.
-  #
-  def test_get_info_with_missing_value
-    @session.should_receive(:write).with("INFO?\n").once().returns { |request| request.length }
-    @session.should_receive(:readline).once().returns { "key1=\n" }
-
-    assert_raise(Exception) { @browser.get_remote_info }
-  end
-
-  # Ensures that, if the server gets a poorly formed ending statement, it
-  # raises an exception.
-  #
-  def test_get_info_with_invalid_end
-    @session.should_receive(:write).with("INFO?\n").once().returns { |request| request.length }
-    @session.should_receive(:readline).once().returns { "key1=value1\n" }
-    @session.should_receive(:write).with("ACK key1\n").once().returns { |request| request.length }
-    @session.should_receive(:readline).once().returns { "ENDIFNO\n" }
-
-    assert_raise(Exception) { @browser.get_remote_info }
-  end
-
-  # Ensures that a well-formed transaction works as expected.
-  #
-  def test_get_info
-    @session.should_receive(:write).with("INFO?\n").once().returns { |request| request.length }
-    @session.should_receive(:readline).once().returns { "key1=value1\n" }
-    @session.should_receive(:write).with("ACK key1\n").once().returns { |request| request.length }
-    @session.should_receive(:readline).once().returns { "key2=value2\n" }
-    @session.should_receive(:write).with("ACK key2\n").once().returns { |request| request.length }
-    @session.should_receive(:readline).once().returns { "ENDINFO\n" }
-
-    info = @browser.get_remote_info
-
-    assert_equal 3,info.keys.size, "Should contain two keys"
-    assert info.include?("IPADDR")
-    assert info.include?("key1")
-    assert info.include?("key2")
-  end
-
-  # Ensures the host browser generates a keytab as expected.
-  #
-  def test_create_keytab
-    @krb5.should_receive(:get_default_realm).once().returns { "ovirt-test-realm" }
-
-    result = @browser.create_keytab(@host_info, at krb5)
-
-    assert_equal @browser.keytab_filename, result, "Should have returned the keytab filename"
-  end
-
-  # Ensures that, if no UUID is present, the server raises an exception.
-  #
-  def test_write_host_info_with_missing_uuid
-    @host_info['UUID'] = nil
-
-    assert_raise(Exception) { @browser.write_host_info(@host_info) }
-  end
-
-  # Ensures that, if the hostname is missing, the server
-  # raises an exception.
-  #
-  def test_write_host_info_with_missing_hostname
-    @host_info['HOSTNAME'] = nil
-
-    assert_raise(Exception) { @browser.write_host_info(@host_info) }
-  end
-
-  # Ensures that, if the number of CPUs is missing, the server raises an exception.
-  #
-  def test_write_host_info_with_missing_numcpus
-    @host_info['NUMCPUS'] = nil
-
-    assert_raise(Exception) { @browser.write_host_info(@host_info) }
-  end
-
-  # Ensures that, if the CPU speed is missing, the server raises an exception.
-  #
-  def test_write_host_info_with_missing_cpuspeed
-    @host_info['CPUSPEED'] = nil
-
-    assert_raise(Exception) { @browser.write_host_info(@host_info) }
-  end
-
-  # Ensures that, if the architecture is missing, the server raises an exception.
-  #
-  def test_write_host_info_with_missing_arch
-    @host_info['ARCH'] = nil
-
-    assert_raise(Exception) { @browser.write_host_info(@host_info) }
-  end
-
-  # Ensures that, if the memory size is missing, the server raises an exception.
-  #
-  def test_write_host_info_info_with_missing_memsize
-    @host_info['MEMSIZE'] = nil
-
-    assert_raise(Exception) { @browser.write_host_info(@host_info) }
-  end
-
-  # Ensures that, if a keytab is present and a key version number available,
-  # the server ends the conversation by returning the key version number.
-  #
-  def test_end_conversation
-    @session.should_receive(:write).with("KTAB 12345\n").once().returns { |request| request.length }
-    @session.should_receive(:readline).once().returns { "ACK\n" }
-    @session.should_receive(:write).with("BYE\n").once().returns { |request| request.length }
-
-    assert_nothing_raised(Exception) { @browser.end_conversation(12345) }
-  end
-
-end
-- 
1.5.4.1




More information about the ovirt-devel mailing list