Beispiel #1
0
 def test_rules_from_current_dir(self):
     # This is a work around to test running tool against current directory
     return_code = -1
     invalid_specs = None
     valid_rule_path = os.path.join(FIXTURES_PATH,
                                    'valid_analysis/policies')
     # test default path, '.'
     with Pause(self.fs):
         original_path = os.getcwd()
         try:
             os.chdir(valid_rule_path)
             args = pat.setup_parser().parse_args('test'.split())
             return_code, invalid_specs = pat.test_analysis(args)
         finally:
             os.chdir(original_path)
     # asserts are outside of the pause to ensure the fakefs gets resumed
     assert_equal(return_code, 0)
     assert_equal(len(invalid_specs), 0)
     return_code = -1
     invalid_specs = None
     # test explicitly setting current dir
     with Pause(self.fs):
         original_path = os.getcwd()
         os.chdir(valid_rule_path)
         args = pat.setup_parser().parse_args('test --path ./'.split())
         return_code, invalid_specs = pat.test_analysis(args)
         os.chdir(original_path)
     # asserts are outside of the pause to ensure the fakefs gets resumed
     assert_equal(return_code, 0)
     assert_equal(len(invalid_specs), 0)
Beispiel #2
0
def copy_fake_directory_to_real_fs(fake_dir: str, real_dir: str, fake_fs):
    """Utility function to copy a directory in the fake file system recursively to the real file system"""
    for root, _, file_names in os.walk(fake_dir):
        relative_root = root.lstrip(os.sep)
        current_real_dir = os.path.join(real_dir, relative_root)
        with Pause(fake_fs):
            os.makedirs(current_real_dir)
        for file_name in file_names:
            with open(os.path.join(root, file_name), "rb") as file:
                data = file.read()
            with Pause(fake_fs):
                with open(os.path.join(current_real_dir, file_name), "w+b") as dst_file:
                    dst_file.write(data)
Beispiel #3
0
 def users(self, fs):
     from pyfakefs.fake_filesystem_unittest import Pause
     with Pause(fs):
         # Pause(fs) stops the fake filesystem and allows Django access to the common password list
         u1 = User.objects.create_user(username='******',
                                       email='*****@*****.**',
                                       password='******',
                                       type=UserType.ACTIVE.name)
         u2 = User.objects.create_user(username='******',
                                       email='*****@*****.**',
                                       password='******',
                                       type=UserType.ACTIVE.name)
         admin = User.objects.create_user(username='******',
                                          email='*****@*****.**',
                                          password='******',
                                          type=UserType.ADMIN.name)
         demo = User.objects.create_user(username='******',
                                         email='*****@*****.**',
                                         password='******',
                                         type=UserType.DEMO_USER.name)
         deleted = User.objects.create_user(username='******',
                                            email='*****@*****.**',
                                            password='******',
                                            type=UserType.DELETED.name,
                                            is_active=False)
     return u1, u2, admin, demo, deleted
