def test_path(self):
        """
        Result path should be results_dir/hostname/topic

        """
        config = {'results_dir': '/blah'}
        ctx = self.create_ctx()
        o = FileOutput(config)
        path = o._path(ctx)
        self.assertEquals(path, '/blah/%s/%s' % (self.hostname, self.topic))
    def test_mkdirs_already_exists(self, mocked_isdir, mocked_makedirs):
        """
        Tests that mkdirs is still successful if directory already
        exists.

        """
        config = {}
        path = '/path/%s/%s' % (self.hostname, self.topic)
        o = FileOutput(config)
        o._mkdirs(path)
        mocked_makedirs.assert_called_with(path)
    def test_mkdirs(self, mocked):
        """
        When writing the results, subdirectories of hostname
        and topic should be created

        """
        config = {}
        path = '/path/%s/%s' % (self.hostname, self.topic)
        o = FileOutput(config)
        o._mkdirs(path)
        mocked.assert_called_with(path)
    def test_mkdirs_other_error(self, mocked_isdir, mocked_makedirs):
        """
        Tests that mkdirs raises OSError if not related to directory
        already existing.

        """
        config = {}
        path = '/path/%s/%s' % (self.hostname, self.topic)
        o = FileOutput(config)
        with self.assertRaises(OSError):
            o._mkdirs(path)
    def test_filename(self):
        """
        Filename should be started_to_ended_pid

        """
        config = {}
        ctx = self.create_ctx()
        o = FileOutput(config)
        name = o._filename(ctx)
        self.assertEquals(
            name,
            "%s_to_%s_%s.stats" % (self.started_string, self.ended_string, self.pid)
        )
    def test_write(self):
        """
        Tests for the write method

        """
        class FakeStats(object):
            pass

        stats = FakeStats()
        stats.save = mock.Mock()

        config = {}
        ctx = self.create_ctx()
        o = FileOutput(config)
        o.write(ctx, stats)

        stats.save.assert_called_with('%s/%s/%s/%s_to_%s_%s.stats' % (
            '/var/log/os_code_profiler',
            self.hostname,
            self.topic,
            self.started_string,
            self.ended_string,
            self.pid
        ))