Ejemplo n.º 1
0
 def setUp(self):
     super().setUp()
     self.parser = ArgumentParser()
     self.cmd = snappy.cmd_config(self.parser)
     self.patch(os, "getuid").return_value = 0
     snap_common = self.make_dir()
     snap_data = self.make_dir()
     self.useFixture(EnvironmentVariableFixture("SNAP_COMMON", snap_common))
     self.useFixture(EnvironmentVariableFixture("SNAP_DATA", snap_data))
Ejemplo n.º 2
0
 def _setUp(self):
     if self.with_persistent:
         env_value = ''
     else:
         env_value = None
     self.useFixture(
         EnvironmentVariableFixture('LP_PERSISTENT_TEST_SERVICES',
                                    env_value))
     self.useFixture(EnvironmentVariableFixture('LP_TEST_INSTANCE'))
Ejemplo n.º 3
0
 def setUp(self):
     super(BaseLayerIsolator, self).setUp()
     if self.with_persistent:
         env_value = ''
     else:
         env_value = None
     self.useFixture(
         EnvironmentVariableFixture('LP_PERSISTENT_TEST_SERVICES',
                                    env_value))
     self.useFixture(EnvironmentVariableFixture('LP_TEST_INSTANCE'))
Ejemplo n.º 4
0
 def test_generates_unique_config(self):
     config.setInstance('testrunner')
     orig_instance = config.instance_name
     self.useFixture(
         EnvironmentVariableFixture('LP_PERSISTENT_TEST_SERVICES'))
     self.useFixture(EnvironmentVariableFixture('LP_TEST_INSTANCE'))
     self.useFixture(EnvironmentVariableFixture('LPCONFIG'))
     with LayerFixture(BaseLayer):
         self.assertEqual('testrunner_%s' % os.environ['LP_TEST_INSTANCE'],
                          config.instance_name)
     self.assertEqual(orig_instance, config.instance_name)
Ejemplo n.º 5
0
 def setUpUser(self, name):
     user = self._factory.makePerson(name=name)
     self.addSSHKey(
         user, os.path.join(os.path.dirname(__file__), 'poppy-sftp.pub'))
     # Set up a temporary home directory for Paramiko's sake
     self._home_dir = tempfile.mkdtemp()
     self.addCleanup(shutil.rmtree, self._home_dir)
     os.mkdir(os.path.join(self._home_dir, '.ssh'))
     os.symlink(os.path.join(os.path.dirname(__file__), 'poppy-sftp'),
                os.path.join(self._home_dir, '.ssh', 'id_rsa'))
     self.useFixture(EnvironmentVariableFixture('HOME', self._home_dir))
     self.useFixture(EnvironmentVariableFixture('SSH_AUTH_SOCK', None))
     self.useFixture(EnvironmentVariableFixture('BZR_SSH', 'paramiko'))
Ejemplo n.º 6
0
    def test_returns_expected_tuple(self):
        self.useFixture(EnvironmentVariableFixture("SNAP", None))
        version = self.version
        actual_tuple = get_version_tuple(version)
        self.assertThat(actual_tuple, Equals(self.expected_tuple), version)

        self.useFixture(
            EnvironmentVariableFixture("SNAP", "/var/snap/maas/123"))
        expected_tuple__snap = list(self.expected_tuple)
        expected_tuple__snap[-1] = True
        expected_tuple__snap = tuple(expected_tuple__snap)
        actual_tuple__snap = get_version_tuple(version)
        self.assertThat(actual_tuple__snap, Equals(expected_tuple__snap),
                        version)
Ejemplo n.º 7
0
 def test_generates_unique_config_dirs(self):
     self.useFixture(
         EnvironmentVariableFixture('LP_PERSISTENT_TEST_SERVICES'))
     self.useFixture(EnvironmentVariableFixture('LP_TEST_INSTANCE'))
     self.useFixture(EnvironmentVariableFixture('LPCONFIG'))
     with LayerFixture(BaseLayer):
         runner_root = 'configs/%s' % config.instance_name
         runner_appserver_root = 'configs/testrunner-appserver_%s' % \
             os.environ['LP_TEST_INSTANCE']
         self.assertTrue(
             os.path.isfile(runner_root + '/launchpad-lazr.conf'))
         self.assertTrue(
             os.path.isfile(runner_appserver_root + '/launchpad-lazr.conf'))
     self.assertFalse(os.path.exists(runner_root))
     self.assertFalse(os.path.exists(runner_appserver_root))
