当我的 django 应用程序通过 Jenkins 运行时,我在运行单元测试时遇到问题。
我有一个运行
python manage.py test
的 CI/CD 管道,目前失败并出现以下错误:
django.db.utils.ProgrammingError: relation "auth_user" does not exist
.
在本地运行测试工作正常,测试数据库正常创建并且测试正常运行。
我通过 SSH 连接到设置 Jenkins 的服务器来尝试手动运行测试,但我得到了相同的结果,所以我不会说这是 Jenkins 特有的任何内容。
postgres 用户拥有数据库上的所有权限和 CREATEDB,因此 postgres 方面似乎一切都很好。
这是完全的例外:
Traceback (most recent call last):
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "auth_user" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/agent/jenkins/workspace/INP-Backend/manage.py", line 22, in <module>
main()
File "/home/agent/jenkins/workspace/INP-Backend/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/commands/test.py", line 24, in run_from_argv
super().run_from_argv(argv)
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/base.py", line 402, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/base.py", line 448, in execute
output = self.handle(*args, **options)
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/commands/test.py", line 68, in handle
failures = test_runner.run_tests(test_labels)
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/test/runner.py", line 1045, in run_tests
old_config = self.setup_databases(
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/test/runner.py", line 941, in setup_databases
return _setup_databases(
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/test/utils.py", line 220, in setup_databases
connection.creation.create_test_db(
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/base/creation.py", line 78, in create_test_db
call_command(
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 198, in call_command
return command.execute(*args, **defaults)
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/base.py", line 448, in execute
output = self.handle(*args, **options)
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/base.py", line 96, in wrapped
res = handle_func(*args, **kwargs)
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 314, in handle
self.sync_apps(connection, executor.loader.unmigrated_apps)
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 480, in sync_apps
self.stdout.write(" Running deferred SQL...")
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 164, in __exit__
self.execute(sql)
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 199, in execute
cursor.execute(sql, params)
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 103, in execute
return super().execute(sql, params)
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/agent/jenkins/workspace/INP-Backend/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "auth_user" does not exist
我已经解决了这个问题。在 @djvg 提到没有迁移的模型之后,我进行了更深入的研究。结果发现应用程序没有自动迁移,必须为其运行手动迁移。
我仍然不知道为什么这会完全停止测试,因为该应用程序不依赖于其他应用程序,并且没有在测试用例中使用,但迁移该应用程序专门解决了我的问题。