Sophie

Sophie

distrib > Fedora > 17 > i386 > by-pkgid > 675c8c8167236dfcf8d66da674f931e8 > files > 233

erlang-doc-R15B-03.3.fc17.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns:fn="http://www.w3.org/2005/02/xpath-functions">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../../../doc/otp_doc.css" type="text/css">
<title>Erlang -- External Configuration Data</title>
</head>
<body bgcolor="white" text="#000000" link="#0000ff" vlink="#ff00ff" alink="#ff0000"><div id="container">
<script id="js" type="text/javascript" language="JavaScript" src="../../../../doc/js/flipmenu/flipmenu.js"></script><script id="js2" type="text/javascript" src="../../../../doc/js/erlresolvelinks.js"></script><script language="JavaScript" type="text/javascript">
            <!--
              function getWinHeight() {
                var myHeight = 0;
                if( typeof( window.innerHeight ) == 'number' ) {
                  //Non-IE
                  myHeight = window.innerHeight;
                } else if( document.documentElement && ( document.documentElement.clientWidth ||
                                                         document.documentElement.clientHeight ) ) {
                  //IE 6+ in 'standards compliant mode'
                  myHeight = document.documentElement.clientHeight;
                } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
                  //IE 4 compatible
                  myHeight = document.body.clientHeight;
                }
                return myHeight;
              }

              function setscrollpos() {
                var objf=document.getElementById('loadscrollpos');
                 document.getElementById("leftnav").scrollTop = objf.offsetTop - getWinHeight()/2;
              }

              function addEvent(obj, evType, fn){
                if (obj.addEventListener){
                obj.addEventListener(evType, fn, true);
                return true;
              } else if (obj.attachEvent){
                var r = obj.attachEvent("on"+evType, fn);
                return r;
              } else {
                return false;
              }
             }

             addEvent(window, 'load', setscrollpos);

             //--></script><div id="leftnav"><div class="innertube">
<img alt="Erlang logo" src="../../../../doc/erlang-logo.png"><br><small><a href="users_guide.html">User's Guide</a><br><a href="index.html">Reference Manual</a><br><a href="release_notes.html">Release Notes</a><br><a href="../pdf/common_test-1.6.3.pdf">PDF</a><br><a href="../../../../doc/index.html">Top</a></small><p><strong>Common Test</strong><br><strong>User's Guide</strong><br><small>Version 1.6.3</small></p>
<br><a href="javascript:openAllFlips()">Expand All</a><br><a href="javascript:closeAllFlips()">Contract All</a><p><small><strong>Chapters</strong></small></p>
<ul class="flipMenu" imagepath="../../../../doc/js/flipmenu">
<li id="no" title="Common Test Basics" expanded="false">Common Test Basics<ul>
<li><a href="basics_chapter.html">
              Top of chapter
            </a></li>
<li title="Introduction"><a href="basics_chapter.html#id66143">Introduction</a></li>
<li title="Test Suite Organisation"><a href="basics_chapter.html#id61412">Test Suite Organisation</a></li>
<li title="Support Libraries"><a href="basics_chapter.html#id67203">Support Libraries</a></li>
<li title="Suites and Test Cases"><a href="basics_chapter.html#id61632">Suites and Test Cases</a></li>
<li title="External Interfaces"><a href="basics_chapter.html#id63930">External Interfaces</a></li>
</ul>
</li>
<li id="no" title="Getting Started" expanded="false">Getting Started<ul>
<li><a href="getting_started_chapter.html">
              Top of chapter
            </a></li>
<li title="Are you new around here?"><a href="getting_started_chapter.html#id61356">Are you new around here?</a></li>
<li title="Test case execution"><a href="getting_started_chapter.html#id63050">Test case execution</a></li>
<li title="A simple test suite"><a href="getting_started_chapter.html#id63909">A simple test suite</a></li>
<li title="A test suite with configuration functions"><a href="getting_started_chapter.html#id68094">A test suite with configuration functions</a></li>
<li title="What happens next?"><a href="getting_started_chapter.html#id62545">What happens next?</a></li>
</ul>
</li>
<li id="no" title="Installation" expanded="false">Installation<ul>
<li><a href="install_chapter.html">
              Top of chapter
            </a></li>
<li title="General information"><a href="install_chapter.html#id66699">General information</a></li>
</ul>
</li>
<li id="no" title="Writing Test Suites" expanded="false">Writing Test Suites<ul>
<li><a href="write_test_chapter.html">
              Top of chapter
            </a></li>
