def test_getboolean(self): '''test libaimdns.getboolean_property interface ''' prop = 'test/bool' value = True setprop = 'setprop %s = boolean: %s' % (prop, str(value).lower()) newvalues = self.SetSCFValue([setprop]) assert getboolean_property(self.svc, prop) == value, \ 'boolean property does not match' del(newvalues) try: rtn = getboolean_property(self.bogus_svc, prop) except aiMDNSError, err: assert err == 'entity not found', \ 'boolean returned for bogus_svc'
def _register_a_service(self, name, interfaces=None, port=0, comments=None): '''Method: _register_a_service, private to class Description: Register a single service on the interfaces Args interfaces - the interfaces to register the service on instance - the SMF service instance handle name - the service name to be registered port - the port that the service is listening on, if port is 0 then registering a service listed in the AI SMF service instance. comments - comments for the ad hoc registered service Returns list_sdrefs - list of service references Raises AImDNSError - if SMF status property does not exist, OR if SMF txt_record property does not exist, OR if SMF port property does not exist. ''' if not self.register_initialized: self.exclude = libaimdns.getboolean_property(common.SRVINST, common.EXCLPROP) self.networks = libaimdns.getstrings_property(common.SRVINST, common.NETSPROP) self.register_initialized = True smf_port = None # if port is 0 then processing an AI service if port == 0: serv = config.get_service_props(name) if not serv: raise AIMDNSError(cw(_('error: aiMDNSError: no such ' 'installation service "%s"') % name)) # ensure the service is enabled if config.PROP_STATUS not in serv: raise AIMDNSError(cw(_('error: aiMDNSError: installation ' 'service key "status" property does ' 'not exist'))) if serv[config.PROP_STATUS] != config.STATUS_ON: print(cw(_('warning: Installation service "%s" is not enabled ' % name))) return None smf_port = config.get_service_port(name) if not smf_port: try: smf_port = libaimdns.getinteger_property(common.SRVINST, common.PORTPROP) smf_port = str(smf_port) except libaimdns.aiMDNSError, err: raise AIMDNSError(cw(_('error: aiMDNSError: port property ' 'failure (%s)') % err))
def get_valid_networks(): '''Description: Gets the valid networks taking into account the all_services/networks and all_services/exclude_networks service properties. Args: None Returns: a set of valid networks. Raises: None ''' # get the currently configured interfaces interfaces = getifaddrs() # get the exclude and networks service property values exclude = getboolean_property(SRVINST, EXCLPROP) networks = getstrings_property(SRVINST, NETSPROP) valid_networks = set() for inf in interfaces: # check the interface IP address against those listed in # the AI service SMF networks property. Our logic for the # SMF exclude_networks and SMF networks list is: # # IF ipv4 is in networks and # SMF exclude_networks == false # THEN include ipv4 # IF ipv4 is not in_networks and # SMF exclude_network == true # THEN include ipv4 # IF ipv4 is in_networks and # SMF exclude_networks == true # THEN exclude ipv4 # IF ipv4 is not in_networks and # SMF exclude_network == false # THEN exclude ipv4 # # Assume that it is excluded and check the first 2 conditions only # as the last 2 conditions are covered by the assumption. in_net = in_networks(interfaces[inf], networks) include_it = False if (in_net and not exclude) or (not in_net and exclude): include_it = True if not include_it: continue mask = interfaces[inf].find('/') if mask == -1: mask = len(interfaces[inf]) valid_networks.add(interfaces[inf][:mask]) return valid_networks
def _register_a_service(self, name, interfaces=None, port=0, comments=None): '''Method: _register_a_service, private to class Description: Register a single service on the interfaces Args interfaces - the interfaces to register the service on instance - the SMF service instance handle name - the service name to be registered port - the port that the service is listening on, if port is 0 then registering a service listed in the AI SMF service instance. comments - comments for the ad hoc registered service Returns list_sdrefs - list of service references Raises AImDNSError - if SMF status property does not exist, OR if SMF txt_record property does not exist, OR if SMF port property does not exist. ''' if not self.register_initialized: self.exclude = libaimdns.getboolean_property( common.SRVINST, common.EXCLPROP) self.networks = libaimdns.getstrings_property( common.SRVINST, common.NETSPROP) self.register_initialized = True smf_port = None # if port is 0 then processing an AI service if port == 0: serv = config.get_service_props(name) if not serv: raise AIMDNSError( cw( _('error: aiMDNSError: no such ' 'installation service "%s"') % name)) # ensure the service is enabled if config.PROP_STATUS not in serv: raise AIMDNSError( cw( _('error: aiMDNSError: installation ' 'service key "status" property does ' 'not exist'))) if serv[config.PROP_STATUS] != config.STATUS_ON: print( cw( _('warning: Installation service "%s" is not enabled ' % name))) return None smf_port = config.get_service_port(name) if not smf_port: try: smf_port = libaimdns.getinteger_property( common.SRVINST, common.PORTPROP) smf_port = str(smf_port) except libaimdns.aiMDNSError, err: raise AIMDNSError( cw( _('error: aiMDNSError: port property ' 'failure (%s)') % err))
class TestLibaimdnsSCF(unittest.TestCase): '''Class: TestLibaimdnsSCF - tests the libaimdns SCF interfaces ''' svc = 'svc:/system/install/server:default' svccfg = '/usr/sbin/svccfg' bogus_svc = 'svc:/system/install/server2:default' bogus_prop = 'config/dummy' class SetSCFValue(object): '''Class SetSCFValue - sets the SCF property value via svccfg ''' svc = 'svc:/system/install/server:default' svccfg = '/usr/sbin/svccfg' def __init__(self, props): self.cmdsname = '/tmp/svctest.cmds' cmds = open(self.cmdsname, 'w+') cmds.write('addpg test application\n') for prop in props: cmds.write('%s\n' % prop) cmds.close() proc = subprocess.Popen([self.svccfg, "-s", self.svc, "-f", self.cmdsname], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stderr = proc.communicate()[1] if stderr: print 'Error:', stderr def __del__(self): proc = subprocess.Popen([self.svccfg, "-s", self.svc, 'delpg test']) stderr = proc.communicate()[1] if stderr: print 'Error:', stderr os.remove(self.cmdsname) def test_getstring(self): '''test libaimdns.getstring_property interface ''' prop = 'test/astr' value = 'howdy' setprop = 'setprop %s = astring: "%s"' % (prop, value) newvalues = self.SetSCFValue([setprop]) assert getstring_property(self.svc, prop) == value, \ 'string does not match failure' del(newvalues) try: assert getstring_property(self.bogus_svc, prop) == None, \ 'string returned for bogus_svc' except aiMDNSError: pass try: assert getstring_property(self.svc, self.bogus_prop) == None, \ 'string returned for bogus_prop' except aiMDNSError: pass def test_getstrings(self): '''test libaimdns.getstrings_property interface ''' prop = 'test/strs' value = ['first', 'second'] setprop = ['addpropvalue %s astring: "%s"' % (prop, value[0])] setprop.append('addpropvalue %s "%s"' % (prop, value[1])) newvalues = self.SetSCFValue(setprop) assert getstrings_property(self.svc, prop) == value, \ 'strings value does not match failure' del(newvalues) try: assert getstrings_property(self.bogus_svc, prop) == None, \ 'strings returned for bogus_svc' except aiMDNSError: pass try: assert getstrings_property(self.svc, self.bogus_prop) == None, \ 'strings returned for bogus_prop' except aiMDNSError: pass def test_getboolean(self): '''test libaimdns.getboolean_property interface ''' prop = 'test/bool' value = True setprop = 'setprop %s = boolean: %s' % (prop, str(value).lower()) newvalues = self.SetSCFValue([setprop]) assert getboolean_property(self.svc, prop) == value, \ 'boolean property does not match' del(newvalues) try: rtn = getboolean_property(self.bogus_svc, prop) except aiMDNSError, err: assert err == 'entity not found', \ 'boolean returned for bogus_svc' try: rtn = getboolean_property(self.svc, self.bogus_prop) except aiMDNSError, err: assert err == 'entity not found', \ 'boolean returned a bogus_prop'