Beispiel #4
0
        def add_prefix_commands(self, prefix):
            commands = prefix_root(prefix) / "commands"

            with Pause(fs):
                for cmd in os.listdir(commands):
                    with open(commands / cmd) as f:
                        self.add_command(cmd, f.read())
    def test_Test_validates_commands_and_fails_validation(self):
        with Pause(self.fs):
            test = Test("t",
                        NullCommand(),
                        repo_root_setup_command=MockCommand(
                            "", [Func(lambda _: False, "lambda")]))
            with pytest.raises(ValidationException) as exc_info:
                test.run()

            assert_that(exc_info.value.args[0]).starts_with(
                "Validation failed: lambda")

            multiline = """FooBar

                        Hello (World)"""

            test = Test("t",
                        NullCommand(),
                        setup_command=MockCommand(multiline,
                                                  [Include("hello")]))
            with pytest.raises(ValidationException) as exc_info:
                test.run()

            assert_that(exc_info.value.args[0]).starts_with(
                "Validation failed: Include(hello)")

            test = Test("t", MockCommand(multiline, [Exclude("Hello")]))
            with pytest.raises(ValidationException) as exc_info:
                test.run()

            assert_that(exc_info.value.args[0]).starts_with(
                "Validation failed: Exclude(Hello)")
    def test_nonzero_return_fails(self):
        with Pause(self.fs):
            test = Test("t", PowershellCommand("Invalid Powershell"))
            with pytest.raises(CalledProcessError) as exc_info:
                test.run()

            assert_that(
                exc_info.value.cmd[-1]).is_equal_to("Invalid Powershell")
    def test_PowershellCommand_calls_process(self):
        with Pause(self.fs):
            command = PowershellCommand("Write-Host -NoNewline 'foobar'")
            assert_that(command.working_directory).is_equal_to(Path.cwd())

            command.run()

            assert_that(command.captured_output).is_equal_to(b"foobar")
    def test_ProcessCommand_calls_process(self):
        with Pause(self.fs):
            command = ProcessCommand(sys.executable, "-c",
                                     "print('foo', end='')")
            assert_that(command.working_directory).is_equal_to(Path.cwd())

            command.run()

            assert_that(command.captured_output).is_equal_to(b"foo")
    def test_Validation_works_with_real_command_output(self):
        with Pause(self.fs):
            command = PowershellCommand("Write-Host -NoNewline This is a test",
                                        validation_checks=[Include("foobar")])

            test = Test("t", command)
            with pytest.raises(ValidationException) as exc_info:
                test.run()

            assert_that(exc_info.value.args[0]).starts_with(
                "Validation failed: Include(foobar)")
 def test_pause_resume_fs_contextmanager(self):
     fake_temp_file = tempfile.NamedTemporaryFile()
     self.assertTrue(self.fs.exists(fake_temp_file.name))
     self.assertTrue(os.path.exists(fake_temp_file.name))
     with Pause(self.fs):
         self.assertTrue(self.fs.exists(fake_temp_file.name))
         self.assertFalse(os.path.exists(fake_temp_file.name))
         real_temp_file = tempfile.NamedTemporaryFile()
         self.assertFalse(self.fs.exists(real_temp_file.name))
         self.assertTrue(os.path.exists(real_temp_file.name))
     self.assertFalse(os.path.exists(real_temp_file.name))
     self.assertTrue(os.path.exists(fake_temp_file.name))
Beispiel #11
0
def test_pause_resume_contextmanager(fs):
    fake_temp_file = tempfile.NamedTemporaryFile()
    assert fs.exists(fake_temp_file.name)
    assert os.path.exists(fake_temp_file.name)
    with Pause(fs):
        assert fs.exists(fake_temp_file.name)
        assert not os.path.exists(fake_temp_file.name)
        real_temp_file = tempfile.NamedTemporaryFile()
        assert not fs.exists(real_temp_file.name)
        assert os.path.exists(real_temp_file.name)
    assert not os.path.exists(real_temp_file.name)
    assert os.path.exists(fake_temp_file.name)
Beispiel #12
0
def compare_files_against_reference(scenario_reference: str,
                                    folder_paths: List[str], fake_fs) -> bool:
    """
    checks if the scenario reference folder exists in the output folder if it doesn't we copy the folders there and
    consider it as reference. This way we can easily recreate the reference files of a scenario by deleting it on disk
    and running the tests
    """
    real_ref_path = os.path.join(scenario_output_path, scenario_reference)

    with Pause(fake_fs):
        if os.path.isdir(real_ref_path):
            # in case the reference path exists we compare the content
            compare_mode = True
        else:
            # otherwise we just write the result to the output folder
            # to be used as new reference for the next run
            compare_mode = False

    result = True
    for folder_path in folder_paths:
        validate_all_mhl_files_against_xml_schema(folder_path)
        assert os.path.isabs(folder_path)
        if compare_mode:
            result &= compare_dir_content(scenario_reference, folder_path)
        else:
            copy_fake_directory_to_real_fs(folder_path, real_ref_path, fake_fs)
            # also copy the log file
            with open('/log.txt', 'rb') as file:
                data = file.read()
            with Pause(fake_fs):
                with open(os.path.join(real_ref_path, 'log.txt'),
                          'w+b') as dst_file:
                    dst_file.write(data)

    if compare_mode:
        # we always assume a log.txt to exists for each scenario, compare it as well to check differences in tool output
        result &= compare_file_content(scenario_reference, '/log.txt')
    return result
    def test_suite_can_set_environment_variables(self):
        with Pause(self.fs):
            assert_that(os.environ).does_not_contain_key("foo")
            initial_environment = os.environ.copy()

            suite_result = TestSuite("s", [
                Test("t", PowershellCommand("Write-Host -NoNewline $env:foo"))
            ], {
                "foo": "bar"
            }).run()

            assert_that(suite_result.test_results[0].command.captured_output
                        ).is_equal_to(b'bar')
            assert_that(os.environ).is_equal_to(initial_environment)
