/usr/share/doc/keystone-doc/html/developing.html is in keystone-doc 2:9.0.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Developing with Keystone — keystone documentation</title>
<link rel="stylesheet" href="_static/nature.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/tweaks.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="keystone documentation" href="index.html" />
<link rel="next" title="Developing Keystone Drivers" href="developing_drivers.html" />
<link rel="prev" title="Getting Involved" href="community.html" />
</head>
<body role="document">
<div id="header">
<h1 id="logo"><a href="http://www.openstack.org/">OpenStack</a></h1>
<ul id="navigation">
<li><a href="http://www.openstack.org/" title="Go to the Home page" class="link">Home</a></li>
<li><a href="http://www.openstack.org/projects/" title="Go to the OpenStack Projects page">Projects</a></li>
<li><a href="http://www.openstack.org/user-stories/" title="Go to the User Stories page" class="link">User Stories</a></li>
<li><a href="http://www.openstack.org/community/" title="Go to the Community page" class="link">Community</a></li>
<li><a href="http://www.openstack.org/blog/" title="Go to the OpenStack Blog">Blog</a></li>
<li><a href="http://wiki.openstack.org/" title="Go to the OpenStack Wiki">Wiki</a></li>
<li><a href="http://docs.openstack.org/" title="Go to OpenStack Documentation" class="current">Documentation</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="developing-with-keystone">
<h1>Developing with Keystone<a class="headerlink" href="#developing-with-keystone" title="Permalink to this headline">¶</a></h1>
<div class="section" id="setup">
<h2>Setup<a class="headerlink" href="#setup" title="Permalink to this headline">¶</a></h2>
<p>Get your development environment set up according to
<a class="reference internal" href="devref/development.environment.html"><em>Setting up a Keystone development environment</em></a>. It is recommended that you install
Keystone into a virtualenv.</p>
</div>
<div class="section" id="configuring-keystone">
<h2>Configuring Keystone<a class="headerlink" href="#configuring-keystone" title="Permalink to this headline">¶</a></h2>
<p>Keystone requires a configuration file. There is a sample configuration file
that can be used to get started:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ cp etc/keystone.conf.sample etc/keystone.conf
</pre></div>
</div>
<p>The defaults are enough to get you going, but you can make any changes if
needed.</p>
</div>
<div class="section" id="running-keystone">
<h2>Running Keystone<a class="headerlink" href="#running-keystone" title="Permalink to this headline">¶</a></h2>
<p>To run the Keystone Admin and API server instances, use:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ keystone-all
</pre></div>
</div>
<p>This runs Keystone with the configuration the etc/ directory of the project.
See <a class="reference internal" href="configuration.html"><em>Configuring Keystone</em></a> for details on how Keystone is configured. By default,
Keystone is configured with SQL backends.</p>
</div>
<div class="section" id="interacting-with-keystone">
<h2>Interacting with Keystone<a class="headerlink" href="#interacting-with-keystone" title="Permalink to this headline">¶</a></h2>
<p>You can interact with Keystone through the command line using
<a class="reference internal" href="man/keystone-manage.html"><em>keystone-manage</em></a> which allows you to initialize keystone, etc.</p>
<p>You can also interact with Keystone through its REST API. There is a Python
Keystone client library <a class="reference external" href="https://git.openstack.org/cgit/openstack/python-keystoneclient">python-keystoneclient</a> which interacts exclusively
through the REST API, and which Keystone itself uses to provide its
command-line interface.</p>
<p>When initially getting set up, after you’ve configured which databases to use,
you’re probably going to need to run the following to your database schema in
place:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ keystone-manage db_sync
</pre></div>
</div>
<p>If the above commands result in a <code class="docutils literal"><span class="pre">KeyError</span></code>, or they fail on a
<code class="docutils literal"><span class="pre">.pyc</span></code> file with the message, <code class="docutils literal"><span class="pre">You</span> <span class="pre">can</span> <span class="pre">only</span> <span class="pre">have</span> <span class="pre">one</span> <span class="pre">Python</span> <span class="pre">script</span> <span class="pre">per</span>
<span class="pre">version</span></code>, then it is possible that there are out-of-date compiled Python
bytecode files in the Keystone directory tree that are causing problems. This
can occur if you have previously installed and ran older versions of Keystone.
These out-of-date files can be easily removed by running a command like the
following from the Keystone root project directory:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ find . -name <span class="s2">"*.pyc"</span> -delete
</pre></div>
</div>
</div>
<div class="section" id="database-schema-migrations">
<h2>Database Schema Migrations<a class="headerlink" href="#database-schema-migrations" title="Permalink to this headline">¶</a></h2>
<p>Keystone uses <a class="reference external" href="https://git.openstack.org/cgit/openstack/sqlalchemy-migrate">SQLAlchemy-migrate</a> to migrate the SQL database between
revisions. For core components, the migrations are kept in a central
repository under <code class="docutils literal"><span class="pre">keystone/common/sql/migrate_repo/versions</span></code>. Each
SQL migration has a version which can be identified by the name of
the script, the version is the number before the underline.
For example, if the script is named <code class="docutils literal"><span class="pre">001_add_X_table.py</span></code> then the
version of the SQL migration is <code class="docutils literal"><span class="pre">1</span></code>.</p>
<p>For the migration to work, both the <code class="docutils literal"><span class="pre">migrate_repo</span></code> and <code class="docutils literal"><span class="pre">versions</span></code>
subdirectories must have <code class="docutils literal"><span class="pre">__init__.py</span></code> files. SQLAlchemy-migrate will look
for a configuration file in the <code class="docutils literal"><span class="pre">migrate_repo</span></code> named <code class="docutils literal"><span class="pre">migrate.cfg</span></code>. This
conforms to a key/value <cite>ini</cite> file format. A sample configuration file with
the minimal set of values is:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span><span class="n">db_settings</span><span class="p">]</span>
<span class="n">repository_id</span><span class="o">=</span><span class="n">my_extension</span>
<span class="n">version_table</span><span class="o">=</span><span class="n">migrate_version</span>
<span class="n">required_dbs</span><span class="o">=</span><span class="p">[]</span>
</pre></div>
</div>
<p>To run a migration for upgrade, simply run:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ keystone-manage db_sync <version>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If no version is specified, then the most recent migration will be used.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Schema downgrades are not supported.</p>
</div>
<p id="online-migration">From Mitaka release, we are starting to write the migration scripts in a
backward compatible way to support <a class="reference external" href="https://specs.openstack.org/openstack/keystone-specs/specs/mitaka/online-schema-migration.html">online schema migration</a>. The following
guidelines for schema and data migrations should be followed:</p>
<ul class="simple">
<li>Additive schema migrations - In general, almost all schema migrations should
be additive. Put simply, they should only create elements like columns,
indices, and tables.</li>
<li>Subtractive schema migrations - To remove an element like a column or table:<ol class="arabic">
<li>Expand phase: The element must be deprecated and retained for backward
compatibility. This allows for graceful upgrade from X release to X+1.</li>
<li>Migrate phase: Data migration must completely migrate data from the old
version of the schema to the new version. Data migrations should have the
ability to run online, while the service is operating normally, so the
keystone service implementation (typically the SQLAlchemy model) has to
be aware that data should be retrieved and/or written from/to more than
one place and format, to maintain consistency (see examples below).</li>
<li>Contract phase: The column can then be removed with a schema migration at
the start of X+2. Contract phase can’t happen if the data migration isn’t
finished (see last point in this section).</li>
</ol>
</li>
<li>Release notes - There should be a release note in case an operation is
“blocking”, “expensive”, or both. You can find information on which DDL
operations are expensive in <a class="reference external" href="https://dev.mysql.com/doc/refman/5.7/en/innodb-create-index-overview.html">MySQL docs</a>. Other supported SQL DBs support
<a class="reference external" href="https://wiki.postgresql.org/wiki/Transactional_DDL_in_PostgreSQL:_A_Competitive_Analysis">transactional DDL</a>, and experienced DBA’s know to take advantage of this
feature.</li>
<li>Constraints - When adding a foreign or unique key constraint, the schema
migration code needs to handle possible problems with data before applying
the constraint. For example, a unique constraint must clean up duplicate
records before applying said constraint.</li>
<li>Data migrations - should be done in an online fashion by custom code in the
SQLAlchemy layer that handles moving data between the old and new portions
of the schema. In addition, for each type of data migration performed,
a keystone-manage command can be added for the operator to manually request
that rows be migrated (see examples below, like the nova flavor migration).</li>
<li>All schema migrations should be idempotent. For example, a migration
should check if an element exists in the schema before attempting to add
it. This logic comes for free in the autogenerated workflow of
the online migrations.</li>
<li>Before running <cite>contract</cite> in the expand/migrate/contract schema migration
workflow, the remaining data migrations should be performed by the contract
script. Alternatively, running a relevant keystone-manage migration should
be enforced, to ensure that all remaining data migrations are completed.
It is a good practice to move data out of the old columns, and ensure they
are filled with null values before removing them.</li>
</ul>
<p>A good example of an online schema migration is documented in a <a class="reference external" href="https://specs.openstack.org/openstack/cinder-specs/specs/mitaka/online-schema-upgrades.html">cinder spec</a>.
See more examples in <a class="reference internal" href="online_schema_migration_examples.html"><em>Online SQL schema migration examples</em></a>.</p>
</div>
<div class="section" id="initial-sample-data">
<h2>Initial Sample Data<a class="headerlink" href="#initial-sample-data" title="Permalink to this headline">¶</a></h2>
<p>There is an included script which is helpful in setting up some initial sample
data for use with keystone:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ <span class="nv">OS_TOKEN</span><span class="o">=</span>ADMIN tools/sample_data.sh
</pre></div>
</div>
<p>Notice it requires a service token read from an environment variable for
authentication. The default value “ADMIN” is from the <code class="docutils literal"><span class="pre">admin_token</span></code>
option in the <code class="docutils literal"><span class="pre">[DEFAULT]</span></code> section in <code class="docutils literal"><span class="pre">etc/keystone.conf</span></code>.</p>
<p>Once run, you can see the sample data that has been created by using the
<a class="reference external" href="https://git.openstack.org/cgit/openstack/python-openstackclient">openstackclient</a> command-line interface:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ openstack --os-token ADMIN --os-url http://127.0.0.1:35357/v2.0/ user list
</pre></div>
</div>
<p>The <a class="reference external" href="https://git.openstack.org/cgit/openstack/python-openstackclient">openstackclient</a> can be installed using the following:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ pip install python-openstackclient
</pre></div>
</div>
</div>
<div class="section" id="filtering-responsibilities-between-controllers-and-drivers">
<h2>Filtering responsibilities between controllers and drivers<a class="headerlink" href="#filtering-responsibilities-between-controllers-and-drivers" title="Permalink to this headline">¶</a></h2>
<p>Keystone supports the specification of filtering on list queries as part of the
v3 identity API. By default these queries are satisfied in the controller
class when a controller calls the <code class="docutils literal"><span class="pre">wrap_collection</span></code> method at the end of a
<code class="docutils literal"><span class="pre">list_{entity}</span></code> method. However, to enable optimum performance, any driver
can implement some or all of the specified filters (for example, by adding
filtering to the generated SQL statements to generate the list).</p>
<p>The communication of the filter details between the controller level and its
drivers is handled by the passing of a reference to a Hints object,
which is a list of dicts describing the filters. A driver that satisfies a
filter must delete the filter from the Hints object so that when it is returned
to the controller level, it knows to only execute any unsatisfied
filters.</p>
<p>The contract for a driver for <code class="docutils literal"><span class="pre">list_{entity}</span></code> methods is therefore:</p>
<ul class="simple">
<li>It MUST return a list of entities of the specified type</li>
<li>It MAY either just return all such entities, or alternatively reduce the
list by filtering for one or more of the specified filters in the passed
Hints reference, and removing any such satisfied filters. An exception to
this is that for identity drivers that support domains, then they should
at least support filtering by domain_id.</li>
</ul>
</div>
<div class="section" id="entity-list-truncation-by-drivers">
<h2>Entity list truncation by drivers<a class="headerlink" href="#entity-list-truncation-by-drivers" title="Permalink to this headline">¶</a></h2>
<p>Keystone supports the ability for a deployment to restrict the number of
entries returned from <code class="docutils literal"><span class="pre">list_{entity}</span></code> methods, typically to prevent poorly
formed searches (e.g. without sufficient filters) from becoming a performance
issue.</p>
<p>These limits are set in the configuration file, either for a specific driver or
across all drivers. These limits are read at the Manager level and passed into
individual drivers as part of the Hints list object. A driver should try and
honor any such limit if possible, but if it is unable to do so then it may
ignore it (and the truncation of the returned list of entities will happen at
the controller level).</p>
</div>
<div class="section" id="identity-entity-id-management-between-controllers-and-drivers">
<h2>Identity entity ID management between controllers and drivers<a class="headerlink" href="#identity-entity-id-management-between-controllers-and-drivers" title="Permalink to this headline">¶</a></h2>
<p>Keystone supports the option of having domain-specific backends for the
identity driver (i.e. for user and group storage), allowing, for example,
a different LDAP server for each domain. To ensure that Keystone can determine
to which backend it should route an API call, starting with Juno, the
identity manager will, provided that domain-specific backends are enabled,
build on-the-fly a persistent mapping table between Keystone Public IDs that
are presented to the controller and the domain that holds the entity, along
with whatever local ID is understood by the driver. This hides, for instance,
the LDAP specifics of whatever ID is being used.</p>
<p>To ensure backward compatibility, the default configuration of either a
single SQL or LDAP backend for Identity will not use the mapping table,
meaning that public facing IDs will be the unchanged. If keeping these IDs
the same for the default LDAP backend is not required, then setting the
configuration variable <code class="docutils literal"><span class="pre">backward_compatible_ids</span></code> to <code class="docutils literal"><span class="pre">False</span></code> will enable
the mapping for the default LDAP driver, hence hiding the LDAP specifics of the
IDs being used.</p>
</div>
<div class="section" id="testing">
<h2>Testing<a class="headerlink" href="#testing" title="Permalink to this headline">¶</a></h2>
<div class="section" id="running-tests">
<h3>Running Tests<a class="headerlink" href="#running-tests" title="Permalink to this headline">¶</a></h3>
<p>Before running tests, you should have <code class="docutils literal"><span class="pre">tox</span></code> installed and available in your
environment (in addition to the other external dependencies in
<a class="reference internal" href="devref/development.environment.html"><em>Setting up a Keystone development environment</em></a>):</p>
<div class="highlight-bash"><div class="highlight"><pre>$ pip install tox
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">You may need to perform both the above operation and the next inside a
python virtualenv, or prefix the above command with <code class="docutils literal"><span class="pre">sudo</span></code>, depending on
your preference.</p>
</div>
<p>To execute the full suite of tests maintained within Keystone, simply run:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ tox
</pre></div>
</div>
<p>This iterates over multiple configuration variations, and uses external
projects to do light integration testing to verify the Identity API against
other projects.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The first time you run <code class="docutils literal"><span class="pre">tox</span></code>, it will take additional time to build
virtualenvs. You can later use the <code class="docutils literal"><span class="pre">-r</span></code> option with <code class="docutils literal"><span class="pre">tox</span></code> to rebuild
your virtualenv in a similar manner.</p>
</div>
<p>To run tests for one or more specific test environments (for example, the most
common configuration of Python 2.7 and PEP-8), list the environments with the
<code class="docutils literal"><span class="pre">-e</span></code> option, separated by spaces:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ tox -e py27,pep8
</pre></div>
</div>
<p>See <code class="docutils literal"><span class="pre">tox.ini</span></code> for the full list of available test environments.</p>
<div class="section" id="running-with-pdb">
<h4>Running with PDB<a class="headerlink" href="#running-with-pdb" title="Permalink to this headline">¶</a></h4>
<p>Using PDB breakpoints with tox and testr normally doesn’t work since the tests
just fail with a BdbQuit exception rather than stopping at the breakpoint.</p>
<p>To run with PDB breakpoints during testing, use the <code class="docutils literal"><span class="pre">debug</span></code> tox environment
rather than <code class="docutils literal"><span class="pre">py27</span></code>. Here’s an example, passing the name of a test since
you’ll normally only want to run the test that hits your breakpoint:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ tox -e debug keystone.tests.unit.test_auth.AuthWithToken.test_belongs_to
</pre></div>
</div>
<p>For reference, the <code class="docutils literal"><span class="pre">debug</span></code> tox environment implements the instructions
here: <a class="reference external" href="https://wiki.openstack.org/wiki/Testr#Debugging_.28pdb.29_Tests">https://wiki.openstack.org/wiki/Testr#Debugging_.28pdb.29_Tests</a></p>
</div>
<div class="section" id="disabling-stream-capture">
<h4>Disabling Stream Capture<a class="headerlink" href="#disabling-stream-capture" title="Permalink to this headline">¶</a></h4>
<p>The stdout, stderr and log messages generated during a test are captured and
in the event of a test failure those streams will be printed to the terminal
along with the traceback. The data is discarded for passing tests.</p>
<p>Each stream has an environment variable that can be used to force captured
data to be discarded even if the test fails: <cite>OS_STDOUT_CAPTURE</cite> for stdout,
<cite>OS_STDERR_CAPTURE</cite> for stderr and <cite>OS_LOG_CAPTURE</cite> for logging. If the value
of the environment variable is not one of (True, true, 1, yes) the stream will
be discarded. All three variables default to 1.</p>
<p>For example, to discard logging data during a test run:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ <span class="nv">OS_LOG_CAPTURE</span><span class="o">=</span><span class="m">0</span> tox -e py27
</pre></div>
</div>
</div>
</div>
<div class="section" id="test-structure">
<h3>Test Structure<a class="headerlink" href="#test-structure" title="Permalink to this headline">¶</a></h3>
<p>Not all of the tests in the keystone/tests/unit directory are strictly unit
tests. Keystone intentionally includes tests that run the service locally and
drives the entire configuration to achieve basic functional testing.</p>
<p>For the functional tests, an in-memory key-value store or in-memory SQLite
database is used to keep the tests fast.</p>
<p>Within the tests directory, the general structure of the backend tests is a
basic set of tests represented under a test class, and then subclasses of those
tests under other classes with different configurations to drive different
backends through the APIs.</p>
<p>For example, <code class="docutils literal"><span class="pre">test_backend.py</span></code> has a sequence of tests under the class
<code class="xref py py-class docutils literal"><span class="pre">IdentityTests</span></code> that will work with
the default drivers as configured in this project’s etc/ directory.
<code class="docutils literal"><span class="pre">test_backend_sql.py</span></code> subclasses those tests, changing the configuration by
overriding with configuration files stored in the <code class="docutils literal"><span class="pre">tests/unit/config_files</span></code>
directory aimed at enabling the SQL backend for the Identity module.</p>
<p><code class="xref py py-class docutils literal"><span class="pre">keystone.tests.unit.test_v2_keystoneclient.ClientDrivenTestCase</span></code>
uses the installed python-keystoneclient, verifying it against a temporarily
running local keystone instance to explicitly verify basic functional testing
across the API.</p>
</div>
<div class="section" id="testing-schema-migrations">
<h3>Testing Schema Migrations<a class="headerlink" href="#testing-schema-migrations" title="Permalink to this headline">¶</a></h3>
<p>The application of schema migrations can be tested using SQLAlchemy Migrate’s
built-in test runner, one migration at a time.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">This may leave your database in an inconsistent state; attempt this in
non-production environments only!</p>
</div>
<p>This is useful for testing the <em>next</em> migration in sequence (both forward &
backward) in a database under version control:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ python keystone/common/sql/migrate_repo/manage.py <span class="nb">test</span> <span class="se">\</span>
--url<span class="o">=</span>sqlite:///test.db <span class="se">\</span>
--repository<span class="o">=</span>keystone/common/sql/migrate_repo/
</pre></div>
</div>
<p>This command references to a SQLite database (test.db) to be used. Depending on
the migration, this command alone does not make assertions as to the integrity
of your data during migration.</p>
</div>
<div class="section" id="writing-tests">
<h3>Writing Tests<a class="headerlink" href="#writing-tests" title="Permalink to this headline">¶</a></h3>
<p>To add tests covering all drivers, update the base test class in
<code class="docutils literal"><span class="pre">test_backend.py</span></code>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>The structure of backend testing is in transition, migrating from having
all classes in a single file (test_backend.py) to one where there is a
directory structure to reduce the size of the test files. See:</p>
<blockquote class="last">
<div><ul class="simple">
<li><code class="xref py py-mod docutils literal"><span class="pre">keystone.tests.unit.backend.role</span></code></li>
<li><code class="xref py py-mod docutils literal"><span class="pre">keystone.tests.unit.backend.domain_config</span></code></li>
</ul>
</div></blockquote>
</div>
<p>To add new drivers, subclass the <code class="docutils literal"><span class="pre">test_backend.py</span></code> (look towards
<code class="docutils literal"><span class="pre">test_backend_sql.py</span></code> or <code class="docutils literal"><span class="pre">test_backend_kvs.py</span></code> for examples) and update the
configuration of the test class in <code class="docutils literal"><span class="pre">setUp()</span></code>.</p>
</div>
<div class="section" id="further-testing">
<h3>Further Testing<a class="headerlink" href="#further-testing" title="Permalink to this headline">¶</a></h3>
<p><a class="reference external" href="http://docs.openstack.org/developer/devstack/">devstack</a> is the <em>best</em> way to quickly deploy Keystone with the rest of the
OpenStack universe and should be critical step in your development workflow!</p>
<p>You may also be interested in either the
<a class="reference external" href="http://docs.openstack.org/infra/system-config">OpenStack Continuous Integration Infrastructure</a> or the
<a class="reference external" href="https://git.openstack.org/cgit/openstack/tempest">OpenStack Integration Testing Project</a>.</p>
</div>
<div class="section" id="ldap-tests">
<h3>LDAP Tests<a class="headerlink" href="#ldap-tests" title="Permalink to this headline">¶</a></h3>
<p>LDAP has a fake backend that performs rudimentary operations. If you
are building more significant LDAP functionality, you should test against
a live LDAP server. Devstack has an option to set up a directory server for
Keystone to use. Add ldap to the <code class="docutils literal"><span class="pre">ENABLED_SERVICES</span></code> environment variable,
and set environment variables <code class="docutils literal"><span class="pre">KEYSTONE_IDENTITY_BACKEND=ldap</span></code> and
<code class="docutils literal"><span class="pre">KEYSTONE_CLEAR_LDAP=yes</span></code> in your <code class="docutils literal"><span class="pre">localrc</span></code> file.</p>
<p>The unit tests can be run against a live server with
<code class="docutils literal"><span class="pre">keystone/tests/unit/test_ldap_livetest.py</span></code> and
<code class="docutils literal"><span class="pre">keystone/tests/unit/test_ldap_pool_livetest.py</span></code>. The default password is
<code class="docutils literal"><span class="pre">test</span></code> but if you have installed devstack with a different LDAP password,
modify the file <code class="docutils literal"><span class="pre">keystone/tests/unit/config_files/backend_liveldap.conf</span></code> and
<code class="docutils literal"><span class="pre">keystone/tests/unit/config_files/backend_pool_liveldap.conf</span></code> to reflect your
password.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">To run the live tests you need to set the environment variable
<code class="docutils literal"><span class="pre">ENABLE_LDAP_LIVE_TEST</span></code> to a non-negative value.</p>
</div>
</div>
<div class="section" id="work-in-progress-tests">
<h3>“Work in progress” Tests<a class="headerlink" href="#work-in-progress-tests" title="Permalink to this headline">¶</a></h3>
<p>Work in progress (WIP) tests are very useful in a variety of situations
including:</p>
<ul class="simple">
<li>During a TDD process they can be used to add tests to a review while
they are not yet working and will not cause test failures. (They should
be removed before the final merge.)</li>
<li>Often bug reports include small snippets of code to show broken
behaviors. Some of these can be converted into WIP tests that can later
be worked on by a developer. This allows us to take code that can be
used to catch bug regressions and commit it before any code is
written.</li>
</ul>
<p>The <code class="xref py py-func docutils literal"><span class="pre">keystone.tests.unit.utils.wip()</span></code> decorator can be used to mark a test
as WIP. A WIP test will always be run. If the test fails then a TestSkipped
exception is raised because we expect the test to fail. We do not pass
the test in this case so that it doesn’t count toward the number of
successfully run tests. If the test passes an AssertionError exception is
raised so that the developer knows they made the test pass. This is a
reminder to remove the decorator.</p>
<p>The <code class="xref py py-func docutils literal"><span class="pre">wip()</span></code> decorator requires that the author
provides a message. This message is important because it will tell other
developers why this test is marked as a work in progress. Reviewers will
require that these messages are descriptive and accurate.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <code class="xref py py-func docutils literal"><span class="pre">wip()</span></code> decorator is not a replacement for
skipping tests.</p>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@wip</span><span class="p">(</span><span class="s1">'waiting on bug #000000'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test</span><span class="p">():</span>
<span class="k">pass</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Another strategy is to not use the wip decorator and instead show how the
code currently incorrectly works. Which strategy is chosen is up to the
developer.</p>
</div>
</div>
</div>
<div class="section" id="generating-updated-sample-config-file">
<h2>Generating Updated Sample Config File<a class="headerlink" href="#generating-updated-sample-config-file" title="Permalink to this headline">¶</a></h2>
<p>Keystone’s sample configuration file <code class="docutils literal"><span class="pre">etc/keystone.conf.sample</span></code> is automatically
generated based upon all of the options available within Keystone. These options
are sourced from the many files around Keystone as well as some external libraries.</p>
<p>The sample configuration file is now kept up to date by an infra job that
generates the config file and if there are any changes will propose a review
as the OpenStack Proposal Bot. Developers should <em>NOT</em> generate the config file
and propose it as part of their patches since the proposal bot will do this for
you.</p>
<p>To generate a new sample configuration to see what it looks like, run:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ tox -egenconfig -r
</pre></div>
</div>
<p>The tox command will place an updated sample config in <code class="docutils literal"><span class="pre">etc/keystone.conf.sample</span></code>.</p>
<p>If there is a new external library (e.g. <code class="docutils literal"><span class="pre">oslo.messaging</span></code>) that utilizes the
<code class="docutils literal"><span class="pre">oslo.config</span></code> package for configuration, it can be added to the list of libraries
found in <code class="docutils literal"><span class="pre">config-generator/keystone.conf</span></code>.</p>
</div>
<div class="section" id="translated-responses">
<h2>Translated responses<a class="headerlink" href="#translated-responses" title="Permalink to this headline">¶</a></h2>
<p>The Keystone server can provide error responses translated into the language in
the <code class="docutils literal"><span class="pre">Accept-Language</span></code> header of the request. In order to test this in your
development environment, there’s a couple of things you need to do.</p>
<ol class="arabic simple">
<li>Build the message files. Run the following command in your keystone
directory:</li>
</ol>
<div class="highlight-bash"><div class="highlight"><pre>$ python setup.py compile_catalog
</pre></div>
</div>
<p>This will generate .mo files like keystone/locale/[lang]/LC_MESSAGES/[lang].mo</p>
<ol class="arabic simple" start="2">
<li>When running Keystone, set the <code class="docutils literal"><span class="pre">KEYSTONE_LOCALEDIR</span></code> environment variable
to the keystone/locale directory. For example:</li>
</ol>
<div class="highlight-bash"><div class="highlight"><pre>$ <span class="nv">KEYSTONE_LOCALEDIR</span><span class="o">=</span>/opt/stack/keystone/keystone/locale keystone-all
</pre></div>
</div>
<p>Now you can get a translated error response:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ curl -s -H <span class="s2">"Accept-Language: zh"</span> http://localhost:5000/notapath <span class="p">|</span> python -mjson.tool
<span class="o">{</span>
<span class="s2">"error"</span>: <span class="o">{</span>
<span class="s2">"code"</span>: 404,
<span class="s2">"message"</span>: <span class="s2">"\u627e\u4e0d\u5230\u8cc7\u6e90\u3002"</span>,
<span class="s2">"title"</span>: <span class="s2">"Not Found"</span>
<span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="caching-layer">
<h2>Caching Layer<a class="headerlink" href="#caching-layer" title="Permalink to this headline">¶</a></h2>
<p>The caching layer is designed to be applied to any <code class="docutils literal"><span class="pre">manager</span></code> object within Keystone
via the use of the <code class="docutils literal"><span class="pre">on_arguments</span></code> decorator provided in the <code class="docutils literal"><span class="pre">keystone.common.cache</span></code>
module. This decorator leverages <a class="reference external" href="http://dogpilecache.readthedocs.org/">dogpile.cache</a> caching system to provide a flexible
caching backend.</p>
<p>It is recommended that each of the managers have an independent toggle within the config
file to enable caching. The easiest method to utilize the toggle within the
configuration file is to define a <code class="docutils literal"><span class="pre">caching</span></code> boolean option within that manager’s
configuration section (e.g. <code class="docutils literal"><span class="pre">identity</span></code>). Once that option is defined you can
pass function to the <code class="docutils literal"><span class="pre">on_arguments</span></code> decorator with the named argument <code class="docutils literal"><span class="pre">should_cache_fn</span></code>.
In the <code class="docutils literal"><span class="pre">keystone.common.cache</span></code> module, there is a function called <code class="docutils literal"><span class="pre">should_cache_fn</span></code>,
which will provide a reference, to a function, that will consult the global cache
<code class="docutils literal"><span class="pre">enabled</span></code> option as well as the specific manager’s caching enable toggle.</p>
<blockquote>
<div><div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If a section-specific boolean option is not defined in the config section specified when
calling <code class="docutils literal"><span class="pre">should_cache_fn</span></code>, the returned function reference will default to enabling
caching for that <code class="docutils literal"><span class="pre">manager</span></code>.</p>
</div>
</div></blockquote>
<p>Example use of cache and <code class="docutils literal"><span class="pre">should_cache_fn</span></code> (in this example, <code class="docutils literal"><span class="pre">token</span></code> is the manager):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">keystone.common</span> <span class="kn">import</span> <span class="n">cache</span>
<span class="n">SHOULD_CACHE</span> <span class="o">=</span> <span class="n">cache</span><span class="o">.</span><span class="n">should_cache_fn</span><span class="p">(</span><span class="s1">'token'</span><span class="p">)</span>
<span class="nd">@cache.on_arguments</span><span class="p">(</span><span class="n">should_cache_fn</span><span class="o">=</span><span class="n">SHOULD_CACHE</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">cacheable_function</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">arg3</span><span class="p">):</span>
<span class="o">...</span>
<span class="k">return</span> <span class="n">some_value</span>
</pre></div>
</div>
<p>With the above example, each call to the <code class="docutils literal"><span class="pre">cacheable_function</span></code> would check to see if
the arguments passed to it matched a currently valid cached item. If the return value
was cached, the caching layer would return the cached value; if the return value was
not cached, the caching layer would call the function, pass the value to the <code class="docutils literal"><span class="pre">SHOULD_CACHE</span></code>
function reference, which would then determine if caching was globally enabled and enabled
for the <code class="docutils literal"><span class="pre">token</span></code> manager. If either caching toggle is disabled, the value is returned but
not cached.</p>
<p>It is recommended that each of the managers have an independent configurable time-to-live (TTL).
If a configurable TTL has been defined for the manager configuration section, it is possible to
pass it to the <code class="docutils literal"><span class="pre">cache.on_arguments</span></code> decorator with the named-argument <code class="docutils literal"><span class="pre">expiration_time</span></code>. For
consistency, it is recommended that this option be called <code class="docutils literal"><span class="pre">cache_time</span></code> and default to <code class="docutils literal"><span class="pre">None</span></code>.
If the <code class="docutils literal"><span class="pre">expiration_time</span></code> argument passed to the decorator is set to <code class="docutils literal"><span class="pre">None</span></code>, the expiration
time will be set to the global default (<code class="docutils literal"><span class="pre">expiration_time</span></code> option in the <code class="docutils literal"><span class="pre">[cache]</span></code>
configuration section.</p>
<p>Example of using a section specific <code class="docutils literal"><span class="pre">cache_time</span></code> (in this example, <code class="docutils literal"><span class="pre">identity</span></code> is the manager):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">keystone.common</span> <span class="kn">import</span> <span class="n">cache</span>
<span class="n">SHOULD_CACHE</span> <span class="o">=</span> <span class="n">cache</span><span class="o">.</span><span class="n">should_cache_fn</span><span class="p">(</span><span class="s1">'identity'</span><span class="p">)</span>
<span class="nd">@cache.on_arguments</span><span class="p">(</span><span class="n">should_cache_fn</span><span class="o">=</span><span class="n">SHOULD_CACHE</span><span class="p">,</span>
<span class="n">expiration_time</span><span class="o">=</span><span class="n">CONF</span><span class="o">.</span><span class="n">identity</span><span class="o">.</span><span class="n">cache_time</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">cachable_function</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">arg3</span><span class="p">):</span>
<span class="o">...</span>
<span class="k">return</span> <span class="n">some_value</span>
</pre></div>
</div>
<p>For cache invalidation, the <code class="docutils literal"><span class="pre">on_arguments</span></code> decorator will add an <code class="docutils literal"><span class="pre">invalidate</span></code> method
(attribute) to your decorated function. To invalidate the cache, you pass the same arguments
to the <code class="docutils literal"><span class="pre">invalidate</span></code> method as you would the normal function.</p>
<p>Example (using the above cacheable_function):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">invalidate_cache</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">arg3</span><span class="p">):</span>
<span class="n">cacheable_function</span><span class="o">.</span><span class="n">invalidate</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">arg3</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">The <code class="docutils literal"><span class="pre">on_arguments</span></code> decorator does not accept keyword-arguments/named arguments. An
exception will be raised if keyword arguments are passed to a caching-decorated function.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">In all cases methods work the same as functions except if you are attempting to invalidate
the cache on a decorated bound-method, you need to pass <code class="docutils literal"><span class="pre">self</span></code> to the <code class="docutils literal"><span class="pre">invalidate</span></code>
method as the first argument before the arguments.</p>
</div>
</div>
<div class="section" id="dogpile-cache-based-key-value-store-kvs">
<h2>dogpile.cache based Key-Value-Store (KVS)<a class="headerlink" href="#dogpile-cache-based-key-value-store-kvs" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal"><span class="pre">dogpile.cache</span></code> based KVS system has been designed to allow for flexible stores for the
backend of the KVS system. The implementation allows for the use of any normal <code class="docutils literal"><span class="pre">dogpile.cache</span></code>
cache backends to be used as a store. All interfacing to the KVS system happens via the
<code class="docutils literal"><span class="pre">KeyValueStore</span></code> object located at <code class="docutils literal"><span class="pre">keystone.common.kvs.KeyValueStore</span></code>.</p>
<p>To utilize the KVS system an instantiation of the <code class="docutils literal"><span class="pre">KeyValueStore</span></code> class is needed. To acquire
a KeyValueStore instantiation use the <code class="docutils literal"><span class="pre">keystone.common.kvs.get_key_value_store</span></code> factory
function. This factory will either create a new <code class="docutils literal"><span class="pre">KeyValueStore</span></code> object or retrieve the
already instantiated <code class="docutils literal"><span class="pre">KeyValueStore</span></code> object by the name passed as an argument. The object must
be configured before use. The KVS object will only be retrievable with the
<code class="docutils literal"><span class="pre">get_key_value_store</span></code> function while there is an active reference outside of the registry.
Once all references have been removed the object is gone (the registry uses a <code class="docutils literal"><span class="pre">weakref</span></code> to
match the object to the name).</p>
<p>Example Instantiation and Configuration:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">kvs_store</span> <span class="o">=</span> <span class="n">kvs</span><span class="o">.</span><span class="n">get_key_value_store</span><span class="p">(</span><span class="s1">'TestKVSRegion'</span><span class="p">)</span>
<span class="n">kvs_store</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="s1">'openstack.kvs.Memory'</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>Any keyword arguments passed to the configure method that are not defined as part of the
KeyValueStore object configuration are passed to the backend for further configuration (e.g.
memcached servers, lock_timeout, etc).</p>
<p>The memcached backend uses the Keystone manager mechanism to support the use of any of the
provided memcached backends (<code class="docutils literal"><span class="pre">bmemcached</span></code>, <code class="docutils literal"><span class="pre">pylibmc</span></code>, and basic <code class="docutils literal"><span class="pre">memcached</span></code>).
By default the <code class="docutils literal"><span class="pre">memcached</span></code> backend is used. Currently the Memcache URLs come from the
<code class="docutils literal"><span class="pre">servers</span></code> option in the <code class="docutils literal"><span class="pre">[memcache]</span></code> configuration section of the Keystone config.</p>
<p>The following is an example showing how to configure the KVS system to use a
KeyValueStore object named “TestKVSRegion” and a specific Memcached driver:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">kvs_store</span> <span class="o">=</span> <span class="n">kvs</span><span class="o">.</span><span class="n">get_key_value_store</span><span class="p">(</span><span class="s1">'TestKVSRegion'</span><span class="p">)</span>
<span class="n">kvs_store</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="s1">'openstack.kvs.Memcached'</span><span class="p">,</span> <span class="n">memcached_backend</span><span class="o">=</span><span class="s1">'Memcached'</span><span class="p">)</span>
</pre></div>
</div>
<p>The memcached backend supports a mechanism to supply an explicit TTL (in seconds) to all keys
set via the KVS object. This is accomplished by passing the argument <code class="docutils literal"><span class="pre">memcached_expire_time</span></code>
as a keyword argument to the <code class="docutils literal"><span class="pre">configure</span></code> method. Passing the <code class="docutils literal"><span class="pre">memcache_expire_time</span></code> argument
will cause the <code class="docutils literal"><span class="pre">time</span></code> argument to be added to all <code class="docutils literal"><span class="pre">set</span></code> and <code class="docutils literal"><span class="pre">set_multi</span></code> calls performed by
the memcached client. <code class="docutils literal"><span class="pre">memcached_expire_time</span></code> is an argument exclusive to the memcached dogpile
backend, and will be ignored if passed to another backend:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">kvs_store</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="s1">'openstack.kvs.Memcached'</span><span class="p">,</span> <span class="n">memcached_backend</span><span class="o">=</span><span class="s1">'Memcached'</span><span class="p">,</span>
<span class="n">memcached_expire_time</span><span class="o">=</span><span class="mi">86400</span><span class="p">)</span>
</pre></div>
</div>
<p>If an explicit TTL is configured via the <code class="docutils literal"><span class="pre">memcached_expire_time</span></code> argument, it is possible to
exempt specific keys from receiving the TTL by passing the argument <code class="docutils literal"><span class="pre">no_expiry_keys</span></code> (list)
as a keyword argument to the <code class="docutils literal"><span class="pre">configure</span></code> method. <code class="docutils literal"><span class="pre">no_expiry_keys</span></code> should be supported by
all OpenStack-specific dogpile backends (memcached) that have the ability to set an explicit TTL:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">kvs_store</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="s1">'openstack.kvs.Memcached'</span><span class="p">,</span> <span class="n">memcached_backend</span><span class="o">=</span><span class="s1">'Memcached'</span><span class="p">,</span>
<span class="n">memcached_expire_time</span><span class="o">=</span><span class="mi">86400</span><span class="p">,</span> <span class="n">no_expiry_keys</span><span class="o">=</span><span class="p">[</span><span class="s1">'key'</span><span class="p">,</span> <span class="s1">'second_key'</span><span class="p">,</span> <span class="o">...</span><span class="p">])</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>For the non-expiring keys functionality to work, the backend must support the ability for
the region to set the key_mangler on it and have the attribute <code class="docutils literal"><span class="pre">raw_no_expiry_keys</span></code>.
In most cases, support for setting the key_mangler on the backend is handled by allowing
the region object to set the <code class="docutils literal"><span class="pre">key_mangler</span></code> attribute on the backend.</p>
<p>The <code class="docutils literal"><span class="pre">raw_no_expiry_keys</span></code> attribute is expected to be used to hold the values of the
keyword argument <code class="docutils literal"><span class="pre">no_expiry_keys</span></code> prior to hashing. It is the responsibility of the
backend to use these raw values to determine if a key should be exempt from expiring
and not set the TTL on the non-expiring keys when the <code class="docutils literal"><span class="pre">set</span></code> or <code class="docutils literal"><span class="pre">set_multi</span></code> methods are
called.</p>
<p class="last">Typically the key will be hashed by the region using its key_mangler method
before being passed to the backend to set the value in the KeyValueStore. This
means that in most cases, the backend will need to either pre-compute the hashed versions
of the keys (when the key_mangler is set) and store a cached copy, or hash each item in
the <code class="docutils literal"><span class="pre">raw_no_expiry_keys</span></code> attribute on each call to <code class="docutils literal"><span class="pre">.set()</span></code> and <code class="docutils literal"><span class="pre">.set_multi()</span></code>. The
<code class="docutils literal"><span class="pre">memcached</span></code> backend handles this hashing and caching of the keys by utilizing an
<code class="docutils literal"><span class="pre">@property</span></code> method for the <code class="docutils literal"><span class="pre">.key_mangler</span></code> attribute on the backend and utilizing the
associated <code class="docutils literal"><span class="pre">.settr()</span></code> method to front-load the hashing work at attribute set time.</p>
</div>
<p>Once a KVS object has been instantiated the method of interacting is the same as most memcache
implementations:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">kvs_store</span> <span class="o">=</span> <span class="n">kvs</span><span class="o">.</span><span class="n">get_key_value_store</span><span class="p">(</span><span class="s1">'TestKVSRegion'</span><span class="p">)</span>
<span class="n">kvs_store</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="c1"># Set a Value</span>
<span class="n">kvs_store</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="o"><</span><span class="n">Key</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">Value</span><span class="o">></span><span class="p">)</span>
<span class="c1"># Retrieve a value:</span>
<span class="n">retrieved_value</span> <span class="o">=</span> <span class="n">kvs_store</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="o"><</span><span class="n">key</span><span class="o">></span><span class="p">)</span>
<span class="c1"># Delete a key/value pair:</span>
<span class="n">kvs_store</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="o"><</span><span class="n">key</span><span class="o">></span><span class="p">)</span>
<span class="c1"># multi-get:</span>
<span class="n">kvs_store</span><span class="o">.</span><span class="n">get_multi</span><span class="p">([</span><span class="o"><</span><span class="n">key</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">key</span><span class="o">></span><span class="p">,</span> <span class="o">...</span><span class="p">])</span>
<span class="c1"># multi-set:</span>
<span class="n">kvs_store</span><span class="o">.</span><span class="n">set_multi</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="o"><</span><span class="n">key</span><span class="o">>=<</span><span class="n">value</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">key</span><span class="o">>=<</span><span class="n">value</span><span class="o">></span><span class="p">,</span> <span class="o">...</span><span class="p">))</span>
<span class="c1"># multi-delete</span>
<span class="n">kvs_store</span><span class="o">.</span><span class="n">delete_multi</span><span class="p">([</span><span class="o"><</span><span class="n">key</span><span class="o">></span><span class="p">,</span> <span class="o"><</span><span class="n">key</span><span class="o">></span><span class="p">,</span> <span class="o">...</span><span class="p">])</span>
</pre></div>
</div>
<p>There is a global configuration option to be aware of (that can be set in the <code class="docutils literal"><span class="pre">[kvs]</span></code> section of
the Keystone configuration file): <code class="docutils literal"><span class="pre">enable_key_mangler</span></code> can be set top false, disabling the use of
key_manglers (modification of the key when saving to the backend to help prevent
collisions or exceeding key size limits with memcached).</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <code class="docutils literal"><span class="pre">enable_key_mangler</span></code> option in the <code class="docutils literal"><span class="pre">[kvs]</span></code> section of the Keystone configuration file
is not the same option (and does not affect the cache-layer key manglers) from the option in the
<code class="docutils literal"><span class="pre">[cache]</span></code> section of the configuration file. Similarly the <code class="docutils literal"><span class="pre">[cache]</span></code> section options
relating to key manglers has no bearing on the <code class="docutils literal"><span class="pre">[kvs]</span></code> objects.</p>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Setting the <code class="docutils literal"><span class="pre">enable_key_mangler</span></code> option to False can have detrimental effects on the
KeyValueStore backend. It is recommended that this value is not set to False except for
debugging issues with the <code class="docutils literal"><span class="pre">dogpile.cache</span></code> backend itself.</p>
</div>
<p>Any backends that are to be used with the <code class="docutils literal"><span class="pre">KeyValueStore</span></code> system need to be registered with
dogpile. For in-tree/provided backends, the registration should occur in
<code class="docutils literal"><span class="pre">keystone/common/kvs/__init__.py</span></code>. For backends that are developed out of tree, the location
should be added to the <code class="docutils literal"><span class="pre">backends</span></code> option in the <code class="docutils literal"><span class="pre">[kvs]</span></code> section of the Keystone configuration:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span><span class="n">kvs</span><span class="p">]</span>
<span class="n">backends</span> <span class="o">=</span> <span class="n">backend_module1</span><span class="o">.</span><span class="n">backend_class1</span><span class="p">,</span><span class="n">backend_module2</span><span class="o">.</span><span class="n">backend_class2</span>
</pre></div>
</div>
<p>All registered backends will receive the “short name” of “openstack.kvs.<class name>” for use in the
<code class="docutils literal"><span class="pre">configure</span></code> method on the <code class="docutils literal"><span class="pre">KeyValueStore</span></code> object. The <code class="docutils literal"><span class="pre"><class</span> <span class="pre">name></span></code> of a backend must be
globally unique.</p>
</div>
<div class="section" id="dogpile-cache-based-mongodb-nosql-backend">
<h2>dogpile.cache based MongoDB (NoSQL) backend<a class="headerlink" href="#dogpile-cache-based-mongodb-nosql-backend" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal"><span class="pre">dogpile.cache</span></code> based MongoDB backend implementation allows for various MongoDB
configurations, e.g., standalone, a replica set, sharded replicas, with or without SSL,
use of TTL type collections, etc.</p>
<p>Example of typical configuration for MongoDB backend:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">dogpile.cache</span> <span class="kn">import</span> <span class="n">region</span>
<span class="n">arguments</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">'db_hosts'</span><span class="p">:</span> <span class="s1">'localhost:27017'</span><span class="p">,</span>
<span class="s1">'db_name'</span><span class="p">:</span> <span class="s1">'ks_cache'</span><span class="p">,</span>
<span class="s1">'cache_collection'</span><span class="p">:</span> <span class="s1">'cache'</span><span class="p">,</span>
<span class="s1">'username'</span><span class="p">:</span> <span class="s1">'test_user'</span><span class="p">,</span>
<span class="s1">'password'</span><span class="p">:</span> <span class="s1">'test_password'</span><span class="p">,</span>
<span class="c1"># optional arguments</span>
<span class="s1">'son_manipulator'</span><span class="p">:</span> <span class="s1">'my_son_manipulator_impl'</span>
<span class="p">}</span>
<span class="n">region</span><span class="o">.</span><span class="n">make_region</span><span class="p">()</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="s1">'keystone.cache.mongo'</span><span class="p">,</span>
<span class="n">arguments</span><span class="o">=</span><span class="n">arguments</span><span class="p">)</span>
</pre></div>
</div>
<p>The optional <cite>son_manipulator</cite> is used to manipulate custom data type while its saved in
or retrieved from MongoDB. If the dogpile cached values contain built-in data types and no
custom classes, then the provided implementation class is sufficient. For further details, refer
<a class="reference external" href="http://api.mongodb.org/python/current/examples/custom_type.html#automatic-encoding-and-decoding">http://api.mongodb.org/python/current/examples/custom_type.html#automatic-encoding-and-decoding</a></p>
<p>Similar to other backends, this backend can be added via Keystone configuration in
<code class="docutils literal"><span class="pre">keystone.conf</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre>[cache]
# Global cache functionality toggle.
enabled = True
# Referring to specific cache backend
backend = keystone.cache.mongo
# Backend specific configuration arguments
backend_argument = db_hosts:localhost:27017
backend_argument = db_name:ks_cache
backend_argument = cache_collection:cache
backend_argument = username:test_user
backend_argument = password:test_password
</pre></div>
</div>
<p>This backend is registered in <code class="docutils literal"><span class="pre">keystone.common.cache.core</span></code> module. So, its usage
is similar to other dogpile caching backends as it implements the same dogpile APIs.</p>
</div>
<div class="section" id="building-the-documentation">
<h2>Building the Documentation<a class="headerlink" href="#building-the-documentation" title="Permalink to this headline">¶</a></h2>
<p>The documentation is generated with Sphinx using the tox command. To create HTML docs and man pages:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ tox -e docs
</pre></div>
</div>
<p>The results are in the doc/build/html and doc/build/man directories respectively.</p>
</div>
<div class="section" id="release-notes">
<h2>Release Notes<a class="headerlink" href="#release-notes" title="Permalink to this headline">¶</a></h2>
<p>The release notes for a patch should be included in the patch. If not, the
release notes should be in a follow-on review.</p>
<p>If the following applies to the patch, a release note is required:</p>
<ul class="simple">
<li>The deployer needs to take an action when upgrading</li>
<li>The backend driver interface changes</li>
<li>A new feature is implemented</li>
<li>Function was removed (hopefully it was deprecated)</li>
<li>Current behavior is changed</li>
<li>A new config option is added that the deployer should consider changing from
the default</li>
<li>A security bug is fixed</li>
</ul>
<p>A release note is suggested if a long-standing or important bug is fixed.
Otherwise, a release note is not required.</p>
<p>Keystone uses <a class="reference external" href="http://docs.openstack.org/developer/reno/usage.html">reno</a> to
generate release notes. Please read the docs for details. In summary, use</p>
<div class="highlight-bash"><div class="highlight"><pre>$ tox -e venv -- reno new <bug-,bp-,whatever>
</pre></div>
</div>
<p>Then edit the sample file that was created and push it with your change.</p>
<p>To see the results:</p>
<div class="highlight-bash"><div class="highlight"><pre>$ git commit <span class="c1"># Commit the change because reno scans git log.</span>
$ tox -e releasenotes
</pre></div>
</div>
<p>Then look at the generated release notes files in releasenotes/build/html in
your favorite browser.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Developing with Keystone</a><ul>
<li><a class="reference internal" href="#setup">Setup</a></li>
<li><a class="reference internal" href="#configuring-keystone">Configuring Keystone</a></li>
<li><a class="reference internal" href="#running-keystone">Running Keystone</a></li>
<li><a class="reference internal" href="#interacting-with-keystone">Interacting with Keystone</a></li>
<li><a class="reference internal" href="#database-schema-migrations">Database Schema Migrations</a></li>
<li><a class="reference internal" href="#initial-sample-data">Initial Sample Data</a></li>
<li><a class="reference internal" href="#filtering-responsibilities-between-controllers-and-drivers">Filtering responsibilities between controllers and drivers</a></li>
<li><a class="reference internal" href="#entity-list-truncation-by-drivers">Entity list truncation by drivers</a></li>
<li><a class="reference internal" href="#identity-entity-id-management-between-controllers-and-drivers">Identity entity ID management between controllers and drivers</a></li>
<li><a class="reference internal" href="#testing">Testing</a><ul>
<li><a class="reference internal" href="#running-tests">Running Tests</a><ul>
<li><a class="reference internal" href="#running-with-pdb">Running with PDB</a></li>
<li><a class="reference internal" href="#disabling-stream-capture">Disabling Stream Capture</a></li>
</ul>
</li>
<li><a class="reference internal" href="#test-structure">Test Structure</a></li>
<li><a class="reference internal" href="#testing-schema-migrations">Testing Schema Migrations</a></li>
<li><a class="reference internal" href="#writing-tests">Writing Tests</a></li>
<li><a class="reference internal" href="#further-testing">Further Testing</a></li>
<li><a class="reference internal" href="#ldap-tests">LDAP Tests</a></li>
<li><a class="reference internal" href="#work-in-progress-tests">“Work in progress” Tests</a></li>
</ul>
</li>
<li><a class="reference internal" href="#generating-updated-sample-config-file">Generating Updated Sample Config File</a></li>
<li><a class="reference internal" href="#translated-responses">Translated responses</a></li>
<li><a class="reference internal" href="#caching-layer">Caching Layer</a></li>
<li><a class="reference internal" href="#dogpile-cache-based-key-value-store-kvs">dogpile.cache based Key-Value-Store (KVS)</a></li>
<li><a class="reference internal" href="#dogpile-cache-based-mongodb-nosql-backend">dogpile.cache based MongoDB (NoSQL) backend</a></li>
<li><a class="reference internal" href="#building-the-documentation">Building the Documentation</a></li>
<li><a class="reference internal" href="#release-notes">Release Notes</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="community.html"
title="previous chapter">Getting Involved</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="developing_drivers.html"
title="next chapter">Developing Keystone Drivers</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/developing.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" size="18" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="developing_drivers.html" title="Developing Keystone Drivers"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="community.html" title="Getting Involved"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">keystone documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2012, OpenStack, LLC.
Last updated on Apr 07, 2016.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.6.
</div>
<script type="text/javascript">
try {
//Tracking docs.openstack.org/developer/<projectname> only
//The URL is built from the project variable in conf.py
var pageTracker = _gat._getTracker("UA-17511903-1");
pageTracker._setCookiePath("/developer/keystone");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>
|