def test_in_container_with_container_type_file(self): """ If the /run/container_type file is found, the content is returned as container type. """ container_type_file = os.path.join(self.run_path, "container_type") self.makeFile(content="lxc", path=container_type_file) self.assertEqual("lxc", get_container_info(run_path=self.run_path))
def test_in_container_with_systemd_container_file(self): """ If the /run/systemd/container file is found, the content is returned as container type. """ os.mkdir(os.path.join(self.run_path, "systemd")) container_type_file = os.path.join(self.run_path, "systemd/container") self.makeFile(content="lxc", path=container_type_file) self.assertEqual("lxc", get_container_info(run_path=self.run_path))
def test_strip_newline(self): """The container type doesn't contain newlines.""" container_type_file = os.path.join(self.run_path, "container_type") self.makeFile(content="lxc\n", path=container_type_file) self.assertEqual("lxc", get_container_info(run_path=self.run_path))
def test_no_container(self): """If not running in a container, an empty string is returned.""" self.assertEqual("", get_container_info(self.run_path))
def _handle_pre_exchange(self): """ An exchange is about to happen. If we don't have a secure id already set, and we have the needed information available, queue a registration message with the server. """ # The point of storing this flag is that if we should *not* register # now, and then after the exchange we *should*, we schedule an urgent # exchange again. Without this flag we would just spin trying to # connect to the server when something is clearly preventing the # registration. self._should_register = self.should_register() if not self._should_register: return # These are just to shorten the code. identity = self._identity account_name = identity.account_name if not account_name: self._reactor.fire("registration-failed", reason="unknown-account") return tags = identity.tags group = identity.access_group registration_key = identity.registration_key self._message_store.delete_all_messages() if not is_valid_tag_list(tags): tags = None logging.error("Invalid tags provided for registration.") message = { "type": "register", "hostname": get_fqdn(), "account_name": account_name, "computer_title": identity.computer_title, "registration_password": identity.registration_key, "tags": tags, "container-info": get_container_info(), "vm-info": get_vm_info() } if self._clone_secure_id: # We use the secure id here because the registration is encrypted # and the insecure id has been already exposed to unencrypted # http from the ping server. In addition it's more straightforward # to get the computer from the server through it than the insecure message["clone_secure_id"] = self._clone_secure_id if group: message["access_group"] = group server_api = self._message_store.get_server_api() # If we have juju data to send and if the server is recent enough to # know how to handle juju data, then we include it in the registration # message. We want to trigger the 3.3 server handler because client # version 14.01 has a different format for the juju-info field, # so this makes sure that the correct schema is used by the server # when validating our message. if self._juju_data and is_version_higher(server_api, b"3.3"): message["juju-info"] = { "environment-uuid": self._juju_data["environment-uuid"], "api-addresses": self._juju_data["api-addresses"], "machine-id": self._juju_data["machine-id"] } # The computer is a normal computer, possibly a container. with_word = "with" if bool(registration_key) else "without" with_tags = "and tags %s " % tags if tags else "" with_group = "in access group '%s' " % group if group else "" logging.info(u"Queueing message to register with account %r %s%s" "%s a password." % (account_name, with_group, with_tags, with_word)) self._exchange.send(message)