无法运行Graphite测试:(1044,“拒绝用户'石墨'@'%'访问数据库'test_graphite'”)

时间:2014-01-23 18:34:09

标签: django unit-testing graphite

我正在尝试在一个或多或少的石墨安装上运行石墨测试。

这意味着我在目录/ opt / graphite / webapp中并调用一个测试:

python manage.py test --settings=tests.settings -p "test_finders.py" -v 3 --traceback

无论我使用什么文件(例如,上面,“test_finders.py”),都会发生错误。

返回的错误文本是:

/usr/lib/python2.6/site-packages/django/conf/__init__.py:75: DeprecationWarning: The ADMIN_MEDIA_PREFIX setting has been removed; use STATIC_URL instead.
  "use STATIC_URL instead.", DeprecationWarning)
graphite.finders.standard.StandardFinder
Creating test database for alias 'default' ('test_graphite')...
Got an error creating the test database: (1044, "Access denied for user 'graphite'@'%' to database 'test_graphite'")
Type 'yes' if you would like to try deleting the test database 'test_graphite', or 'no' to cancel: yes
Destroying old test database 'default'...
Got an error recreating the test database: (1044, "Access denied for user 'graphite'@'%' to database 'test_graphite'")

我没有找到关于如何完成这项工作的答案。

1 个答案:

答案 0 :(得分:0)

事实证明,我没有正确设置我的Django环境。

  1. 确保您可以调用django-admin命令而不会出现错误。我试过这个,并不断收到错误消息:

    django-admin shell 错误:无法导入设置,因为未定义环境变量DJANGO_SETTINGS_MODULE。

  2. 但是,设置此变量并不明显应该设置为什么。我一直使用/ opt / graphite / webapp / graphite / settings的路径概念,这是错误的。相反它应该是:

    export DJANGO_SETTINGS_MODULE=graphite.settings
    

    然后调用django-admin shell并查看:

    /usr/lib/python2.6/site-packages/django/conf/__init__.py:75: DeprecationWarning: The ADMIN_MEDIA_PREFIX setting has been removed; use STATIC_URL instead.
      "use STATIC_URL instead.", DeprecationWarning)
    /usr/lib/python2.6/site-packages/django/core/cache/__init__.py:82: DeprecationWarning:  settings.CACHE_* is deprecated; use settings.CACHES instead.
      DeprecationWarning
    Python 2.6.6 (r266:84292, Jul 20 2011, 10:22:43) 
    [GCC 4.4.5 20110214 (Red Hat 4.4.5-6)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>> 
    

    我们看到'拒绝用户访问'的问题的根本原因是Django需要存储有关它在数据库中运行的测试的数据。它正在尝试连接到settings.py模块中指定的任何数据库(如果您在那里定义了数据库,则为local_settings.py)。

    基本上,它无法在您的设置文件中指定的数据库中创建名为“test_graphite”的新数据库。因此,您需要(a)允许您的数据库用户具有create_database权限,或者(b)更改为使用标准sqlite数据库,它可以创建此数据库。

    要查看我们遇到问题,请调用dbshel​​l并观察出现的数据库:

    django-admin dbshell
    

    这应该显示您正在使用的数据库。

    1. 为了不使用我的生产数据库进行测试,我移除了local_settings.py文件(重命名)并期望Django使用settings.py中指定的Sqlite3。
    2. 错误!

      我忘了离开local_settings.pyc文件(是的,PYC文件再次搞砸了我们)。一旦我这样做,我得到了正确的答复:

      $ python manage.py test --settings=tests.settings -p "test_finders.py" -v 3 --traceback
      Could not import graphite.local_settings, using defaults!
      /opt/graphite.github/webapp/graphite/settings.py:204: UserWarning: SECRET_KEY is set to an unsafe   default. This should be set in local_settings.py for better security
        warn('SECRET_KEY is set to an unsafe default. This should be set in local_settings.py for better  security')
      /usr/lib/python2.6/site-packages/django/conf/__init__.py:75: DeprecationWarning: The ADMIN_MEDIA_PREFIX setting has been removed; use STATIC_URL instead.
        "use STATIC_URL instead.", DeprecationWarning)
      graphite.finders.standard.StandardFinder
      Creating test database for alias 'default' (':memory:')...
      Creating tables ...
      Creating table account_profile
      Creating table account_variable
      Creating table account_view
      Creating table account_window
      ...
      Installed 0 object(s) from 0 fixture(s)
      test_custom_finder (tests.test_finders.FinderTest) ... tests.test_finders.DummyFinder
      ok
      
      ----------------------------------------------------------------------
      Ran 1 test in 0.004s
      
      OK
      Destroying test database for alias 'default' (':memory:')...
      

      现在它有效!