<commit_before>#
# Copyright (C) 2008-2009 rPath, Inc.
# All rights reserved.
#

import cherrypy
import raa.web
import os
import subprocess
import pwd
import raapluginstest
import raatest
import tempfile
from testutils import mock

import raa.identity
from raa.modules.raasrvplugin import rAASrvPlugin
from raa.lib import command
from raa.rpath_error import RestartWebException
from mint_test.mintraatests import webPluginTest
from rPath.rbasetup import lib as rbasetup_lib
from rPath.rbasetup.srv import rbasetup as rbasetup_srv

from mint import config
from mint import helperfuncs
from mint import mint_error
from mint import shimclient
from mint import rmake_setup

MINT_CONFIG_ROOT="""
# A file that approxiamates what the setup plugin will read.

# This is rBuilder
productName             rBuilder

# Use SQLite backend
dbDriver                sqlite
dbPath                  /srv/rbuilder/data/db

# Conary repository paths
reposPath               /srv/rbuilder/repos/
reposContentsDir        /srv/rbuilder/repos/%s/contents/

# Base directory (as seen by webserver)
basePath                /

# Support contact information
supportContactTXT       your local system administrator
supportContactHTML      <p>your local system administrator</p>

# Group template label
groupApplianceLabel     rap.rpath.com@rpath:linux-1

# This gets overridden by the rBuilder-generated configuration
configured              False
companyName             Your Company Name Here
corpSite                http://www.example.com/
defaultBranch           yournamespace:yourtag
namespace               yournamespace

# Miscellany
authUser                mintauth
authPass                <PASSWORD>
debugMode               False

# Buildtypes that are not supported (remove and use at your own risk)
excludeBuildTypes       STUB_IMAGE
excludeBuildTypes       NETBOOT_IMAGE
excludeBuildTypes       PARALLELS
excludeBuildTypes       AMI

# Activate Internal Conary Proxy for rBuilder
useInternalConaryProxy  True

# include our "generated" file; to be replaced when loading
includeConfigFile %(testGeneratedFile)s
"""

FAKE_GENERATED_CONFIG = """
configured                True
hostName                  justified
siteDomainName            mumu.com
companyName               Trancentral
corpSite                  http://www.mumu.com/
namespace                 klf
projectDomainName         mumu.com
SSL                       True
secureHost                justified.mumu.com
bugsEmail                 <EMAIL>
adminMail                 <EMAIL>
requireSigs               False
authPass                  <PASSWORD>
reposDBDriver             sqlite
reposDBPath               /what/time/is/%s/love
"""

DEFAULT_STARTING_CONFIG = dict(hostName='justified',
                               siteDomainName='mumu.com',
                               namespace='yournamespace',
                               requireSigs=False,
                               )

