Пример #1
0
 def test_create_directory_failed_no_parent(self, isdir_mock, os_dirname_mock, 
                                     os_path_exists_mock):
   os_path_exists_mock.return_value = False
   os_dirname_mock.return_value = "/a/b/c"
   isdir_mock.return_value = False
   
   
   try:
     with Environment('/') as env:
       Directory('/a/b/c/d',
            action='create',
            mode=0777,
            owner="hdfs",
            group="hadoop"
       )
     self.fail("Must fail because parent directory /a/b/c doesn't exist")
   except Fail as e:
     self.assertEqual('Applying Directory[\'/a/b/c/d\'] failed, parent directory /a/b/c doesn\'t exist',
                      str(e))
Пример #2
0
    def test_download_source_get_content_nocache(self, exists_mock,
                                                 makedirs_mock, urlopen_mock):
        """
    Testing DownloadSource.get_content without cache
    """
        exists_mock.return_value = True
        web_file_mock = MagicMock()
        web_file_mock.read.return_value = 'web_content'
        urlopen_mock.return_value = web_file_mock

        with Environment("/base") as env:
            download_source = DownloadSource("http://download/source",
                                             cache=False)
        content = download_source.get_content()

        self.assertEqual('web_content', content)
        self.assertEqual(urlopen_mock.call_count, 1)
        urlopen_mock.assert_called_with('http://download/source')
        self.assertEqual(web_file_mock.read.call_count, 1)
Пример #3
0
    def test_action_create_nonexistent(self, popen_mock, getgrnam_mock):
        subproc_mock = MagicMock()
        subproc_mock.returncode = 0
        popen_mock.return_value = subproc_mock
        getgrnam_mock.side_effect = KeyError()

        with Environment('/') as env:
            Group('hadoop', action='create', password='******')

        self.assertEqual(popen_mock.call_count, 1)
        popen_mock.assert_called_with(
            ['/bin/bash', '--login', '-c', 'groupadd -p secure hadoop'],
            shell=False,
            preexec_fn=None,
            stderr=-2,
            stdout=-1,
            env=None,
            cwd=None)
        getgrnam_mock.assert_called_with('hadoop')
Пример #4
0
  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')
Пример #5
0
 def test_action_install_regex_installed_ubuntu(self, shell_mock,
                                                call_mock):
     call_mock.side_effect = [
         (0, None), (0, "some-package1\nsome-package2"),
         (0, "Some text.\nStatus: install ok installed\nSome text"),
         (0, "Some text.\nStatus: install ok installed\nSome text"),
         (0, None)
     ]
     with Environment('/') as env:
         Package("some_package.*", )
     call_mock.assert_has_calls([
         call(
             "dpkg --get-selections | grep -v deinstall | awk '{print $1}' | grep ^some-package.*$"
         )
     ])
     self.assertEqual(call_mock.call_count, 1,
                      "Package should not be installed")
     self.assertEqual(shell_mock.call_count, 0,
                      "shell.checked_call shouldn't be called")
Пример #6
0
 def test_action_install_rhel(self, shell_mock):
     shell_mock.return_value = (0, '')
     sys.modules['rpm'] = MagicMock()
     sys.modules['rpm'].TransactionSet.return_value = MagicMock()
     sys.modules['rpm'].TransactionSet.return_value.dbMatch.return_value = [
         {
             'name': 'some_packag'
         }
     ]
     with Environment('/') as env:
         Package("some_package", logoutput=False)
     self.assertTrue(
         sys.modules['rpm'].TransactionSet.return_value.dbMatch.called)
     shell_mock.assert_called_with([
         '/usr/bin/yum', '-d', '0', '-e', '0', '-y', 'install',
         'some_package'
     ],
                                   logoutput=False,
                                   sudo=True)
