Exemplo n.º 1
0
    def test_hooks_exist(self):
        self.assertThat(self.hooks_dir.join('no-such-hook'),
                        matchers.Not(matchers.FileExists()))

        for hook in self.fake_hooks:
            hook_path = self.hooks_dir.join(hook)
            self.assertThat(hook_path, matchers.FileExists())
    def test_cleanup_changed(self):
        with tempfile.NamedTemporaryFile(delete=False) as f:
            f.write(json.dumps([self.data]))
            f.flush()
            self.env['HEAT_SHELL_CONFIG'] = f.name

            returncode, stdout, stderr = self.run_cmd([self.cleanup_path],
                                                      self.env)

        # on the first run, abcdef001.json is written out, no docker calls made
        configs_path = os.path.join(self.env['HEAT_DOCKER_CMD_WORKING'],
                                    'abcdef001.json')
        self.assertThat(configs_path, matchers.FileExists())
        self.assertThat(self.test_state_path,
                        matchers.Not(matchers.FileExists()))

        # run again with changed config data
        new_data = copy.deepcopy(self.data)
        new_data['config']['web']['image'] = 'yyy'
        with tempfile.NamedTemporaryFile(delete=False) as f:
            f.write(json.dumps([new_data]))
            f.flush()
            self.env['HEAT_SHELL_CONFIG'] = f.name

            returncode, stdout, stderr = self.run_cmd([self.cleanup_path],
                                                      self.env)

        # on the second run, abcdef001.json is written with the new data,
        # docker rm is run on both containers
        configs_path = os.path.join(self.env['HEAT_DOCKER_CMD_WORKING'],
                                    'abcdef001.json')
        self.assertThat(configs_path, matchers.FileExists())
        state_0 = self.json_from_file(self.test_state_path)
        state_1 = self.json_from_file('%s_1' % self.test_state_path)
        self.assertEqual([
            self.fake_tool_path,
            'rm',
            '-f',
            'abcdef001__web',
        ], state_0['args'])
        self.assertEqual([
            self.fake_tool_path,
            'rm',
            '-f',
            'abcdef001__db',
        ], state_1['args'])
Exemplo n.º 3
0
    def test_xdelta3(self):
        self.generate_snap_pair()
        base_delta = deltas.XDelta3Generator(source_path=self.source_file,
                                             target_path=self.target_file)
        path = base_delta.make_delta(is_for_test=True)

        self.assertThat(path, m.FileExists())
        expect_path = "{}.{}".format(base_delta.target_path,
                                     base_delta.delta_file_extname)
        self.assertThat(path, m.Equals(expect_path))
Exemplo n.º 4
0
    def test_execute(self):

        with mock.patch.object(os.path, 'isfile', return_value=True):
            self.assertIsNone(
                renamer.execute('/tmp/test_file.txt', 'Test_file.txt'))

        tempfile = self.create_tempfiles([('test_file', 'test data')])[0]
        new_file = os.path.join(os.path.dirname(tempfile), 'other_file.conf')
        renamer.execute(tempfile, new_file)
        self.assertThat(new_file, matchers.FileExists())
        self.assertThat(tempfile, matchers.Not(matchers.FileExists()))

        tempfiles = self.create_tempfiles([('test_file', 'test data'),
                                           ('other_file', 'test data')])
        self.CONF.set_override('overwrite_file_enabled', True)
        renamer.execute(tempfiles[0], tempfiles[1])
        self.assertThat(tempfiles[1], matchers.FileExists())
        self.assertThat(tempfiles[0], matchers.Not(matchers.FileExists()))

        tempfile = self.create_tempfiles([('my_file', 'test data')])[0]
        new_file = os.path.join(os.path.dirname(tempfile), 'alt_file.conf')
        self.CONF.set_override('dryrun', True)
        renamer.execute(tempfile, new_file)
        self.assertThat(tempfile, matchers.FileExists())
        self.assertThat(new_file, matchers.Not(matchers.FileExists()))
Exemplo n.º 5
0
    def test_xdelta3_with_custom_output_dir(self):
        self.generate_snap_pair()
        base_delta = deltas.XDelta3Generator(source_path=self.source_file,
                                             target_path=self.target_file)
        delta_filename = "{}.{}".format(
            os.path.split(base_delta.target_path)[1],
            base_delta.delta_file_extname)

        existed_output_dir = self.useFixture(fixtures.TempDir()).path
        path = base_delta.make_delta(existed_output_dir, is_for_test=True)

        expect_path = os.path.join(existed_output_dir, delta_filename)
        self.assertThat(path, m.FileExists())
        self.assertThat(path, m.Equals(expect_path))

        none_existed_output_dir = (self.useFixture(fixtures.TempDir()).path +
                                   "/whatever/")
        path = base_delta.make_delta(none_existed_output_dir, is_for_test=True)

        expect_path = os.path.join(none_existed_output_dir, delta_filename)
        self.assertThat(path, m.FileExists())
        self.assertThat(path, m.Equals(expect_path))