<li title="Support for test suite authors"><a href="write_test_chapter.html#id71900">Support for test suite authors</a></li>
<li title="Test suites"><a href="write_test_chapter.html#id70761">Test suites</a></li>
<li title="Init and end per suite"><a href="write_test_chapter.html#id70806">Init and end per suite</a></li>
<li title="Init and end per test case"><a href="write_test_chapter.html#id70917">Init and end per test case</a></li>
<li title="Test cases"><a href="write_test_chapter.html#id71162">Test cases</a></li>
<li title="Test case info function"><a href="write_test_chapter.html#id71322">Test case info function</a></li>
<li title="Test suite info function"><a href="write_test_chapter.html#id71521">Test suite info function</a></li>
<li title="Test case groups"><a href="write_test_chapter.html#id71611">Test case groups</a></li>
<li title="The parallel property and nested groups"><a href="write_test_chapter.html#id72823">The parallel property and nested groups</a></li>
<li title="Parallel test cases and IO"><a href="write_test_chapter.html#id72860">Parallel test cases and IO</a></li>
<li title="Repeated groups"><a href="write_test_chapter.html#id72908">Repeated groups</a></li>
<li title="Shuffled test case order"><a href="write_test_chapter.html#id73046">Shuffled test case order</a></li>
<li title="Group info function"><a href="write_test_chapter.html#id73105">Group info function</a></li>
<li title="Info functions for init- and end-configuration"><a href="write_test_chapter.html#id73139">Info functions for init- and end-configuration</a></li>
<li title="Data and Private Directories"><a href="write_test_chapter.html#id73202">Data and Private Directories</a></li>
<li title="Execution environment"><a href="write_test_chapter.html#id73327">Execution environment</a></li>
<li title="Timetrap timeouts"><a href="write_test_chapter.html#id73364">Timetrap timeouts</a></li>
<li title="Logging - categories and verbosity levels"><a href="write_test_chapter.html#id73549">Logging - categories and verbosity levels</a></li>
<li title="Illegal dependencies"><a href="write_test_chapter.html#id73756">Illegal dependencies</a></li>
</ul>
</li>
<li id="no" title="Test Structure" expanded="false">Test Structure<ul>
<li><a href="test_structure_chapter.html">
              Top of chapter
            </a></li>
<li title="Test structure"><a href="test_structure_chapter.html#id73930">Test structure</a></li>
<li title="Skipping test cases"><a href="test_structure_chapter.html#id73944">Skipping test cases</a></li>
<li title="Definition of terms"><a href="test_structure_chapter.html#id74020">Definition of terms</a></li>
</ul>
</li>
<li id="no" title="Examples and Templates" expanded="false">Examples and Templates<ul>
<li><a href="example_chapter.html">
              Top of chapter
            </a></li>
<li title="Test suite example"><a href="example_chapter.html#id74246">Test suite example</a></li>
<li title="Test suite templates"><a href="example_chapter.html#id74298">Test suite templates</a></li>
</ul>
</li>
<li id="no" title="Running Tests" expanded="false">Running Tests<ul>
<li><a href="run_test_chapter.html">
              Top of chapter
            </a></li>
<li title="Using the Common Test Framework"><a href="run_test_chapter.html#id74488">Using the Common Test Framework</a></li>
<li title="Automatic compilation of test suites and help modules"><a href="run_test_chapter.html#id74528">Automatic compilation of test suites and help modules</a></li>
<li title="Running tests from the OS command line"><a href="run_test_chapter.html#id74645">Running tests from the OS command line</a></li>
<li title="Running tests from the Erlang shell or from an Erlang program"><a href="run_test_chapter.html#id75142">Running tests from the Erlang shell or from an Erlang program</a></li>
<li title="Test case group execution"><a href="run_test_chapter.html#id75304">Test case group execution</a></li>
<li title="Running the interactive shell mode"><a href="run_test_chapter.html#id75821">Running the interactive shell mode</a></li>
<li title="Step by step execution of test cases with the Erlang Debugger"><a href="run_test_chapter.html#id76003">Step by step execution of test cases with the Erlang Debugger</a></li>
<li title="Test Specifications"><a href="run_test_chapter.html#id76102">Test Specifications</a></li>
<li title="Running tests from the Web based GUI"><a href="run_test_chapter.html#id76643">Running tests from the Web based GUI</a></li>
<li title="Log files"><a href="run_test_chapter.html#id76735">Log files</a></li>
<li title="HTML Style Sheets"><a href="run_test_chapter.html#id77053">HTML Style Sheets</a></li>
<li title="Repeating tests"><a href="run_test_chapter.html#id77201">Repeating tests</a></li>
<li title="Silent Connections"><a href="run_test_chapter.html#id77412">Silent Connections</a></li>
</ul>
</li>
<li id="loadscrollpos" title="External Configuration Data" expanded="true">External Configuration Data<ul>
<li><a href="config_file_chapter.html">
              Top of chapter
            </a></li>
