123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- '''
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- '''
- from unittest import TestCase
- from mock.mock import patch, MagicMock
- import os
- import sys
- from resource_management.core import Environment, Fail
- from resource_management.core.resources import File
- from resource_management.core.system import System
- from resource_management.core import sudo
- import resource_management.core.providers.system
- import resource_management
- @patch.object(System, "os_family", new = 'redhat')
- class TestFileResource(TestCase):
- @patch.object(os.path, "dirname")
- @patch.object(os.path, "isdir")
- def test_action_create_dir_exist(self, isdir_mock, dirname_mock):
- """
- Tests if 'create' action fails when path is existent directory
- """
- isdir_mock.side_effect = [True, False]
- try:
- with Environment('/') as env:
- File('/existent_directory',
- action='create',
- mode=0777,
- content='file-content'
- )
-
- self.fail("Must fail when directory with name 'path' exist")
- except Fail as e:
- self.assertEqual("Applying File['/existent_directory'] failed, directory with name /existent_directory exists",
- str(e))
- self.assertFalse(dirname_mock.called)
- @patch.object(os.path, "dirname")
- @patch.object(os.path, "isdir")
- def test_action_create_parent_dir_non_exist(self, isdir_mock, dirname_mock):
- """
- Tests if 'create' action fails when parent directory of path
- doesn't exist
- """
- isdir_mock.side_effect = [False, False]
- dirname_mock.return_value = "/non_existent_directory"
- try:
- with Environment('/') as env:
- File('/non_existent_directory/file',
- action='create',
- mode=0777,
- content='file-content'
- )
-
- self.fail('Must fail on non existent parent directory')
- except Fail as e:
- self.assertEqual(
- "Applying File['/non_existent_directory/file'] failed, parent directory /non_existent_directory doesn't exist",
- str(e))
- self.assertTrue(dirname_mock.called)
- @patch("resource_management.core.providers.system._ensure_metadata")
- @patch.object(sudo, "read_file")
- @patch.object(sudo, "create_file")
- @patch.object(os.path, "exists")
- @patch.object(os.path, "isdir")
- def test_action_create_non_existent_file(self, isdir_mock, exists_mock, create_file_mock, read_file_mock, ensure_mock):
- """
- Tests if 'create' action create new non existent file and write proper data
- """
- isdir_mock.side_effect = [False, True]
- exists_mock.return_value = False
- with Environment('/') as env:
- File('/directory/file',
- action='create',
- mode=0777,
- content='file-content'
- )
-
- create_file_mock.assert_called_with('/directory/file', 'file-content')
- self.assertEqual(create_file_mock.call_count, 1)
- ensure_mock.assert_called()
- @patch("resource_management.core.providers.system._ensure_metadata")
- @patch.object(sudo, "read_file")
- @patch.object(sudo, "create_file")
- @patch.object(os.path, "exists")
- @patch.object(os.path, "isdir")
- def test_action_create_replace(self, isdir_mock, exists_mock, create_file_mock, read_file_mock, ensure_mock):
- """
- Tests if 'create' action rewrite existent file with new data
- """
- isdir_mock.side_effect = [False, True]
- exists_mock.return_value = True
- with Environment('/') as env:
- File('/directory/file',
- action='create',
- mode=0777,
- backup=False,
- content='new-content'
- )
- read_file_mock.assert_called_with('/directory/file')
- create_file_mock.assert_called_with('/directory/file', 'new-content')
- @patch.object(sudo, "unlink")
- @patch.object(os.path, "exists")
- @patch.object(os.path, "isdir")
- def test_action_delete_is_directory(self, isdir_mock, exist_mock, unlink_mock):
- """
- Tests if 'delete' action fails when path is directory
- """
- isdir_mock.return_value = True
- try:
- with Environment('/') as env:
- File('/directory/file',
- action='delete',
- mode=0777,
- backup=False,
- content='new-content'
- )
-
- self.fail("Should fail when deleting directory")
- except Fail:
- pass
- self.assertEqual(isdir_mock.call_count, 1)
- self.assertEqual(exist_mock.call_count, 0)
- self.assertEqual(unlink_mock.call_count, 0)
- @patch.object(sudo, "unlink")
- @patch.object(os.path, "exists")
- @patch.object(os.path, "isdir")
- def test_action_delete(self, isdir_mock, exist_mock, unlink_mock):
- """
- Tests if 'delete' action removes file
- """
- isdir_mock.return_value = False
- with Environment('/') as env:
- File('/directory/file',
- action='delete',
- mode=0777,
- backup=False,
- content='new-content'
- )
-
- self.assertEqual(isdir_mock.call_count, 1)
- self.assertEqual(exist_mock.call_count, 1)
- self.assertEqual(unlink_mock.call_count, 1)
- @patch.object(os.path, "isdir")
- def test_attribute_path(self, isdir_mock):
- """
- Tests 'path' attribute
- """
- isdir_mock.side_effect = [True, False]
- try:
- with Environment('/') as env:
- File('/existent_directory',
- action='create',
- mode=0777,
- content='file-content'
- )
-
- self.fail("Must fail when directory with name 'path' exist")
- except Fail as e:
- pass
- isdir_mock.assert_called_with('/existent_directory')
- @patch.object(resource_management.core.Environment, "backup_file")
- @patch("resource_management.core.providers.system._ensure_metadata")
- @patch.object(sudo, "read_file")
- @patch.object(sudo, "create_file")
- @patch.object(os.path, "exists")
- @patch.object(os.path, "isdir")
- def test_attribute_backup(self, isdir_mock, exists_mock, create_file_mock, read_file_mock, ensure_mock, backup_file_mock):
- """
- Tests 'backup' attribute
- """
- isdir_mock.side_effect = [False, True, False, True]
- exists_mock.return_value = True
- with Environment('/') as env:
- File('/directory/file',
- action='create',
- mode=0777,
- backup=False,
- content='new-content'
- )
-
- self.assertEqual(backup_file_mock.call_count, 0)
- with Environment('/') as env:
- File('/directory/file',
- action='create',
- mode=0777,
- backup=True,
- content='new-content'
- )
-
- self.assertEqual(backup_file_mock.call_count, 1)
- backup_file_mock.assert_called_with('/directory/file')
- @patch("resource_management.core.providers.system._ensure_metadata")
- @patch("__builtin__.open")
- @patch.object(os.path, "exists")
- @patch.object(os.path, "isdir")
- def test_attribute_replace(self, isdir_mock, exists_mock, open_mock, ensure_mock):
- """
- Tests 'replace' attribute
- """
- isdir_mock.side_effect = [False, True]
- old_file, new_file = MagicMock(), MagicMock()
- open_mock.side_effect = [old_file, new_file]
- old_file.read.return_value = 'old-content'
- exists_mock.return_value = True
- with Environment('/') as env:
- File('/directory/file',
- action='create',
- mode=0777,
- backup=False,
- content='new-content',
- replace=False
- )
-
- old_file.read.assert_called()
- self.assertEqual(new_file.__enter__().write.call_count, 0)
- ensure_mock.assert_called()
- self.assertEqual(open_mock.call_count, 0)
- @patch("resource_management.core.providers.system._coerce_uid")
- @patch("resource_management.core.providers.system._coerce_gid")
- @patch.object(sudo, "chown")
- @patch.object(sudo, "chmod")
- @patch.object(os, "stat")
- @patch.object(sudo, "create_file")
- @patch.object(os.path, "exists")
- @patch.object(os.path, "isdir")
- def test_ensure_metadata(self, isdir_mock, exists_mock, create_file_mock, stat_mock, chmod_mock, chown_mock, gid_mock,
- uid_mock):
- """
- Tests if _ensure_metadata changes owner, usergroup and permissions of file to proper values
- """
- isdir_mock.side_effect = [False, True, False, True]
- exists_mock.return_value = False
- class stat():
- def __init__(self):
- self.st_mode = 0666
- self.st_uid = 1
- self.st_gid = 1
- stat_mock.return_value = stat()
- gid_mock.return_value = 0
- uid_mock.return_value = 0
- with Environment('/') as env:
- File('/directory/file',
- action='create',
- mode=0777,
- content='file-content',
- owner='root',
- group='hdfs'
- )
-
- create_file_mock.assert_called_with('/directory/file', 'file-content')
- self.assertEqual(create_file_mock.call_count, 1)
- stat_mock.assert_called_with('/directory/file')
- self.assertEqual(chmod_mock.call_count, 1)
- self.assertEqual(chown_mock.call_count, 2)
- gid_mock.assert_called_once_with('hdfs')
- uid_mock.assert_called_once_with('root')
- chmod_mock.reset_mock()
- chown_mock.reset_mock()
- gid_mock.return_value = 1
- uid_mock.return_value = 1
- with Environment('/') as env:
- File('/directory/file',
- action='create',
- mode=0777,
- content='file-content',
- owner='root',
- group='hdfs'
- )
-
- self.assertEqual(chmod_mock.call_count, 1)
- self.assertEqual(chown_mock.call_count, 0)
- @patch("resource_management.core.providers.system._ensure_metadata")
- @patch("resource_management.core.providers.system.FileProvider._get_content")
- @patch.object(sudo, "read_file")
- @patch.object(sudo, "create_file")
- @patch.object(os.path, "exists")
- @patch.object(os.path, "isdir")
- def test_action_create_encoding(self, isdir_mock, exists_mock, create_file_mock, read_file_mock, get_content_mock ,ensure_mock):
- isdir_mock.side_effect = [False, True]
- content_mock = MagicMock()
- old_content_mock = MagicMock()
- get_content_mock.return_value = content_mock
- read_file_mock.return_value = old_content_mock
- exists_mock.return_value = True
- with Environment('/') as env:
- File('/directory/file',
- action='create',
- mode=0777,
- content='file-content',
- encoding = "UTF-8"
- )
- read_file_mock.assert_called_with('/directory/file')
- content_mock.encode.assert_called_with('UTF-8')
- old_content_mock.decode.assert_called_with('UTF-8')
|