def test_node_set_path_empty(): """Test that Node throws an error when setting path to empty string.""" node = Node() with pytest.raises(ValueError): node.path = '' with pytest.raises(ValueError): node = Node({'path' : ''})
def test_node_set_path_invalid_char(invalid_char): """Test that Node throws an error when setting path to invalid character.""" node = Node() with pytest.raises(ValueError): node.path = '/path/to' + invalid_char with pytest.raises(ValueError): node = Node({'path' : '/path/to' + invalid_char})
def test_node_exists(mock_path_exists, atts, expected): """Tests node's exist method.""" mock_path_exists.return_value = expected node = Node(atts) assert expected == node.exists() if atts['path'] is not None: mock_path_exists.assert_called_once_with(atts['path'])
def test_node_set_perms_invalid(): """Tests setting node's perms as invalid values.""" node = Node(DEFUALT_ATTS) with pytest.raises(ValueError): node.perms = 'a' with pytest.raises(ValueError): node.perms = 9999 # pylint: disable=redefined-variable-type with pytest.raises(ValueError): node.perms = -9999
def test_node_chmod(mock_chmod, mock_path_exists, atts, expected): """Tests Node's chmod method.""" mock_path_exists.return_value = True # Assume this is working for this test node = Node(atts) assert expected == node.chmod() if atts['path'] and node.perms: mock_chmod.assert_called_once_with(atts['path'], atts['perms']) else: assert not mock_chmod.called
def test_node_get_atts(atts, expected): """Test Node get_atts method.""" node = Node(atts) if 'perms' not in atts: atts['perms'] = None if 'owner' not in atts: atts['owner'] = None if 'group' not in atts: atts['group'] = None assert node.get_atts(string=True) == expected assert node.get_atts(string=False) == atts
def test_node_chown(mock_chown, mock_getgrnam, mock_getpwnam, mock_path_exists, atts, expected): # pylint: disable=too-many-arguments """Tests Node's chown method.""" node = Node(atts) if 'owner' not in atts: atts['owner'] = CURRENT_USER if 'group' not in atts: atts['group'] = 'nogroup' if atts['owner'] == 'nobody' else CURRENT_GROUP mock_path_exists.return_value = True # Assume this is working for this test mock_getpwnam(atts['owner']).pw_uid = 123 # Just a number to use for mocking mock_getgrnam(atts['group']).gr_gid = 123 assert expected == node.chown() if atts['path'] is not None: mock_getpwnam.assert_called_with(CURRENT_USER if not atts['owner'] else atts['owner']) mock_getgrnam.assert_called_with(CURRENT_GROUP if not atts['group'] else atts['group']) mock_chown.assert_called_once_with(atts['path'], 123, 123)
def test_node_verify(mock_exists, mock_actual_perms, mock_actual_owner, mock_actual_group, atts): """Test Node's method verify.""" # Test passing verification mock_exists.return_value = True node = Node(atts) node.actual_perms = None if 'perms' not in atts else atts['perms'] node.actual_owner = None if 'owner' not in atts else atts['owner'] node.actual_group = None if 'group' not in atts else atts['group'] assert node.verify(False) # Test failing verification if atts['path']: # If there is no path, it's a stub and should always verify true. mock_exists.return_value = False mock_actual_perms.return_value = None mock_actual_owner.return_value = None mock_actual_group.return_value = None bad_node = Node(atts) assert not bad_node.verify(False)
def test_node_repr(atts, expected): """Test Node repr.""" node = Node(atts) assert node.__repr__() == "Node(" + expected + ")"
def test_node_set_group_root(): """Tests setting node's group to root.""" node = Node(DEFUALT_ATTS) node.group = 'root' assert node.group == 'root'
def test_node_set_bad_group(): """Tests setting node's group to an invalid user.""" node = Node(DEFUALT_ATTS) with pytest.raises(KeyError): node.group = 'HopefullyNot_a_RealGroup'
def test_node_set_owner_root(): """Tests setting node's owner to root.""" node = Node(DEFUALT_ATTS) node.owner = 'root' assert node.owner == 'root'
def test_node_set_bad_owner(): """Tests setting node's owner to an invalid user.""" node = Node(DEFUALT_ATTS) with pytest.raises(KeyError): node.owner = 'HopefullyNot_a_RealUser'
def test_node_chown_nonexisting(mock_path_exists): """Tests Node's chown method with a nonexisting node.""" mock_path_exists.return_value = False node = Node(DEFUALT_ATTS) with pytest.raises(IOError): node.chown()
def test_node_repair(mock_verify): """Test that repair() method calls verify(True).""" node = Node() node.repair() mock_verify.assert_called_once_with(True)
def test_node_remove(): """Test that Node throws an error when calling remove().""" node = Node({'path' : '/the/path'}) with pytest.raises(NotImplementedError): node.remove()