def test_required_property(self): class A(Node): p = required_property() def action(self): self.p() env = Env(A()) # NotImplementedError wrapped in ActionException self.assertRaises(ActionException, env.action) try: env.action() except ActionException, e: self.assertIsInstance(e.inner_exception, NotImplementedError)
def test_assignments_in_node(self): # It's not allowed to change attributes from a Node Environment. class MyNode(Node): def action(self): self.variable = 'value' # AttributeError wrapped in ActionException env = Env(MyNode()) self.assertRaises(ActionException, env.action) try: env.action() except ActionException as e: self.assertIsInstance(e.inner_exception, AttributeError)
def test_assignments_in_node(self): # It's not allowed to change attributes from a Node Environment. class MyNode(Node): def action(self): self.variable = 'value' # AttributeError wrapped in ActionException env = Env(MyNode()) self.assertRaises(ActionException, env.action) try: env.action() except ActionException, e: self.assertIsInstance(e.inner_exception, AttributeError)
def test_required_property(self): class A(Node): p = required_property() def action(self): self.p() env = Env(A()) # NotImplementedError wrapped in ActionException self.assertRaises(ActionException, env.action) try: env.action() except ActionException as e: self.assertIsInstance(e.inner_exception, NotImplementedError)
def test_hostcontainer_from_node(self): this = self class A(Node): class Hosts: role1 = LocalHost1 def action(self): with self.hosts.cd('/tmp'): this.assertEqual(self.hosts[0].run('pwd').strip(), '/tmp') with self.hosts.cd('/'): this.assertEqual(self.hosts[0].run('pwd').strip(), '/') this.assertEqual(self.hosts[0].run('pwd').strip(), '/tmp') env = Env(A()) env.action()
def test_hostcontainer_from_node(self): this = self class A(Node): class Hosts: role1 = LocalHost1 def action(self): with self.hosts.cd('/tmp'): this.assertEqual(self.hosts.getcwd(), ['/tmp']) this.assertEqual(self.hosts[0].getcwd(), '/tmp') this.assertEqual(self.hosts[0].run('pwd').strip(), '/tmp') with self.hosts.cd('/'): this.assertEqual(self.hosts[0].run('pwd').strip(), '/') this.assertEqual(self.hosts[0].run('pwd').strip(), '/tmp') env = Env(A()) env.action()
def test_super_call(self): # Calling the superclass class A(Node): def action(self): return 'result' class B(A): def action(self): return 'The result was %s' % A.action(self) env = Env(B()) self.assertEqual(env.action(), 'The result was result')
def test_action_aliases(self): # We can define multiple aliases for an action. class A(Node): @alias('my_alias2') @alias('my_alias') def action(self): return 'result' env = Env(A()) self.assertEqual(env.action(), 'result') self.assertEqual(env.my_alias(), 'result') self.assertEqual(env.my_alias2(), 'result')
def test_wrapping_middle_node_in_env(self): class S(Node): class Hosts: role1 = LocalHost1, LocalHost2 class T(Node): def action(self): return len(self.hosts) s = S() env = Env(s.T) self.assertEqual(env.action(), 2)
def test_wrapping_middle_node_in_env(self): class S(Node): class Hosts: role1 = {LocalHost1, LocalHost2} class T(Node): def action(self): return len(self.hosts) s = S() env = Env(s.T) self.assertEqual(env.action(), 2)
def test_initialize_node(self): class A(Node): class Hosts: role1 = LocalHost2 role2 = LocalHost4, LocalHost5 def action(self): # Define a new Node-tree class B(Node): class Hosts: role2 = self.hosts.filter('role2') def action2(self): return len(self.hosts) # Initialize it in the current Env, and call an action of that one. return self.initialize_node(B).action2() env = Env(A()) self.assertEqual(env.action(), 2)
def test_initialize_node(self): class A(Node): class Hosts: role1 = {LocalHost2} role2 = {LocalHost4, LocalHost5} def action(self): # Define a new Node-tree class B(Node): class Hosts: role2 = self.hosts.filter('role2') def action2(self): return len(self.hosts) # Initialize it in the current Env, and call an action of that one. return self.initialize_node(B).action2() env = Env(A()) self.assertEqual(env.action(), 2)