Ejemplo n.º 8
0
 def setUp(self):
     super().setUp()
     self.tmpdir = self.make_dir()
     self.proxy_path = Path(self.tmpdir) / config.MAAS_PROXY_CONF_NAME
     self.useFixture(
         EnvironmentVariableFixture("MAAS_PROXY_CONFIG_DIR", self.tmpdir)
     )
Ejemplo n.º 9
0
 def test_makeService_with_import_services(self):
     options = Options()
     service_maker = RegionWorkerServiceMaker("Harry", "Hill")
     # Disable _configureThreads() as it's too invasive right now.
     self.patch_autospec(service_maker, "_configureThreads")
     # Set the environment variable to create the import services.
     self.useFixture(
         EnvironmentVariableFixture(
             'MAAS_REGIOND_RUN_IMPORTER_SERVICE', 'true'))
     service = service_maker.makeService(options)
     self.assertIsInstance(service, MultiService)
     expected_services = [
         "database-tasks",
         "postgres-listener-worker",
         "rack-controller",
         "rpc",
         "status-worker",
         "web",
         "ipc-worker",
         "import-resources",
         "import-resources-progress",
     ]
     self.assertItemsEqual(expected_services, service.namedServices.keys())
     self.assertEqual(
         len(service.namedServices), len(service.services),
         "Not all services are named.")
     self.assertThat(
         logger.configure, MockCalledOnceWith(
             options["verbosity"], logger.LoggingMode.TWISTD))
     self.assertThat(crochet.no_setup, MockCalledOnceWith())
Ejemplo n.º 10
0
 def set_envvar(self, filepath=None):
     """Set the env. variable named by `ExampleConfiguration.envvar"."""
     self.useFixture(
         EnvironmentVariableFixture(
             self.example_configuration.envvar, filepath
         )
     )
Ejemplo n.º 11
0
 def test_packagediff_max_size(self):
     # debdiff is killed if it generates more than the size limit.
     self.pushConfig("packagediff", debdiff_max_size=1024)
     temp_dir = self.makeTemporaryDirectory()
     mock_debdiff_path = os.path.join(temp_dir, "debdiff")
     marker_path = os.path.join(temp_dir, "marker")
     with open(mock_debdiff_path, "w") as mock_debdiff:
         print(dedent("""\
             #! /bin/sh
             (echo "$$"; echo "$TMPDIR") >%s
             yes | head -n2048 || exit 2
             sleep 5
             """ % marker_path),
               end="",
               file=mock_debdiff)
     os.chmod(mock_debdiff_path, 0o755)
     mock_path = "%s:%s" % (temp_dir, os.environ["PATH"])
     diff = create_proper_job(self.factory)
     with EnvironmentVariableFixture("PATH", mock_path):
         diff.performDiff()
     self.assertEqual(PackageDiffStatus.FAILED, diff.status)
     with open(marker_path) as marker:
         debdiff_pid = int(marker.readline())
         debdiff_tmpdir = marker.readline().rstrip("\n")
         err = self.assertRaises(OSError, os.kill, debdiff_pid, 0)
         self.assertEqual(errno.ESRCH, err.errno)
         self.assertFalse(os.path.exists(debdiff_tmpdir))
Ejemplo n.º 12
0
 def setUp(self):
     super(TestProxyUpdateConfig, self).setUp()
     self.tmpdir = self.make_dir()
     self.proxy_path = Path(self.tmpdir) / proxyconfig.MAAS_PROXY_CONF_NAME
     self.service_monitor = self.patch(proxyconfig, "service_monitor")
     self.useFixture(
         EnvironmentVariableFixture('MAAS_PROXY_CONFIG_DIR', self.tmpdir))
Ejemplo n.º 13
0
 def make_fake_templates_dir(self):
     """Set up a fake PXE templates dir, and return its path."""
     fake_etc_maas = self.make_dir()
     self.useFixture(EnvironmentVariableFixture(
         'MAAS_CONFIG_DIR', fake_etc_maas))
     fake_templates = os.path.join(fake_etc_maas, config.TEMPLATES_DIR)
     os.makedirs(fake_templates)
     return fake_templates