class rBASetupTest(raatest.rAATest):

    def _createTempfile(self):
        fd, tmpfilename = tempfile.mkstemp()
        os.close(fd)
        return tmpfilename

    def _removeFile(self, fn):
        if fn:
            try:
                os.unlink(fn)
            except Exception, e:
                print "Failed to unlink %s during test (reason: %s)" % \
                        (self.testconfigfile, str(e))

    def setUp(self):
        # Save for later
        self.oldinit = rbasetup_srv.rBASetup.__init__
        self.oldConfig = config.RBUILDER_CONFIG
        self.oldGeneratedConfig = config.RBUILDER_GENERATED_CONFIG

        # Mock out a ton of crap by default
        self.mockOSSystem = mock.MockObject()
        self.mockOSSetUID = mock.MockObject()
        self.mockOSSetGID = mock.MockObject()
        self.mockOSSetGroups = mock.MockObject()
        self.mockOSFork = mock.MockObject()
        self.mockOS_exit = mock.MockObject()
        self.mockOSwaitpid = mock.MockObject()
        self.mockOSchown = mock.MockObject()
        self.mockpwdGetPwNam = mock.MockObject()
        self.mockShimMintClient = mock.MockObject()
        self.mockPipe = mock.MockObject()
        self.mock(os, 'system', self.mockOSSystem)
        self.mock(os, 'setuid', self.mockOSSetUID)
        self.mock(os, 'setgid', self.mockOSSetGID)
        self.mock(os, 'setgroups', self.mockOSSetGroups)
        self.mock(os, 'fork', self.mockOSFork)
        self.mock(os, '_exit', self.mockOS_exit)
        self.mock(os, 'waitpid', self.mockOSwaitpid)
        self.mock(os, 'chown', self.mockOSchown)
        self.mock(pwd, 'get<PASSWORD>', self.mockpwdGetPwNam)
        self.mock(shimclient, 'ShimMintClient', self.mockShimMintClient)
        self.mock(subprocess, 'PIPE', self.mockPipe)

        # Make fork always pretend to be a child
        self.mockOSFork._mock.setDefaultReturn(0)

        # Make sure getpwname returns something stable
        self.mockpwdGetPwNam._mock.setReturn(
                ('apache', 'x', 48, 48, 'Apache', '/var/www', '/sbin/nologin'),
                'apache')

        rbasetup_srv.rBASetup.__init__ = lambda *args: None
        self.rbasetupsrv = rbasetup_srv.rBASetup()

        # Create some default configurations to work with for the sake of testing
        config.RBUILDER_CONFIG = self._createTempfile()
        config.RBUILDER_GENERATED_CONFIG = self._createTempfile()

        # Write out our test rbuilder config
        f = open(config.RBUILDER_CONFIG, 'w')
        f.write(MINT_CONFIG_ROOT % {'testGeneratedFile': config.RBUILDER_GENERATED_CONFIG})
        f.close()

        # Write out our test rbuilder generated config
        f = open(config.RBUILDER_GENERATED_CONFIG, 'w')
        f.write("# This file is blank and waiting to be filled in")
        f.close()

        raaFramework = webPluginTest()
        raaFramework.pseudoroot = raa.web.getWebRoot().rbasetup.rBASetup
        self.rbasetupweb = raa.web.getWebRoot().rbasetup.rBASetup
        self.root = raaFramework.pseudoroot
        self.rbasetupweb.server = self.rbasetupweb
        _ignored, self.initialConfigurableOptions = rbasetup_lib.getRBAConfiguration()

        raatest.rAATest.setUp(self)

    def tearDown(self):
        # Cleanup our configuration files
        self._removeFile(config.RBUILDER_CONFIG)
        self._removeFile(config.RBUILDER_GENERATED_CONFIG)

        # Reset
        rbasetup_srv.rBASetup.__init__ = self.oldinit
        config.RBUILDER_CONFIG = self.oldConfig
        config.RBUILDER_GENERATED_CONFIG = self.oldGeneratedConfig

        raatest.rAATest.tearDown(self)
<commit_msg>Fix test now that we require a valid entitlemetns<commit_after>#
# Copyright (C) 2008-2009 rPath, Inc.
# All rights reserved.
#

import cherrypy
import raa.web
import os
import subprocess
import pwd
import raapluginstest
import raatest
import tempfile
from testutils import mock

import raa.identity
from raa.modules.raasrvplugin import rAASrvPlugin
from raa.lib import command
from raa.rpath_error import RestartWebException
from mint_test.mintraatests import webPluginTest
from rPath.rbasetup import lib as rbasetup_lib
from rPath.rbasetup.srv import rbasetup as rbasetup_srv

from mint import config
from mint import helperfuncs
from mint import mint_error
from mint import shimclient
from mint import rmake_setup

MINT_CONFIG_ROOT="""
# A file that approxiamates what the setup plugin will read.

# This is rBuilder
productName             rBuilder

# Use SQLite backend
dbDriver                sqlite
dbPath                  /srv/rbuilder/data/db

# Conary repository paths
reposPath               /srv/rbuilder/repos/
reposContentsDir        /srv/rbuilder/repos/%s/contents/

# Base directory (as seen by webserver)
basePath                /

# Support contact information
supportContactTXT       your local system administrator
supportContactHTML      <p>your local system administrator</p>

# Group template label
groupApplianceLabel     rap.rpath.com@rpath:linux-1

# This gets overridden by the rBuilder-generated configuration
configured              False
companyName             Your Company Name Here
corpSite                http://www.example.com/
defaultBranch           yournamespace:yourtag
namespace               yournamespace

# Miscellany
authUser                mintauth
authPass                <PASSWORD>
debugMode               False

# Buildtypes that are not supported (remove and use at your own risk)
excludeBuildTypes       STUB_IMAGE
excludeBuildTypes       NETBOOT_IMAGE
excludeBuildTypes       PARALLELS
excludeBuildTypes       AMI

# Activate Internal Conary Proxy for rBuilder
useInternalConaryProxy  True

# include our "generated" file; to be replaced when loading
includeConfigFile %(testGeneratedFile)s
"""

FAKE_GENERATED_CONFIG = """
configured                True
hostName                  justified
siteDomainName            mumu.com
companyName               Trancentral
corpSite                  http://www.mumu.com/
namespace                 klf
projectDomainName         mumu.com
SSL                       True
secureHost                justified.mumu.com
bugsEmail                 <EMAIL>
adminMail                 <EMAIL>
requireSigs               False
authPass                  <PASSWORD>
reposDBDriver             sqlite
reposDBPath               /what/time/is/%s/love
"""