Beispiel #14
0
def test_validate(fs):
    configs = yaml.safe_load(open('/etc/ocf/validate.yaml')).items()

    for shortname, metadata in configs:
        schema_filename = os.path.join('schemas', metadata['schema'])
        config_filename = os.path.join('/etc/ocf', shortname + '.yaml')

        # PyYAML reads YAML date objects as Python date objects, which confuses
        # jsonschema. To get around this, we convert the object to and from
        # JSON, using the str function to convert the date objects to strings.
        config = json.loads(
            json.dumps(yaml.safe_load(open(config_filename)), default=str), )

        with Pause(fs):
            schema = json.load(open(schema_filename))

        jsonschema.validate(config, schema)
    def test_Test_validates_commands_and_passes_validation(self):
        with Pause(self.fs):
            test = Test("t",
                        NullCommand(),
                        repo_root_setup_command=MockCommand(
                            "", [Func(lambda _: True, "lambda")]))
            test.run()

            multiline = """FooBar
            
                        Hello (World)"""
            test = Test("t",
                        NullCommand(),
                        setup_command=MockCommand(multiline,
                                                  [Include("(Worl")]))
            test.run()

            test = Test("t", MockCommand(multiline, [Exclude("wor")]))
            test.run()
Beispiel #16
0
def populate_root(fs, prefix):
    outside_root = prefix_root(prefix)

    with Pause(fs):
        for dir in os.listdir(outside_root):
            if not os.path.isdir(outside_root / dir):
                raise NotImplementedError(
                    "currently only directories are supported at root level")
            if dir == 'commands':
                continue

            try:
                fs.remove_object('/' + dir)
            except FileNotFoundError:
                pass

            fs.add_real_directory(outside_root / dir,
                                  lazy_read=False,
                                  read_only=False,
                                  target_path='/' + dir)
    def test_Validation_can_be_added_to(self):
        with Pause(self.fs):
            command = MockCommand("FooBar", [Exclude("oba")])
            command = command.add_validation_checks([Exclude("Bar")])

            test = Test("t", command)
            with pytest.raises(ValidationException) as exc_info:
                test.run()

            assert_that(exc_info.value.args[0]).starts_with(
                "Validation failed: Exclude(Bar)")

            command = MockCommand("FooBar", [Exclude("oBa")])
            command = command.add_validation_checks([Include("Bar")])
            command.run()
            with pytest.raises(ValidationException) as exc_info:
                command.validate()

            assert_that(exc_info.value.args[0]).starts_with(
                "Validation failed: Exclude(oBa)")
    def users(self, fs):
        from pyfakefs.fake_filesystem_unittest import Pause
        with Pause(fs):
            # Pause(fs) stops the fake filesystem and allows Django access to the common password list
            u1 = User.objects.create_user(username="******",
                                          email="*****@*****.**",
                                          password="******",
                                          remaining_images=20)
            u2 = User.objects.create_user(username="******",
                                          email="*****@*****.**",
                                          password="******")
            admin = User.objects.create_user(username="******",
                                             email="*****@*****.**",
                                             password="******",
                                             type=UserType.ADMIN.name)

            Token.objects.create(user=u1)
            Token.objects.create(user=u2)
            Token.objects.create(user=admin)
            return u1, u2, admin
Beispiel #19
0
def assert_file_unchanged(path, fs, prefix):
    with Pause(fs):
        original = content(prefix_root(prefix) / path.lstrip("/"))

    assert original == content(path)
Beispiel #20
0
 def tearDown(self) -> None:
     with Pause(self.fs):
         for data_model_module in self.data_model_modules:
             os.remove(os.path.join(_DATAMODEL_FOLDER, os.path.split(data_model_module)[-1]))