<li title="General"><a href="config_file_chapter.html#id77631">General</a></li>
<li title="Syntax"><a href="config_file_chapter.html#id77671">Syntax</a></li>
<li title="Requiring and reading configuration data"><a href="config_file_chapter.html#id77695">Requiring and reading configuration data</a></li>
<li title="Using configuration variables defined in multiple files"><a href="config_file_chapter.html#id77839">Using configuration variables defined in multiple files</a></li>
<li title="Encrypted configuration files"><a href="config_file_chapter.html#id77870">Encrypted configuration files</a></li>
<li title="Opening connections by using configuration data"><a href="config_file_chapter.html#id77934">Opening connections by using configuration data</a></li>
<li title="User specific configuration data formats"><a href="config_file_chapter.html#id78002">User specific configuration data formats</a></li>
<li title="Examples of configuration data handling"><a href="config_file_chapter.html#id78216">Examples of configuration data handling</a></li>
<li title="Example of user specific configuration handler"><a href="config_file_chapter.html#id78268">Example of user specific configuration handler</a></li>
</ul>
</li>
<li id="no" title="Code Coverage Analysis" expanded="false">Code Coverage Analysis<ul>
<li><a href="cover_chapter.html">
              Top of chapter
            </a></li>
<li title="General"><a href="cover_chapter.html#id78384">General</a></li>
<li title="Usage"><a href="cover_chapter.html#id78404">Usage</a></li>
<li title="The cover specification file"><a href="cover_chapter.html#id78510">The cover specification file</a></li>
<li title="Logging"><a href="cover_chapter.html#id78563">Logging</a></li>
</ul>
</li>
<li id="no" title="Using Common Test for Large Scale Testing" expanded="false">Using Common Test for Large Scale Testing<ul>
<li><a href="ct_master_chapter.html">
              Top of chapter
            </a></li>
<li title="General"><a href="ct_master_chapter.html#id78634">General</a></li>
<li title="Usage"><a href="ct_master_chapter.html#id78668">Usage</a></li>
<li title="Test Specifications"><a href="ct_master_chapter.html#id78815">Test Specifications</a></li>
<li title="Automatic startup of test target nodes"><a href="ct_master_chapter.html#id78987">Automatic startup of test target nodes</a></li>
</ul>
</li>
<li id="no" title="Event Handling" expanded="false">Event Handling<ul>
<li><a href="event_handler_chapter.html">
              Top of chapter
            </a></li>
<li title="General"><a href="event_handler_chapter.html#id79190">General</a></li>
<li title="Usage"><a href="event_handler_chapter.html#id79226">Usage</a></li>
</ul>
</li>
<li id="no" title="Dependencies between Test Cases and Suites" expanded="false">Dependencies between Test Cases and Suites<ul>
<li><a href="dependencies_chapter.html">
              Top of chapter
            </a></li>
<li title="General"><a href="dependencies_chapter.html#id80143">General</a></li>
<li title="Saving configuration data"><a href="dependencies_chapter.html#id80265">Saving configuration data</a></li>
<li title="Sequences"><a href="dependencies_chapter.html#id80430">Sequences</a></li>
</ul>
</li>
<li id="no" title="Common Test Hooks" expanded="false">Common Test Hooks<ul>
<li><a href="ct_hooks_chapter.html">
              Top of chapter
            </a></li>