Пример #7
0
    def test_download_source_get_content_nocache(self, exists_mock,
                                                 request_mock, opener_mock):
        """
    Testing DownloadSource.get_content without cache
    """
        exists_mock.return_value = True
        web_file_mock = MagicMock()
        web_file_mock.read.return_value = 'web_content'
        opener_mock.return_value.open = MagicMock(return_value=web_file_mock)

        with Environment("/base", tmp_dir='/var/tmp/downloads') as env:
            download_source = DownloadSource("http://download/source",
                                             redownload_files=True)
        content = download_source.get_content()

        self.assertEqual('web_content', content)
        self.assertEqual(opener_mock.call_count, 1)
        request_mock.assert_called_with('http://download/source')
        self.assertEqual(web_file_mock.read.call_count, 1)
Пример #8
0
    def test_action_create_existent(self, popen_mock, getgrnam_mock):
        subproc_mock = MagicMock()
        subproc_mock.returncode = 0
        popen_mock.return_value = subproc_mock
        getgrnam_mock.return_value = "mapred"

        with Environment('/') as env:
            Group('mapred', action='create', gid=2, password='******')

        self.assertEqual(popen_mock.call_count, 1)
        popen_mock.assert_called_with(
            ['/bin/bash', '--login', '-c', 'groupmod -p secure -g 2 mapred'],
            shell=False,
            preexec_fn=None,
            stderr=-2,
            stdout=-1,
            env=None,
            cwd=None)
        getgrnam_mock.assert_called_with('mapred')
Пример #9
0
    def test_template_loader_absolute_path(self, exists_mock, getmtime_mock,
                                           open_mock):
        """
    Testing template loader with absolute file-path
    """
        exists_mock.return_value = True
        getmtime_mock.return_value = 10
        file_mock = MagicMock(name='file_mock')
        file_mock.__enter__.return_value = file_mock
        file_mock.read.return_value = 'template content'
        open_mock.return_value = file_mock

        with Environment("/base") as env:
            template = Template("/absolute/path/test.j2")

        self.assertEqual(open_mock.call_count, 1)
        open_mock.assert_called_with('/absolute/path/test.j2', 'rb')
        self.assertEqual(getmtime_mock.call_count, 1)
        getmtime_mock.assert_called_with('/absolute/path/test.j2')
Пример #10
0
  def test_download_source_get_content_cache_existent(self, exists_mock, open_mock):
    """
    Testing DownloadSource.get_content with cache on cached resource
    """
    exists_mock.side_effect = [True, True]

    file_mock = MagicMock(name = 'file_mock')
    file_mock.__enter__.return_value = file_mock
    file_mock.read.return_value = 'cached_content'
    open_mock.return_value = file_mock


    with Environment("/base", tmp_dir='/var/tmp/downloads') as env:
      download_source = DownloadSource("http://download/source", redownload_files=False)
    content = download_source.get_content()

    self.assertEqual('cached_content', content)
    self.assertEqual(open_mock.call_count, 1)
    self.assertEqual(file_mock.read.call_count, 1)
