def setUp(self, parameters=None): """ Sets up a Shotgun Mockgun instance with a project and a basic project scaffold on disk. :param parameters: Dictionary with additional parameters to control the setup. The method currently supports the following parameters: - 'project_tank_name': 'name' - Set the tank_name of the project to something explicit. If not specified, this will default to 'project_code' - 'mockgun_schema_path': '/path/to/file' - Pass a specific schema to use with mockgun. If not specified, the tk-core fixture schema will be used. - 'mockgun_schema_entity_path': '/path/to/file' - Pass a specific entity schema to use with mockgun. If not specified, the tk-core fixture schema will be used. """ parameters = parameters or {} if "project_tank_name" in parameters: project_tank_name = parameters["project_tank_name"] else: # default project name project_tank_name = "project_code" if "mockgun_schema_path" in parameters: mockgun_schema_path = parameters["mockgun_schema_path"] else: mockgun_schema_path = os.path.join(self.fixtures_root, "mockgun", "schema.pickle") if "mockgun_schema_entity_path" in parameters: mockgun_schema_entity_path = parameters[ "mockgun_schema_entity_path"] else: mockgun_schema_entity_path = os.path.join(self.fixtures_root, "mockgun", "schema_entity.pickle") # set up mockgun to use our schema mockgun.Shotgun.set_schema_paths(mockgun_schema_path, mockgun_schema_entity_path) self.tank_temp = TANK_TEMP self.init_cache_location = os.path.join(self.tank_temp, "init_cache.cache") shotgun_authentication.session_cache._get_cache_location = lambda: os.path.join( self.tank_temp, "session_cache") def _get_cache_location_mock(): return self.init_cache_location self._original_get_cache_location = tank.pipelineconfig_factory._get_cache_location tank.pipelineconfig_factory._get_cache_location = _get_cache_location_mock # Mock this so that authentication manager works even tough we are not in a config. # If we don't mock it than the path cache calling get_current_user will fail. def _get_associated_sg_config_data_mock(): return {"host": "https://somewhere.shotguntudio.com"} self._original_get_associated_sg_config_data = tank.util.shotgun.get_associated_sg_config_data tank.util.shotgun.get_associated_sg_config_data = _get_associated_sg_config_data_mock # define entity for test project self.project = { "type": "Project", "id": 1, "tank_name": project_tank_name, "name": "project_name" } self.project_root = os.path.join( self.tank_temp, self.project["tank_name"].replace("/", os.path.sep)) self.pipeline_config_root = os.path.join(self.tank_temp, "pipeline_configuration") # move away previous data self._move_project_data() # create new structure os.makedirs(self.project_root) os.makedirs(self.pipeline_config_root) # project level config directories self.project_config = os.path.join(self.pipeline_config_root, "config") # create project cache directory project_cache_dir = os.path.join(self.pipeline_config_root, "cache") os.mkdir(project_cache_dir) # define entity for pipeline configuration self.sg_pc_entity = { "type": "PipelineConfiguration", "code": "Primary", "id": 123, "project": self.project, "windows_path": self.pipeline_config_root, "mac_path": self.pipeline_config_root, "linux_path": self.pipeline_config_root } # add files needed by the pipeline config pc_yml = os.path.join(self.pipeline_config_root, "config", "core", "pipeline_configuration.yml") pc_yml_data = ( "{ project_name: %s, use_shotgun_path_cache: true, pc_id: %d, " "project_id: %d, pc_name: %s}\n\n" % (self.project["tank_name"], self.sg_pc_entity["id"], self.project["id"], self.sg_pc_entity["code"])) self.create_file(pc_yml, pc_yml_data) loc_yml = os.path.join(self.pipeline_config_root, "config", "core", "install_location.yml") loc_yml_data = "Windows: '%s'\nDarwin: '%s'\nLinux: '%s'" % ( self.pipeline_config_root, self.pipeline_config_root, self.pipeline_config_root) self.create_file(loc_yml, loc_yml_data) roots = {"primary": {}} for os_name in ["windows_path", "linux_path", "mac_path"]: #TODO make os specific roots roots["primary"][os_name] = self.tank_temp roots_path = os.path.join(self.pipeline_config_root, "config", "core", "roots.yml") roots_file = open(roots_path, "w") roots_file.write(yaml.dump(roots)) roots_file.close() self.pipeline_configuration = sgtk.pipelineconfig_factory.from_path( self.pipeline_config_root) self.tk = tank.Tank(self.pipeline_configuration) # set up mockgun and make sure shotgun connection calls route via mockgun self.mockgun = mockgun.Shotgun("http://unit_test_mock_sg", "mock_user", "mock_key") def get_associated_sg_base_url_mocker(): return "http://unit_test_mock_sg" def create_sg_connection_mocker(): return self.mockgun self._original_get_associated_sg_base_url = tank.util.shotgun.get_associated_sg_base_url tank.util.shotgun.get_associated_sg_base_url = get_associated_sg_base_url_mocker self._original_create_sg_connection = tank.util.shotgun.create_sg_connection tank.util.shotgun.create_sg_connection = create_sg_connection_mocker # add project to mock sg and path cache db self.add_production_path(self.project_root, self.project) # add pipeline configuration self.add_to_sg_mock_db(self.sg_pc_entity) # add local storage self.primary_storage = { "type": "LocalStorage", "id": 7777, "code": "primary", "windows_path": self.tank_temp, "linux_path": self.tank_temp, "mac_path": self.tank_temp } self.add_to_sg_mock_db(self.primary_storage)
def setUp(self, parameters=None): """ Sets up a Shotgun Mockgun instance with a project and a basic project scaffold on disk. :param parameters: Dictionary with additional parameters to control the setup. The method currently supports the following parameters: - 'project_tank_name': 'name' - Set the tank_name of the project to something explicit. If not specified, this will default to 'project_code' - 'mockgun_schema_path': '/path/to/file' - Pass a specific schema to use with mockgun. If not specified, the tk-core fixture schema will be used. - 'mockgun_schema_entity_path': '/path/to/file' - Pass a specific entity schema to use with mockgun. If not specified, the tk-core fixture schema will be used. - 'primary_root_name': 'name' - Set the primary root name, default to 'unit_tests'. """ self.addCleanup(self._assert_teardown_called) # Override SHOTGUN_HOME so that unit tests can be sandboxed. self._old_shotgun_home = os.environ.get(self.SHOTGUN_HOME) os.environ[self.SHOTGUN_HOME] = TANK_TEMP # Make sure the global settings instance has been reset so anything from a previous test doesn't # leak into the next one. UserSettings.clear_singleton() parameters = parameters or {} self._do_io = parameters.get("do_io", True) if "project_tank_name" in parameters: project_tank_name = parameters["project_tank_name"] else: # default project name project_tank_name = "project_code" # now figure out mockgun location # 1. see if we have it explicitly specified in the parameters # 2. if not, check if the fixtures location has a mockgun folder # 3. if not, fall back on built in mockgun fixtures if "mockgun_schema_path" in parameters: mockgun_schema_path = parameters["mockgun_schema_path"] elif os.path.exists(os.path.join(self.fixtures_root, "mockgun")): mockgun_schema_path = os.path.join( self.fixtures_root, "mockgun", "schema.pickle" ) else: # use the std core fixtures mockgun_schema_path = os.path.join( self.tank_source_path, "tests", "fixtures", "mockgun", "schema.pickle" ) if "mockgun_schema_entity_path" in parameters: mockgun_schema_entity_path = parameters["mockgun_schema_entity_path"] elif os.path.exists(os.path.join(self.fixtures_root, "mockgun")): mockgun_schema_entity_path = os.path.join( self.fixtures_root, "mockgun", "schema_entity.pickle" ) else: # use the std core fixtures mockgun_schema_entity_path = os.path.join( self.tank_source_path, "tests", "fixtures", "mockgun", "schema_entity.pickle" ) # The name to use for our primary storage self.primary_root_name = parameters.get("primary_root_name", "unit_tests") # set up mockgun to use our schema mockgun.Shotgun.set_schema_paths(mockgun_schema_path, mockgun_schema_entity_path) self.tank_temp = TANK_TEMP self.cache_root = os.path.join(self.tank_temp, "cache_root") # Mock this so that authentication manager works even tough we are not in a config. # If we don't mock it than the path cache calling get_current_user will fail. self._mock_return_value( "tank.util.shotgun.connection.get_associated_sg_config_data", {"host": "https://somewhere.shotgunstudio.com"} ) # define entity for test project self.project = { "type": "Project", "id": 1, "tank_name": project_tank_name, "name": "project_name", "archived": False, } self.project_root = os.path.join(self.tank_temp, self.project["tank_name"].replace("/", os.path.sep)) self.pipeline_config_root = os.path.join(self.tank_temp, "pipeline_configuration") if self._do_io: # move away previous data self._move_project_data() # create new structure os.makedirs(self.project_root) os.makedirs(self.pipeline_config_root) # # copy tank util scripts shutil.copy( os.path.join(self.tank_source_path, "setup", "root_binaries", "tank"), os.path.join(self.pipeline_config_root, "tank") ) shutil.copy( os.path.join(self.tank_source_path, "setup", "root_binaries", "tank.bat"), os.path.join(self.pipeline_config_root, "tank.bat") ) # project level config directories self.project_config = os.path.join(self.pipeline_config_root, "config") # create project cache directory project_cache_dir = os.path.join(self.pipeline_config_root, "cache") if self._do_io: os.mkdir(project_cache_dir) # define entity for pipeline configuration self.sg_pc_entity = {"type": "PipelineConfiguration", "code": "Primary", "id": 123, "project": self.project, "windows_path": self.pipeline_config_root, "mac_path": self.pipeline_config_root, "linux_path": self.pipeline_config_root} # add files needed by the pipeline config pc_yml = os.path.join(self.pipeline_config_root, "config", "core", "pipeline_configuration.yml") pc_yml_data = ("{ project_name: %s, use_shotgun_path_cache: true, pc_id: %d, " "project_id: %d, pc_name: %s}\n\n" % (self.project["tank_name"], self.sg_pc_entity["id"], self.project["id"], self.sg_pc_entity["code"])) if self._do_io: self.create_file(pc_yml, pc_yml_data) loc_yml = os.path.join(self.pipeline_config_root, "config", "core", "install_location.yml") loc_yml_data = "Windows: '%s'\nDarwin: '%s'\nLinux: '%s'" % ( self.pipeline_config_root, self.pipeline_config_root, self.pipeline_config_root ) if self._do_io: self.create_file(loc_yml, loc_yml_data) # inject this file which toolkit is probing for to determine # if an installation has been localized. localize_token_file = os.path.join(self.pipeline_config_root, "install", "core", "_core_upgrader.py") if self._do_io: self.create_file(localize_token_file, "foo bar") roots = {self.primary_root_name: {}} for os_name in ["windows_path", "linux_path", "mac_path"]: # TODO make os specific roots roots[self.primary_root_name][os_name] = self.tank_temp if self._do_io: roots_path = os.path.join(self.pipeline_config_root, "config", "core", "roots.yml") roots_file = open(roots_path, "w") roots_file.write(yaml.dump(roots)) roots_file.close() # clear bundle in-memory cache sgtk.descriptor.io_descriptor.factory.g_cached_instances = {} if self._do_io: self.pipeline_configuration = sgtk.pipelineconfig_factory.from_path(self.pipeline_config_root) self.tk = tank.Tank(self.pipeline_configuration) # set up mockgun and make sure shotgun connection calls route via mockgun self.mockgun = mockgun.Shotgun("http://unit_test_mock_sg", "mock_user", "mock_key") # fake a version response from the server self.mockgun.server_info = {"version": (7, 0, 0)} self._mock_return_value("tank.util.shotgun.connection.get_associated_sg_base_url", "http://unit_test_mock_sg") self._mock_return_value("tank.util.shotgun.connection.create_sg_connection", self.mockgun) self._mock_return_value("tank.util.shotgun.get_associated_sg_base_url", "http://unit_test_mock_sg") self._mock_return_value("tank.util.shotgun.create_sg_connection", self.mockgun) # add project to mock sg and path cache db if self._do_io: self.add_production_path(self.project_root, self.project) # add pipeline configuration self.add_to_sg_mock_db(self.project) self.add_to_sg_mock_db(self.sg_pc_entity) # add local storage self.primary_storage = {"type": "LocalStorage", "id": 7777, "code": self.primary_root_name, "windows_path": self.tank_temp, "linux_path": self.tank_temp, "mac_path": self.tank_temp} self.add_to_sg_mock_db(self.primary_storage) # back up the authenticated user in case a unit test doesn't clean up correctly. self._authenticated_user = sgtk.get_authenticated_user()
def _setUp(self, parameters): """ See documentation for setUp. """ self.addCleanup(self._assert_teardown_called) # Override SHOTGUN_HOME so that unit tests can be sandboxed. self._old_shotgun_home = os.environ.get(self.SHOTGUN_HOME) os.environ[self.SHOTGUN_HOME] = TANK_TEMP # Make sure the global settings instance has been reset so anything from a previous test doesn't # leak into the next one. UserSettings.clear_singleton() parameters = parameters or {} self._do_io = parameters.get("do_io", True) # now figure out mockgun location # 1. see if we have it explicitly specified in the parameters # 2. if not, check if the fixtures location has a mockgun folder # 3. if not, fall back on built in mockgun fixtures if "mockgun_schema_path" in parameters: mockgun_schema_path = parameters["mockgun_schema_path"] elif os.path.exists(os.path.join(self.fixtures_root, "mockgun")): mockgun_schema_path = os.path.join(self.fixtures_root, "mockgun", "schema.pickle") else: # use the std core fixtures mockgun_schema_path = os.path.join(self.tank_source_path, "tests", "fixtures", "mockgun", "schema.pickle") if "mockgun_schema_entity_path" in parameters: mockgun_schema_entity_path = parameters[ "mockgun_schema_entity_path"] elif os.path.exists(os.path.join(self.fixtures_root, "mockgun")): mockgun_schema_entity_path = os.path.join(self.fixtures_root, "mockgun", "schema_entity.pickle") else: # use the std core fixtures mockgun_schema_entity_path = os.path.join( self.tank_source_path, "tests", "fixtures", "mockgun", "schema_entity.pickle", ) # set up mockgun to use our schema mockgun.Shotgun.set_schema_paths(mockgun_schema_path, mockgun_schema_entity_path) # Mock this so that authentication manager works even tough we are not in a config. # If we don't mock it than the path cache calling get_current_user will fail. self._mock_return_value( "tank.util.shotgun.connection.get_associated_sg_config_data", {"host": "https://somewhere.shotgunstudio.com"}, ) # set up mockgun and make sure shotgun connection calls route via mockgun self.mockgun = mockgun.Shotgun("http://unit_test_mock_sg", "mock_user", "mock_key") # fake a version response from the server self.mockgun.server_info = {"version": (7, 0, 0)} self._mock_return_value( "tank.util.shotgun.connection.get_associated_sg_base_url", "http://unit_test_mock_sg", ) self._mock_return_value( "tank.util.shotgun.connection.create_sg_connection", self.mockgun) self._mock_return_value("tank.util.shotgun.get_associated_sg_base_url", "http://unit_test_mock_sg") self._mock_return_value("tank.util.shotgun.create_sg_connection", self.mockgun) # now, set up a project, pipeline configuration and storage root for testing # The name to use for our primary storage self.primary_root_name = parameters.get("primary_root_name", "unit_tests") self.tank_temp = TANK_TEMP self.cache_root = os.path.join(self.tank_temp, "cache_root") # Initialize storage roots self.roots = {self.primary_root_name: {}} for os_name in ["windows_path", "linux_path", "mac_path"]: # TODO make os specific roots self.roots[self.primary_root_name][os_name] = self.tank_temp # initialize list of projects for testing. if "project_tank_name" in parameters: project_tank_name = parameters["project_tank_name"] else: # default project name project_tank_name = "project_code" self.project, self.project_root = self.create_project( {"tank_name": project_tank_name}) ( self.sg_pc_entity, self.pipeline_configuration, self.pipeline_config_root, self.project_config, self.tk, ) = self.create_pipeline_configuration(self.project, {"code": "Primary"}) if self._do_io: # add the project root to the production path (once self.tk is initialized) self.add_production_path(self.project_root, self.project) # add local storage self.primary_storage = { "type": "LocalStorage", "id": 7777, "code": self.primary_root_name, "windows_path": self.tank_temp, "linux_path": self.tank_temp, "mac_path": self.tank_temp, } self.add_to_sg_mock_db(self.primary_storage) # back up the authenticated user in case a unit test doesn't clean up correctly. self._authenticated_user = sgtk.get_authenticated_user() sgtk.util.login.g_shotgun_current_user_cache = "unknown" sgtk.util.login.g_shotgun_user_cache = "unknown"