<li title="General"><a href="ct_hooks_chapter.html#id80622">General</a></li>
<li title="Installing a CTH"><a href="ct_hooks_chapter.html#id80676">Installing a CTH</a></li>
<li title="CTH Scope"><a href="ct_hooks_chapter.html#id80835">CTH Scope</a></li>
<li title="Manipulating tests"><a href="ct_hooks_chapter.html#id81189">Manipulating tests</a></li>
<li title="Example CTH"><a href="ct_hooks_chapter.html#id81482">Example CTH</a></li>
<li title="Built-in CTHs"><a href="ct_hooks_chapter.html#id81535">Built-in CTHs</a></li>
</ul>
</li>
<li id="no" title="Some thoughts about testing" expanded="false">Some thoughts about testing<ul>
<li><a href="why_test_chapter.html">
              Top of chapter
            </a></li>
<li title="Goals"><a href="why_test_chapter.html#id81722">Goals</a></li>
<li title="What to test?"><a href="why_test_chapter.html#id81742">What to test?</a></li>
</ul>
</li>
</ul>
</div></div>
<div id="content">
<div class="innertube">
<h1>8 External Configuration Data</h1>
  

    <a name="top"></a>

  <h3><a name="id77631">8.1 
        General</a></h3>
    

    <p>To avoid hard coding data values related to the test and/or SUT (System
    Under Test) in the test suites, the data may instead be specified by means
    of configuration files or strings that Common Test reads before
    the start of a test run. External configuration data makes it possible to
    change test properties without having to modify the actual test suites
    using the data. Examples of configuration data:</p>

    <ul>
      <li>Addresses to the test plant or other instruments</li>
      <li>User login information</li>
      <li>Names of files needed by the test</li>
      <li>Names of programs that should be executed during the test</li>
      <li>Any other variable needed by the test</li>
    </ul>

  

  <h3><a name="id77671">8.2 
        Syntax</a></h3>
    

    <p>A configuration file can contain any number of elements of the type:</p>
    <div class="example"><pre>
      {CfgVarName,Value}.</pre></div>

    <p>where</p>
    <div class="example"><pre>
      CfgVarName = atom()
      Value = term() | [{CfgVarName,Value}]</pre></div>

  

  <h3><a name="id77695">8.3 
        Requiring and reading configuration data</a></h3>
    
    <a name="require_config_data"></a>

    <p>In a test suite, one must <strong>require</strong> that a configuration 
    variable (<span class="code">CfgVarName</span> in the definition above) exists before
    attempting to read the associated value in a test case or config function.</p>

    <p><span class="code">require</span> is an assert statement that can be part of the <span class="bold_code"><a href="write_test_chapter.html#suite">test suite info function</a></span> or
    <span class="bold_code"><a href="write_test_chapter.html#info_function">test case info
    function</a></span>. If the required variable is not available, the
    test is skipped (unless a default value has been specified, see the
    <span class="bold_code"><a href="write_test_chapter.html#info_function">test case info
    function</a></span> chapter for details). There is also a function
    <span class="code"><span class="bold_code"><a href="ct.html#require-1">ct:require/1/2</a></span></span> which can be called from a test case
    in order to check if a specific variable is available. The return 
    value from this function must be checked explicitly and appropriate 
    action be taken depending on the result (e.g. to skip the test case
    if the variable in question doesn't exist).</p>

    <p>A <span class="code">require</span> statement in the test suite info- or test case 
    info-list should look like this:
    <span class="code">{require,CfgVarName}</span> or <span class="code">{require,AliasName,CfgVarName}</span>.
    The arguments <span class="code">AliasName</span> and <span class="code">CfgVarName</span> are the same as the
    arguments to <span class="code"><span class="bold_code"><a href="ct.html#require-1">ct:require/1/2</a></span></span> which are described in the
    reference manual for <span class="bold_code"><a href="ct.html">ct</a></span>.
    <span class="code">AliasName</span> becomes an alias for the configuration variable,
    and can be used as reference to the configuration data value.
    The configuration variable may be associated with an
    arbitrary number of alias names, but each name must be unique within
    the same test suite. There are two main uses for alias names:</p>
    <ul>
      <li>They may be introduced to identify connections (see below).</li>
      <li>They may used to help adapt configuration data to a test suite 
        (or test case) and improve readability.</li>
    </ul>
    <p>To read the value of a config variable, use the function
    <span class="code"><span class="bold_code"><a href="ct.html#get_config-1">get_config/1/2/3</a></span></span>
    which is also described in the reference
    manual for <span class="bold_code"><a href="ct.html">ct</a></span>.</p>
    <p>Example:</p>
    <div class="example"><pre>
      suite() -&gt; 
          [{require, domain, 'CONN_SPEC_DNS_SUFFIX'}].

      ...
      
      testcase(Config) -&gt;
          Domain = ct:get_config(domain),
	  ...</pre></div>      
  

  <h3><a name="id77839">8.4 
        Using configuration variables defined in multiple files</a></h3>
  
    <p>If a configuration variable is defined in multiple files and you 
      want to access all possible values, you may use the <span class="code"><span class="bold_code"><a href="ct.html#get_config-3">ct:get_config/3</a></span></span>
      function and specify <span class="code">all</span> in the options list. The values will then
      be returned in a list and the order of the elements corresponds to the order 
      that the config files were specified at startup. Please see 
      the <span class="bold_code"><a href="ct.html">ct</a></span> reference manual for details.</p>
  

  <h3><a name="id77870">8.5 
        Encrypted configuration files</a></h3>
    
    <a name="encrypted_config_files"></a>
    <p>It is possible to encrypt configuration files containing sensitive data
      if these files must be stored in open and shared directories.</p> 
      <p>Call <span class="code"><span class="bold_code"><a href="ct.html#encrypt_config_file-2">ct:encrypt_config_file/2/3</a></span></span> to have Common Test encrypt a
      specified file using the DES3 function in the OTP <span class="code">crypto</span> application.
      The encrypted file can then be used as a regular configuration file,
      in combination with other encrypted files or normal text files. The key
      for decrypting the configuration file must be provided when running the test,
      however. This can be done by means of the <span class="code">decrypt_key</span> or
      <span class="code">decrypt_file</span> flag/option, or a key file in a predefined location.</p>
      
      <p>Common Test also provides decryption functions, 
      <span class="code"><span class="bold_code"><a href="ct.html#decrypt_config_file-2">ct:decrypt_config_file/2/3</a></span></span>, for recreating the original text
      files.</p> 

      <p>Please see the <span class="bold_code"><a href="ct.html">ct</a></span> reference manual for
      more information.</p>
  

  <h3><a name="id77934">8.6 
        Opening connections by using configuration data</a></h3>
    
    <p>There are two different methods for opening a connection
      by means of the support functions in e.g. <span class="code"><span class="bold_code"><a href="ct_ssh.html">ct_ssh</a></span></span>, <span class="code"><span class="bold_code"><a href="ct_ftp.html">ct_ftp</a></span></span>,
      and <span class="code"><span class="bold_code"><a href="ct_telnet.html">ct_telnet</a></span></span>:</p>
    <ul>
      <li>Using a configuration target name (an alias) as reference.</li>
      <li>Using the configuration variable as reference.</li>
    </ul>    
    <p>When a target name is used for referencing the configuration data
      (that specifies the connection to be opened), the same name may be used 
      as connection identity in all subsequent calls related to the connection
      (also for closing it). It's only possible to have one open connection
      per target name. If attempting to open a new connection using a name
      already associated with an open connection, Common Test will
      return the already existing handle so that the previously opened connection
      will be used. This is a practical feature since it makes it possible to
      call the function for opening a particular connection whenever 
      useful. An action like this will not necessarily open any new 
      connections unless it's required (which could be the case if e.g. the 
      previous connection has been closed unexpectedly by the server).
      Another benefit of using named connections is that it's not
      necessary to pass handle references around in the suite for these 
      connections.
    </p>
    <p>When a configuration variable name is used as reference to the data
      specifying the connection, the handle returned as a result of opening
      the connection must be used in all subsequent calls (also for closing
      the connection). Repeated calls to the open function with the same
      variable name as reference will result in multiple connections
      being opened. This can be useful e.g. if a test case needs to open
      multiple connections to the same server on the target node (using the
      same configuration data for each connection).
    </p>
  

  <h3><a name="id78002">8.7 
        User specific configuration data formats</a></h3>
    

    <p>It is possible for the user to specify configuration data on a
      different format than key-value tuples in a text file, as described
      so far. The data can e.g. be read from arbitrary files, fetched from
      the web over http, or requested from a user specific process.
      To support this, Common Test provides a callback module plugin
      mechanism to handle configuration data.</p>

    <h4>Default callback modules for handling configuration data</h4>
      
      <p>The Common Test application includes default callback modules
	for handling configuration data specified in standard config files
	(see above) and in xml files:</p>
      <ul>
        <li>
          <span class="code">ct_config_plain</span> - for reading configuration files with
          key-value tuples (standard format). This handler will be used to
          parse configuration files if no user callback is specified.
        </li>
        <li>
          <span class="code">ct_config_xml</span> - for reading configuration data from XML
          files.
        </li>
      </ul>
    

    <h4>Using XML configuration files</h4>
      
      <p>This is an example of an XML configuration file:</p>
      <div class="example"><pre>
