def test_ambari_config_get(self): config = AmbariConfig() #default self.assertEqual(config.get("security", "keysdir"), "/tmp/ambari-agent") #non-default config.set("security", "keysdir", "/tmp/non-default-path") self.assertEqual(config.get("security", "keysdir"), "/tmp/non-default-path") #whitespace handling config.set("security", "keysdir", " /tmp/non-stripped") self.assertEqual(config.get("security", "keysdir"), "/tmp/non-stripped")
def test_registration_build(self, get_os_version_mock, get_os_type_mock, run_os_cmd_mock): config = AmbariConfig().getConfig() tmpdir = tempfile.gettempdir() config.set('agent', 'prefix', tmpdir) config.set('agent', 'current_ping_port', '33777') get_os_type_mock.return_value = "suse" get_os_version_mock.return_value = "11" run_os_cmd_mock.return_value = (3, "", "") ver_file = os.path.join(tmpdir, "version") with open(ver_file, "w") as text_file: text_file.write("1.3.0") register = Register(config) data = register.build(1) #print ("Register: " + pprint.pformat(data)) self.assertEquals(len(data['hardwareProfile']) > 0, True, "hardwareProfile should contain content") self.assertEquals(data['hostname'] != "", True, "hostname should not be empty") self.assertEquals(data['publicHostname'] != "", True, "publicHostname should not be empty") self.assertEquals(data['responseId'], 1) self.assertEquals(data['timestamp'] > 1353678475465L, True, "timestamp should not be empty") self.assertEquals(len(data['agentEnv']) > 0, True, "agentEnv should not be empty") self.assertEquals(data['agentVersion'], '1.3.0', "agentVersion should not be empty") print data['agentEnv']['umask'] self.assertEquals(not data['agentEnv']['umask']== "", True, "agents umask should not be empty") self.assertEquals(data['currentPingPort'] == 33777, True, "current ping port should be 33777") self.assertEquals(data['prefix'], config.get('agent', 'prefix'), 'The prefix path does not match') self.assertEquals(len(data), 9) os.remove(ver_file)
def test_registration_build(self, get_os_version_mock, get_os_type_mock, run_os_cmd_mock): config = AmbariConfig().getConfig() tmpdir = tempfile.gettempdir() config.set('agent', 'prefix', tmpdir) config.set('agent', 'current_ping_port', '33777') get_os_type_mock.return_value = "suse" get_os_version_mock.return_value = "11" run_os_cmd_mock.return_value = (3, "", "") register = Register(config) reference_version = '2.1.0' data = register.build(reference_version, 1) #print ("Register: " + pprint.pformat(data)) self.assertEquals( len(data['hardwareProfile']) > 0, True, "hardwareProfile should contain content") self.assertEquals(data['hostname'] != "", True, "hostname should not be empty") self.assertEquals(data['publicHostname'] != "", True, "publicHostname should not be empty") self.assertEquals(data['responseId'], 1) self.assertEquals(data['timestamp'] > 1353678475465L, True, "timestamp should not be empty") self.assertEquals( len(data['agentEnv']) > 0, True, "agentEnv should not be empty") self.assertEquals(data['agentVersion'], reference_version, "agentVersion should not be empty") print data['agentEnv']['umask'] self.assertEquals(not data['agentEnv']['umask'] == "", True, "agents umask should not be empty") self.assertEquals(data['currentPingPort'] == 33777, True, "current ping port should be 33777") self.assertEquals(data['prefix'], config.get('agent', 'prefix'), 'The prefix path does not match') self.assertEquals(len(data), 9)
def test_registration_build(self, get_os_version_mock, get_os_family_mock, get_os_type_mock, run_os_cmd_mock, Popen_mock): config = AmbariConfig() tmpdir = tempfile.gettempdir() config.set('agent', 'prefix', tmpdir) config.set('agent', 'current_ping_port', '33777') get_os_family_mock.return_value = "suse" get_os_type_mock.return_value = "suse" get_os_version_mock.return_value = "11" run_os_cmd_mock.return_value = (3, "", "") from ambari_agent.Register import Register register = Register(config) data = register.build() self.assertEquals( len(data['hardwareProfile']) > 0, True, "hardwareProfile should contain content") self.assertEquals(data['hostname'] != "", True, "hostname should not be empty") self.assertEquals(data['publicHostname'] != "", True, "publicHostname should not be empty") self.assertEquals(data['id'], -1) self.assertEquals(data['timestamp'] > 1353678475465L, True, "timestamp should not be empty") self.assertEquals( len(data['agentEnv']) > 0, True, "agentEnv should not be empty") self.assertEquals(not data['agentEnv']['umask'] == "", True, "agents umask should not be empty") self.assertEquals(data['prefix'], config.get('agent', 'prefix'), 'The prefix path does not match') self.assertEquals(len(data), 10)
def test_RetryAction(self): action={'id' : 'tttt'} config = AmbariConfig().getConfig() actionQueue = ActionQueue(config) path = actionQueue.getInstallFilename(action['id']) configFile = { "data" : "test", "owner" : os.getuid(), "group" : os.getgid() , "permission" : 0700, "path" : path, "umask" : 022 } #note that the command in the action is just a listing of the path created #we just want to ensure that 'ls' can run on the data file (in the actual world #this 'ls' would be a puppet or a chef command that would work on a data #file badAction = { 'id' : 'tttt', 'kind' : 'INSTALL_AND_CONFIG_ACTION', 'workDirComponent' : 'abc-hdfs', 'file' : configFile, 'clusterDefinitionRevision' : 12, 'command' : ['/bin/ls',"/foo/bar/badPath1234"] } path=getFilePath(action,path) goodAction = { 'id' : 'tttt', 'kind' : 'INSTALL_AND_CONFIG_ACTION', 'workDirComponent' : 'abc-hdfs', 'file' : configFile, 'clusterDefinitionRevision' : 12, 'command' : ['/bin/ls',path] } actionQueue.start() response = {'actions' : [badAction,goodAction]} actionQueue.maxRetries = 2 actionQueue.sleepInterval = 1 result = actionQueue.put(response) results = actionQueue.result() sleptCount = 1 while (len(results) < 2 and sleptCount < 15): time.sleep(1) sleptCount += 1 results = actionQueue.result() actionQueue.stop() actionQueue.join() self.assertEqual(len(results), 2, 'Number of results is not 2.') result = results[0] maxretries = config.get('command', 'maxretries') self.assertEqual(int(result['retryActionCount']), int(maxretries), "Number of retries is %d and not %d" % (int(result['retryActionCount']), int(str(maxretries)))) result = results[1] self.assertEqual(int(result['retryActionCount']), 1, "Number of retries is %d and not %d" % (int(result['retryActionCount']), 1))
def test_server_hostname(self): hostname.cached_server_hostname = None config = AmbariConfig() default_server_hostname = config.get('server', 'hostname') config.set('server', 'hostname', 'ambari-host') self.assertEquals('ambari-host', hostname.server_hostname(config), "hostname should equal the socket-based hostname") config.set('server', 'hostname', default_server_hostname) pass
def test_ambari_config_get(self): config = AmbariConfig() #default self.assertEqual(config.get("security", "keysdir"), "/tmp/ambari-agent") #non-default config.set("security", "keysdir", "/tmp/non-default-path") self.assertEqual(config.get("security", "keysdir"), "/tmp/non-default-path") #whitespace handling config.set("security", "keysdir", " /tmp/non-stripped") self.assertEqual(config.get("security", "keysdir"), "/tmp/non-stripped") # test default value open_files_ulimit = config.get_ulimit_open_files() self.assertEqual(open_files_ulimit, 0) # set a value open_files_ulimit = 128000 config.set_ulimit_open_files(open_files_ulimit) self.assertEqual(config.get_ulimit_open_files(), open_files_ulimit)
def test_server_hostnames(self): hostname.cached_server_hostnames = [] config = AmbariConfig() default_server_hostname = config.get('server', 'hostname') config.set('server', 'hostname', 'ambari-host') server_hostnames = hostname.server_hostnames(config) self.assertEquals(['ambari-host'], server_hostnames, "expected host name ['ambari-host']; got {0}".format(server_hostnames)) config.set('server', 'hostname', default_server_hostname) pass
def test_server_hostnames(self): hostname.cached_server_hostnames = [] config = AmbariConfig() default_server_hostname = config.get('server', 'hostname') config.set('server', 'hostname', 'ambari-host') server_hostnames = hostname.server_hostnames(config) self.assertEquals(['ambari-host'], server_hostnames, "expected host name ['ambari-host']; got {0}".format( server_hostnames)) config.set('server', 'hostname', default_server_hostname) pass
def test_registration_build(self, get_os_version_mock, get_os_type_mock, run_os_cmd_mock, Popen_mock): config = AmbariConfig().getConfig() tmpdir = tempfile.gettempdir() config.set('agent', 'prefix', tmpdir) config.set('agent', 'current_ping_port', '33777') get_os_type_mock.return_value = "suse" get_os_version_mock.return_value = "11" run_os_cmd_mock.return_value = (3, "", "") register = Register(config) reference_version = '2.1.0' data = register.build(reference_version, 1) self.assertEquals(len(data['hardwareProfile']) > 0, True, "hardwareProfile should contain content") self.assertEquals(data['hostname'] != "", True, "hostname should not be empty") self.assertEquals(data['publicHostname'] != "", True, "publicHostname should not be empty") self.assertEquals(data['responseId'], 1) self.assertEquals(data['timestamp'] > 1353678475465L, True, "timestamp should not be empty") self.assertEquals(len(data['agentEnv']) > 0, True, "agentEnv should not be empty") self.assertEquals(data['agentVersion'], reference_version, "agentVersion should not be empty") print data['agentEnv']['umask'] self.assertEquals(not data['agentEnv']['umask']== "", True, "agents umask should not be empty") self.assertEquals(data['currentPingPort'] == 33777, True, "current ping port should be 33777") self.assertEquals(data['prefix'], config.get('agent', 'prefix'), 'The prefix path does not match') self.assertEquals(len(data), 9)
def test_osdisks_no_ignore_property(self, shell_call_mock, isfile_mock, chk_writable_mount_mock): df_output = \ """Filesystem Type 1024-blocks Used Available Capacity Mounted on /dev/mapper/docker-253:0-4980899-d45c264d37ab18c8ed14f890f4d59ac2b81e1c52919eb36a79419787209515f3 xfs 31447040 1282384 30164656 5% / """ isfile_mock.return_value = False chk_writable_mount_mock.return_value = True shell_call_mock.return_value = (0, df_output, '') config = AmbariConfig() # check, that config do not define ignore_mount_points property self.assertEquals( "test", config.get('agent', 'ignore_mount_points', default="test")) result = Hardware(config=config, cache_info=False).osdisks() self.assertEquals(1, len(result)) expected_mounts_left = ["/"] mounts_left = [item["mountpoint"] for item in result] self.assertEquals(expected_mounts_left, mounts_left)
class TestSecurity(unittest.TestCase): @patch.object(OSCheck, "os_distribution", new=MagicMock(return_value=os_distro_value)) def setUp(self): # disable stdout out = StringIO.StringIO() sys.stdout = out # Create config self.config = AmbariConfig() self.config.set('security', 'ssl_verify_cert', '0') # Instantiate CachedHTTPSConnection (skip connect() call) with patch.object(security.VerifiedHTTPSConnection, "connect"): self.cachedHTTPSConnection = security.CachedHTTPSConnection( self.config, "example.com") def tearDown(self): # enable stdout sys.stdout = sys.__stdout__ ### VerifiedHTTPSConnection ### @patch.object(security.CertificateManager, "initSecurity") @patch("socket.create_connection") @patch("ssl.wrap_socket") def test_VerifiedHTTPSConnection_connect(self, wrap_socket_mock, create_connection_mock, init_security_mock): init_security_mock.return_value = None self.config.set('security', 'keysdir', '/dummy-keysdir') connection = security.VerifiedHTTPSConnection( "example.com", self.config.get('server', 'secured_url_port'), self.config) connection._tunnel_host = False connection.sock = None connection.connect() self.assertTrue(wrap_socket_mock.called) ### VerifiedHTTPSConnection with no certificates creation @patch.object(security.CertificateManager, "initSecurity") @patch("socket.create_connection") @patch("ssl.wrap_socket") def test_Verified_HTTPSConnection_non_secure_connect( self, wrap_socket_mock, create_connection_mock, init_security_mock): connection = security.VerifiedHTTPSConnection( "example.com", self.config.get('server', 'secured_url_port'), self.config) connection._tunnel_host = False connection.sock = None connection.connect() self.assertFalse(init_security_mock.called) ### VerifiedHTTPSConnection with two-way SSL authentication enabled @patch.object(security.CertificateManager, "initSecurity") @patch("socket.create_connection") @patch("ssl.wrap_socket") def test_Verified_HTTPSConnection_two_way_ssl_connect( self, wrap_socket_mock, create_connection_mock, init_security_mock): wrap_socket_mock.side_effect = ssl.SSLError() connection = security.VerifiedHTTPSConnection( "example.com", self.config.get('server', 'secured_url_port'), self.config) self.config.isTwoWaySSLConnection = MagicMock(return_value=True) connection._tunnel_host = False connection.sock = None try: connection.connect() except ssl.SSLError: pass self.assertTrue(init_security_mock.called) ### CachedHTTPSConnection ### @patch.object(security.VerifiedHTTPSConnection, "connect") def test_CachedHTTPSConnection_connect(self, vhc_connect_mock): self.config.set('server', 'hostname', 'dummy.server.hostname') self.config.set('server', 'secured_url_port', '443') # Testing not connected case self.cachedHTTPSConnection.connected = False self.cachedHTTPSConnection.connect() self.assertTrue(vhc_connect_mock.called) vhc_connect_mock.reset_mock() # Testing already connected case self.cachedHTTPSConnection.connect() self.assertFalse(vhc_connect_mock.called) @patch.object(security.CachedHTTPSConnection, "connect") def test_forceClear(self, connect_mock): # Testing if httpsconn instance changed old = self.cachedHTTPSConnection.httpsconn self.cachedHTTPSConnection.forceClear() self.assertNotEqual(old, self.cachedHTTPSConnection.httpsconn) @patch.object(security.CachedHTTPSConnection, "connect") def test_request(self, connect_mock): httpsconn_mock = MagicMock(create=True) self.cachedHTTPSConnection.httpsconn = httpsconn_mock dummy_request = MagicMock(create=True) dummy_request.get_method.return_value = "dummy_get_method" dummy_request.get_full_url.return_value = "dummy_full_url" dummy_request.get_data.return_value = "dummy_get_data" dummy_request.headers = "dummy_headers" responce_mock = MagicMock(create=True) responce_mock.read.return_value = "dummy responce" httpsconn_mock.getresponse.return_value = responce_mock # Testing normal case responce = self.cachedHTTPSConnection.request(dummy_request) self.assertEqual(responce, responce_mock.read.return_value) httpsconn_mock.request.assert_called_once_with( dummy_request.get_method.return_value, dummy_request.get_full_url.return_value, dummy_request.get_data.return_value, dummy_request.headers) # Testing case of exception try: def side_eff(): raise Exception("Dummy exception") httpsconn_mock.read.side_effect = side_eff responce = self.cachedHTTPSConnection.request(dummy_request) self.fail("Should raise IOError") except Exception, err: # Expected pass
class TestSecurity(unittest.TestCase): def setUp(self): # disable stdout out = StringIO.StringIO() sys.stdout = out # Create config self.config = AmbariConfig().getConfig() # Instantiate CachedHTTPSConnection (skip connect() call) with patch.object(security.VerifiedHTTPSConnection, "connect"): self.cachedHTTPSConnection = security.CachedHTTPSConnection(self.config) def tearDown(self): # enable stdout sys.stdout = sys.__stdout__ ### VerifiedHTTPSConnection ### @patch.object(security.CertificateManager, "initSecurity") @patch("socket.create_connection") @patch("ssl.wrap_socket") def test_VerifiedHTTPSConnection_connect(self, wrap_socket_mock, create_connection_mock, init_security_mock): init_security_mock.return_value = None self.config.set('security', 'keysdir', '/dummy-keysdir') connection = security.VerifiedHTTPSConnection("example.com", self.config.get('server', 'secured_url_port'), self.config) connection._tunnel_host = False connection.sock = None connection.connect() self.assertTrue(wrap_socket_mock.called) ### VerifiedHTTPSConnection with no certificates creation @patch.object(security.CertificateManager, "initSecurity") @patch("socket.create_connection") @patch("ssl.wrap_socket") def test_Verified_HTTPSConnection_non_secure_connect(self, wrap_socket_mock, create_connection_mock, init_security_mock): connection = security.VerifiedHTTPSConnection("example.com", self.config.get('server', 'secured_url_port'), self.config) connection._tunnel_host = False connection.sock = None connection.connect() self.assertFalse(init_security_mock.called) ### VerifiedHTTPSConnection with two-way SSL authentication enabled @patch.object(security.CertificateManager, "initSecurity") @patch("socket.create_connection") @patch("ssl.wrap_socket") def test_Verified_HTTPSConnection_two_way_ssl_connect(self, wrap_socket_mock, create_connection_mock, init_security_mock): wrap_socket_mock.side_effect=ssl.SSLError() connection = security.VerifiedHTTPSConnection("example.com", self.config.get('server', 'secured_url_port'), self.config) connection._tunnel_host = False connection.sock = None try: connection.connect() except ssl.SSLError: pass self.assertTrue(init_security_mock.called) ### CachedHTTPSConnection ### @patch.object(security.VerifiedHTTPSConnection, "connect") def test_CachedHTTPSConnection_connect(self, vhc_connect_mock): self.config.set('server', 'hostname', 'dummy.server.hostname') self.config.set('server', 'secured_url_port', '443') # Testing not connected case self.cachedHTTPSConnection.connected = False self.cachedHTTPSConnection.connect() self.assertTrue(vhc_connect_mock.called) vhc_connect_mock.reset_mock() # Testing already connected case self.cachedHTTPSConnection.connect() self.assertFalse(vhc_connect_mock.called) @patch.object(security.CachedHTTPSConnection, "connect") def test_forceClear(self, connect_mock): # Testing if httpsconn instance changed old = self.cachedHTTPSConnection.httpsconn self.cachedHTTPSConnection.forceClear() self.assertNotEqual(old, self.cachedHTTPSConnection.httpsconn) @patch.object(security.CachedHTTPSConnection, "connect") def test_request(self, connect_mock): httpsconn_mock = MagicMock(create = True) self.cachedHTTPSConnection.httpsconn = httpsconn_mock dummy_request = MagicMock(create = True) dummy_request.get_method.return_value = "dummy_get_method" dummy_request.get_full_url.return_value = "dummy_full_url" dummy_request.get_data.return_value = "dummy_get_data" dummy_request.headers = "dummy_headers" responce_mock = MagicMock(create = True) responce_mock.read.return_value = "dummy responce" httpsconn_mock.getresponse.return_value = responce_mock # Testing normal case responce = self.cachedHTTPSConnection.request(dummy_request) self.assertEqual(responce, responce_mock.read.return_value) httpsconn_mock.request.assert_called_once_with( dummy_request.get_method.return_value, dummy_request.get_full_url.return_value, dummy_request.get_data.return_value, dummy_request.headers) # Testing case of exception try: def side_eff(): raise Exception("Dummy exception") httpsconn_mock.read.side_effect = side_eff responce = self.cachedHTTPSConnection.request(dummy_request) self.fail("Should raise IOError") except Exception, err: # Expected pass