DEFAULT_STARTING_CONFIG = dict(hostName='justified',
                               siteDomainName='mumu.com',
                               namespace='yournamespace',
                               requireSigs=False,
                               )

class rBASetupTest(raatest.rAATest):

    def _createTempfile(self):
        fd, tmpfilename = tempfile.mkstemp()
        os.close(fd)
        return tmpfilename

    def _removeFile(self, fn):
        if fn:
            try:
                os.unlink(fn)
            except Exception, e:
                print "Failed to unlink %s during test (reason: %s)" % \
                        (self.testconfigfile, str(e))

    def setUp(self):
        # Save for later
        self.oldinit = rbasetup_srv.rBASetup.__init__
        self.oldConfig = config.RBUILDER_CONFIG
        self.oldGeneratedConfig = config.RBUILDER_GENERATED_CONFIG

        # Mock out a ton of crap by default
        self.mockOSSystem = mock.MockObject()
        self.mockOSSetUID = mock.MockObject()
        self.mockOSSetGID = mock.MockObject()
        self.mockOSSetGroups = mock.MockObject()
        self.mockOSFork = mock.MockObject()
        self.mockOS_exit = mock.MockObject()
        self.mockOSwaitpid = mock.MockObject()
        self.mockOSchown = mock.MockObject()
        self.mockpwdGetPwNam = mock.MockObject()
        self.mockShimMintClient = mock.MockObject()
        self.mockPipe = mock.MockObject()
        self.mock(os, 'system', self.mockOSSystem)
        self.mock(os, 'setuid', self.mockOSSetUID)
        self.mock(os, 'setgid', self.mockOSSetGID)
        self.mock(os, 'setgroups', self.mockOSSetGroups)
        self.mock(os, 'fork', self.mockOSFork)
        self.mock(os, '_exit', self.mockOS_exit)
        self.mock(os, 'waitpid', self.mockOSwaitpid)
        self.mock(os, 'chown', self.mockOSchown)
        self.mock(pwd, 'getpwnam', self.mockpwdGetPwNam)
        self.mock(shimclient, 'ShimMintClient', self.mockShimMintClient)
        self.mock(subprocess, 'PIPE', self.mockPipe)

        # Make fork always pretend to be a child
        self.mockOSFork._mock.setDefaultReturn(0)

        # Make sure getpwname returns something stable
        self.mockpwdGetPwNam._mock.setReturn(
                ('apache', 'x', 48, 48, 'Apache', '/var/www', '/sbin/nologin'),
                'apache')

        rbasetup_srv.rBASetup.__init__ = lambda *args: None
        self.rbasetupsrv = rbasetup_srv.rBASetup()

        # Create some default configurations to work with for the sake of testing
        config.RBUILDER_CONFIG = self._createTempfile()
        config.RBUILDER_GENERATED_CONFIG = self._createTempfile()

        # Write out our test rbuilder config
        f = open(config.RBUILDER_CONFIG, 'w')
        f.write(MINT_CONFIG_ROOT % {'testGeneratedFile': config.RBUILDER_GENERATED_CONFIG})
        f.close()

        # Write out our test rbuilder generated config
        f = open(config.RBUILDER_GENERATED_CONFIG, 'w')
        f.write("# This file is blank and waiting to be filled in")
        f.close()

        raaFramework = webPluginTest()
        raaFramework.pseudoroot = raa.web.getWebRoot().rbasetup.rBASetup
        self.rbasetupweb = raa.web.getWebRoot().rbasetup.rBASetup
        self.rbasetupweb.validateNewEntitlement = lambda *args, **kwargs: {}
        self.root = raaFramework.pseudoroot
        self.rbasetupweb.server = self.rbasetupweb
        _ignored, self.initialConfigurableOptions = rbasetup_lib.getRBAConfiguration()

        raatest.rAATest.setUp(self)

    def tearDown(self):
        # Cleanup our configuration files
        self._removeFile(config.RBUILDER_CONFIG)
        self._removeFile(config.RBUILDER_GENERATED_CONFIG)

        # Reset
        rbasetup_srv.rBASetup.__init__ = self.oldinit
        config.RBUILDER_CONFIG = self.oldConfig
        config.RBUILDER_GENERATED_CONFIG = self.oldGeneratedConfig

        raatest.rAATest.tearDown(self)

{'fraction_non_alphanumeric': 0.0757002271006813, 'fraction_numerical': 0.0021196063588190765, 'mean_word_length': 2.4092903225806452, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2761577', 'n_tokens_mistral': 3945, 'n_tokens_neox': 3878, 'n_words': 916}