diff --git a/volttron/platform/agent/base_weather.py b/volttron/platform/agent/base_weather.py index 655a32eaff..c61d59797c 100644 --- a/volttron/platform/agent/base_weather.py +++ b/volttron/platform/agent/base_weather.py @@ -79,6 +79,8 @@ FORECAST_TIME TIMESTAMP NOT NULL, POINTS TEXT NOT NULL);""" +AGENT_DATA_DIR = os.path.basename(os.getcwd()) + ".agent-data" + CACHE_READ_ERROR = "Cache read failed" CACHE_WRITE_ERROR = "Cache write failed" CACHE_FULL = "cache_full" @@ -102,7 +104,7 @@ class BaseWeatherAgent(Agent): """ def __init__(self, - database_file="weather.sqlite", + database_file=os.path.join(AGENT_DATA_DIR, "weather.sqlite"), api_key=None, max_size_gb=None, poll_locations=None, @@ -113,6 +115,8 @@ def __init__(self, # Initial agent configuration try: super(BaseWeatherAgent, self).__init__(**kwargs) + if os.path.dirname(database_file) == AGENT_DATA_DIR and not os.path.isdir(AGENT_DATA_DIR): + os.mkdir(AGENT_DATA_DIR) self._database_file = database_file self._async_call = AsyncCall() self._api_key = api_key @@ -426,7 +430,7 @@ def _configure(self, config_name, actions, contents): "Configuration of weather agent " "successful") except sqlite3.OperationalError as error: - _log.error("Error initializing cache {}".format(error)) + _log.error("Error initializing cache: {}".format(error)) self.vip.health.set_status(STATUS_BAD, "Cache failed to start " "during configuration") diff --git a/volttrontesting/services/weather/test_base_weather.py b/volttrontesting/services/weather/test_base_weather.py index 76d560ae00..b371f26b0f 100644 --- a/volttrontesting/services/weather/test_base_weather.py +++ b/volttrontesting/services/weather/test_base_weather.py @@ -71,6 +71,8 @@ "name": "fake4"} } +DATABASE_FILE = None + @pytest.fixture(scope="module") def query_agent(request, volttron_instance): @@ -205,6 +207,10 @@ def weather(request, volttron_instance): ) gevent.sleep(2) + global DATABASE_FILE + DATABASE_FILE = agent._database_file + assert DATABASE_FILE.endswith("weather.sqlite") + yield agent agent.core.stop() request.addfinalizer(remove_temp_file) @@ -228,7 +234,7 @@ def test_create_tables(weather): connection = weather._cache._sqlite_conn cursor = connection.cursor() - assert os.path.isfile(weather._database_file) + assert os.path.isfile(DATABASE_FILE) weather._cache.create_tables() @@ -282,7 +288,8 @@ def test_manage_cache_size(volttron_instance): connection = weather._cache._sqlite_conn cursor = connection.cursor() - assert os.path.isfile("weather.sqlite") + database_file = weather._cache._db_file_path + assert os.path.isfile(database_file) for service_name in weather._api_services: query = "DELETE FROM {};".format(service_name) @@ -962,7 +969,6 @@ def test_poll_location(volttron_instance, query_agent): def test_poll_multiple_locations(volttron_instance, query_agent, config, result_topics): gevent.sleep(1) - agent = None query_agent.poll_callback.reset_mock() try: @@ -1039,9 +1045,8 @@ def test_poll_errors(volttron_instance, query_agent, config, def delete_database_file(): - db_path = "weather.sqlite" - if os.path.isfile(db_path): - os.remove(db_path) + if os.path.isfile(DATABASE_FILE): + os.remove(DATABASE_FILE) @pytest.mark.weather2 @@ -1057,8 +1062,8 @@ def test_unhandled_cache_store_exception(volttron_instance, weather, conn.commit() # workaround to open the file in read only mode weather._cache._sqlite_conn.close() - os.chmod(weather._database_file, 0o444) - weather._cache._sqlite_conn = sqlite3.connect(weather._database_file) + os.chmod(DATABASE_FILE, 0o444) + weather._cache._sqlite_conn = sqlite3.connect(DATABASE_FILE) query_agent.alert_callback.reset_mock() results1 = query_agent.vip.rpc.call(identity, "get_current_weather", @@ -1105,8 +1110,8 @@ def test_unhandled_cache_store_exception(volttron_instance, weather, assert results1["observation_time"] != results2["observation_time"] finally: weather._cache._sqlite_conn.close() - os.chmod(weather._database_file, 0o666) - weather._cache._sqlite_conn = sqlite3.connect(weather._database_file) + os.chmod(DATABASE_FILE, 0o666) + weather._cache._sqlite_conn = sqlite3.connect(DATABASE_FILE) @pytest.mark.weather2 @@ -1155,4 +1160,4 @@ def test_unhandled_cache_read_exception(volttron_instance, weather, assert read_warning finally: # make sure the cache is ready to be used again - weather._cache._sqlite_conn = sqlite3.connect(weather._database_file) + weather._cache._sqlite_conn = sqlite3.connect(DATABASE_FILE)