Пример #11
0
    def test_action_create_non_existent_file(self, isdir_mock, exists_mock,
                                             open_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
        new_file = MagicMock()
        open_mock.return_value = new_file
        with Environment('/') as env:
            File('/directory/file',
                 action='create',
                 mode=0777,
                 content='file-content')
        env.run()

        open_mock.assert_called_with('/directory/file', 'wb')
        new_file.__enter__().write.assert_called_with('file-content')
        self.assertEqual(open_mock.call_count, 1)
        ensure_mock.assert_called()
Пример #12
0
    def test_action_delete(self, getpwnam_mock, popen_mock):
        subproc_mock = MagicMock()
        subproc_mock.returncode = 0
        subproc_mock.stdout.readline = MagicMock(side_effect=['OK'])
        popen_mock.return_value = subproc_mock
        getpwnam_mock.return_value = 1

        with Environment('/') as env:
            user = User("mapred", action="remove", shell="/bin/bash")

        popen_mock.assert_called_with(
            ['/bin/bash', '--login', '--noprofile', '-c', 'userdel mapred'],
            shell=False,
            preexec_fn=None,
            stderr=-2,
            stdout=5,
            bufsize=1,
            env={'PATH': '/bin'},
            cwd=None)
        self.assertEqual(popen_mock.call_count, 1)
  def test_action_create_arguments(self, os_path_isdir_mock ,os_path_exists_mock, file_mock):

    os_path_isdir_mock.side_effect = [False, True]
    os_path_exists_mock.return_value = False

    with Environment() as env:
      XmlConfig('xmlFile.xml',
                conf_dir='/dir/conf',
                configurations={'property1': 'value1'},
                configuration_attributes={'attr': {'property1': 'attr_value'}},
                mode = 0755,
                owner = "hdfs",
                group = "hadoop",
                encoding = "Code"
      )

    self.assertEqual(file_mock.call_args[0][0],'/dir/conf/xmlFile.xml')
    call_args = file_mock.call_args[1].copy()
    del call_args['content']
    self.assertEqual(call_args,{'owner': 'hdfs', 'group': 'hadoop', 'mode': 0755, 'encoding' : 'Code'})
Пример #14
0
    def test_action_remove(self, popen_mock, getgrnam_mock):

        subproc_mock = MagicMock()
        subproc_mock.returncode = 0
        popen_mock.return_value = subproc_mock
        getgrnam_mock.return_value = "mapred"

        with Environment('/') as env:
            Group('mapred', action='remove')

        self.assertEqual(popen_mock.call_count, 1)
        popen_mock.assert_called_with(
            ['/bin/bash', '--login', '-c', 'groupdel mapred'],
            shell=False,
            preexec_fn=None,
            stderr=-2,
            stdout=-1,
            env=None,
            cwd=None)
        getgrnam_mock.assert_called_with('mapred')
Пример #15
0
    def test_static_file_absolute_path(self, join_mock, open_mock):
        """
    Testing StaticFile source with absolute path
    """
        file_mock = MagicMock(name='file_mock')
        file_mock.__enter__.return_value = file_mock
        file_mock.read.return_value = 'content'
        open_mock.return_value = file_mock

        filepath = "/absolute/path/file"
        if IS_WINDOWS:
            filepath = "\\absolute\\path\\file"

        with Environment("/base") as env:
            static_file = StaticFile(filepath)
            content = static_file.get_content()

        self.assertEqual('content', content)
        self.assertEqual(file_mock.read.call_count, 1)
        self.assertEqual(join_mock.call_count, 0)
Пример #16
0
    def test_attribute_comment(self, getpwnam_mock, popen_mock):
        subproc_mock = MagicMock()
        subproc_mock.returncode = 0
        popen_mock.return_value = subproc_mock
        getpwnam_mock.return_value = 1

        with Environment('/') as env:
            user = User("mapred", action="create", comment="testComment")

        popen_mock.assert_called_with([
            '/bin/bash', '--login', '-c',
            'usermod -c testComment -s /bin/bash mapred'
        ],
                                      shell=False,
                                      preexec_fn=None,
                                      stderr=-2,
                                      stdout=-1,
                                      env=None,
                                      cwd=None)
        self.assertEqual(popen_mock.call_count, 1)
Пример #17
0
    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', encoding=None)
        create_file_mock.assert_called_with('/directory/file',
                                            'new-content',
                                            encoding=None)
Пример #18
0
    def test_action_install_ubuntu(self, shell_mock, call_mock):
        call_mock.side_effect = [(1, None), (0, None)]
        with Environment('/') as env:
            Package("some_package", )
        call_mock.assert_has_calls([
            call(
                "dpkg --get-selections | grep -v deinstall | awk '{print $1}' | grep '^some-package$'"
            ),
            call([
                '/usr/bin/apt-get', '-q', '-o',
                'Dpkg::Options::=--force-confdef', '--allow-unauthenticated',
                '--assume-yes', 'install', 'some-package'
            ],
                 logoutput=False,
                 sudo=True,
                 env={'DEBIAN_FRONTEND': 'noninteractive'})
        ])

        self.assertEqual(shell_mock.call_count, 0,
                         "shell.checked_call shouldn't be called")
Пример #19
0
    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')
Пример #20
0
  def test_template_loader_arguments(self, exists_mock, getmtime_mock, open_mock):
    """
    Testing template loader additional arguments in template and absolute file-path
    """
    exists_mock.return_value = True
    getmtime_mock.return_value = 10
    file_mock = MagicMock(name = 'file_mock')
    file_mock.__enter__.return_value = file_mock
    file_mock.read.return_value = '{{test_arg1}} template content'
    open_mock.return_value = file_mock

    with Environment("/base") as env:
      template = Template("/absolute/path/test.j2", [], test_arg1 = "test")
      content = template.get_content()
    self.assertEqual(open_mock.call_count, 1)

    self.assertEqual(u'test template content', content)
    open_mock.assert_called_with('/absolute/path/test.j2', 'rb')
    self.assertEqual(getmtime_mock.call_count, 1)
    getmtime_mock.assert_called_with('/absolute/path/test.j2')
Пример #21
0
    def test_action_create_nonexistent(self, getpwnam_mock, popen_mock):
        subproc_mock = MagicMock()
        subproc_mock.returncode = 0
        subproc_mock.stdout = subproc_stdout
        popen_mock.return_value = subproc_mock
        getpwnam_mock.return_value = None
        with Environment('/') as env:
            user = User("mapred", action="create", shell="/bin/bash")

        popen_mock.assert_called_with([
            '/bin/bash', '--login', '--noprofile', '-c',
            "ambari-sudo.sh  PATH=/bin -H -E useradd -m -s /bin/bash mapred"
        ],
                                      shell=False,
                                      preexec_fn=preexec_fn,
                                      stderr=-2,
                                      stdout=-1,
                                      env={'PATH': '/bin'},
                                      cwd=None,
                                      close_fds=True)
        self.assertEqual(popen_mock.call_count, 1)
Пример #22
0
    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)
