~wgrant/ubuntu/natty/landscape-client/natty-updates-broken

« back to all changes in this revision

Viewing changes to landscape/broker/tests/test_registration.py

  • Committer: Bazaar Package Importer
  • Author(s): Mathias Gug, Free Ekanayaka
  • Date: 2009-07-22 14:54:50 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20090722145450-pvbp13gh8734c8ft
Tags: 1.3.2.2-0ubuntu0.9.10.1
[ Free Ekanayaka ]
* New upstream release:
  - Include the README file in landscape-client (LP: #396260)
  - Fix client capturing stderr from run_command when constructing
    hash-id-databases url (LP: #397480)
  - Use substvars to conditionally depend on update-motd or
    libpam-modules (LP: #393454)
  - Fix reporting wrong version to the server (LP: #391225)
  - The init script does not wait for the network to be available
    before checking for EC2 user data (LP: #383336)
  - When the broker is restarted by the watchdog, the state of the client
    is inconsistent (LP: #380633)
  - Package stays unknown forever in the client with hash-id-databases
    support (LP: #381356)
  - Standard error not captured when calling smart-update (LP: #387441)
  - Changer calls reporter without switching groups, just user (LP: #388092)
  - Run smart update in the package-reporter instead of having a cronjob (LP: #362355)
  - Package changer does not inherit proxy settings (LP: #381241)
  - The ./test script doesn't work in landscape-client (LP: #381613)
  - The source package should build on all supported releases (LP: #385098)
  - Strip smart update's output (LP: #387331)
  - The fetch() timeout isn't based on activity (#389224)
  - Client can use a UUID of "None" when fetching the hash-id-database (LP: #381291)
  - Registration should use the fqdn rather than just the hostname (LP: #385730)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import unittest
1
2
import logging
2
3
import pycurl
3
4
import socket
5
6
from twisted.internet.defer import succeed, fail
6
7
 
7
8
from landscape.broker.registration import (
8
 
    InvalidCredentialsError, RegistrationHandler)
 
9
    InvalidCredentialsError, RegistrationHandler, is_cloud_managed, EC2_HOST,
 
10
    EC2_API)
9
11
 
10
12
from landscape.broker.deployment import BrokerConfiguration
11
13
from landscape.tests.helpers import LandscapeTest, ExchangeHelper
12
14
from landscape.lib.bpickle import dumps
13
 
from landscape.lib.fetch import HTTPCodeError
 
15
from landscape.lib.fetch import HTTPCodeError, FetchError
14
16
 
15
17
 
16
18
class RegistrationTest(LandscapeTest):
23
25
        self.identity = self.broker_service.identity
24
26
        self.handler = self.broker_service.registration
25
27
        logging.getLogger().setLevel(logging.INFO)
26
 
        self.hostname = "ooga"
27
 
        self.addCleanup(setattr, socket, "gethostname", socket.gethostname)
28
 
        socket.gethostname = lambda: self.hostname
 
28
        self.hostname = "ooga.local"
 
29
        self.addCleanup(setattr, socket, "getfqdn", socket.getfqdn)
 
30
        socket.getfqdn = lambda: self.hostname
29
31
 
30
32
    def check_persist_property(self, attr, persist_name):
31
33
        value = "VALUE"
141
143
                              "computer_title": "Computer Title",
142
144
                              "account_name": "account_name",
143
145
                              "registration_password": None,
144
 
                              "hostname": "ooga"}
 
146
                              "hostname": "ooga.local"}
145
147
                            ])
146
148
        self.assertEquals(self.logfile.getvalue().strip(),
147
149
                          "INFO: Queueing message to register with account "
159
161
                              "computer_title": "Computer Title",
160
162
                              "account_name": "account_name",
161
163
                              "registration_password": "SEKRET",
162
 
                              "hostname": "ooga"}
 
164
                              "hostname": "ooga.local"}
163
165
                            ])
164
166
        self.assertEquals(self.logfile.getvalue().strip(),
165
167
                          "INFO: Queueing message to register with account "
341
343
                              "computer_title": "Computer Title",
342
344
                              "account_name": "account_name",
343
345
                              "registration_password": "SEKRET",
344
 
                              "hostname": socket.gethostname()}
 
346
                              "hostname": socket.getfqdn()}
345
347
                             ])
346
348
 
347
349
    def get_registration_handler_for_cloud(
406
408
        """
407
409
        message = dict(type="register-cloud-vm",
408
410
                       otp="otp1",
409
 
                       hostname="ooga",
 
411
                       hostname="ooga.local",
410
412
                       local_hostname="ooga.local",
411
413
                       public_hostname="ooga.amazon.com",
412
414
                       instance_key=u"key1",
641
643
                              "computer_title": u"whatever",
642
644
                              "account_name": u"onward",
643
645
                              "registration_password": u"password",
644
 
                              "hostname": socket.gethostname()}])
 
646
                              "hostname": socket.getfqdn()}])
645
647
 
646
648
    def test_should_register_in_cloud(self):
647
649
        """
729
731
        config.computer_title = None
730
732
        self.broker_service.identity.secure_id = None
731
733
        self.assertFalse(handler.should_register())
 
734
 
 
735
 
 
736
class IsCloudManagedTests(LandscapeTest):
 
737
 
 
738
    def setUp(self):
 
739
        super(IsCloudManagedTests, self).setUp()
 
740
        self.urls = []
 
741
        self.responses = []
 
742
 
 
743
    def fake_fetch(self, url, connect_timeout=None):
 
744
        self.urls.append((url, connect_timeout))
 
745
        return self.responses.pop(0)
 
746
 
 
747
    def mock_socket(self):
 
748
        """
 
749
        Mock out socket usage by is_cloud_managed to wait for the network.
 
750
        """
 
751
        # Mock the socket.connect call that it also does
 
752
        socket_class = self.mocker.replace("socket.socket", passthrough=False)
 
753
        socket = socket_class()
 
754
        socket.connect((EC2_HOST, 80))
 
755
        socket.close()
 
756
        
 
757
 
 
758
    def test_is_managed(self):
 
759
        """
 
760
        L{is_cloud_managed} returns True if the EC2 user-data contains Landscape
 
761
        instance information.  It fetches the EC2 data with low timeouts.
 
762
        """
 
763
        user_data = {"otps": ["otp1"], "exchange-url": "http://exchange",
 
764
                     "ping-url": "http://ping"}
 
765
        self.responses = [dumps(user_data), "0"]
 
766
 
 
767
        self.mock_socket()
 
768
        self.mocker.replay()
 
769
 
 
770
        self.assertTrue(is_cloud_managed(self.fake_fetch))
 
771
        self.assertEquals(
 
772
            self.urls,
 
773
            [(EC2_API + "/user-data", 5),
 
774
             (EC2_API + "/meta-data/ami-launch-index", 5)])
 
775
 
 
776
    def test_is_managed_index(self):
 
777
        user_data = {"otps": ["otp1", "otp2"],
 
778
                     "exchange-url": "http://exchange",
 
779
                     "ping-url": "http://ping"}
 
780
        self.responses = [dumps(user_data), "1"]
 
781
        self.mock_socket()
 
782
        self.mocker.replay()
 
783
        self.assertTrue(is_cloud_managed(self.fake_fetch))
 
784
 
 
785
    def test_is_managed_wrong_index(self):
 
786
        user_data = {"otps": ["otp1"], "exchange-url": "http://exchange",
 
787
                     "ping-url": "http://ping"}
 
788
        self.responses = [dumps(user_data), "1"]
 
789
        self.mock_socket()
 
790
        self.mocker.replay()
 
791
        self.assertFalse(is_cloud_managed(self.fake_fetch))
 
792
 
 
793
    def test_is_managed_exchange_url(self):
 
794
        user_data = {"otps": ["otp1"], "ping-url": "http://ping"}
 
795
        self.responses = [dumps(user_data), "0"]
 
796
        self.mock_socket()
 
797
        self.mocker.replay()
 
798
        self.assertFalse(is_cloud_managed(self.fake_fetch))
 
799
 
 
800
    def test_is_managed_ping_url(self):
 
801
        user_data = {"otps": ["otp1"], "exchange-url": "http://exchange"}
 
802
        self.responses = [dumps(user_data), "0"]
 
803
        self.mock_socket()
 
804
        self.mocker.replay()
 
805
        self.assertFalse(is_cloud_managed(self.fake_fetch))
 
806
 
 
807
    def test_is_managed_bpickle(self):
 
808
        self.responses = ["some other user data", "0"]
 
809
        self.mock_socket()
 
810
        self.mocker.replay()
 
811
        self.assertFalse(is_cloud_managed(self.fake_fetch))
 
812
 
 
813
    def test_is_managed_no_data(self):
 
814
        self.responses = ["", "0"]
 
815
        self.mock_socket()
 
816
        self.mocker.replay()
 
817
        self.assertFalse(is_cloud_managed(self.fake_fetch))
 
818
 
 
819
    def test_is_managed_fetch_not_found(self):
 
820
        def fake_fetch(url, connect_timeout=None):
 
821
            raise HTTPCodeError(404, "ohnoes")
 
822
        self.mock_socket()
 
823
        self.mocker.replay()
 
824
        self.assertFalse(is_cloud_managed(fake_fetch))
 
825
 
 
826
    def test_is_managed_fetch_error(self):
 
827
        def fake_fetch(url, connect_timeout=None):
 
828
            raise FetchError(7, "couldn't connect to host")
 
829
        self.mock_socket()
 
830
        self.mocker.replay()
 
831
        self.assertFalse(is_cloud_managed(fake_fetch))
 
832
 
 
833
    def test_waits_for_network(self):
 
834
        """
 
835
        is_cloud_managed will wait until the network before trying to fetch
 
836
        the EC2 user data.
 
837
        """
 
838
        user_data = {"otps": ["otp1"], "exchange-url": "http://exchange",
 
839
                     "ping-url": "http://ping"}
 
840
        self.responses = [dumps(user_data), "0"]
 
841
 
 
842
        self.mocker.order()
 
843
        time_sleep = self.mocker.replace("time.sleep", passthrough=False)
 
844
        socket_class = self.mocker.replace("socket.socket", passthrough=False)
 
845
        socket_obj = socket_class()
 
846
        socket_obj.connect((EC2_HOST, 80))
 
847
        self.mocker.throw(socket.error("woops"))
 
848
        time_sleep(1)
 
849
        socket_obj = socket_class()
 
850
        socket_obj.connect((EC2_HOST, 80))
 
851
        self.mocker.result(None)
 
852
        socket_obj.close()
 
853
        self.mocker.replay()
 
854
        self.assertTrue(is_cloud_managed(self.fake_fetch))
 
855
 
 
856
    def test_waiting_times_out(self):
 
857
        """
 
858
        We'll only wait five minutes for the network to come up.
 
859
        """
 
860
        def fake_fetch(url, connect_timeout=None):
 
861
            raise FetchError(7, "couldn't connect to host")
 
862
 
 
863
        self.mocker.order()
 
864
        time_sleep = self.mocker.replace("time.sleep", passthrough=False)
 
865
        time_time = self.mocker.replace("time.time", passthrough=False)
 
866
        time_time()
 
867
        self.mocker.result(100)
 
868
        socket_class = self.mocker.replace("socket.socket", passthrough=False)
 
869
        socket_obj = socket_class()
 
870
        socket_obj.connect((EC2_HOST, 80))
 
871
        self.mocker.throw(socket.error("woops"))
 
872
        time_sleep(1)
 
873
        time_time()
 
874
        self.mocker.result(401)
 
875
        self.mocker.replay()
 
876
        # Mocking time.time is dangerous, because the test harness calls it. So
 
877
        # we explicitly reset mocker before returning from the test.
 
878
        try:
 
879
            self.assertFalse(is_cloud_managed(fake_fetch))
 
880
        finally:
 
881
            self.mocker.reset()