Ejemplo n.º 14
0
 def setUp(self):
     super().setUp()
     self.now = datetime(2019, 1, 31, 1, 2, 3, 4)
     self.datetime_mock = self.patch(debug, "datetime")
     self.datetime_mock.now.return_value = self.now
     self.maas_root = self.useFixture(TempDir()).path
     self.useFixture(EnvironmentVariableFixture("MAAS_ROOT",
                                                self.maas_root))
Ejemplo n.º 15
0
 def setUp(self):
     super(DisplayFixture, self).setUp()
     self.process = Popen(self.command, stdin=PIPE, stdout=PIPE)
     self.display = self.process.stdout.readline().strip()
     if not self.display or self.process.poll() is not None:
         raise CalledProcessError(self.process.returncode, self.command)
     self.useFixture(EnvironmentVariableFixture("DISPLAY", self.display))
     self.addCleanup(self.shutdown)
Ejemplo n.º 16
0
    def test_process_package_cleans_up_after_unpack_failure(self):
        archive_root = self.useTempDir()
        pool_dir = os.path.join(archive_root, "pool/main/f/foo")
        os.makedirs(pool_dir)

        with open(os.path.join(
            pool_dir, "foo_1.0.orig.tar.gz"), "wb+") as buffer:
            orig_tar = LaunchpadWriteTarFile(buffer)
            orig_tar.add_directory("foo-1.0")
            orig_tar.close()
            buffer.seek(0)
            orig_tar_contents = buffer.read()
        with open(os.path.join(
            pool_dir, "foo_1.0-1.debian.tar.gz"), "wb+") as buffer:
            debian_tar = LaunchpadWriteTarFile(buffer)
            debian_tar.add_file("debian/source/format", "3.0 (quilt)\n")
            debian_tar.add_file("debian/patches/series", "--- corrupt patch\n")
            debian_tar.add_file("debian/rules", "")
            debian_tar.close()
            buffer.seek(0)
            debian_tar_contents = buffer.read()
        dsc_path = os.path.join(pool_dir, "foo_1.0-1.dsc")
        with open(dsc_path, "w") as dsc:
            dsc.write(dedent("""\
                Format: 3.0 (quilt)
                Source: foo
                Binary: foo
                Architecture: all
                Version: 1.0-1
                Maintainer: Foo Bar <*****@*****.**>
                Files:
                 %s %s foo_1.0.orig.tar.gz
                 %s %s foo_1.0-1.debian.tar.gz
                """ % (
                    hashlib.md5(orig_tar_contents).hexdigest(),
                    len(orig_tar_contents),
                    hashlib.md5(debian_tar_contents).hexdigest(),
                    len(debian_tar_contents))))

        dsc_contents = parse_tagfile(dsc_path)
        dsc_contents["Directory"] = pool_dir
        dsc_contents["Package"] = "foo"
        dsc_contents["Component"] = "main"
        dsc_contents["Section"] = "misc"

        sp_data = SourcePackageData(**dsc_contents)
        unpack_tmpdir = self.makeTemporaryDirectory()
        with EnvironmentVariableFixture("TMPDIR", unpack_tmpdir):
            # Force tempfile to recheck TMPDIR.
            tempfile.tempdir = None
            try:
                self.assertRaises(
                    ExecutionError,
                    sp_data.process_package, "ubuntu", archive_root)
            finally:
                # Force tempfile to recheck TMPDIR for future tests.
                tempfile.tempdir = None
        self.assertEqual([], os.listdir(unpack_tmpdir))
Ejemplo n.º 17
0
 def test_removes_https_proxy_from_environment(self):
     https_proxy = factory.make_name("https-proxy")
     initial = EnvironmentVariableFixture("https_proxy", https_proxy)
     self.useFixture(initial)
     # On entry, https_proxy is removed from the environment.
     with ProxiesDisabledFixture():
         self.assertNotIn("https_proxy", os.environ)
     # On exit, http_proxy is restored.
     self.assertEqual(https_proxy, os.environ.get("https_proxy"))
Ejemplo n.º 18
0
 def setUp(self):
     super(TestProxyUpdateConfig, self).setUp()
     self.tmpdir = self.make_dir()
     self.proxy_path = Path(self.tmpdir) / config.MAAS_PROXY_CONF_NAME
     self.service_monitor = self.patch(proxyconfig, "service_monitor")
     # Setting the http_proxy Config will cause the boot sources to be
     # re-cached. Disable the signals so no threads are dirty.
     bootsources.signals.disable()
     self.useFixture(
         EnvironmentVariableFixture('MAAS_PROXY_CONFIG_DIR', self.tmpdir))