Пример #23
0
    def test_create_directory_recursive(self, _coerce_gid_mock,
                                        _coerce_uid_mock, os_chmod_mock,
                                        os_stat_mock, isdir_mock,
                                        os_makedirs_mock, os_path_exists_mock):
        os_path_exists_mock.return_value = False
        isdir_mock.return_value = True
        _coerce_uid_mock.return_value = 66
        _coerce_gid_mock.return_value = 77
        os_stat_mock.return_value = type(
            "", (), dict(st_mode=0755, st_uid=0, st_gid=0))()

        with Environment('/') as env:
            Directory('/a/b/c/d',
                      action='create',
                      mode=0777,
                      owner="hdfs",
                      group="hadoop",
                      recursive=True)

        os_makedirs_mock.assert_called_with('/a/b/c/d', 0777)
        os_chmod_mock.assert_called_with('/a/b/c/d', 0777)
Пример #24
0
    def test_action_create_empty_properties_without_dir(
            self, time_asctime_mock, os_path_isdir_mock, os_path_exists_mock,
            create_file_mock, ensure_mock):
        """
    Tests if 'action_create' - creates new non existent file and write proper data
    1) properties={}
    2) dir=None
    """
        os_path_isdir_mock.side_effect = [False, True]
        os_path_exists_mock.return_value = False
        time_asctime_mock.return_value = 'Today is Wednesday'

        with Environment('/') as env:
            PropertiesFile('/somewhere_in_system/one_file.properties',
                           dir=None,
                           properties={})

        create_file_mock.assert_called_with(
            '/somewhere_in_system/one_file.properties',
            u'# Generated by Apache Ambari. Today is Wednesday\n    \n    \n')
        ensure_mock.assert_called()
