def test_create_package_with_id(test_package): """ Test if we can create a wheel file given a agent directory and vip id Expected result: 1. wheel file with the name <distribution_name>-<version>-py2-none-any.whl 2. Wheel file should contains the identity passed in a file called 'IDENTITY_TEMPLATE' in <distribution_name>-<version>.dist-info folder :param test_package: fixture that creates the fake agent directory and returns the directory name, distribution name, version of the fake/test package, and package name """ tmpdir, distribution_name, version, package_name = test_package wheel_dir = os.path.join(tmpdir, "wheel_dir") result = create_package(tmpdir, wheel_dir, "test_vip_id") assert result == os.path.join(wheel_dir, '-'.join( [distribution_name, version, 'py2-none-any.whl'])) extract_dir = tempfile.mkdtemp() result2 = extract_package(result, extract_dir) dist_info_dir = os.path.join(result2, distribution_name + "-" + version + ".dist-info") files = os.listdir(dist_info_dir) assert 'IDENTITY_TEMPLATE' in files with open(os.path.join(dist_info_dir, 'IDENTITY_TEMPLATE'), 'r') as f: data = f.read().replace('\n', '') assert data == "test_vip_id"
def test_can_create_package(self): ''' Tests that a proper wheel package is created from the create_package method of the AgentPackage class. ''' agent_name = AGENT_TESTCASE1_NAME package_tmp_dir = os.path.join(self.tmpdir, 'create_package') expected_package_name = 'listeneragent-0.1-py2-none-any.whl' returned_package = create_package( self.get_agent_fixture(agent_name), package_tmp_dir) self.assertIsNotNone( returned_package, "Invalid package name {}".format(returned_package)) self.assertTrue(os.path.exists(returned_package)) self.assertEqual( expected_package_name, os.path.basename(returned_package)) # Wheel is in the correct location. self.assertEqual( os.path.join(package_tmp_dir, expected_package_name), returned_package) self.assertTrue(os.path.exists(returned_package)) try: wf = WheelFile(returned_package) # sets up the expected hashes for all of the wheel directory. self.assertIsNone(wf.verify()) # Reading the files # if the hash doesn't match it will throw an exception. for o in wf.zipfile.infolist(): wf.zipfile.open(o).read() wf.zipfile.close() finally: shutil.rmtree(package_tmp_dir)
def build_agentpackage(self, agent_dir, config_file): assert os.path.exists(agent_dir) assert os.path.exists(config_file) wheel_path = packaging.create_package(agent_dir, self.packaged_dir) packaging.add_files_to_package(wheel_path, { 'config_file': os.path.join('./', config_file) }) return wheel_path
def test_create_package_invalid_input(): """ Test error handling in create_package when invalid package directory is passed """ wheel_dir = os.path.join(tempfile.mkdtemp(), "wheel_dir") try: create_package("/abc/def/ghijkl", wheel_dir) pytest.fail("Expecting AgentPackageError got none") except AgentPackageError as e: assert e.message == "Invalid agent package directory specified" try: create_package(tempfile.mkdtemp(), wheel_dir) pytest.fail("Expecting NotImplementedError got none") except NotImplementedError: pass
def test_raises_error_if_agent_dir_not_exists(self): ''' This test passes under the following conditions: 1. An AgentPackageError is thrown if the passed agent directory doesen't exists. ''' # fake_agent = package_tmp_dir = os.path.join(self.tmpdir, 'fake') if os.path.exists(fake_agent): shutil.rmtree(fake_agent, True) self.assertRaises( AgentPackageError, lambda: create_package(fake_agent))
def test_create_package_no_id(test_package): """ Test if we can create a wheel file given a valid agent directory. Expected result: wheel file with the name <distribution_name>-<version>-py2-none-any.whl :param test_package: fixture that creates the fake agent directory and returns the directory name, distribution name, version of the fake/test package, and package name """ print ("cwd {}".format(os.getcwd())) tmpdir, distribution_name, version, package_name = test_package wheel_dir = os.path.join(tmpdir, "wheel_dir") result = create_package(tmpdir, wheel_dir) assert result == os.path.join(wheel_dir, '-'.join( [distribution_name, version, 'py2-none-any.whl']))
def build_agentpackage(self, agent_dir, config_file={}): if isinstance(config_file, dict): cfg_path = os.path.join(agent_dir, "config_temp") with open(cfg_path, "w") as tmp_cfg: tmp_cfg.write(jsonapi.dumps(config_file)) config_file = cfg_path # Handle relative paths from the volttron git directory. if not os.path.isabs(agent_dir): agent_dir = os.path.join(self.volttron_root, agent_dir) assert os.path.exists(config_file) assert os.path.exists(agent_dir) wheel_path = packaging.create_package(agent_dir, self.packaged_dir) packaging.add_files_to_package( wheel_path, {'config_file': os.path.join('./', config_file)}) return wheel_path
def build_agentpackage(self, agent_dir, config_file={}): if isinstance(config_file, dict): cfg_path = os.path.join(agent_dir, "config_temp") with open(cfg_path, "w") as tmp_cfg: tmp_cfg.write(jsonapi.dumps(config_file)) config_file = cfg_path # Handle relative paths from the volttron git directory. if not os.path.isabs(agent_dir): agent_dir = os.path.join(self.volttron_root, agent_dir) assert os.path.exists(config_file) assert os.path.exists(agent_dir) wheel_path = packaging.create_package(agent_dir, self.packaged_dir) packaging.add_files_to_package(wheel_path, { 'config_file': os.path.join('./', config_file) }) return wheel_path
def test_can_create_package(self): ''' Tests that a proper wheel package is created from the create_package method of the AgentPackage class. ''' agent_name = AGENT_TESTCASE1_NAME package_tmp_dir = os.path.join(self.tmpdir, 'create_package') expected_package_name = 'listeneragent-0.1-py2-none-any.whl' returned_package = create_package(self.get_agent_fixture(agent_name), package_tmp_dir) self.assertIsNotNone( returned_package, "Invalid package name {}".format(returned_package)) self.assertTrue(os.path.exists(returned_package)) self.assertEqual(expected_package_name, os.path.basename(returned_package)) # Wheel is in the correct location. self.assertEqual(os.path.join(package_tmp_dir, expected_package_name), returned_package) self.assertTrue(os.path.exists(returned_package)) try: wf = WheelFile(returned_package) # sets up the expected hashes for all of the wheel directory. self.assertIsNone(wf.verify()) # Reading the files # if the hash doesn't match it will throw an exception. for o in wf.zipfile.infolist(): wf.zipfile.open(o).read() wf.zipfile.close() finally: shutil.rmtree(package_tmp_dir)
# Note we don't remove the agent here because if we do that will # not allow us to update without losing the keys. The # install_agent method either installs or upgrades the agent. if opts.force and opts.vip_identity is None: # If force is specified then identity must be specified to indicate the target of the force log.error( "Force option specified without a target identity to force.") sys.exit(-10) if not opts.skip_requirements: # use pip requirements.txt file and install dependencies if nessary. install_requirements(agent_source) opts.package = create_package(agent_source, wheelhouse, opts.vip_identity) if not os.path.isfile(opts.package): log.error("The wheel file for the agent was unable to be created.") sys.exit(-10) jsonobj = None # At this point if we have opts.config, it will be an open reference to the # passed config file. if opts.config: # Attempt to use the yaml parser directly first try: tmp_cfg_load = yaml.safe_load(opts.config.read()) opts.config = tmp_cfg_load except yaml.scanner.ScannerError:
def direct_build_agentpackage(self, agent_dir): wheel_path = packaging.create_package(os.path.join('./', agent_dir), self.wheelhouse) return wheel_path
sys.exit(-10) # Note we don't remove the agent here because if we do that will # not allow us to update without losing the keys. The # install_agent method either installs or upgrades the agent. if opts.force and opts.vip_identity is None: # If force is specified then identity must be specified to indicate the target of the force log.error("Force option specified without a target identity to force.") sys.exit(-10) if not opts.skip_requirements: # use pip requirements.txt file and install dependencies if nessary. install_requirements(agent_source) opts.package = create_package(agent_source, wheelhouse, opts.vip_identity) if not os.path.isfile(opts.package): log.error("The wheel file for the agent was unable to be created.") sys.exit(-10) jsonobj = None # At this point if we have opts.config, it will be an open reference to the # passed config file. if opts.config: # Attempt to use the yaml parser directly first try: tmp_cfg_load = yaml.safe_load(opts.config.read()) opts.config = tmp_cfg_load except yaml.scanner.ScannerError:
def install_agent_directory(opts, publickey=None, secretkey=None): """ The main installation method for installing the agent on the correct local platform instance. :param opts: :param package: :param agent_config: :return: """ if not os.path.isfile(os.path.join(opts.install_path, "setup.py")): _log.error("Agent source must contain a setup.py file.") sys.exit(-10) assert opts.connection, "Connection must have been created to access this feature." if not opts.skip_requirements: install_requirements(opts.install_path) wheelhouse = os.path.join(get_home(), "packaged") opts.package = create_package(opts.install_path, wheelhouse, opts.vip_identity) if not os.path.isfile(opts.package): raise InstallRuntimeError( "The wheel file for the agent was unable to be created.") agent_uuid = None if not opts.vip_identity: agent_default_id_file = Path(opts.install_path).joinpath("IDENTITY") if agent_default_id_file.is_file(): with open(str(agent_default_id_file)) as fin: opts.vip_identity = fin.read().strip() # Verify and load agent_config up from the opts. agent_config will # be a yaml config file. agent_config = opts.agent_config if agent_config is None: agent_config = {} # if not a dict then config should be a filename if not isinstance(agent_config, dict): config_file = agent_config if not Path(config_file).exists(): raise InstallRuntimeError( f"Config file {config_file} does not exist!") else: cfg = tempfile.NamedTemporaryFile() with open(cfg.name, 'w') as fout: fout.write(yaml.safe_dump(agent_config)) config_file = cfg.name try: with open(config_file) as fp: data = yaml.safe_load(fp) except Exception as exc: raise InstallRuntimeError(exc) try: # Configure the whl file before installing. add_files_to_package(opts.package, {'config_file': config_file}) except FileNotFoundError: raise InstallRuntimeError(f"File not found: {config_file}") agent_uuid = _send_and_intialize_agent(opts, publickey, secretkey) return agent_uuid