&lt;config&gt;
    &lt;ftp_host&gt;
        &lt;ftp&gt;"targethost"&lt;/ftp&gt;
        &lt;username&gt;"tester"&lt;/username&gt;
        &lt;password&gt;"letmein"&lt;/password&gt;
    &lt;/ftp_host&gt;
    &lt;lm_directory&gt;"/test/loadmodules"&lt;/lm_directory&gt;
&lt;/config&gt;</pre></div>

      <p>This configuration file, once read, will produce the same configuration
      variables as the following text file:</p>
      <div class="example"><pre>
{ftp_host, [{ftp,"targethost"},
            {username,"tester"},
            {password,"letmein"}]}.

{lm_directory, "/test/loadmodules"}.</pre></div>
    

    <h4>How to implement a user specific handler</h4>
      

      <p>The user specific handler can be written to handle special
	configuration file formats. The parameter can be either file
	name(s) or configuration string(s) (the empty list is valid).</p>

      <p>The callback module implementing the handler is responsible for
	checking correctness of configuration strings.</p>

      <p>To perform validation of the configuration strings, the callback module
	should have the following function exported:</p>

      <p><span class="code">Callback:check_parameter/1</span></p>
      <p>The input argument will be passed from Common Test, as defined in the test
	specification or given as an option to <span class="code">ct_run</span> or <span class="code">ct:run_test</span>.</p>

      <p>The return value should be any of the following values indicating if given
	configuration parameter is valid:</p>
      <ul>
        <li>
          <span class="code">{ok, {file, FileName}}</span> - parameter is a file name and
          the file exists,
        </li>
        <li>
          <span class="code">{ok, {config, ConfigString}}</span> - parameter is a config string
          and it is correct,
        </li>
        <li>
          <span class="code">{error, {nofile, FileName}}</span> - there is no file with the given
          name in the current directory,
        </li>
        <li>
          <span class="code">{error, {wrong_config, ConfigString}}</span> - the configuration string
          is wrong.
        </li>
      </ul>

      <p>To perform reading of configuration data - initially before the tests
	start, or as a result of data being reloaded during test execution -
	the following function should be exported from the callback module:</p>

      <p><span class="code">Callback:read_config/1</span></p>

      <p>The input argument is the same as for the <span class="code">check_parameter/1</span> function.</p>
      <p>The return value should be either:</p>

      <ul>
        <li>
          <span class="code">{ok, Config}</span> - if the configuration variables are read successfully,
        </li>
        <li>
          <span class="code">{error, {Error, ErrorDetails}}</span> - if the callback module fails to
          proceed with the given configuration parameters.
        </li>
      </ul>
      <p><span class="code">Config</span> is the proper Erlang key-value list, with possible
	key-value sublists as values, like for the configuration file
	example above:</p>

      <div class="example"><pre>
        [{ftp_host, [{ftp, "targethost"}, {username, "tester"}, {password, "letmein"}]},
         {lm_directory, "/test/loadmodules"}]</pre></div>

    

  

  <h3><a name="id78216">8.8 
        Examples of configuration data handling</a></h3>
    

    <p>A config file for using the FTP client to access files on a remote
      host could look like this:</p>

    <div class="example"><pre>
    {ftp_host, [{ftp,"targethost"},
                {username,"tester"},
                {password,"letmein"}]}.

    {lm_directory, "/test/loadmodules"}.</pre></div>

    <p>The XML version shown in the chapter above can also be used, but it should be
    explicitly specified that the <span class="code">ct_config_xml</span> callback module is to be
    used by Common Test.</p>

    <p>Example of how to assert that the configuration data is available and
      use it for an FTP session:</p>
    <div class="example"><pre>
    init_per_testcase(ftptest, Config) -&gt;
        {ok,_} = ct_ftp:open(ftp),
        Config.

    end_per_testcase(ftptest, _Config) -&gt;
        ct_ftp:close(ftp).

    ftptest() -&gt;
        [{require,ftp,ftp_host},
         {require,lm_directory}].

    ftptest(Config) -&gt;
        Remote = filename:join(ct:get_config(lm_directory), "loadmodX"),
        Local = filename:join(?config(priv_dir,Config), "loadmodule"),
        ok = ct_ftp:recv(ftp, Remote, Local),
        ...</pre></div>
    
    <p>An example of how the above functions could be rewritten
      if necessary to open multiple connections to the FTP server:</p>
    <div class="example"><pre>
    init_per_testcase(ftptest, Config) -&gt;
        {ok,Handle1} = ct_ftp:open(ftp_host),
        {ok,Handle2} = ct_ftp:open(ftp_host),
        [{ftp_handles,[Handle1,Handle2]} | Config].

    end_per_testcase(ftptest, Config) -&gt;
        lists:foreach(fun(Handle) -&gt; ct_ftp:close(Handle) end, 
                      ?config(ftp_handles,Config)).

    ftptest() -&gt;
        [{require,ftp_host},
         {require,lm_directory}].

    ftptest(Config) -&gt;
        Remote = filename:join(ct:get_config(lm_directory), "loadmodX"),
        Local = filename:join(?config(priv_dir,Config), "loadmodule"),
        [Handle | MoreHandles] = ?config(ftp_handles,Config),
        ok = ct_ftp:recv(Handle, Remote, Local),
        ...</pre></div>
      
  

  <h3><a name="id78268">8.9 
        Example of user specific configuration handler</a></h3>
    
    <p>A simple configuration handling driver which will ask an external server for
      configuration data can be implemented this way:</p>
    <div class="example"><pre>
