<commit_before>#!/usr/bin/python2.4
#
# Copyright (c) 2008 rPath, Inc.
#

import os, sys
# SUB_LEVEL is the number of levels beneath the main test dir that we are
SUB_LEVEL = 1
mainPath = os.path.dirname(os.path.abspath(__file__))
for sub in range(SUB_LEVEL):
    mainPath = os.path.dirname(mainPath)
if mainPath not in sys.path: sys.path.append(mainPath)
import testsuite
testsuite.setup()

import os, signal
import SimpleHTTPServer

import StringIO
import conary.lib.util
from conary.conaryclient import filetypes
from conary import changelog
from conary import versions

import fixtures
import mint_rephelp

from mint import packagecreator
from mint.web import whizzyupload
from mint_rephelp import MINT_HOST, MINT_DOMAIN
from mint.server import deriveBaseFunc
import mint.mint_error
from conary.conarycfg import ConaryConfiguration
from conary import conaryclient
from factory_test.factorydatatest import basicXmlDef
import pcreator
from pcreator.factorydata import FactoryDefinition
from pcreator import server as pcreatorServer

from conary_test import resources as conaryTestResources

class PkgCreatorTest(fixtures.FixturedUnitTest):
    """ Unit Tests the MintClient and corresponding MintServer methods, but mocks
    out certain pcreator.backend methods."""

    def _set_up_path(self):
        self.uploadSes = u'99999'
        conary.lib.util.mkdirChain(os.path.join(self.cfg.dataPath, 'tmp',
                'rb-pc-upload-%s' % self.uploadSes))
        self.client = self.getClient("owner")

    def tearDown(self):
        if hasattr(self, 'uploadSes') and self.uploadSes:
            conary.lib.util.rmtree(packagecreator.getUploadDir(self.cfg, self.uploadSes))
            self.uploadSes = None
        fixtures.FixturedUnitTest.tearDown(self)

    @fixtures.fixture('Full')
    @testsuite.context('more_cowbell')
    def testPollUploadStatus(self, db, data):
        self._set_up_path()
        # Have to bypass the auth checks
        pollUploadStatus = deriveBaseFunc(self.client.server._server.pollUploadStatus)

        #Try to fetch status for an id that doesn't exist
        self.assertRaises(mint.mint_error.PermissionDenied, pollUploadStatus, self.client.server._server, '99998', 'some-fieldname')

        # We're not going to worry about all the different permutations at this
        # level, since they're covered by the library tests
        ret = pollUploadStatus(self.client.server._server, 99999, 'some-fieldname')
        self.assertEquals(ret['read'], 0)
        self.assertEquals(ret['finished'], {})
        assert ret['currenttime'], 'The starttime should be non-zero'

    @fixtures.fixture('Full')
    @testsuite.context('more_cowbell')
    def testCancelUpload(self, db, data):
        self._set_up_path()
        # Have to bypass the auth checks
        cancelUploadProcess = deriveBaseFunc(self.client.server._server.cancelUploadProcess)    

        assert not cancelUploadProcess(self.client.server._server, '99998', [])

        assert cancelUploadProcess(self.client.server._server, self.uploadSes, ['some-fieldname'])

    @fixtures.fixture('Full')
    def testCreatePkgTmpDir(self, db, data):
        self.client = self.getClient('owner')
        self.uploadSes = self.client.createPackageTmpDir()

        #Check to see that the directory was in fact created
        wd = packagecreator.getUploadDir(self.cfg, self.uploadSes)

        assert os.path.isdir(wd), "The working directory for createPackage was not created"

    @fixtures.fixture('Full')
    def testStartSessionDir(self, db, data):
        raise testsuite.SkipTestException('session dir has been broken out from upload dir. this test needs to be corrected')
        class MockProdDef(object):
            name = 'mock'
            getSearchPaths = lambda *args, **kwargs: {}
            getFactorySources = lambda *args, **kwargs: {}
            getStages = lambda x: [x]
            getLabelForStage = lambda *args: 'localhost@rpl:linux'
        self.mock(pcreator.backend.BaseBackend, '_loadProductDefinitionFromProdDefDict', lambda *args: MockProdDef())
        self.client = self.getClient('owner')
        self.uploadSes = self.client.createPackageTmpDir()

        wd = packagecreator.getUploadDir(self.cfg, self.uploadSes)

        pc = packagecreator.getPackageCreatorClient(self.cfg, ('owner', "%dpass" % data['owner']))
        project = self.client.getProject(data['projectId'])
        cfg = project.getConaryConfig()
        cfg['name'] = 'owner'
        cfg['contact'] = ''
        mincfg = packagecreator.MinimalConaryConfiguration( cfg)

        pc.startSession({}, mincfg)
        # ensure we have a viable session
        self.assertEquals(os.listdir(os.path.join(wd, 'owner')), [self.uploadSes])
        self.assertEquals(wd.endswith(self.uploadSes), True)
        refKeys = ['develStageLabel', 'productDefinition', 'searchPath',
                'factorySources', 'mincfg']
        assert(set(refKeys).issubset(set(os.listdir(os.path.join(wd, 'owner', self.uploadSes)))))


    #
    ## Tests for the package creator client (ui backend)
    #

    def _setup_mocks(self, getPackageFactoriesMethod, writeManifest=True):
        self._set_up_path()

        #Create the manifest files
        wd = packagecreator.getUploadDir(self.cfg, self.uploadSes)
        if writeManifest:
            fup = whizzyupload.fileuploader(wd, 'uploadfile')
            i = open(fup.manifestfile, 'wt')
            i.write('''fieldname='uploadfile'
filename=garbage.txt
tempfile=asdfasdf
content-type=text/plain
''')
            i.close()

        i = open(os.path.join(wd, 'asdfasdf'), 'wt')
        i.write('a' * 350)
        i.close()

        @pcreator.backend.public
        def startSession(s, *args, **kwargs):
            self.assertEquals(args[0], {'shortname': 'foo', 'version': 'FooV1', 'namespace': 'ns', 'hostname': 'foo.rpath.local2'})
            self.assertEquals(len(args), 3, "The number of arguments to saveSession has changed")
            self.assertEquals(kwargs, {})
            return '88889'

        if getPackageFactoriesMethod:
            getPackageFactoriesMethod._isPublic = True
        self.mock(pcreator.backend.BaseBackend,
                '_getCandidateBuildFactories', getPackageFactoriesMethod)
        self.mock(pcreator.backend.BaseBackend, '_startSession', startSession)

    @fixtures.fixture('Full')
    def testCreatePackage(self, db, data):
        def getCandidateBuildFactories(s, sesH):
            self.assertEquals(sesH, '88889')
            return [('rpm', basicXmlDef, {'a': 'b'})]

        self._setup_mocks(getCandidateBuildFactories)
        projectId = data['projectId']
        sesH, factories = self.client.getPackageFactories(projectId, self.uploadSes, 1)
        self.assertEquals(sesH, '88889')
        self.assertEquals(factories[0][0], 'rpm')
        assert isinstance(factories[0][1], FactoryDefinition)
        self.assertEquals(factories[0][2], {'a': 'b'})

    @fixtures.fixture('Full')
    def testCreatePackagePreExistSession(self, db, data):
        def getCandidateBuildFactories(s, sesH):
            self.assertEquals(sesH, 'session_handle_test')
            return [('rpm', basicXmlDef, {'a': 'b'})]
        self._setup_mocks(getCandidateBuildFactories)
        projectId = data['projectId']
        sesH, factories = self.client.getPackageFactories(projectId, self.uploadSes, 1, 'session_handle_test')
        self.assertEquals(sesH, 'session_handle_test')
        self.assertEquals(factories[0][0], 'rpm')
        assert isinstance(factories[0][1], FactoryDefinition)
        self.assertEquals(factories[0][2], {'a': 'b'})

    @fixtures.fixture('Full')
    def testCreatePackageNoManifest(self, db, data):
        self._setup_mocks(None, writeManifest=False)
        projectId = data['projectId']
        self.assertRaises(mint.mint_error.PackageCreatorError, self.client.getPackageFactories, projectId, self.uploadSes, 1)

    @fixtures.fixture('Full')
    def testCreatePackageFactoriesError(self, db, data):
        def raises(x, sesH):
            self.assertEquals(sesH, '88889')
            raise packagecreator.errors.UnsupportedFileFormat('bogus error')<commit_msg>Add test for getting factory data from preexisting troves (RDST-320)<commit_after>#!/usr/bin/python2.4