Ejemplo n.º 19
0
    def parse_headers_and_body(self, headers, body):
        """Parse ingredients of a web request.

        The headers and body are as passed to :class:`MAASDispatcher`.
        """
        # Make Django STFU; just using Django's multipart code causes it to
        # pull in a settings module, and it will throw up if it can't.
        self.useFixture(
            EnvironmentVariableFixture("DJANGO_SETTINGS_MODULE", __name__))

        post, files = parse_headers_and_body_with_django(headers, body)
        return post, files
Ejemplo n.º 20
0
    def test_does_not_require_config(self):
        defaults = Config.get_defaults()
        no_file = os.path.join(self.make_dir(), factory.make_name() + '.yaml')
        self.useFixture(
            EnvironmentVariableFixture('MAAS_PROVISIONING_SETTINGS', no_file))

        parser = make_arg_parser(factory.getRandomString())

        args = parser.parse_args('')
        self.assertEqual(defaults['boot']['ephemeral']['images_directory'],
                         args.output)
        self.assertItemsEqual([], args.filters)
Ejemplo n.º 21
0
    def setUp(self):
        super(PGBouncerFixture, self).setUp()

        # reconnect_store cleanup added first so it is run last, after
        # the environment variables have been reset.
        self.addCleanup(self._maybe_reconnect_stores)

        # Abuse the PGPORT environment variable to get things connecting
        # via pgbouncer. Otherwise, we would need to temporarily
        # overwrite the database connection strings in the config.
        self.useFixture(EnvironmentVariableFixture('PGPORT', str(self.port)))

        # Reset database connections so they go through pgbouncer.
        self._maybe_reconnect_stores()
Ejemplo n.º 22
0
 def setUp(self):
     super(ConfigFixture, self).setUp()
     # Create a real configuration file, and populate it.
     self.dir = self.useFixture(TempDirectory()).path
     self.filename = path.join(self.dir, "config.yaml")
     with open(self.filename, "wb") as stream:
         yaml.safe_dump(self.config, stream=stream)
     # Export this filename to the environment, so that subprocesses will
     # pick up this configuration. Define the new environment as an
     # instance variable so that users of this fixture can use this to
     # extend custom subprocess environments.
     self.environ = {"MAAS_PROVISIONING_SETTINGS": self.filename}
     for name, value in self.environ.items():
         self.useFixture(EnvironmentVariableFixture(name, value))
Ejemplo n.º 23
0
 def setUp(self):
     super().setUp()
     # Create a real configuration file, and populate it.
     self.dir = self.useFixture(TempDirectory()).path
     self.filename = path.join(self.dir, self.name)
     with open(self.filename, "wb") as stream:
         yaml.safe_dump(self.config, stream=stream, encoding="utf-8")
     # Export this filename to the environment, so that subprocesses will
     # pick up this configuration. Define the new environment as an
     # instance variable so that users of this fixture can use this to
     # extend custom subprocess environments.
     self.environ = {self.schema.envvar: self.filename}
     for name, value in self.environ.items():
         self.useFixture(EnvironmentVariableFixture(name, value))
Ejemplo n.º 24
0
 def test_dehydrate_includes_version(self):
     self.useFixture(
         EnvironmentVariableFixture("SNAP", "/var/snap/maas/123"))
     owner = factory.make_admin()
     handler = ControllerHandler(owner, {}, None)
     rack = factory.make_RackController()
     version = "2.3.0~alpha1-6000-gabc123"
     ControllerInfo.objects.set_version(rack, version)
     result = handler.list({})
     self.assertEqual(version, result[0].get("version"))
     self.assertEqual("2.3.0~alpha1", result[0].get("version__short"))
     self.assertEqual(
         "2.3.0~alpha1 (6000-gabc123) (snap)",
         result[0].get("version__long"),
     )
Ejemplo n.º 25
0
 def test_worker_calls_update_leases(self):
     # In bug 1041158, the worker's upload_leases task tried to call
     # the update_leases API at the wrong URL path.  It has the right
     # path now.
     self.useFixture(
         EnvironmentVariableFixture("MAAS_URL", settings.DEFAULT_MAAS_URL))
     nodegroup = factory.make_node_group(status=NODEGROUP_STATUS.ACCEPTED)
     refresh_worker(nodegroup)
     self.patch(MAASClient, 'post', Mock())
     leases = factory.make_random_leases()
     send_leases(leases)
     nodegroup_path = reverse('nodegroup_handler', args=[nodegroup.uuid])
     nodegroup_path = nodegroup_path.decode('ascii').lstrip('/')
     MAASClient.post.assert_called_once_with(nodegroup_path,
                                             'update_leases',
                                             leases=json.dumps(leases))