-module(config_driver).
-export([read_config/1, check_parameter/1]).

read_config(ServerName)-&gt;
    ServerModule = list_to_atom(ServerName),
    ServerModule:start(),
    ServerModule:get_config().

check_parameter(ServerName)-&gt;
    ServerModule = list_to_atom(ServerName),
    case code:is_loaded(ServerModule) of
        {file, _}-&gt;
            {ok, {config, ServerName}};
        false-&gt;
            case code:load_file(ServerModule) of
                {module, ServerModule}-&gt;
                    {ok, {config, ServerName}};
                {error, nofile}-&gt;
                    {error, {wrong_config, "File not found: " ++ ServerName ++ ".beam"}}
            end
    end.</pre></div>

    <p>The configuration string for this driver may be "config_server", if the
      config_server.erl module below is compiled and exists in the code path
      during test execution:</p>
    <div class="example"><pre>
-module(config_server).
-export([start/0, stop/0, init/1, get_config/0, loop/0]).

-define(REGISTERED_NAME, ct_test_config_server).

start()-&gt;
    case whereis(?REGISTERED_NAME) of
        undefined-&gt;
            spawn(?MODULE, init, [?REGISTERED_NAME]),
            wait();
        _Pid-&gt;
        ok
    end,
    ?REGISTERED_NAME.