Пример #25
0
    def test_action_install_ubuntu(self, shell_mock, call_mock):
        call_mock.side_effect = [(1, ''), (0, '')]
        shell_mock.return_value = (0, '')
        with Environment('/') as env:
            Package("some_package", logoutput=False)
        call_mock.assert_has_calls([
            call(
                "dpkg --get-selections | grep -v deinstall | awk '{print $1}' | grep ^some-package$"
            )
        ])

        shell_mock.assert_has_call([
            call([
                '/usr/bin/apt-get', '-q', '-o',
                'Dpkg::Options::=--force-confdef', '--allow-unauthenticated',
                '--assume-yes', 'install', 'some-package'
            ],
                 logoutput=False,
                 sudo=True,
                 env={'DEBIAN_FRONTEND': 'noninteractive'})
        ])
 def test_action_install_pattern_suse(self, shell_mock, call_mock):
     call_mock.side_effect = [
         (0, None),
         (0,
          "Loading repository data...\nReading installed packages...\n\nS | Name\n--+-----\n  | Pack"
          )
     ]
     with Environment('/') as env:
         Package("some_package*", )
     call_mock.assert_has_calls([
         call(
             "installed_pkgs=`rpm -qa 'some_package*'` ; [ ! -z \"$installed_pkgs\" ]"
         ),
         call(
             "zypper --non-interactive search --type package --uninstalled-only --match-exact 'some_package*'"
         )
     ])
     self.assertEquals(shell_mock.call_args_list[0][0][0], [
         '/usr/bin/zypper', '--quiet', 'install',
         '--auto-agree-with-licenses', '--no-confirm', 'some_package*'
     ])
Пример #27
0
    def test_action_create_nonexistent(self, getpwnam_mock, popen_mock):
        subproc_mock = MagicMock()
        subproc_mock.returncode = 0
        subproc_mock.stdout.readline = MagicMock(side_effect=['OK'])
        popen_mock.return_value = subproc_mock
        getpwnam_mock.return_value = None
        with Environment('/') as env:
            user = User("mapred", action="create", shell="/bin/bash")

        popen_mock.assert_called_with([
            '/bin/bash', '--login', '--noprofile', '-c',
            "/usr/bin/sudo  PATH=/bin -H -E useradd -m -s /bin/bash mapred"
        ],
                                      shell=False,
                                      preexec_fn=None,
                                      stderr=-2,
                                      stdout=5,
                                      env={'PATH': '/bin'},
                                      bufsize=1,
                                      cwd=None)
        self.assertEqual(popen_mock.call_count, 1)
Пример #28
0
    def test_download_source_get_content_cache_existent_md5_match(
            self, exists_mock, makedirs_mock, open_mock, urlopen_mock):
        """
    Testing DownloadSource.get_content with cache on cached resource with md5 check
    """
        exists_mock.side_effect = [True, True, False]

        file_mock = MagicMock(name='file_mock')
        file_mock.__enter__.return_value = file_mock
        file_mock.read.return_value = 'cached_content'
        open_mock.return_value = file_mock

        with Environment("/base") as env:
            download_source = DownloadSource("http://download/source",
                                             cache=True)
        content = download_source.get_content()

        self.assertEqual('cached_content', content)
        self.assertEqual(open_mock.call_count, 1)
        open_mock.assert_called_with('/var/tmp/downloads/source')
        self.assertEqual(file_mock.read.call_count, 1)
        self.assertEqual(urlopen_mock.call_count, 0)
Пример #29
0
    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)
Пример #30
0
    def test_action_create_empty_xml_config(self, time_asctime_mock,
                                            os_path_isdir_mock,
                                            os_path_exists_mock,
                                            create_file_mock, ensure_mock):
        """
    Tests if 'create' action - creates new non existent xml file and write proper data
    where configurations={}
    """
        os_path_isdir_mock.side_effect = [False, True]
        os_path_exists_mock.return_value = False
        time_asctime_mock.return_value = 'Wed 2014-02'

        with Environment('/') as env:
            XmlConfig('file.xml',
                      conf_dir='/dir/conf',
                      configurations={},
                      configuration_attributes={})

        create_file_mock.assert_called_with(
            '/dir/conf/file.xml',
            u'  <configuration>\n    \n  </configuration>',
            encoding='UTF-8')