Exemplo n.º 6
0
    def test_xdelta3_with_progress_indicator(self):
        self.generate_snap_pair()
        base_delta = deltas.XDelta3Generator(source_path=self.source_file,
                                             target_path=self.target_file)

        message = "creating delta file from {!r}...".format(self.source_file)
        maxval = 10
        progress_indicator = ProgressBar(widgets=[message,
                                                  AnimatedMarker()],
                                         maxval=maxval)
        progress_indicator.start()

        path = base_delta.make_delta(progress_indicator=progress_indicator,
                                     is_for_test=True)
        progress_indicator.finish()

        self.assertThat(path, m.FileExists())
        expect_path = "{}.{}".format(base_delta.target_path,
                                     base_delta.delta_file_extname)
        self.assertThat(path, m.Equals(expect_path))
Exemplo n.º 7
0
    def test_run_heat_config(self):

        with self.write_config_file(self.data) as config_file:

            env = os.environ.copy()
            env.update({
                'HEAT_KUBELET_MANIFESTS': self.manifests_dir.join(),
                'HEAT_SHELL_CONFIG': config_file.name
            })
            returncode, stdout, stderr = self.run_cmd(
                [self.heat_config_kubelet_path], env)

            self.assertEqual(0, returncode, stderr)

        for config in self.data:
            manifest_name = '%s.json' % config['id']
            manifest_path = self.manifests_dir.join(manifest_name)
            self.assertThat(manifest_path, matchers.FileExists())

            #manifest file should match manifest config
            self.assertEqual(config['config'],
                             self.json_from_file(manifest_path))
Exemplo n.º 8
0
    def test_run_heat_config(self, mock_request):
        mock_request.register_uri('POST', 'mock://192.0.2.2/foo')
        mock_request.register_uri('POST', 'mock://192.0.2.3/foo')

        with self.write_config_file(self.data) as config_file:

            env = os.environ.copy()
            env.update({
                'HEAT_CONFIG_HOOKS': self.hooks_dir.join(),
                'HEAT_SHELL_CONFIG': config_file.name
            })
            returncode, stdout, stderr = self.run_cmd([self.heat_config_path],
                                                      env)

            self.assertEqual(0, returncode, stderr)

        for config in self.data:
            hook = config['group']
            hook_path = self.hooks_dir.join(hook)
            stdin_path = self.hooks_dir.join('%s.stdin' % hook)
            stdout_path = self.hooks_dir.join('%s.stdout' % hook)

            if hook == 'no-such-hook':
                self.assertThat(hook_path, matchers.Not(matchers.FileExists()))
                self.assertThat(stdin_path,
                                matchers.Not(matchers.FileExists()))
                self.assertThat(stdout_path,
                                matchers.Not(matchers.FileExists()))
                continue

            self.assertTrue(hook_path, matchers.FileExists())
            self.assertTrue(stdin_path, matchers.FileExists())
            self.assertTrue(stdout_path, matchers.FileExists())

            # parsed stdin should match the config item
            self.assertEqual(config, self.json_from_file(stdin_path))

            self.assertEqual(self.outputs[hook],
                             self.json_from_file(stdout_path))
