def test_modify_compute_tags(self): computes = db.get_root()['oms_root']['computes'] cmpt = self.make_compute() cid = computes.add(cmpt) transaction.commit() self._cmd('set computes/%s tags=taga,tagb' % cid) self.terminal.reset_mock() self._cmd('cat computes/%s' % cid) with assert_mock(self.terminal) as t: whatever(t) t.write( 'Tags: arch:centos, arch:linux, arch:x86_64, label:taga, label:tagb, state:active, type:compute\n' ) self._cmd('set computes/%s tags=taga,-tagb' % cid) self.terminal.reset_mock() self._cmd('cat computes/%s' % cid) with assert_mock(self.terminal) as t: whatever(t) t.write( 'Tags: arch:centos, arch:linux, arch:x86_64, label:taga, state:active, type:compute\n' )
def test_modify_compute(self): computes = db.get_root()['oms_root']['computes'] cid = computes.add(self.make_compute()) transaction.commit() self._cmd('set computes/%s hostname=TUX-FOR-TEST' % cid) self.terminal.reset_mock() self._cmd('cat computes/%s' % cid) with assert_mock(self.terminal) as t: t.write('Host name: TUX-FOR-TEST\n') whatever(t) t.write('Architecture: x86_64, linux, centos\n') whatever(t) t.write('State: active\n') whatever(t) t.write('RAM Size: 2000\n') self.terminal.reset_mock() self._cmd('set computes/123') with assert_mock(self.terminal) as t: t.write("No such object: computes/123\n") self.terminal.reset_mock() self._cmd('set computes') with assert_mock(self.terminal) as t: t.write("No schema found for object\n")
def test_acl(self): self._cmd('setfacl / -m u:user:r') self.terminal.reset_mock() self._cmd('getfacl /') with assert_mock(self.terminal) as t: t.write('user:user:+r\n') self._cmd('setfacl / -m u:user:w') self.terminal.reset_mock() self._cmd('getfacl /') with assert_mock(self.terminal) as t: t.write('user:user:+rw\n') self._cmd('setfacl / -d u:user:a') self.terminal.reset_mock() self._cmd('getfacl /') with assert_mock(self.terminal) as t: t.write('user:user:+rw\n') t.write('user:user:-a\n') self._cmd('setfacl / -d u:user:w') self.terminal.reset_mock() self._cmd('getfacl /') with assert_mock(self.terminal) as t: t.write('user:user:+r\n') t.write('user:user:-aw\n') self.terminal.reset_mock() self._cmd('setfacl / -d u:user:G') with assert_mock(self.terminal) as t: t.write("No such permission 'G'\n")
def test_rm_compute(self): self._cmd('cat computes/1') with assert_mock(self.terminal) as t: t.write("No such object: computes/1\n") self.terminal.reset_mock() computes = db.get_root()['oms_root']['computes'] cid = computes.add(self.make_compute()) transaction.commit() self._cmd('cat computes/%s' % cid) with assert_mock(self.terminal) as t: t.write('Host name: tux-for-test\n') whatever(t) t.write('Architecture: x86_64, linux, centos\n') whatever(t) t.write('State: active\n') whatever(t) t.write('RAM Size: 2000\n') self._cmd('rm computes/%s' % cid) self.terminal.reset_mock() self._cmd('cat computes/%s' % cid) with assert_mock(self.terminal) as t: t.write("No such object: computes/%s\n" % cid) self.terminal.reset_mock() self._cmd('rm computes/%s' % cid) with assert_mock(self.terminal) as t: t.write("No such object: computes/%s\n" % cid)
def test_no_more_calls(self): with assert_not_raises(): with assert_mock(self.mock) as m: no_more_calls(m) self.mock.foo() with assert_raises(AssertionError) as cm: with assert_mock(self.mock) as m: no_more_calls(m) assert cm.exception.args == ("There should be no more method calls but there are: foo()", )
def test_complete_keyword_switches_mk(self): self.oms_ssh.lineReceived('cd computes') self._tab_after('mk compute st') with assert_mock(self.terminal) as t: t.write('ate=') no_more_calls(t) self._tab_after('ina') with assert_mock(self.terminal) as t: t.write('ctive ') no_more_calls(t)
def test_complete_consumed_switches(self): self._tab_after('ls --help') with assert_mock(self.terminal) as t: t.write(' ') no_more_calls(t) self._tab_after('-') with assert_mock(self.terminal) as t: skip(t, 2) with current_call(t) as c: assert 'help' not in c.arg assert '-h' not in c.arg
def test_modify_compute_verbose(self): computes = db.get_root()['oms_root']['computes'] cid = computes.add(self.make_compute()) transaction.commit() self._cmd('set computes/%s hostname=TUX-FOR-TEST -v' % cid) with assert_mock(self.terminal) as t: t.write("Setting hostname=TUX-FOR-TEST\n") self.terminal.reset_mock() self._cmd('cat computes/%s' % cid) with assert_mock(self.terminal) as t: t.write('Host name: TUX-FOR-TEST\n')
def test_complete_keyword_switches(self): computes = db.get_root()['oms_root']['computes'] cid = computes.add(self.make_compute()) transaction.commit() self._tab_after('set /computes/%s st' % cid) with assert_mock(self.terminal) as t: t.write('ate=') no_more_calls(t) self._tab_after('ina') with assert_mock(self.terminal) as t: t.write('ctive ') no_more_calls(t)
def test_with_wrong_kwargs(self): self.mock.foo(arg='bar') with assert_raises(AssertionError) as cm: with assert_mock(self.mock) as m: m.foo() assert cm.exception.args == ("Expected a foo() call but found foo(arg='bar') instead", ) self.mock.reset_mock() self.mock.foo() with assert_raises(AssertionError) as cm: with assert_mock(self.mock) as m: m.foo(arg='bar') assert cm.exception.args == ("Expected a foo(arg='bar') call but found foo() instead", )
def test_cmd_path(self): self._cmd('/bin/echo test') with assert_mock(self.terminal) as t: t.write('test\n') self.terminal.reset_mock() self._cmd('bin/echo test') with assert_mock(self.terminal) as t: t.write('test\n') self._cmd('cd computes') self.terminal.reset_mock() self._cmd('../bin/echo test') with assert_mock(self.terminal) as t: t.write('test\n')
def test_complete_prefix(self): self._tab_after('he') with assert_mock(self.terminal) as t: t.write('l') no_more_calls(t) # hit tab twice self.terminal.reset_mock() self.oms_ssh.handle_TAB() with assert_mock(self.terminal) as t: t.write('') t.nextLine() t.write('help hello\n') t.write(self.oms_ssh.ps[0] + 'hel') no_more_calls(t)
def test_complete_consumed_keyword_switches_mk(self): """Test consuming of already completed switches when there are mandatory arguments.""" self.oms_ssh.lineReceived('cd computes') self._tab_after('mk compute st') with assert_mock(self.terminal) as t: t.write('ate=') no_more_calls(t) self._tab_after('ina') with assert_mock(self.terminal) as t: t.write('ctive ') no_more_calls(t) self._tab_after('st') assert not self.terminal.method_calls
def test_with_wrong_name(self): self.mock.foo() with assert_raises(AssertionError) as cm: with assert_mock(self.mock) as m: m.bar() assert cm.exception.args == ("Expected a bar() call but found foo() instead", )
def test_one_call(self): self.mock.foo() with assert_not_raises(): with assert_mock(self.mock): pass with assert_not_raises(): with assert_mock(self.mock) as m: m.foo() with assert_raises(AssertionError) as cm: with assert_mock(self.mock) as m: m.foo() m.foo() assert cm.exception.args == ("Expected a foo() call but instead there was no call", )
def test_create_compute_invalid_args(self): self._cmd("cd /computes") self.terminal.reset_mock() self._cmd("mk compute hostname=x memory=2 state=active") with assert_mock(self.terminal) as t: t.write("hostname: Value is too short\n")
def test_create_compute_mandatory_args(self): self._cmd("cd /computes") self.terminal.reset_mock() self._cmd("mk compute hostname=TUX-FOR-TEST memory=2000") with assert_mock(self.terminal) as t: t.write("argument =state is required")
def test_cat_folders(self): for folder in self.tlds: self._cmd('cat %s' % folder) with assert_mock(self.terminal) as t: skip(t, 1) skip(t, 1) t.write("user@oms:/# ") self.terminal.reset_mock()
def test_modify_compute_errors(self): computes = db.get_root()['oms_root']['computes'] cid = computes.add(self.make_compute()) transaction.commit() self._cmd('set computes/%s hostname=x' % cid) with assert_mock(self.terminal) as t: t.write("hostname: Value is too short\n")
def test_complete_container_symlink(self): computes = db.get_root()['oms_root']['computes'] cid = computes.add(self.make_compute()) transaction.commit() self._tab_after('cd /computes/%s' % cid) with assert_mock(self.terminal) as t: t.write('/')
def test_current_call_with_single_arg(self): self.mock.foo('bar') with assert_not_raises(): with assert_mock(self.mock) as m: with current_call(m) as c: c.arg == 'bar' self.mock.reset_mock() self.mock.foo('bar', 'baz') with assert_raises(AssertionError) as cm: with assert_mock(self.mock) as m: with current_call(m) as c: c.arg assert cm.exception.args == ("Call should only have a single argument", )
def test_complete_context_dependent_no_context(self): """Test whether context dependent arguments are correctly built when the context argument (i.e. the `set` cmd `path` argument) is not yet present. """ self._tab_after('set /comp') with assert_mock(self.terminal) as t: t.write('utes/') no_more_calls(t)
def test_context_dependent_help(self): computes = db.get_root()['oms_root']['computes'] cid = computes.add(self.make_compute()) transaction.commit() self.terminal.reset_mock() self._cmd('set computes/%s -h' % cid) with assert_mock(self.terminal) as t: assert 'hostname=' in current_call(t).arg
def test_complete_positional_choice(self): self.oms_ssh.lineReceived('cd computes') self._tab_after('mk comp') with assert_mock(self.terminal) as t: t.write('ute ') no_more_calls(t) self._tab_after('comp') assert not self.terminal.method_calls
def test_wildcard(self): self._cmd('echo /c*') with assert_mock(self.terminal) as t: t.write('/computes\n') self.terminal.reset_mock() self._cmd('echo /[cx]omputes') with assert_mock(self.terminal) as t: t.write('/computes\n') self.terminal.reset_mock() computes = db.get_root()['oms_root']['computes'] cid = computes.add(self.make_compute()) transaction.commit() self._cmd('echo /computes/*-[a-z0-9]*-*') with assert_mock(self.terminal) as t: t.write('/computes/%s\n' % (cid))
def test_current_call(self): self.mock.foo('one two three') self.mock.foo(foo='bar') with assert_not_raises(): with assert_mock(self.mock) as m: assert current_call(m).name == 'foo' assert 'two' in current_call(m).args[0] skip(m, 1) assert current_call(m).kwargs['foo'] == 'bar'
def test_complete_mk_legal_types(self): """Test that only legal types are shown.""" self.oms_ssh.lineReceived('cd computes') self._tab_after('mk net') assert not self.terminal.method_calls self.oms_ssh.handle_RETURN() self.terminal.reset_mock() self._tab_after('mk comp') #~ eq_(self.terminal.method_calls, [('write', ('ute ',), {})]) with assert_mock(self.terminal) as t: t.write('ute ') no_more_calls(t) self._tab_after('arch') with assert_mock(self.terminal) as t: t.write('itecture=') no_more_calls(t)
def test_modify_compute_tags(self): computes = db.get_root()['oms_root']['computes'] cmpt = self.make_compute() cid = computes.add(cmpt) transaction.commit() self._cmd('set computes/%s tags=taga,tagb' % cid) self.terminal.reset_mock() self._cmd('cat computes/%s' % cid) with assert_mock(self.terminal) as t: whatever(t) t.write('Tags: arch:centos, arch:linux, arch:x86_64, label:taga, label:tagb, state:active, type:compute\n') self._cmd('set computes/%s tags=taga,-tagb' % cid) self.terminal.reset_mock() self._cmd('cat computes/%s' % cid) with assert_mock(self.terminal) as t: whatever(t) t.write('Tags: arch:centos, arch:linux, arch:x86_64, label:taga, state:active, type:compute\n')
def test_ls_l(self): self.terminal.reset_mock() self._cmd('ls /computes -l') with assert_mock(self.terminal) as t: t.write('a---r-v-x root <transient> \tby-name/\t\n') skip(t, 1) no_more_calls(t) computes = db.get_root()['oms_root']['computes'] compute = self.make_compute() cid = computes.add(compute) transaction.commit() self.terminal.reset_mock() self._cmd('ls /computes -l') with assert_mock(self.terminal) as t: t.write('a---r-v-x root %s\t%s@\t/machines/%s : tux-for-test\n' % (datetime.datetime.fromtimestamp(compute.mtime).isoformat(), cid, cid)) t.write('a---r-v-x root <transient> \tby-name/\t\n') skip(t, 1) no_more_calls(t)
def test_complete_mk_legal_types_interface(self): class ITest(Interface): pass class Test(Model): implements(ITest) def __init__(self): pass class TestInterfaceContainer(Container): __contains__ = ITest class TestClassContainer(Container): __contains__ = Test creatable_models['some-test'] = Test orig_current_object = commands.CreateObjCmd.current_obj try: commands.CreateObjCmd.current_obj = TestInterfaceContainer() self._tab_after('mk ') with assert_mock(self.terminal) as t: t.write('some-test ') no_more_calls(t) self.oms_ssh.handle_RETURN() self.terminal.reset_mock() commands.CreateObjCmd.current_obj = TestClassContainer() self._tab_after('mk ') with assert_mock(self.terminal) as t: t.write('some-test ') no_more_calls(t) finally: commands.CreateObjCmd.current_obj = orig_current_object del creatable_models['some-test']
def test_rename_compute(self): computes = db.get_root()['oms_root']['computes'] compute = self.make_compute() cid = computes.add(compute) transaction.commit() self._cmd('mv /machines/%s /machines/123' % cid) eq_(compute.__name__, '123') self.terminal.reset_mock() self._cmd('cat /machines/123') with assert_mock(self.terminal) as t: t.write('Host name: tux-for-test\n')
def test_complete_switches(self): self._tab_after('quit ') with assert_mock(self.terminal) as t: no_more_calls(t) # hit tab twice self.oms_ssh.handle_TAB() with assert_mock(self.terminal) as t: no_more_calls(t) # now try with a dash self._tab_after('-') with assert_mock(self.terminal) as t: t.write('') t.nextLine() t.write('-h --help\n') t.write(self.oms_ssh.ps[0] + 'quit -') no_more_calls(t) # disambiguate self._tab_after('-') with assert_mock(self.terminal) as t: t.write('help ') no_more_calls(t)
def test_ls_l(self): self.terminal.reset_mock() self._cmd('ls /computes -l') with assert_mock(self.terminal) as t: t.write( 'a---r-v-x root <transient> \tby-name/\t\n') skip(t, 1) no_more_calls(t) computes = db.get_root()['oms_root']['computes'] compute = self.make_compute() cid = computes.add(compute) transaction.commit() self.terminal.reset_mock() self._cmd('ls /computes -l') with assert_mock(self.terminal) as t: t.write('a---r-v-x root %s\t%s@\t/machines/%s : tux-for-test\n' % (datetime.datetime.fromtimestamp( compute.mtime).isoformat(), cid, cid)) t.write( 'a---r-v-x root <transient> \tby-name/\t\n') skip(t, 1) no_more_calls(t)
def test_cd_errors(self): computes = db.get_root()['oms_root']['computes'] computes.add(self.make_compute()) # TODO: reenable this when we'll have another leaf object. #self._cmd('cd /computes/%s' % cid) #with assert_mock(self.terminal) as t: # t.write('Cannot cd to a non-container\n') #self.terminal.reset_mock() self._cmd('cd /nonexisting') with assert_mock(self.terminal) as t: t.write('No such object: /nonexisting\n')
def test_create_compute(self): self._cmd("cd /computes") self._cmd("mk compute hostname=TUX-FOR-TEST memory=2000 state=active") cid = self.terminal.method_calls[-2][1][0] self.terminal.reset_mock() self._cmd('cat %s' % cid) with assert_mock(self.terminal) as t: t.write('Host name: TUX-FOR-TEST\n') whatever(t) t.write('Architecture: x86_64, linux, centos\n') whatever(t) t.write('State: active\n') whatever(t) t.write('RAM Size: 2000\n')
def test_last_error(self): class meta(FakeModule): class FailingCommand(Cmd): command('fail') def execute(self, args): raise Exception('some mock error') grok('martiantest.fake.meta') self._cmd('fail') self.terminal.reset_mock() self._cmd('last_error') with assert_mock(self.terminal) as t: assert 'some mock error' in current_call( t).arg, 'Apparently, fail command did not fail'
def test_ls(self): computes = db.get_root()['oms_root']['computes'] computes.add(self.make_compute()) # TODO: put back this when we find another leaf object #self.terminal.reset_mock() #self._cmd('ls /computes/%s' % cid) #with assert_mock(self.terminal) as t: # t.write('/computes/%s\n' % cid) self.terminal.reset_mock() self._cmd('ls /computes/x') with assert_mock(self.terminal) as t: t.write('No such object: /computes/x\n')
def test_cat_l_compute(self): self.terminal.reset_mock() computes = db.get_root()['oms_root']['computes'] cid = computes.add(self.make_compute()) transaction.commit() self._cmd('cat -l computes/%s' % cid) with assert_mock(self.terminal) as t: whatever(t) t.write('Architecture: x86_64\n' ' linux\n' ' centos\n') whatever(t) t.write('Diskspace Utilization: boot: 49.3\n' ' storage: 748.3\n' ' root: 249.0\n')