#
# Copyright (c) 2008 rPath, Inc.
#

import os, sys
# SUB_LEVEL is the number of levels beneath the main test dir that we are
SUB_LEVEL = 1
mainPath = os.path.dirname(os.path.abspath(__file__))
for sub in range(SUB_LEVEL):
    mainPath = os.path.dirname(mainPath)
if mainPath not in sys.path: sys.path.append(mainPath)
import testsuite
testsuite.setup()

import os, signal
import SimpleHTTPServer

import StringIO
import conary.lib.util
from conary.conaryclient import filetypes
from conary import changelog
from conary import versions

import fixtures
import mint_rephelp

from mint import packagecreator
from mint.web import whizzyupload
from mint_rephelp import MINT_HOST, MINT_DOMAIN
from mint.server import deriveBaseFunc
import mint.mint_error
from conary.conarycfg import ConaryConfiguration
from conary import conaryclient
from factory_test.factorydatatest import basicXmlDef
import pcreator
from pcreator.factorydata import FactoryDefinition
from pcreator import server as pcreatorServer

from conary_test import resources as conaryTestResources

class PkgCreatorTest(fixtures.FixturedUnitTest):
    """ Unit Tests the MintClient and corresponding MintServer methods, but mocks
    out certain pcreator.backend methods."""

    def _set_up_path(self):
        self.uploadSes = u'99999'
        conary.lib.util.mkdirChain(os.path.join(self.cfg.dataPath, 'tmp',
                'rb-pc-upload-%s' % self.uploadSes))
        self.client = self.getClient("owner")

    def tearDown(self):
        if hasattr(self, 'uploadSes') and self.uploadSes:
            conary.lib.util.rmtree(packagecreator.getUploadDir(self.cfg, self.uploadSes))
            self.uploadSes = None
        fixtures.FixturedUnitTest.tearDown(self)

    @fixtures.fixture('Full')
    @testsuite.context('more_cowbell')
    def testPollUploadStatus(self, db, data):
        self._set_up_path()
        # Have to bypass the auth checks
        pollUploadStatus = deriveBaseFunc(self.client.server._server.pollUploadStatus)

        #Try to fetch status for an id that doesn't exist
        self.assertRaises(mint.mint_error.PermissionDenied, pollUploadStatus, self.client.server._server, '99998', 'some-fieldname')

        # We're not going to worry about all the different permutations at this
        # level, since they're covered by the library tests
        ret = pollUploadStatus(self.client.server._server, 99999, 'some-fieldname')
        self.assertEquals(ret['read'], 0)
        self.assertEquals(ret['finished'], {})
        assert ret['currenttime'], 'The starttime should be non-zero'

    @fixtures.fixture('Full')
    @testsuite.context('more_cowbell')
    def testCancelUpload(self, db, data):
        self._set_up_path()
        # Have to bypass the auth checks
        cancelUploadProcess = deriveBaseFunc(self.client.server._server.cancelUploadProcess)    

        assert not cancelUploadProcess(self.client.server._server, '99998', [])

        assert cancelUploadProcess(self.client.server._server, self.uploadSes, ['some-fieldname'])

    @fixtures.fixture('Full')
    def testCreatePkgTmpDir(self, db, data):
        self.client = self.getClient('owner')
        self.uploadSes = self.client.createPackageTmpDir()

        #Check to see that the directory was in fact created
        wd = packagecreator.getUploadDir(self.cfg, self.uploadSes)

        assert os.path.isdir(wd), "The working directory for createPackage was not created"

    @fixtures.fixture('Full')
    def testStartSessionDir(self, db, data):
        raise testsuite.SkipTestException('session dir has been broken out from upload dir. this test needs to be corrected')
        class MockProdDef(object):
            name = 'mock'
            getSearchPaths = lambda *args, **kwargs: {}
            getFactorySources = lambda *args, **kwargs: {}
            getStages = lambda x: [x]
            getLabelForStage = lambda *args: 'localhost@rpl:linux'
        self.mock(pcreator.backend.BaseBackend, '_loadProductDefinitionFromProdDefDict', lambda *args: MockProdDef())
        self.client = self.getClient('owner')
        self.uploadSes = self.client.createPackageTmpDir()

        wd = packagecreator.getUploadDir(self.cfg, self.uploadSes)

        pc = packagecreator.getPackageCreatorClient(self.cfg, ('owner', "%dpass" % data['owner']))
        project = self.client.getProject(data['projectId'])
        cfg = project.getConaryConfig()
        cfg['name'] = 'owner'
        cfg['contact'] = ''
        mincfg = packagecreator.MinimalConaryConfiguration( cfg)

        pc.startSession({}, mincfg)
        # ensure we have a viable session
        self.assertEquals(os.listdir(os.path.join(wd, 'owner')), [self.uploadSes])
        self.assertEquals(wd.endswith(self.uploadSes), True)
        refKeys = ['develStageLabel', 'productDefinition', 'searchPath',
                'factorySources', 'mincfg']
        assert(set(refKeys).issubset(set(os.listdir(os.path.join(wd, 'owner', self.uploadSes)))))


    #
    ## Tests for the package creator client (ui backend)
    #

    def _setup_mocks(self, getPackageFactoriesMethod, writeManifest=True):
        self._set_up_path()

        #Create the manifest files
        wd = packagecreator.getUploadDir(self.cfg, self.uploadSes)
        if writeManifest:
            fup = whizzyupload.fileuploader(wd, 'uploadfile')
            i = open(fup.manifestfile, 'wt')
            i.write('''fieldname='uploadfile'
filename=garbage.txt
tempfile=asdfasdf
content-type=text/plain
''')
            i.close()

        i = open(os.path.join(wd, 'asdfasdf'), 'wt')
        i.write('a' * 350)
        i.close()

        @pcreator.backend.public
        def startSession(s, *args, **kwargs):
            self.assertEquals(args[0], {'shortname': 'foo', 'version': 'FooV1', 'namespace': 'ns', 'hostname': 'foo.rpath.local2'})
            self.assertEquals(len(args), 3, "The number of arguments to saveSession has changed")
            self.assertEquals(kwargs, {})
            return '88889'

        if getPackageFactoriesMethod:
            getPackageFactoriesMethod._isPublic = True
        self.mock(pcreator.backend.BaseBackend,
                '_getCandidateBuildFactories', getPackageFactoriesMethod)
        self.mock(pcreator.backend.BaseBackend, '_startSession', startSession)

    @fixtures.fixture('Full')
    def testCreatePackage(self, db, data):
        def getCandidateBuildFactories(s, sesH):
            self.assertEquals(sesH, '88889')
            return [('rpm', basicXmlDef, {'a': 'b'})]

        self._setup_mocks(getCandidateBuildFactories)
        projectId = data['projectId']
        sesH, factories = self.client.getPackageFactories(projectId, self.uploadSes, 1)
        self.assertEquals(sesH, '88889')
        self.assertEquals(factories[0][0], 'rpm')
        assert isinstance(factories[0][1], FactoryDefinition)
        self.assertEquals(factories[0][2], {'a': 'b'})

    @fixtures.fixture('Full')
    def testCreatPackagePreexistingTrove(self, db, data):
        def getCandidateBuildFactories(s, sesH):
            self.assertEquals(sesH, 'session-88889')
            return [('rpm', basicXmlDef, {'a': 'b'})]
        self._setup_mocks(getCandidateBuildFactories)
        @pcreator.backend.public
        def startSession(*args):
            return 'session-88889'
        self.mock(pcreator.backend.BaseBackend, '_startSession', startSession)
        projectId = data['projectId']
        sesH, factories = self.client.getPackageFactoriesFromRepoArchive(projectId, 1, 'gina', 'foobar', '<EMAIL>:sdi-1-devel')
        self.assertEquals(sesH, 'session-88889')
        self.assertEquals(factories[0][0], 'rpm')
        assert isinstance(factories[0][1], FactoryDefinition)
        self.assertEquals(factories[0][2], {'a': 'b'})
 
    @fixtures.fixture('Full')
    def testCreatePackagePreExistSession(self, db, data):
        def getCandidateBuildFactories(s, sesH):
            self.assertEquals(sesH, 'session_handle_test')
            return [('rpm', basicXmlDef, {'a': 'b'})]
        self._setup_mocks(getCandidateBuildFactories)
        projectId = data['projectId']
        sesH, factories = self.client.getPackageFactories(projectId, self.uploadSes, 1, 'session_handle_test')
        self.assertEquals(sesH, 'session_handle_test')
        self.assertEquals(factories[0][0], 'rpm')
        assert isinstance(factories[0][1], FactoryDefinition)
        self.assertEquals(factories[0][2], {'a': 'b'})

    @fixtures.fixture('Full')
    def testCreatePackageNoManifest(self, db, data):
        self._setup_mocks(None, writeManifest=False)
        projectId = data['projectId']
        self.assertRaises(mint.mint_error.PackageCreatorError, self.client.getPackageFactories, projectId, self.uploadSes, 1)

    @fixtures.fixture('Full')
    def testCreatePackageFactoriesError(self, db, data):
        def raises(x, sesH):
            self.assertEquals(sesH, '88889')
            raise packagecreator.errors.UnsupportedFileFormat('bogus error')
{'fraction_non_alphanumeric': 0.11452594955059438, 'fraction_numerical': 0.009625978544505654, 'mean_word_length': 3.4129989764585464, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3095456', 'n_tokens_mistral': 5280, 'n_tokens_neox': 5106, 'n_words': 1119}