Exemplo n.º 9
0
    def test_run_heat_config(self):

        returncode, stdout, stderr = self.run_heat_config(self.data)

        for config in self.data:
            hook = config['group']
            stdin_path = self.hooks_dir.join('%s.stdin' % hook)
            stdout_path = self.hooks_dir.join('%s.stdout' % hook)
            deployed_file = self.deployed_dir.join('%s.json' % config['id'])
            notify_file = self.deployed_dir.join('%s.notify.json' %
                                                 config['id'])

            if hook == 'no-such-hook':
                self.assertThat(stdin_path,
                                matchers.Not(matchers.FileExists()))
                self.assertThat(stdout_path,
                                matchers.Not(matchers.FileExists()))
                continue

            # parsed stdin should match the config item
            self.assertThat(stdin_path, matchers.FileExists())
            self.assertEqual(config, self.json_from_file(stdin_path))

            # parsed stdin should match the written deployed file
            self.assertEqual(config, self.json_from_file(deployed_file))

            if hook != 'hook-raises':
                self.assertEqual(self.outputs[hook],
                                 self.json_from_file(notify_file))
                self.assertEqual(self.outputs[hook],
                                 self.json_from_file(stdout_path))
                self.assertThat(stdout_path, matchers.FileExists())
                os.remove(stdout_path)
            else:
                notify_data = self.json_from_file(notify_file)
                self.assertEqual(
                    self.outputs[hook]['deploy_status_code'],
                    six.text_type(notify_data['deploy_status_code']))
                self.assertIn(self.outputs[hook]['deploy_stderr'],
                              notify_data['deploy_stderr'])

            # clean up files in preparation for second run
            os.remove(stdin_path)

        # run again with no changes, assert no new files
        self.run_heat_config(self.data)
        for config in self.data:
            hook = config['group']
            stdin_path = self.hooks_dir.join('%s.stdin' % hook)
            stdout_path = self.hooks_dir.join('%s.stdout' % hook)

            self.assertThat(stdin_path, matchers.Not(matchers.FileExists()))
            self.assertThat(stdout_path, matchers.Not(matchers.FileExists()))

        # run again changing the puppet config
        data = copy.deepcopy(self.data)
        for config in data:
            if config['id'] == '4444':
                config['id'] = '44444444'
        self.run_heat_config(data)
        for config in self.data:
            hook = config['group']
            stdin_path = self.hooks_dir.join('%s.stdin' % hook)
            stdout_path = self.hooks_dir.join('%s.stdout' % hook)

            if hook == 'puppet':
                self.assertThat(stdin_path, matchers.FileExists())
                self.assertThat(stdout_path, matchers.FileExists())
            else:
                self.assertThat(stdin_path,
                                matchers.Not(matchers.FileExists()))
                self.assertThat(stdout_path,
                                matchers.Not(matchers.FileExists()))

        # run again with a different deployed_dir
        old_deployed_dir = self.deployed_dir
        self.env['HEAT_CONFIG_DEPLOYED_OLD'] = old_deployed_dir.join()
        self.deployed_dir = self.useFixture(fixtures.TempDir())
        # make sure the new deployed_dir doesn't exist to trigger the migration
        shutil.rmtree(self.deployed_dir.join())

        self.run_heat_config(data)
        for config in self.data:
            hook = config['group']
            if hook in ('no-such-hook', 'hook-raises'):
                continue
            deployed_file = self.deployed_dir.join('%s.json' % config['id'])
            old_deployed_file = old_deployed_dir.join('%s.json' % config['id'])
            self.assertEqual(config, self.json_from_file(deployed_file))
            self.assertThat(old_deployed_file,
                            matchers.Not(matchers.FileExists()))
Exemplo n.º 10
0
    def test_run_heat_config(self):

        self.run_heat_config(self.data)

        for config in self.data:
            hook = config['group']
            stdin_path = self.hooks_dir.join('%s.stdin' % hook)
            stdout_path = self.hooks_dir.join('%s.stdout' % hook)
            deployed_file = self.deployed_dir.join('%s.json' % config['id'])

            if hook == 'no-such-hook':
                self.assertThat(stdin_path,
                                matchers.Not(matchers.FileExists()))
                self.assertThat(stdout_path,
                                matchers.Not(matchers.FileExists()))
                continue

            self.assertThat(stdin_path, matchers.FileExists())
            self.assertThat(stdout_path, matchers.FileExists())

            # parsed stdin should match the config item
            self.assertEqual(config, self.json_from_file(stdin_path))

            # parsed stdin should match the written deployed file
            self.assertEqual(config, self.json_from_file(deployed_file))

            self.assertEqual(self.outputs[hook],
                             self.json_from_file(stdout_path))

            # clean up files in preperation for second run
            os.remove(stdin_path)
            os.remove(stdout_path)

        # run again with no changes, assert no new files
        self.run_heat_config(self.data)
        for config in self.data:
            hook = config['group']
            stdin_path = self.hooks_dir.join('%s.stdin' % hook)
            stdout_path = self.hooks_dir.join('%s.stdout' % hook)

            self.assertThat(stdin_path, matchers.Not(matchers.FileExists()))
            self.assertThat(stdout_path, matchers.Not(matchers.FileExists()))

        # run again changing the puppet config
        data = copy.deepcopy(self.data)
        for config in data:
            if config['id'] == '4444':
                config['id'] = '44444444'
        self.run_heat_config(data)
        for config in self.data:
            hook = config['group']
            stdin_path = self.hooks_dir.join('%s.stdin' % hook)
            stdout_path = self.hooks_dir.join('%s.stdout' % hook)

            if hook == 'puppet':
                self.assertThat(stdin_path, matchers.FileExists())
                self.assertThat(stdout_path, matchers.FileExists())
            else:
                self.assertThat(stdin_path,
                                matchers.Not(matchers.FileExists()))
                self.assertThat(stdout_path,
                                matchers.Not(matchers.FileExists()))