def test_attr(table=None, owner=None): """ Tests the Scoping rules for attributes. Called without arguments this test the global attributes. table = os | environment | appliance | host owner = osname | environmentname | appliancename | hostname """ if not table: table = '' if not owner: owner = '' attr = 'a.b.c.d' result = Call('remove %s attr' % table, ('%s attr=%s' % (owner, attr)).split()) assert ReturnCode() == 0 and result == [] value = str(random.randint(0, 100)) result = Call('set %s attr' % table, ('%s attr=%s value=%s' % (owner, attr, value)).split()) assert ReturnCode() == 0 and result == [] result = Call('list %s attr' % table, ('%s attr=%s' % (owner, attr)).split()) assert ReturnCode() == 0 and len(result) == 1 assert result[0]['attr'] == attr assert result[0]['value'] == value result = Call('remove %s attr' % table, ('%s attr=%s' % (owner, attr)).split()) assert ReturnCode() == 0 and result == []
def test_scale(): print('...') t0 = time.time() Call('list host') t1 = time.time() print('list host (%.3fs)' % (t1 - t0)) t0 = time.time() Call('list host profile', ['backend-1000-0']) t1 = time.time() print('list host profile (%.3fs)' % (t1 - t0))
def setup_module(module): Call('add environment %s' % ENVIRONMENT) for rack in range(1000, 1000 + NUMRACKS): for rank in range(0, RACKSIZE): host = 'backend-%d-%d' % (rack, rank) Call('add host %s environment=%s' % (host, ENVIRONMENT)) Call('set environment attr %s attr=key value=value' % ENVIRONMENT) result = Call('list host %s' % ENVIRONMENT) assert ReturnCode() == 0 and len(result) == (NUMRACKS * RACKSIZE)
def test_name_does_not_exist(): for o in [ 'appliance', 'box', 'cart', 'environment', 'host', 'network', 'os', 'pallet' ]: result = Call('list %s' % o, [DNE], stderr=False) assert ReturnCode() == 255
def setup_hosts(numHosts): rackSize = 40 t0 = time.time() rank = rack = 999 # start at rack 1000 (yeah I know this could be smarter) for i in range(0, numHosts): if rank >= rackSize: rank = 0 rack += 1 hostname = 'backend-%d-%d' % (rack, rank) Call('add host %s environment=%s' % (hostname, ENVIRONMENT)) rank += 1 t = (time.time() - t0) print('add host'.ljust(32), '%.3fs' % t) result = Call('list host e:%s' % ENVIRONMENT) assert ReturnCode() == 0 and len(result) == (numHosts)
def test_scale(): print() for size in [10, 20, 30, 40, 100, 1000]: print('size = %d' % size) setup_hosts(size) t0 = time.time() rows = Call('list host e:%s' % ENVIRONMENT) t = (time.time() - t0) print('list host'.ljust(32), '%.3fs' % t) assert rows[0]['host'] # grab the first hostname t0 = time.time() Call('list host profile', [rows[0]['host']]) t = (time.time() - t0) print('list host profile'.ljust(32), '%.3fs' % t) teardown_hosts()
def test_call(): """ Test the stack.api.Call function to verify it returns a list of dictionaries. """ found = False for dict in Call('list appliance'): if dict['appliance'] == 'backend': found = True assert str2bool(dict['public']) assert found
def getUsers(self, args=[]): """Return a list of users sorted by user id. """ authType = None for row in Call('list attr', ['attr=user.auth']): authType = row['value'] if authType == 'unix': return self.getUsers_unix(args) else: CommandError(self, 'authentication attr unset')
def test_all_commands_have_help(self): ''' test that all stack commands ship with a functional help command ''' missing = [] malformed = [] commands = Call('list.pallet.command', ['stacki']) for row in commands: command_name = row['command'] module = importlib.import_module( f"stack.commands.{command_name.replace(' ', '.')}") cmd_obj = module.Command(None) try: cmd_obj.help(command_name) except CommandError: malformed.append(command_name) continue docstring = cmd_obj.getText() if not docstring: missing.append(command_name) elif docstring == f'stack {row["command"]} \n\nDescription\n': missing.append(command_name) errors = [] if missing: errors.append('the following commands are missing docstrings: ' + ', '.join(missing)) if malformed: errors.append( 'the following commands have malformed docstrings: ' + ', '.join(malformed)) assert not any(missing + malformed), '\n'.join(errors)
def teardown_hosts(): t0 = time.time() Call('remove host e:%s' % ENVIRONMENT, stderr=False) t = (time.time() - t0) print('remove host'.ljust(32), '%.3fs' % t)
def teardown_module(module): Call('remove environment %s' % ENVIRONMENT)
def setup_module(module): Call('remove host e:%s' % ENVIRONMENT, stderr=False) Call('add environment %s' % ENVIRONMENT)
def test_box(): """ Test list/add/remove/set box. """ # Search for a unused box name and use it for # the following tests. done = False while not done: box = 'default-%s' % str(random.randint(0, 100)) result = Call('list box', [box], stderr=False) if ReturnCode() and not result: done = True assert box # add box result = Call('add box', [box]) assert ReturnCode() == 0 and result == [] # lookup current box for host result = Call('list host', ['localhost']) assert ReturnCode() == 0 and len(result) == 1 prevBox = result[0]['box'] # set box for this host result = Call('set host box', ['localhost', 'box=%s' % box]) assert ReturnCode() == 0 # verify box was set result = Call('list host', ['localhost']) assert ReturnCode() == 0 and len(result) == 1 assert result[0]['box'] == box # restore prev setting result = Call('set host box', ['localhost', 'box=%s' % prevBox]) assert ReturnCode() == 0 # remove box result = Call('remove box', [box]) assert ReturnCode() == 0 # try to remove default # "remove box" should protect against this result = Call('remove box', ['default'], stderr=False) assert ReturnCode() == 255 # remove multiple boxes # Add the first box back result = Call('add box', [box]) assert ReturnCode() == 0 and result == [] # get a second box name done = False while not done: second_box = 'default-%s' % str(random.randint(0, 100)) result = Call('list box', [second_box], stderr=False) if ReturnCode() and not result: done = True assert second_box result = Call('add box', [second_box]) assert ReturnCode() == 0 and result == [] # remove multiple boxes result = Call('remove box', [box, second_box]) assert ReturnCode() == 0
def teardown_module(module): Call('remove host e:%s' % ENVIRONMENT, stderr=False) Call('remove environment attr %s attr=*' % ENVIRONMENT) Call('remove environment %s' % ENVIRONMENT)
def setup_module(module): Call('add host %s' % HOST) Call('add environment %s' % ENVIRONMENT) Call('set host environment %s environment=%s' % (HOST, ENVIRONMENT)) Call('set environment attr %s attr=key value=value' % ENVIRONMENT)
def teardown_module(module): Call('remove host %s' % ENVIRONMENT) Call('remove environment attr %s attr=*' % ENVIRONMENT) Call('remove environment %s' % ENVIRONMENT)