def test_find_image(self): osystem = make_osystem(self, factory.make_name("os")) tmpdir = self.make_dir() arch = factory.make_name("arch") subarch = factory.make_name("subarch") release = factory.make_name("release") label = factory.make_name("label") dir_path = os.path.join(tmpdir, osystem.name, arch, subarch, release, label) os.makedirs(dir_path) if self.fname: factory.make_file(dir_path, self.fname) self.useFixture(ClusterConfigurationFixture(tftp_root=tmpdir)) self.assertItemsEqual( self.expected, osystem._find_image( arch, subarch, release, label, tgz=self.tgz, dd=self.dd, squashfs=self.squashfs, ), )
def configure_storage(self, storage_dir, make_current_dir=True): """Create a storage config.""" current_dir = os.path.join(storage_dir, "current") os.makedirs(current_dir) self.useFixture(ClusterConfigurationFixture(tftp_root=current_dir)) if not make_current_dir: os.rmdir(current_dir)
def setUp(self): super().setUp() self.useFixture(ClusterConfigurationFixture()) self.patch(provisioningserver, "services", MultiService()) self.patch_autospec(crochet, "no_setup") self.patch_autospec(logger, "configure") self.patch(plugin_module, "generate_certificate_if_needed")
def setUp(self): super(TestProvisioningServiceMaker, self).setUp() self.useFixture(ClusterConfigurationFixture()) self.patch(provisioningserver, "services", MultiService()) self.patch_autospec(crochet, "no_setup") self.patch_autospec(logger, "configure") self.tempdir = self.make_dir()
def prepare_tftp_root(test): """Create a `current` directory and configure its use.""" test.tftp_root = os.path.join(test.make_dir(), "current") os.mkdir(test.tftp_root) test.patch(boot_images, "CACHED_BOOT_IMAGES", None) config = ClusterConfigurationFixture(tftp_root=test.tftp_root) test.useFixture(config)
def test_report_boot_images_with_real_things_to_report(self): # tftppath.report_boot_images()'s return value matches the # arguments schema that ReportBootImages declares, and is # serialised correctly. # Example boot image definitions. archs = "i386", "amd64" subarchs = "generic", "special" releases = "precise", "trusty" purposes = "commission", "install" # Create a TFTP file tree with a variety of subdirectories. tftpdir = self.make_dir() for options in product(archs, subarchs, releases, purposes): os.makedirs(os.path.join(tftpdir, *options)) # Ensure that report_boot_images() uses the above TFTP file tree. self.useFixture(ClusterConfigurationFixture(tftp_root=tftpdir)) images = [ {"architecture": arch, "subarchitecture": subarch, "release": release, "purpose": purpose} for arch, subarch, release, purpose in product( archs, subarchs, releases, purposes) ] d = call_responder(Region(), ReportBootImages, { "uuid": factory.make_name("uuid"), "images": images, }) def check(response): self.assertEqual({}, response) return d.addCallback(check)
def test_debug_enabled_cached(self): debug = factory.pick_bool() self.useFixture(ClusterConfigurationFixture(debug=debug)) # Prime cache debug_enabled() mock_open = self.patch(config_module.ClusterConfiguration, "open") self.assertEqual(debug, debug_enabled()) mock_open.assert_not_called()
def test__run_import_sets_proxy_for_source_host(self): host = factory.make_name("host").lower() maas_url = "http://%s/" % host self.useFixture(ClusterConfigurationFixture(maas_url=maas_url)) sources, _ = make_sources() fake = self.patch_boot_resources_function() _run_import(sources=sources) self.assertItemsEqual( fake.env['no_proxy'].split(','), ["localhost", "::ffff:127.0.0.1", "127.0.0.1", "::1"] + [host])
def test_integration(self): tftproot = self.make_dir() self.useFixture(ClusterConfigurationFixture(tftp_root=tftproot)) action = factory.make_name("action") script = MainScript(action) script.register(action, provisioningserver.boot.install_grub) script.execute((action, )) config_filename = os.path.join("grub", "grub.cfg") self.assertThat(os.path.join(tftproot, config_filename), FileExists())
def test_compose_template_namespace_include_debug(self): debug = factory.pick_bool() boot.debug_enabled.cache_clear() self.addClassCleanup(boot.debug_enabled.cache_clear) self.useFixture(ClusterConfigurationFixture(debug=debug)) kernel_params = make_kernel_parameters() method = FakeBootMethod() template_namespace = method.compose_template_namespace(kernel_params) self.assertEqual(debug, template_namespace["debug"])
def setUp(self): super().setUp() self.useFixture(ClusterConfigurationFixture()) self.patch(provisioningserver, "services", MultiService()) self.patch_autospec(crochet, "no_setup") self.patch_autospec(logger, "configure") self.mock_generate_certificate = self.patch( plugin_module, "generate_certificate_if_needed") # by default, define a shared secret so that sevices are populated self.mock_get_shared_secret = self.patch( plugin_module, "get_shared_secret_from_filesystem") self.mock_get_shared_secret.return_value = "secret"
def test_get_resouce_path(self): fake_tftproot = self.make_dir() self.useFixture(ClusterConfigurationFixture(tftp_root=fake_tftproot)) method = WindowsPXEBootMethod() fake_path = factory.make_name('path') fake_kernelparams = make_kernel_parameters() result = method.get_resource_path(fake_kernelparams, fake_path) expected = os.path.join( fake_tftproot, 'windows', fake_kernelparams.arch, fake_kernelparams.subarch, fake_kernelparams.release, fake_kernelparams.label, fake_path) self.assertEqual(expected, result)
def make_resource_path(self, files=[]): tmpdir = self.make_dir() arch = factory.make_name("arch") subarch = factory.make_name("subarch") release = factory.make_name("release") label = factory.make_name("label") dirpath = os.path.join(tmpdir, "windows", arch, subarch, release, label) os.makedirs(dirpath) for fname in files: factory.make_file(dirpath, fname) self.useFixture(ClusterConfigurationFixture(tftp_root=tmpdir)) return arch, subarch, release, label
def test_update_targets_only_runs_when_conf_exists(self): # Regression test for LP:1655721 temp_dir = self.useFixture(TempDirectory()).path self.useFixture(ClusterConfigurationFixture(tftp_root=temp_dir)) mock_ensureService = self.patch(boot_resources.service_monitor, "ensureService") mock_call_and_check = self.patch(boot_resources, "call_and_check") mock_path_exists = self.patch(boot_resources.os.path, 'exists') mock_path_exists.return_value = False boot_resources.update_targets_conf(temp_dir) self.assertThat(mock_ensureService, MockCalledOnceWith("tgt")) self.assertThat(mock_path_exists, MockCalledOnceWith(os.path.join(temp_dir, 'maas.tgt'))) self.assertThat(mock_call_and_check, MockNotCalled())
def test_update_last_image_sync_end_to_end_import_not_performed(self): self.useFixture(ClusterConfigurationFixture()) fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop( region.UpdateLastImageSync) protocol.UpdateLastImageSync.return_value = succeed({}) self.addCleanup((yield connecting)) self.patch_autospec(boot_resources, 'import_images') boot_resources.import_images.return_value = False sources, hosts = make_sources() yield boot_images.import_boot_images(sources) self.assertThat(boot_resources.import_images, MockCalledOnceWith(sources)) self.assertThat(protocol.UpdateLastImageSync, MockNotCalled())
def make_resource_path(self, filename): self.useFixture(ClusterConfigurationFixture()) tmpdir = self.make_dir() arch = factory.make_name('arch') subarch = factory.make_name('subarch') release = factory.make_name('release') label = factory.make_name('label') current_dir = os.path.join(tmpdir, 'current') + '/' dirpath = os.path.join(current_dir, 'custom', arch, subarch, release, label) os.makedirs(dirpath) factory.make_file(dirpath, filename) with ClusterConfiguration.open_for_update() as config: config.tftp_root = current_dir return arch, subarch, release, label
def test_makeService_with_EXPERIMENTAL_tftp_offload_service(self): """ Only the site service is created when no options are given. """ # Activate the offload service by setting port to 0. self.useFixture(ClusterConfigurationFixture(tftp_port=0)) options = Options() service_maker = ProvisioningServiceMaker("Harry", "Hill") service = service_maker.makeService(options, clock=None) self.assertIsInstance(service, MultiService) self.assertThat(service.namedServices, Not(Contains("tftp"))) self.assertThat(service.namedServices, Contains("tftp-offload")) tftp_offload_service = service.getServiceNamed("tftp-offload") self.assertThat(tftp_offload_service, IsInstance(TFTPOffloadService))
def setUp(self): super(TestMain, self).setUp() self.useFixture(ClusterConfigurationFixture()) self.storage = self.make_dir() current_dir = os.path.join(self.storage, 'current') + os.sep os.makedirs(current_dir) with ClusterConfiguration.open_for_update() as config: config.tftp_root = current_dir os.rmdir(current_dir) # Forcing arch to amd64 causes pxelinux.0 to be installed, giving more # test coverage. self.image = make_image_spec(arch='amd64') self.os, self.arch, self.subarch, self.kflavor, \ self.release, self.label = self.image self.repo = self.make_simplestreams_repo(self.image)
def make_resource_path(self, filename): self.useFixture(ClusterConfigurationFixture()) tmpdir = self.make_dir() arch = factory.make_name("arch") subarch = factory.make_name("subarch") release = factory.make_name("release") label = factory.make_name("label") current_dir = os.path.join(tmpdir, "current") + "/" dirpath = os.path.join(current_dir, "custom", arch, subarch, release, label) os.makedirs(dirpath) factory.make_file(dirpath, filename) with ClusterConfiguration.open_for_update() as config: config.tftp_root = current_dir return arch, subarch, release, label
def test_process_node_tags_integration(self): self.useFixture( ClusterConfigurationFixture( maas_url=factory.make_simple_http_url())) get_hw_system1 = factory.make_response( http.client.OK, bson.BSON.encode({"lshw": b"<node />"}), "application/bson", ) get_hw_system2 = factory.make_response( http.client.OK, bson.BSON.encode({"lshw": b"<not-node />"}), "application/bson", ) mock_get = self.patch(MAASClient, "get") mock_get.side_effect = [get_hw_system1, get_hw_system2] mock_post = self.patch(MAASClient, "post") mock_post.return_value = factory.make_response( http.client.OK, b'{"added": 1, "removed": 1}', "application/json") tag_name = factory.make_name("tag") tag_definition = "//lshw:node" tag_nsmap = {"lshw": "lshw"} rack_id = factory.make_name("rack") tags.process_node_tags( rack_id, [{ "system_id": "system-id1" }, { "system_id": "system-id2" }], tag_name, tag_definition, tag_nsmap, self.fake_client(), ) tag_url = "/MAAS/api/2.0/tags/%s/" % (tag_name, ) self.assertThat( mock_post, MockCalledOnceWith( tag_url, as_json=True, op="update_nodes", rack_controller=rack_id, definition=tag_definition, add=["system-id1"], remove=["system-id2"], ), )
def test_update_last_image_sync_end_to_end(self): get_maas_id = self.patch(boot_images, "get_maas_id") get_maas_id.return_value = factory.make_string() self.useFixture(ClusterConfigurationFixture()) fixture = self.useFixture(MockLiveClusterToRegionRPCFixture()) protocol, connecting = fixture.makeEventLoop( region.UpdateLastImageSync) protocol.UpdateLastImageSync.return_value = succeed({}) self.addCleanup((yield connecting)) self.patch_autospec(boot_resources, 'import_images') boot_resources.import_images.return_value = True sources, hosts = make_sources() yield boot_images.import_boot_images(sources) self.assertThat(boot_resources.import_images, MockCalledOnceWith(sources)) self.assertThat(protocol.UpdateLastImageSync, MockCalledOnceWith(protocol, system_id=get_maas_id()))
def test_get_xinstall_parameters(self): # The base OperatingSystems class should only look for root-tgz, # child classes can override. osystem = make_osystem(self, factory.make_name('os')) tmpdir = self.make_dir() arch = factory.make_name('arch') subarch = factory.make_name('subarch') release = factory.make_name('release') label = factory.make_name('label') dir_path = os.path.join(tmpdir, osystem.name, arch, subarch, release, label) os.makedirs(dir_path) for fname in ['squashfs', 'root-tgz', 'root-dd']: factory.make_file(dir_path, fname) self.useFixture(ClusterConfigurationFixture(tftp_root=tmpdir)) self.assertItemsEqual( ('root-tgz', 'tgz'), osystem.get_xinstall_parameters(arch, subarch, release, label))
def test_get_xinstall_parameters(self): # The base OperatingSystems class should only look for root-tgz, # child classes can override. osystem = make_osystem(self, factory.make_name("os")) tmpdir = self.make_dir() arch = factory.make_name("arch") subarch = factory.make_name("subarch") release = factory.make_name("release") label = factory.make_name("label") dir_path = os.path.join(tmpdir, osystem.name, arch, subarch, release, label) os.makedirs(dir_path) for fname in ["squashfs", "root-tgz", "root-dd"]: factory.make_file(dir_path, fname) self.useFixture(ClusterConfigurationFixture(tftp_root=tmpdir)) self.assertItemsEqual( ("root-tgz", "tgz"), osystem.get_xinstall_parameters(arch, subarch, release, label), )
def setUp(self): """Start the rackd service.""" super(MAASClusterServiceFixture, self).setUp() self.useFixture( ClusterConfigurationFixture( cluster_uuid="adfd3977-f251-4f2c-8d61-745dbd690bf2", maas_url="http://0.0.0.0:5253/MAAS/", tftp_port=5255)) # Fork the process to have rackd run in its own process. twistd_pid = os.fork() if twistd_pid == 0: # Redirect all output to /dev/null redirect_to_devnull() # Add command line options to start twistd. sys.argv[1:] = ["--nodaemon", "--pidfile", "", "maas-rackd"] # Start twistd. try: twistd.run() except: traceback.print_exc() os._exit(2) finally: os._exit(0) else: # Add cleanup to stop the twistd service. self.addCleanup(self.stop_twistd, twistd_pid) # Check that the child process is still running after a few # seconds. This makes sure that everything started okay and it # is still running. time.sleep(2) try: os.kill(twistd_pid, 0) except OSError: # Not running. raise ServiceError( "Failed to start rackd. Check that another test is " "not running at the same time.")
def test_process_node_tags_integration(self): self.useFixture( ClusterConfigurationFixture( maas_url=factory.make_simple_http_url())) get_hw_system1 = factory.make_response( http.client.OK, bson.BSON.encode({'lshw': b'<node />'}), 'application/bson', ) get_hw_system2 = factory.make_response( http.client.OK, bson.BSON.encode({'lshw': b'<not-node />'}), 'application/bson', ) mock_get = self.patch(MAASClient, 'get') mock_get.side_effect = [get_hw_system1, get_hw_system2] mock_post = self.patch(MAASClient, 'post') mock_post.return_value = factory.make_response( http.client.OK, b'{"added": 1, "removed": 1}', 'application/json', ) tag_name = factory.make_name('tag') tag_definition = '//lshw:node' tag_nsmap = {"lshw": "lshw"} rack_id = factory.make_name('rack') tags.process_node_tags(rack_id, [{ "system_id": "system-id1" }, { "system_id": "system-id2" }], tag_name, tag_definition, tag_nsmap, self.fake_client()) tag_url = '/MAAS/api/2.0/tags/%s/' % (tag_name, ) self.assertThat( mock_post, MockCalledOnceWith(tag_url, as_json=True, op='update_nodes', rack_controller=rack_id, definition=tag_definition, add=['system-id1'], remove=['system-id2']))
def setUp(self): super(TestUpdateMaasClusterConf, self).setUp() self.useFixture(ClusterConfigurationFixture())
def test_get_local_cluster_UUID_returns_cluster_UUID(self): uuid = factory.make_UUID() self.useFixture(ClusterConfigurationFixture(cluster_uuid=uuid)) self.assertEqual(uuid, get_local_cluster_UUID())
def test_get_local_cluster_UUID_returns_None_if_not_set(self): self.useFixture(ClusterConfigurationFixture()) self.assertIsNone(get_local_cluster_UUID())
def setUp(self): super(TestProvisioningHTTPServiceMaker, self).setUp() self.useFixture(ClusterConfigurationFixture()) self.patch_autospec(logger, "configure")
def setUp(self): super(TestRegisterMAASRack, self).setUp() self.useFixture(ClusterConfigurationFixture()) self.mock_call_and_check = self.patch_autospec(register_command, "call_and_check")