def _configure(self, config_yml, dockerArgs={}, dockerOnSocket=False): self.dockerAPI = TrafficLoggingFactory(testtools.FakeDockerServer(**dockerArgs), "docker-") if dockerOnSocket: self.socketPath = self.mktemp() self.dockerServer = reactor.listenUNIX(self.socketPath, self.dockerAPI) else: self.dockerServer = reactor.listenTCP(0, self.dockerAPI) self.dockerPort = self.dockerServer.getHost().port self.config = PluginConfiguration() tmp = self.mktemp() self.config._default_file = tmp fp = FilePath(tmp) fp.setContent(config_yml) self.parser = EndpointParser(self.config) if dockerOnSocket: self.proxyAPI = TrafficLoggingFactory(powerstrip.ServerProtocolFactory( dockerSocket=self.socketPath, config=self.config), "proxy-") else: self.proxyAPI = TrafficLoggingFactory( powerstrip.ServerProtocolFactory( dockerAddr="127.0.0.1", dockerPort=self.dockerPort, config=self.config), "proxy-") self.proxyServer = reactor.listenTCP(0, self.proxyAPI) self.proxyPort = self.proxyServer.getHost().port
def test_twistd(self): """ Should run twistd with the given arguments """ runner = Runner() fake_twistd = FilePath(self.mktemp()) fake_twistd.setContent('#!%s\n' 'import sys, os\n' 'print " ".join(sys.argv[1:])\n' 'print os.environ["FOO"]\n' 'print os.path.abspath(os.curdir)\n' 'sys.stdout.flush()\n' 'sys.stderr.write("error\\n")\n' 'print "stdout"\n' 'sys.exit(4)\n' % sys.executable) fake_twistd.chmod(0777) runner._twistdBin = lambda: fake_twistd.path path = FilePath(self.mktemp()) path.makedirs() d = runner.twistd(['foo', 'bar', 'baz'], env={'FOO': 'foo value'}, path=path.path) def check(result): out, err, code = result self.assertEqual(code, 4) self.assertEqual(out, 'foo bar baz\n' 'foo value\n' '%s\n' 'stdout\n' % path.path) self.assertEqual(err, 'error\n') return d.addCallback(check)
def test_createSSLPort(self): """ If a given valid strport description of an SSL port and the storeID of an extant factory, I{axiomatic port create} creates a new L{SSLPort} with the specified configuration and referring to that factory. The certificate file specified is copied to a path inside the Store's files directory. The port is also powered up on the store for L{IService}. """ pemPath = FilePath(self.mktemp()) pemPath.setContent(CERTIFICATE_DATA + PRIVATEKEY_DATA) store = Store(filesdir=self.mktemp()) factory = DummyFactory(store=store) self.assertSuccessStatus(self._makeConfig(store), [ "create", "--strport", "ssl:8443:certKey=" + pemPath.path + ":privateKey=" + pemPath.path, "--factory-identifier", str(factory.storeID) ]) self.assertEqual("Created.\n", sys.stdout.getvalue()) [ssl] = list(store.query(SSLPort)) self.assertEqual(ssl.portNumber, 8443) self.assertEqual(ssl.certificatePath.getContent(), CERTIFICATE_DATA + PRIVATEKEY_DATA) self.assertIdentical(ssl.factory, factory) self.assertEqual(pemPath.getContent(), CERTIFICATE_DATA + PRIVATEKEY_DATA) self.assertEqual(list(store.interfacesFor(ssl)), [IService])
def test_createSSLPortInconsistentCertificateAndKeyFiles(self): """ If different values are specified for the certificate file and the private key file when creating an SSL port with I{axiomatic port create}, a short error message is written to standard output. This reflects an implementation limitation which may be lifted in the future. """ certPath = FilePath(self.mktemp()) certPath.setContent(CERTIFICATE_DATA) keyPath = FilePath(self.mktemp()) keyPath.setContent(PRIVATEKEY_DATA) store = Store() factory = DummyFactory(store=store) self.assertFailStatus(1, self._makeConfig(store), [ "create", "--strport", "ssl:8443:privateKey=" + keyPath.path + ":certKey=" + certPath.path, "--factory-identifier", str(factory.storeID) ]) self.assertEqual( "You must specify the same file for certKey and privateKey.\n", sys.stdout.getvalue()) self.assertEqual(store.query(SSLPort).count(), 0)
def test_interface(self, cert_and_key): """ ``makeService`` returns an object that provides ``IService``. """ scratch = FilePath(self.mktemp()) scratch.makedirs() cert, key = pem.parse(cert_and_key) config = write_kubernetes_configuration( scratch, cert, key, ) access_key_id_path = FilePath(self.mktemp()) access_key_id_path.setContent(b"foo") secret_access_key_path = FilePath(self.mktemp()) secret_access_key_path.setContent(b"bar") options = Options() options.parseOptions([ b"--domain", b"s4.example.com", b"--kubernetes-namespace", b"testing", b"--endpoint", b"http://localhost:8000/", b"--aws-access-key-id-path", access_key_id_path.path, b"--aws-secret-access-key-path", secret_access_key_path.path, b"--introducer-image", b"introducer", b"--storageserver-image", b"storageserver", b"--kubernetes", b"kubernetes", b"--k8s-context", u"testing", b"--k8s-config", config.path, ]) service = makeService(options) verifyObject(IService, service)
def test_addPyListings(self): """ L{tree.addPyListings} accepts a document with nodes with their I{class} attribute set to I{py-listing} and replaces those nodes with Python source listings from the file given by the node's I{href} attribute. """ listingPath = FilePath(self.mktemp()) listingPath.setContent('def foo():\n pass\n') parent = dom.Element('div') listing = dom.Element('a') listing.setAttribute('href', listingPath.basename()) listing.setAttribute('class', 'py-listing') parent.appendChild(listing) tree.addPyListings(parent, listingPath.dirname()) expected = """\ <div><div class="py-listing"><pre><p class="py-linenumber">1 2 </p><span class="py-src-keyword">def</span> <span class="py-src-identifier">foo</span>(): <span class="py-src-keyword">pass</span> </pre><div class="caption"> - <a href="temp"><span class="filename">temp</span></a></div></div></div>""" self.assertEqual(parent.toxml(), expected)
def load_or_create_client_key(key_file): """Load the ACME account key from a file, creating it if it does not exist. Args: key_file (str): name of the file to use as the account key """ # this is based on txacme.endpoint.load_or_create_client_key, but doesn't # hardcode the 'client.key' filename acme_key_file = FilePath(key_file) if acme_key_file.exists(): logger.info("Loading ACME account key from '%s'", acme_key_file) key = serialization.load_pem_private_key( acme_key_file.getContent(), password=None, backend=default_backend() ) else: logger.info("Saving new ACME account key to '%s'", acme_key_file) key = generate_private_key("rsa") acme_key_file.setContent( key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.TraditionalOpenSSL, encryption_algorithm=serialization.NoEncryption(), ) ) return JWKRSA(key=key)
def test_loadAccountsFromFile(self): """ L{LoadSimulator.fromCommandLine} takes an account loader from the config file and uses it to create user records for use in the simulation. """ accounts = FilePath(self.mktemp()) accounts.setContent("foo,bar,baz,quux\nfoo2,bar2,baz2,quux2\n") config = VALID_CONFIG.copy() config["accounts"] = { "loader": "contrib.performance.loadtest.sim.recordsFromCSVFile", "params": { "path": accounts.path }, } configpath = FilePath(self.mktemp()) configpath.setContent(writePlistToString(config)) io = StringIO() sim = LoadSimulator.fromCommandLine(['--config', configpath.path], io) self.assertEquals(io.getvalue(), "Loaded 2 accounts.\n") self.assertEqual(2, len(sim.records)) self.assertEqual(sim.records[0].uid, 'foo') self.assertEqual(sim.records[0].password, 'bar') self.assertEqual(sim.records[0].commonName, 'baz') self.assertEqual(sim.records[0].email, 'quux') self.assertEqual(sim.records[1].uid, 'foo2') self.assertEqual(sim.records[1].password, 'bar2') self.assertEqual(sim.records[1].commonName, 'baz2') self.assertEqual(sim.records[1].email, 'quux2')
def test_single_run(self): """ Running for the first time successfully reads and parses the configuration. """ yml = """endpoints: # adapters are applied in order "POST /*/containers/create": pre: [flocker, weave] post: [weave, flocker] "DELETE /*/containers/*": pre: [flocker, weave] post: [weave, flocker] adapters: flocker: http://flocker/flocker-adapter weave: http://weave/weave-adapter""" tmp = self.mktemp() self.config._default_file = tmp fp = FilePath(tmp) fp.setContent(yml) self.config.read_and_parse() self.assertEquals((self.config._endpoints, self.config._adapters), ({ "POST /*/containers/create": { "pre": ["flocker", "weave"], "post": ["weave", "flocker"], }, "DELETE /*/containers/*": { "pre": ["flocker", "weave"], "post": ["weave", "flocker"], }, }, { "flocker": "http://flocker/flocker-adapter", "weave": "http://weave/weave-adapter", }))
def test_file(self): """ An existing file has these attributes """ root = FilePath(self.mktemp()) root.setContent('the content') root.chmod(0777) stdout, stderr, code = self.runScript(['inspect'], json.dumps({ 'kind': 'file', 'path': root.path, })) data = json.loads(stdout) self.assertEqual(data['kind'], 'file') self.assertEqual(data['path'], root.path) self.assertEqual(data['exists'], True) self.assertEqual(data['filetype'], 'file') self.assertEqual(data['owner'], pwd.getpwuid(os.geteuid()).pw_name) self.assertEqual(data['group'], grp.getgrgid(os.getegid()).gr_name) self.assertEqual(data['perms'], '0777') root.restat() self.assertEqual(data['ctime'], int(root.statinfo.st_ctime)) self.assertEqual(type(data['ctime']), int) self.assertEqual(data['mtime'], int(root.statinfo.st_mtime)) self.assertEqual(type(data['mtime']), int) self.assertEqual(data['atime'], int(root.statinfo.st_atime)) self.assertEqual(type(data['atime']), int) self.assertEqual(data['sha1'], sha1('the content').hexdigest()) self.assertEqual(data['size'], len('the content'))
def test_createSSLPort(self): """ If a given valid strport description of an SSL port and the storeID of an extant factory, I{axiomatic port create} creates a new L{SSLPort} with the specified configuration and referring to that factory. The certificate file specified is copied to a path inside the Store's files directory. The port is also powered up on the store for L{IService}. """ pemPath = FilePath(self.mktemp()) pemPath.setContent(CERTIFICATE_DATA + PRIVATEKEY_DATA) store = Store(filesdir=self.mktemp()) factory = DummyFactory(store=store) self.assertSuccessStatus( self._makeConfig(store), ["create", "--strport", "ssl:8443:certKey=" + pemPath.path + ":privateKey=" + pemPath.path, "--factory-identifier", str(factory.storeID)]) self.assertEqual("Created.\n", sys.stdout.getvalue()) [ssl] = list(store.query(SSLPort)) self.assertEqual(ssl.portNumber, 8443) self.assertEqual( ssl.certificatePath.getContent(), CERTIFICATE_DATA + PRIVATEKEY_DATA) self.assertIdentical(ssl.factory, factory) self.assertEqual( pemPath.getContent(), CERTIFICATE_DATA + PRIVATEKEY_DATA) self.assertEqual(list(store.interfacesFor(ssl)), [IService])
def test_createSSLPortInconsistentCertificateAndKeyFiles(self): """ If different values are specified for the certificate file and the private key file when creating an SSL port with I{axiomatic port create}, a short error message is written to standard output. This reflects an implementation limitation which may be lifted in the future. """ certPath = FilePath(self.mktemp()) certPath.setContent(CERTIFICATE_DATA) keyPath = FilePath(self.mktemp()) keyPath.setContent(PRIVATEKEY_DATA) store = Store() factory = DummyFactory(store=store) self.assertFailStatus( 1, self._makeConfig(store), ["create", "--strport", "ssl:8443:privateKey=" + keyPath.path + ":certKey=" + certPath.path, "--factory-identifier", str(factory.storeID)]) self.assertEqual( "You must specify the same file for certKey and privateKey.\n", sys.stdout.getvalue()) self.assertEqual(store.query(SSLPort).count(), 0)
def test_loadPopulationParameters(self): """ Client weights and profiles are loaded from the [clients] section of the configuration file specified. """ config = FilePath(self.mktemp()) config.setContent(writePlistToString({ "clients": [{ "software": "contrib.performance.loadtest.ical.OS_X_10_6", "params": {"foo": "bar"}, "profiles": [{ "params": { "interval": 25, "eventStartDistribution": { "type": "contrib.performance.stats.NormalDistribution", "params": { "mu": 123, "sigma": 456, }}}, "class": "contrib.performance.loadtest.profiles.Eventer"}], "weight": 3, }]})) sim = LoadSimulator.fromCommandLine( ['--config', config.path, '--clients', config.path] ) expectedParameters = PopulationParameters() expectedParameters.addClient( 3, ClientType(OS_X_10_6, {"foo": "bar"}, [ProfileType(Eventer, { "interval": 25, "eventStartDistribution": NormalDistribution(123, 456)})])) self.assertEquals(sim.parameters, expectedParameters)
def setUp(self): """ Create a temporary file with a fixed payload of 64 bytes. Create a resource for that file and create a request which will be for that resource. Each test can set a different range header to test different aspects of the implementation. """ path = FilePath(self.mktemp()) # This is just a jumble of random stuff. It's supposed to be a good # set of data for this test, particularly in order to avoid # accidentally seeing the right result by having a byte sequence # repeated at different locations or by having byte values which are # somehow correlated with their position in the string. self.payload = ('\xf8u\xf3E\x8c7\xce\x00\x9e\xb6a0y0S\xf0\xef\xac\xb7' '\xbe\xb5\x17M\x1e\x136k{\x1e\xbe\x0c\x07\x07\t\xd0' '\xbckY\xf5I\x0b\xb8\x88oZ\x1d\x85b\x1a\xcdk\xf2\x1d' '&\xfd%\xdd\x82q/A\x10Y\x8b') path.setContent(self.payload) self.file = path.open() self.resource = static.File(self.file.name) self.resource.isLeaf = 1 self.request = DummyRequest(['']) self.request.uri = self.file.name self.catcher = [] log.addObserver(self.catcher.append)
def pathWithContent(self, content): """ Return a FilePath with the given initial content. """ fp = FilePath(self.mktemp()) fp.setContent(content) return fp
def _send_configuration(self, application_config_yaml=COMPLEX_APPLICATION_YAML, deployment_config_yaml=COMPLEX_DEPLOYMENT_YAML): """ Run ``flocker-deploy`` against the API server. :param application_config: Application configuration dictionary. :param deployment_config: Deployment configuration dictionary. :return: ``Deferred`` that fires with a tuple (stdout, stderr, exit code). """ app_config = FilePath(self.mktemp()) app_config.setContent(safe_dump(application_config_yaml)) deployment_config = FilePath(self.mktemp()) deployment_config.setContent(safe_dump(deployment_config_yaml)) # This duplicates some code in the acceptance tests... # https://clusterhq.atlassian.net/browse/FLOC-1904 return getProcessOutputAndValue( b"flocker-deploy", [ b"--certificates-directory", self.certificate_path.path, b"--port", unicode(self.port_number).encode("ascii"), b"localhost", deployment_config.path, app_config.path ], env=environ)
def _configure(self, config_yml, dockerArgs={}, dockerOnSocket=False, realDockerSocket=False, powerstripPort=0, nullAdapter=False): if not realDockerSocket: self.dockerAPI = testtools.FakeDockerServer(**dockerArgs) if dockerOnSocket: self.socketPath = self.mktemp() self.dockerServer = reactor.listenUNIX(self.socketPath, self.dockerAPI) else: self.dockerServer = reactor.listenTCP(0, self.dockerAPI) self.dockerPort = self.dockerServer.getHost().port else: # NB: only supports real docker on socket (not tcp) at the # moment assert dockerOnSocket, ("must pass dockerOnSocket=True " "if specifying realDockerSocket") self.socketPath = realDockerSocket self.config = PluginConfiguration() tmp = self.mktemp() self.config._default_file = tmp fp = FilePath(tmp) fp.setContent(config_yml) self.parser = EndpointParser(self.config) if dockerOnSocket: self.proxyAPI = powerstrip.ServerProtocolFactory( dockerSocket=self.socketPath, config=self.config) else: self.proxyAPI = powerstrip.ServerProtocolFactory( dockerAddr="127.0.0.1", dockerPort=self.dockerPort, config=self.config) self.proxyServer = reactor.listenTCP(powerstripPort, self.proxyAPI) self.proxyPort = self.proxyServer.getHost().port
def test_loadDefaultAccountsFromFile(self): """ L{LoadSimulator.fromCommandLine} takes an account loader (with empty path)from the config file and uses it to create user records for use in the simulation. """ config = VALID_CONFIG.copy() config["accounts"] = { "loader": "contrib.performance.loadtest.sim.recordsFromCSVFile", "params": { "path": "" }, } configpath = FilePath(self.mktemp()) configpath.setContent(writePlistToString(config)) sim = LoadSimulator.fromCommandLine(['--config', configpath.path], StringIO()) self.assertEqual(99, len(sim.records)) self.assertEqual(sim.records[0].uid, 'user01') self.assertEqual(sim.records[0].password, 'user01') self.assertEqual(sim.records[0].commonName, 'User 01') self.assertEqual(sim.records[0].email, '*****@*****.**') self.assertEqual(sim.records[98].uid, 'user99') self.assertEqual(sim.records[98].password, 'user99') self.assertEqual(sim.records[98].commonName, 'User 99') self.assertEqual(sim.records[98].email, '*****@*****.**')
def test_parseFileAndReportMismatchedTags(self): """ If the contents of the file passed to L{tree.parseFileAndReport} contain a mismatched tag, L{process.ProcessingFailure} is raised indicating the location of the open and close tags which were mismatched. """ path = FilePath(self.mktemp()) path.setContent(' <foo>\n\n </bar>') err = self.assertRaises( process.ProcessingFailure, tree.parseFileAndReport, path.path) self.assertEqual( str(err), "mismatched close tag at line 3, column 4; expected </foo> " "(from line 1, column 2)") # Test a case which requires involves proper close tag handling. path.setContent('<foo><bar></bar>\n </baz>') err = self.assertRaises( process.ProcessingFailure, tree.parseFileAndReport, path.path) self.assertEqual( str(err), "mismatched close tag at line 2, column 4; expected </foo> " "(from line 1, column 0)")
def test_generateRecordsNonDefaultPatterns(self): """ L{LoadSimulator.fromCommandLine} takes an account loader from the config file and uses it to generate user records for use in the simulation. """ config = VALID_CONFIG.copy() config["accounts"] = { "loader": "contrib.performance.loadtest.sim.generateRecords", "params": { "count": 3, "uidPattern": "USER%03d", "passwordPattern": "PASSWORD%03d", "namePattern": "Test User %03d", "emailPattern": "*****@*****.**", }, } configpath = FilePath(self.mktemp()) configpath.setContent(writePlistToString(config)) sim = LoadSimulator.fromCommandLine(['--config', configpath.path], StringIO()) self.assertEqual(3, len(sim.records)) self.assertEqual(sim.records[0].uid, 'USER001') self.assertEqual(sim.records[0].password, 'PASSWORD001') self.assertEqual(sim.records[0].commonName, 'Test User 001') self.assertEqual(sim.records[0].email, '*****@*****.**') self.assertEqual(sim.records[2].uid, 'USER003') self.assertEqual(sim.records[2].password, 'PASSWORD003') self.assertEqual(sim.records[2].commonName, 'Test User 003') self.assertEqual(sim.records[2].email, '*****@*****.**')
class NoDefault(unittest.TestCase): def setUp(self): # setup tahoe.cfg and basedir/private/introducers # create a custom tahoe.cfg self.basedir = os.path.dirname(self.mktemp()) c = open(os.path.join(self.basedir, "tahoe.cfg"), "w") config = {'hide-ip':False, 'listen': 'tcp', 'port': None, 'location': None, 'hostname': 'example.net'} write_node_config(c, config) c.write("[client]\n") c.write("# introducer.furl =\n") # omit default c.write("[storage]\n") c.write("enabled = false\n") c.close() os.mkdir(os.path.join(self.basedir,"private")) self.yaml_path = FilePath(os.path.join(self.basedir, "private", "introducers.yaml")) def test_ok(self): connections = {'introducers': { u'one': { 'furl': 'furl1' }, }} self.yaml_path.setContent(yamlutil.safe_dump(connections)) myclient = Client(self.basedir) tahoe_cfg_furl = myclient.introducer_furls[0] self.assertEquals(tahoe_cfg_furl, 'furl1') def test_introducerless(self): connections = {'introducers': {} } self.yaml_path.setContent(yamlutil.safe_dump(connections)) myclient = Client(self.basedir) self.assertEquals(len(myclient.introducer_furls), 0)
def test_loadDefaultAccountsFromFile(self): """ L{LoadSimulator.fromCommandLine} takes an account loader (with empty path)from the config file and uses it to create user records for use in the simulation. """ config = VALID_CONFIG.copy() config["accounts"] = { "loader": "contrib.performance.loadtest.sim.recordsFromCSVFile", "params": { "path": "", "interleavePods": True, }, } configpath = FilePath(self.mktemp()) configpath.setContent(writePlistToString(config)) sim = LoadSimulator.fromCommandLine(['--config', configpath.path], StringIO()) self.assertEqual(99, len(sim.records)) self.assertEqual(sim.records[0].uid, 'user01') self.assertEqual(sim.records[0].password, 'user01') self.assertEqual(sim.records[0].commonName, 'User 01') self.assertEqual(sim.records[0].email, '*****@*****.**') self.assertEqual(sim.records[98].uid, 'user99') self.assertEqual(sim.records[98].password, 'user99') self.assertEqual(sim.records[98].commonName, 'User 99') self.assertEqual(sim.records[98].email, '*****@*****.**')
def test_signalledExit(self): """ An error should be reported if the JavaScript interpreter exits because it received a signal. """ segfault = FilePath(self.mktemp()) segfault.setContent("""\ #!/usr/bin/python # Generate an unhandled SIGSEGV for this process immediately upon import. import os, signal os.kill(os.getpid(), signal.SIGSEGV) """) def stubFinder(): return sys.executable def stubScript(testModule): return segfault.path self.case.findJavascriptInterpreter = stubFinder self.case.makeScript = stubScript result = TestResult() self.case.run(result) self.assertEqual(len(result.errors), 1) self.assertEquals( result.errors[0][1], 'Exception: JavaScript interpreter exited due to signal 11\n')
def test_generateRecordsDefaultPatterns(self): """ L{LoadSimulator.fromCommandLine} takes an account loader from the config file and uses it to generate user records for use in the simulation. """ config = VALID_CONFIG.copy() config["accounts"] = { "loader": "contrib.performance.loadtest.sim.generateRecords", "params": { "count": 2 }, } configpath = FilePath(self.mktemp()) configpath.setContent(writePlistToString(config)) sim = LoadSimulator.fromCommandLine(['--config', configpath.path], StringIO()) self.assertEqual(2, len(sim.records)) self.assertEqual(sim.records[0].uid, 'user1') self.assertEqual(sim.records[0].password, 'user1') self.assertEqual(sim.records[0].commonName, 'User 1') self.assertEqual(sim.records[0].email, '*****@*****.**') self.assertEqual(sim.records[1].uid, 'user2') self.assertEqual(sim.records[1].password, 'user2') self.assertEqual(sim.records[1].commonName, 'User 2') self.assertEqual(sim.records[1].email, '*****@*****.**')
def test_parseFileAndReportMismatchedTags(self): """ If the contents of the file passed to L{tree.parseFileAndReport} contain a mismatched tag, L{process.ProcessingFailure} is raised indicating the location of the open and close tags which were mismatched. """ path = FilePath(self.mktemp()) path.setContent(' <foo>\n\n </bar>') err = self.assertRaises(process.ProcessingFailure, tree.parseFileAndReport, path.path) self.assertEqual( str(err), "mismatched close tag at line 3, column 4; expected </foo> " "(from line 1, column 2)") # Test a case which requires involves proper close tag handling. path.setContent('<foo><bar></bar>\n </baz>') err = self.assertRaises(process.ProcessingFailure, tree.parseFileAndReport, path.path) self.assertEqual( str(err), "mismatched close tag at line 2, column 4; expected </foo> " "(from line 1, column 0)")
def test_signalledExit(self): """ An error should be reported if the JavaScript interpreter exits because it received a signal. """ segfault = FilePath(self.mktemp()) segfault.setContent("""\ #!/usr/bin/python # Generate an unhandled SIGSEGV for this process immediately upon import. import os, signal os.kill(os.getpid(), signal.SIGSEGV) """) def stubFinder(): return sys.executable def stubScript(testModule): return segfault.path self.case.findJavascriptInterpreter = stubFinder self.case.makeScript = stubScript result = TestResult() self.case.run(result) self.assertEqual(len(result.errors), 1) self.assertEqual( result.errors[0][1], 'Exception: JavaScript interpreter exited due to signal 11\n')
def test_loadAccountsFromFile(self): """ L{LoadSimulator.fromCommandLine} takes an account loader from the config file and uses it to create user records for use in the simulation. """ accounts = FilePath(self.mktemp()) accounts.setContent( "foo,bar,baz,quux,goo\nfoo2,bar2,baz2,quux2,goo2\n") config = VALID_CONFIG.copy() config["accounts"] = { "loader": "contrib.performance.loadtest.sim.recordsFromCSVFile", "params": { "path": accounts.path, "interleavePods": True, }, } configpath = FilePath(self.mktemp()) configpath.setContent(writePlistToString(config)) io = StringIO() sim = LoadSimulator.fromCommandLine(['--config', configpath.path], io) self.assertEquals(io.getvalue(), "Loaded 2 accounts.\n") self.assertEqual(2, len(sim.records)) self.assertEqual(sim.records[0].uid, 'foo') self.assertEqual(sim.records[0].password, 'bar') self.assertEqual(sim.records[0].commonName, 'baz') self.assertEqual(sim.records[0].email, 'quux') self.assertEqual(sim.records[1].uid, 'foo2') self.assertEqual(sim.records[1].password, 'bar2') self.assertEqual(sim.records[1].commonName, 'baz2') self.assertEqual(sim.records[1].email, 'quux2')
def test_config_change(self): """ If the config is changed, the new config is reflected. """ yml = """endpoints: # adapters are applied in order "POST /*/containers/create": pre: [flocker, weave] post: [weave, flocker] "DELETE /*/containers/*": pre: [flocker, weave] post: [weave, flocker] adapters: flocker: http://flocker/flocker-adapter weave: http://weave/weave-adapter""" tmp = self.mktemp() self.config._default_file = tmp fp = FilePath(tmp) fp.setContent(yml) self.config.read_and_parse() self.assertEquals((self.config._endpoints, self.config._adapters), ({ "POST /*/containers/create": { "pre": ["flocker", "weave"], "post": ["weave", "flocker"], }, "DELETE /*/containers/*": { "pre": ["flocker", "weave"], "post": ["weave", "flocker"], }, }, { "flocker": "http://flocker/flocker-adapter", "weave": "http://weave/weave-adapter", })) yml = """endpoints: # adapters are applied in order "POST /*/containers/stop": pre: [flocker] adapters: flocker: http://flocker/flocker-adapter""" tmp = self.mktemp() self.config._default_file = tmp fp = FilePath(tmp) fp.setContent(yml) self.config.read_and_parse() self.assertEquals((self.config._endpoints, self.config._adapters), ({ "POST /*/containers/stop": { "pre": ["flocker"], "post": [], }, }, { "flocker": "http://flocker/flocker-adapter", }))
def test_full(self): """ Running C{calendarserver_export} on the command line exports an ics file. (Almost-full integration test, starting from the main point, using as few test fakes as possible.) Note: currently the only test for directory interaction. """ yield populateCalendarsFrom( { "user02": { # TODO: more direct test for skipping inbox "inbox": { "inbox-item.ics": (valentines, {}) }, "calendar1": { "peruser.ics": (dataForTwoUsers, {}), # EST } } }, self.store ) augmentsData = """ <augments> <record> <uid>Default</uid> <enable>true</enable> <enable-calendar>true</enable-calendar> <enable-addressbook>true</enable-addressbook> </record> </augments> """ augments = FilePath(self.mktemp()) augments.setContent(augmentsData) accountsData = """ <accounts realm="Test Realm"> <user> <uid>user-under-test</uid> <guid>user02</guid> <name>Not Interesting</name> <password>very-secret</password> </user> </accounts> """ accounts = FilePath(self.mktemp()) accounts.setContent(accountsData) output = FilePath(self.mktemp()) self.accountsFile = accounts.path self.augmentsFile = augments.path main(['calendarserver_export', '--output', output.path, '--user', 'user-under-test'], reactor=self) yield self.waitToStop self.assertEquals( Component.fromString(resultForUser2), Component.fromString(output.getContent()) )
def writeScript(self, content): """ Write L{content} to a new temporary file, returning the L{FilePath} for the new file. """ script = FilePath(self.mktemp()) script.setContent(content.encode("ascii")) return script
def setUp(self): """ Create a file with two users. """ self.filename = self.mktemp() f = FilePath(self.filename) f.setContent(':'.join(self.usernamePassword)) self.options = Options()
def loaderFactory(self) -> ITemplateLoader: """ @return: an L{XMLString} constructed with a filename that points to a file containing C{self.templateString}. """ fp = FilePath(self.mktemp()) fp.setContent(self.templateString.encode("utf8")) return XMLFile(fp.path) # type: ignore[arg-type]
def test_caseInsensitively(self): """ L{searchFileForAll} searches for names case-insensitively. """ hosts = FilePath(self.mktemp()) hosts.setContent("127.0.0.1 foobar.EXAMPLE.com\n") self.assertEqual(["127.0.0.1"], searchFileForAll(hosts, "FOOBAR.example.com"))
def loaderFactory(self): """ @return: an L{XMLString} constructed with a filename that points to a file containing C{self.templateString}. """ fp = FilePath(self.mktemp()) fp.setContent(self.templateString.encode('utf8')) return XMLFile(fp.path)
def test_emptyCertificate(self): certFilePath = FilePath(self.mktemp()) certFilePath.setContent("") success, _ignore_reason = verifyTLSCertificate( ConfigDict({ "SSLCertificate": certFilePath.path, })) self.assertFalse(success)
def loaderFactory(self): """ @return: an L{XMLString} constructed with a L{FilePath} pointing to a file that contains C{self.templateString}. """ fp = FilePath(self.mktemp()) fp.setContent(self.templateString.encode("utf8")) return XMLFile(fp)
def loaderFactory(self): """ @return: an L{XMLString} constructed with a L{FilePath} pointing to a file that contains C{self.templateString}. """ fp = FilePath(self.mktemp()) fp.setContent(self.templateString) return XMLFile(fp)
def loaderFactory(self): """ @return: an L{XMLString} constructed with a filename that points to a file containing C{self.templateString}. """ fp = FilePath(self.mktemp()) fp.setContent(self.templateString) return XMLFile(fp.path)
def test_caseInsensitively(self): """ L{searchFileForAll} searches for names case-insensitively. """ hosts = FilePath(self.mktemp()) hosts.setContent("127.0.0.1 foobar.EXAMPLE.com\n") self.assertEqual( ["127.0.0.1"], searchFileForAll(hosts, "FOOBAR.example.com"))
def setUp(self): """ Create a file with two users. """ self.filename = self.mktemp() f = FilePath(self.filename) f.setContent(b':'.join(self.usernamePassword)) self.options = Options()
def test_getChildFiles_file(self): """ Given a file, return the file """ root = FilePath(self.mktemp()) root.setContent('something') b = FileBuilder() r = list(b._getChildFiles(root)) self.assertEqual(r, [root])
def getCAPrivateCert(): privatePath = FilePath(b"ca-private-cert.pem") if privatePath.exists(): return PrivateCertificate.loadPEM(privatePath.getContent()) else: caKey = KeyPair.generate(size=4096) caCert = caKey.selfSignedCert(1, CN="the-authority") privatePath.setContent(caCert.dumpPEM()) return caCert
def getCAPrivateCert(): l_privatePath = FilePath(b"ca-private-cert.pem") if l_privatePath.exists(): return PrivateCertificate.loadPEM(l_privatePath.getContent()) else: l_caKey = KeyPair.generate(size=4096) l_caCert = l_caKey.selfSignedCert(1, CN="the-authority") l_privatePath.setContent(l_caCert.dumpPEM()) return l_caCert
def test_specifyRuntime(self): """ L{LoadSimulator.fromCommandLine} recognizes the I{--runtime} option to specify a limit on how long the simulation will run. """ config = FilePath(self.mktemp()) config.setContent(VALID_CONFIG_PLIST) sim = LoadSimulator.fromCommandLine(['--config', config.path, '--runtime', '123']) self.assertEqual(123, sim.runtime)
def test_read_from_yaml_file_invalid(self): """ ``_read_from_yaml_file`` raises ``NoConfiguration`` if the file is not valid YAML. """ content = "{' unclosed" fp = FilePath(self.mktemp()) fp.setContent(content) self.assertRaises(InvalidConfiguration, self.config._read_from_yaml_file, fp)
def test_findAddress(self): """ If there is an IPv4 address for the hostname passed to L{searchFileFor}, it is returned. """ hosts = FilePath(self.mktemp()) hosts.setContent("10.2.3.4 foo.example.com\n") self.assertEqual("10.2.3.4", searchFileFor(hosts.path, "foo.example.com"))
def test_notFoundAddress(self): """ If there is no address information for the hostname passed to L{searchFileFor}, C{None} is returned. """ hosts = FilePath(self.mktemp()) hosts.setContent("10.2.3.4 foo.example.com\n") self.assertIdentical(None, searchFileFor(hosts.path, "bar.example.com"))
def test_firstAddress(self): """ The first address associated with the given hostname is returned. """ hosts = FilePath(self.mktemp()) hosts.setContent("::1 foo.example.com\n" "10.1.2.3 foo.example.com\n" "fe80::21b:fcff:feee:5a1d foo.example.com\n") self.assertEqual("::1", searchFileFor(hosts.path, "foo.example.com"))
def test_provision_conflicting_file(self): """ Calling L{Storage.provision} when there is a file in the way raises L{StorageError}. """ fp = FilePath(self.mktemp()) store = self.storage(provisioned=False, fp=fp) fp.setContent(b"") self.assertRaises(StorageError, store.provision)
def test_loadPopulationParameters(self): """ Client weights and profiles are loaded from the [clients] section of the configuration file specified. """ config = FilePath(self.mktemp()) config.setContent( writePlistToString( { "clients": [ { "software": "contrib.performance.loadtest.ical.OS_X_10_6", "params": { "foo": "bar" }, "profiles": [ { "params": { "interval": 25, "eventStartDistribution": { "type": "contrib.performance.stats.NormalDistribution", "params": { "mu": 123, "sigma": 456, } } }, "class": "contrib.performance.loadtest.profiles.Eventer" } ], "weight": 3, } ] } ) ) sim = LoadSimulator.fromCommandLine( ['--config', config.path, '--clients', config.path] ) expectedParameters = PopulationParameters() expectedParameters.addClient( 3, ClientType( OS_X_10_6, {"foo": "bar"}, [ ProfileType( Eventer, { "interval": 25, "eventStartDistribution": NormalDistribution(123, 456) } ) ] ) ) self.assertEquals(sim.parameters, expectedParameters)
def test_default_config_path(self): """ When ``network_kubernetes_from_context`` does not find ``KUBECONFIG`` in the environment it uses ``default_config_path`` as the path to the configuration file. """ key_path, cert_path = self_signed_certificate_paths( FilePath(self.mktemp()), FilePath(self.mktemp()), u"x.invalid", ) userauth = { "client-certificate": cert_path.path, "client-key": key_path.path, } config = FilePath(self.mktemp()) yaml = safe_dump({ "apiVersion": "v1", "kind": "Config", "contexts": [ { "name": "a", "context": { "cluster": "a", "user": "******" } }, ], "clusters": [{ "name": "a", "cluster": { "server": "https://a.example.com/", "certificate-authority": cert_path.path, }, }], "users": [ { "name": "a", "user": userauth }, ], }) config.setContent(native_string_to_bytes(yaml)) kubernetes = network_kubernetes_from_context( MemoryReactorClock(), context=u"a", environ={}, default_config_path=config, ) self.assertEqual( "https://a.example.com/", kubernetes.base_url.asText(), )
def test_default_os_environ(self): """ If no value is passed for the ``environ`` parameter then ``network_kubernetes_from_context`` uses ``os.environ`` to look up the possible value of ``KUBECONFIG``. """ key_path, cert_path = self_signed_certificate_paths( FilePath(self.mktemp()), FilePath(self.mktemp()), u"x.invalid", ) userauth = { "client-certificate": cert_path.path, "client-key": key_path.path, } config = FilePath(self.mktemp()) yaml = safe_dump({ "apiVersion": "v1", "kind": "Config", "contexts": [ { "name": "a", "context": { "cluster": "a", "user": "******" } }, ], "clusters": [{ "name": "a", "cluster": { "server": "https://a.example.com/", "certificate-authority": cert_path.path, }, }], "users": [ { "name": "a", "user": userauth }, ], }) config.setContent(native_string_to_bytes(yaml)) import os self.patch(os, "environ", {u"KUBECONFIG": config.path}) kubernetes = network_kubernetes_from_context( MemoryReactorClock(), context=u"a", ) self.assertEqual( "https://a.example.com/", kubernetes.base_url.asText(), )