init(Name)-&gt;
    register(Name, self()),
    loop().

get_config()-&gt;
    call(self(), get_config).

stop()-&gt;
    call(self(), stop).

call(Client, Request)-&gt;
    case whereis(?REGISTERED_NAME) of
        undefined-&gt;
            {error, {not_started, Request}};
        Pid-&gt;
            Pid ! {Client, Request},
            receive
                Reply-&gt;
                    {ok, Reply}
            after 4000-&gt;
                {error, {timeout, Request}}
            end
    end.

loop()-&gt;
    receive
        {Pid, stop}-&gt;
            Pid ! ok;
        {Pid, get_config}-&gt;
            {D,T} = erlang:localtime(),
            Pid !
                [{localtime, [{date, D}, {time, T}]},
                 {node, erlang:node()},
                 {now, erlang:now()},
                 {config_server_pid, self()},
                 {config_server_vsn, ?vsn}],
            ?MODULE:loop()
    end.

wait()-&gt;
    case whereis(?REGISTERED_NAME) of
        undefined-&gt;
            wait();
        _Pid-&gt;
            ok
    end.</pre></div>

    <p>In this example, the handler also provides the ability to dynamically reload
      configuration variables. If <span class="code">ct:reload_config(localtime)</span> is called from
      the test case function, all variables loaded with <span class="code">config_driver:read_config/1</span>
      will be updated with their latest values, and the new value for variable
      <span class="code">localtime</span> will be returned.</p>
  

</div>
<div class="footer">
<hr>
<p>Copyright © 2003-2012 Ericsson AB. All Rights Reserved.</p>
</div>
</div>
</div></body>
</html>