Ejemplo n.º 26
0
    def patch_template(self, template_content=sample_template):
        """Patch the DHCP config template with the given contents.

        Returns a `tempita.Template` of the given template, so that a test
        can make its own substitutions and compare to those made by the
        code being tested.
        """
        fake_etc_maas = self.make_dir()
        self.useFixture(
            EnvironmentVariableFixture('MAAS_CONFIG_DIR', fake_etc_maas))
        template_dir = path.join(fake_etc_maas, 'templates', 'dhcp')
        makedirs(template_dir)
        template = factory.make_file(template_dir,
                                     'dhcpd.conf.template',
                                     contents=template_content)
        return tempita.Template(template_content, name=template)
Ejemplo n.º 27
0
 def setUp(self):
     super(ConfigurationFixtureBase, self).setUp()
     # Create a real configuration file, and populate it.
     self.path = path.join(
         self.useFixture(TempDirectory()).path,
         path.basename(self.configuration.DEFAULT_FILENAME))
     with self.configuration.open_for_update(self.path) as config:
         for key, value in self.options.items():
             setattr(config, key, value)
     # Export this filename to the environment, so that subprocesses will
     # pick up this configuration. Define the new environment as an
     # instance variable so that users of this fixture can use this to
     # extend custom subprocess environments.
     self.environ = {self.configuration.envvar: self.path}
     for name, value in self.environ.items():
         self.useFixture(EnvironmentVariableFixture(name, value))
Ejemplo n.º 28
0
 def test_ensure_boot_source_definition_updates_default_source_snap(self):
     BootSource.objects.all().delete()
     self.assertTrue(ensure_boot_source_definition())
     source = BootSource.objects.first()
     self.assertEqual(
         source.keyring_filename,
         "/usr/share/keyrings/ubuntu-cloudimage-keyring.gpg",
     )
     self.useFixture(
         EnvironmentVariableFixture("SNAP", "/snap/maas/current"))
     self.patch(
         bootsources,
         "DEFAULT_KEYRINGS_PATH",
         "/some/other/path/keyring.gpg",
     )
     self.assertFalse(ensure_boot_source_definition())
     source = reload_object(source)
     self.assertEqual(source.keyring_filename,
                      "/some/other/path/keyring.gpg")
Ejemplo n.º 29
0
    def test_service_config(self):
        # Rabbit needs to fully isolate itself: an existing per user
        # .erlang.cookie has to be ignored, and ditto bogus HOME if other
        # tests fail to cleanup.
        self.useFixture(EnvironmentVariableFixture('HOME', '/nonsense/value'))

        # RabbitServer pokes some .ini configuration into its config.
        fixture = self.useFixture(RabbitServer())
        service_config = SafeConfigParser()
        service_config.readfp(StringIO(fixture.config.service_config))
        self.assertEqual(["rabbitmq"], service_config.sections())
        expected = {
            "host": "localhost:%d" % fixture.config.port,
            "userid": "guest",
            "password": "******",
            "virtual_host": "/",
        }
        observed = dict(service_config.items("rabbitmq"))
        self.assertEqual(expected, observed)
Ejemplo n.º 30
0
    def test_makeService_cleanup_prometheus_dir(self):
        tmpdir = Path(self.useFixture(TempDirectory()).path)
        self.useFixture(
            EnvironmentVariableFixture("prometheus_multiproc_dir", str(tmpdir))
        )
        pid = os.getpid()
        file1 = tmpdir / "histogram_{}.db".format(pid)
        file1.touch()
        file2 = tmpdir / "histogram_{}.db".format(self.get_unused_pid())
        file2.touch()

        service_maker = RegionMasterServiceMaker("Harry", "Hill")
        # Disable _ensureConnection() its not allowed in the reactor.
        self.patch_autospec(service_maker, "_ensureConnection")
        # Disable _configureThreads() as it's too invasive right now.
        self.patch_autospec(service_maker, "_configureThreads")
        service_maker.makeService(Options())
        self.assertTrue(file1.exists())
        self.assertFalse(file2.exists())