def test_has_same_roles(self): """ Test role set intersection. """ with settings(roledefs={'foo':['bar','baz','foo'], 'bar':['baz','foo']}): self.assertTrue(has_same_roles(['baz','foo'])) self.assertFalse(has_same_roles(['bar','foo'])) self.assertFalse(has_same_roles(['bar','baz']))
def resolve_model(parser, options): """ Ensure that a valid environment, host, and model have been defined. If no hosts are defined, attempt to derive these from the current environment. If hosts are defined, validate that these are members of the current environment. If no roles are defined, attempt to derive these from the current hosts. If roles are defined, validate that hosts have these roles. """ environment_hosts = get_hosts_for_environment(options.environment) # Validate hosts if not options.hosts: # Try using all hosts for the environment if not environment_hosts: parser.error('Unrecognized or missing environment definition: {environment}'. format(environment=options.environment)) elif not has_same_roles(environment_hosts): # Cannot use all hosts for the enviroment; roles don't match parser.error('All hosts for environment do not have the same role; ' + 'please specify a host list.') options.hosts = environment_hosts else: options.hosts = options.hosts.split(',') # Ensure that all specified hosts are part of this environment if not set(options.hosts).issubset(environment_hosts): parser.error('All hosts are not part of environment: {environment}'. format(environment=options.environment)) # Validate roles if not options.roles: # Try using all roles for hosts hosts_roles = has_same_roles(options.hosts) if not hosts_roles: parser.error('All hosts do not have the same role; please specify a role list.') options.roles = hosts_roles else: options.roles = options.roles.split(',') if not has_roles(options.hosts, options.roles): # Ensure that specified roles are valid parser.error('All hosts do not have all specified roles; please specify different roles.')