- move TestSetup from include to a library, together with a non-yet working
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Thu, 16 Oct 2008 17:23:16 +0000 (17:23 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Thu, 16 Oct 2008 17:23:16 +0000 (17:23 +0000)
  WebServer utility to test http scenarios.

tests/CMakeLists.txt
tests/include/TestSetup.h [deleted file]
tests/lib/CMakeLists.txt [new file with mode: 0644]
tests/lib/TestSetup.h [new file with mode: 0644]
tests/lib/WebServer.cc [new file with mode: 0644]
tests/lib/WebServer.h [new file with mode: 0644]
tests/zypp/CMakeLists.txt
tests/zypp/Fetcher_test.cc

index 1e522a39b12dc0bdd720c130a2401575648f95f5..df3ec1dcfa1261e4038862a4b1e9ea575bbb1629 100644 (file)
@@ -1,5 +1,9 @@
+
+ADD_SUBDIRECTORY(lib)
+
 ENABLE_TESTING()
-INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/tests/include )
+INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/tests/lib )
+
 ADD_DEFINITIONS( -DTESTS_SRC_DIR="${CMAKE_CURRENT_SOURCE_DIR}" -DTESTS_BUILD_DIR="${CMAKE_CURRENT_BINARY_DIR}" )
 
 ADD_SUBDIRECTORY( media )
diff --git a/tests/include/TestSetup.h b/tests/include/TestSetup.h
deleted file mode 100644 (file)
index 27f566d..0000000
+++ /dev/null
@@ -1,122 +0,0 @@
-#ifndef INCLUDE_TESTSETUP
-#define INCLUDE_TESTSETUP
-#include <iostream>
-
-#ifndef INCLUDE_TESTSETUP_WITHOUT_BOOST
-#include <boost/test/auto_unit_test.hpp>
-using boost::unit_test::test_case;
-#endif
-
-#include "zypp/base/LogTools.h"
-#include "zypp/ZYppFactory.h"
-#include "zypp/ZYpp.h"
-#include "zypp/TmpPath.h"
-#include "zypp/PathInfo.h"
-#include "zypp/RepoManager.h"
-#include "zypp/Target.h"
-#include "zypp/ResPool.h"
-
-using std::cout;
-using std::endl;
-using namespace zypp;
-
-/** Build a test environment below a temp. root directory.
- * If a \c rootdir_r was provided to the ctor, this directory
- * will be used and it will \b not be removed.
- *
- * \note The lifetime of this objects is the lifetime of the temp. root directory.
- *
- * \code
- * #include "TestSetup.h"
- *
- * BOOST_AUTO_TEST_CASE(WhatProvides)
- * {
- *   TestSetup test( Arch_x86_64 );
- *   // test.loadTarget(); // initialize and load target
- *   test.loadRepo( TESTS_SRC_DIR"/data/openSUSE-11.1" );
- *
- *   // Here the pool is ready to be used.
- *
- * }
- * \endcode
-*/
-class TestSetup
-{
-  public:
-    TestSetup( const Arch & sysarch_r = Arch() )
-    { _ctor( Pathname(), sysarch_r ); }
-
-    TestSetup( const Pathname & rootdir_r, const Arch & sysarch_r = Arch() )
-    { _ctor( rootdir_r, sysarch_r ); }
-
-    ~TestSetup()
-    { USR << "DELETE TESTSETUP below " << _rootdir << endl; }
-
-  public:
-    const Pathname & root() const { return _rootdir; }
-
-    Target &     target()      { if ( ! getZYpp()->getTarget() ) getZYpp()->initializeTarget( _rootdir ); return *getZYpp()->getTarget(); }
-    RepoManager  repomanager() { return RepoManager( RepoManagerOptions::makeTestSetup( _rootdir ) ); }
-    ResPool      pool()        { return ResPool::instance(); }
-    ResPoolProxy poolProxy()   { return pool().proxy(); }
-    sat::Pool    satpool()     { return sat::Pool::instance(); }
-    Resolver &   resolver()    { return *getZYpp()->resolver(); }
-
-  public:
-    /** Load target repo. */
-    void loadTarget()
-    {
-      target().load();
-    }
-
-    /** Directly load repoinfo to pool. */
-    void loadRepo( RepoInfo nrepo )
-    {
-      RepoManager rmanager( repomanager() );
-      rmanager.buildCache( nrepo );
-      rmanager.loadFromCache( nrepo );
-    }
-    /** Directly load repo from url to pool. */
-    void loadRepo( const Url & url_r, const std::string & alias_r = std::string() )
-    {
-      RepoInfo nrepo;
-      nrepo.setAlias( alias_r.empty() ? url_r.getHost()+":"+Pathname::basename(url_r.getPathName()) : alias_r );
-      nrepo.addBaseUrl( url_r );
-      nrepo.setGpgCheck( false );
-      loadRepo( nrepo );
-    }
-    /** Directly load repo from metadata(dir) or solvfile(file) to pool.
-     * An empty alias is guessed.
-    */
-    void loadRepo( const Pathname & path_r, const std::string & alias_r = std::string() )
-    {
-      if ( filesystem::PathInfo( path_r ).isDir() )
-      {
-        loadRepo( path_r.asUrl(), alias_r );
-        return;
-      }
-      // .solv file is loaded directly using a faked RepoInfo
-      RepoInfo nrepo;
-      nrepo.setAlias( alias_r.empty() ? path_r.basename() : alias_r );
-      satpool().addRepoSolv( path_r, nrepo );
-    }
-
-  private:
-    void _ctor( const Pathname & rootdir_r, const Arch & sysarch_r )
-    {
-      if ( rootdir_r.empty() )
-        _rootdir = _tmprootdir.path();
-      else
-        filesystem::assert_dir( (_rootdir = rootdir_r) );
-
-      if ( ! sysarch_r.empty() )
-        ZConfig::instance().setSystemArchitecture( sysarch_r );
-      USR << "CREATED TESTSETUP below " << _rootdir << endl;
-    }
-  private:
-    filesystem::TmpDir _tmprootdir;
-    Pathname           _rootdir;
-};
-
-
-#endif //INCLUDE_TESTSETUP
diff --git a/tests/lib/CMakeLists.txt b/tests/lib/CMakeLists.txt
new file mode 100644 (file)
index 0000000..d08b738
--- /dev/null
@@ -0,0 +1,10 @@
+
+INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/vendor/shttpd )
+
+ADD_LIBRARY(zypp_test_utils
+ TestSetup.h
+ WebServer.h
+ WebServer.cc
+)
+
+TARGET_LINK_LIBRARIES(zypp_test_utils shttp zypp)
diff --git a/tests/lib/TestSetup.h b/tests/lib/TestSetup.h
new file mode 100644 (file)
index 0000000..27f566d
--- /dev/null
@@ -0,0 +1,122 @@
+#ifndef INCLUDE_TESTSETUP
+#define INCLUDE_TESTSETUP
+#include <iostream>
+
+#ifndef INCLUDE_TESTSETUP_WITHOUT_BOOST
+#include <boost/test/auto_unit_test.hpp>
+using boost::unit_test::test_case;
+#endif
+
+#include "zypp/base/LogTools.h"
+#include "zypp/ZYppFactory.h"
+#include "zypp/ZYpp.h"
+#include "zypp/TmpPath.h"
+#include "zypp/PathInfo.h"
+#include "zypp/RepoManager.h"
+#include "zypp/Target.h"
+#include "zypp/ResPool.h"
+
+using std::cout;
+using std::endl;
+using namespace zypp;
+
+/** Build a test environment below a temp. root directory.
+ * If a \c rootdir_r was provided to the ctor, this directory
+ * will be used and it will \b not be removed.
+ *
+ * \note The lifetime of this objects is the lifetime of the temp. root directory.
+ *
+ * \code
+ * #include "TestSetup.h"
+ *
+ * BOOST_AUTO_TEST_CASE(WhatProvides)
+ * {
+ *   TestSetup test( Arch_x86_64 );
+ *   // test.loadTarget(); // initialize and load target
+ *   test.loadRepo( TESTS_SRC_DIR"/data/openSUSE-11.1" );
+ *
+ *   // Here the pool is ready to be used.
+ *
+ * }
+ * \endcode
+*/
+class TestSetup
+{
+  public:
+    TestSetup( const Arch & sysarch_r = Arch() )
+    { _ctor( Pathname(), sysarch_r ); }
+
+    TestSetup( const Pathname & rootdir_r, const Arch & sysarch_r = Arch() )
+    { _ctor( rootdir_r, sysarch_r ); }
+
+    ~TestSetup()
+    { USR << "DELETE TESTSETUP below " << _rootdir << endl; }
+
+  public:
+    const Pathname & root() const { return _rootdir; }
+
+    Target &     target()      { if ( ! getZYpp()->getTarget() ) getZYpp()->initializeTarget( _rootdir ); return *getZYpp()->getTarget(); }
+    RepoManager  repomanager() { return RepoManager( RepoManagerOptions::makeTestSetup( _rootdir ) ); }
+    ResPool      pool()        { return ResPool::instance(); }
+    ResPoolProxy poolProxy()   { return pool().proxy(); }
+    sat::Pool    satpool()     { return sat::Pool::instance(); }
+    Resolver &   resolver()    { return *getZYpp()->resolver(); }
+
+  public:
+    /** Load target repo. */
+    void loadTarget()
+    {
+      target().load();
+    }
+
+    /** Directly load repoinfo to pool. */
+    void loadRepo( RepoInfo nrepo )
+    {
+      RepoManager rmanager( repomanager() );
+      rmanager.buildCache( nrepo );
+      rmanager.loadFromCache( nrepo );
+    }
+    /** Directly load repo from url to pool. */
+    void loadRepo( const Url & url_r, const std::string & alias_r = std::string() )
+    {
+      RepoInfo nrepo;
+      nrepo.setAlias( alias_r.empty() ? url_r.getHost()+":"+Pathname::basename(url_r.getPathName()) : alias_r );
+      nrepo.addBaseUrl( url_r );
+      nrepo.setGpgCheck( false );
+      loadRepo( nrepo );
+    }
+    /** Directly load repo from metadata(dir) or solvfile(file) to pool.
+     * An empty alias is guessed.
+    */
+    void loadRepo( const Pathname & path_r, const std::string & alias_r = std::string() )
+    {
+      if ( filesystem::PathInfo( path_r ).isDir() )
+      {
+        loadRepo( path_r.asUrl(), alias_r );
+        return;
+      }
+      // .solv file is loaded directly using a faked RepoInfo
+      RepoInfo nrepo;
+      nrepo.setAlias( alias_r.empty() ? path_r.basename() : alias_r );
+      satpool().addRepoSolv( path_r, nrepo );
+    }
+
+  private:
+    void _ctor( const Pathname & rootdir_r, const Arch & sysarch_r )
+    {
+      if ( rootdir_r.empty() )
+        _rootdir = _tmprootdir.path();
+      else
+        filesystem::assert_dir( (_rootdir = rootdir_r) );
+
+      if ( ! sysarch_r.empty() )
+        ZConfig::instance().setSystemArchitecture( sysarch_r );
+      USR << "CREATED TESTSETUP below " << _rootdir << endl;
+    }
+  private:
+    filesystem::TmpDir _tmprootdir;
+    Pathname           _rootdir;
+};
+
+
+#endif //INCLUDE_TESTSETUP
diff --git a/tests/lib/WebServer.cc b/tests/lib/WebServer.cc
new file mode 100644 (file)
index 0000000..b3edc66
--- /dev/null
@@ -0,0 +1,38 @@
+
+#include <sys/types.h>
+#include <sys/select.h>
+#include <sys/wait.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+
+#include "zypp/ZYpp.h"
+
+using namespace zypp;
+
+#include "shttpd.h"
+
+#include "WebServer.h"
+
+WebServer::WebServer(const Pathname root, unsigned int port)
+    : _docroot(root), _port(port)
+{
+    _ctx = shttpd_init(0, NULL);
+    shttpd_set_option(_ctx, "root", root.c_str());
+    shttpd_set_option(_ctx, "ports", str::numstring(port).c_str());
+
+}
+
+void WebServer::operator()()
+{
+    for (;;)
+        shttpd_poll(_ctx, 1000);
+}
+
+WebServer::~WebServer()
+{
+    shttpd_fini(_ctx);
+}
+
+
diff --git a/tests/lib/WebServer.h b/tests/lib/WebServer.h
new file mode 100644 (file)
index 0000000..45d948e
--- /dev/null
@@ -0,0 +1,25 @@
+
+#ifndef ZYPP_WEBSERVER_H
+#define ZYPP_WEBSERVER_H
+
+#include "zypp/Pathname.h"
+
+struct shttpd_ctx;
+
+class WebServer
+{
+ public:
+  WebServer(const zypp::Pathname root, unsigned int port);
+  ~WebServer();
+  void start();
+  
+  void operator()();
+
+
+ private:
+   struct shttpd_ctx *_ctx;
+   zypp::Pathname _docroot;
+   unsigned int _port;
+};
+
+#endif
index 742e25c7db76b4fd6f0f3eb7f45402c01e0c964a..81ed86c84d8ebc5763d297dba21dfe352d1b1545 100644 (file)
@@ -32,3 +32,4 @@ ADD_TESTS(
   Vendor2
 )
 
+#TARGET_LINK_LIBRARIES(Fetcher_test boost_thread-mt)
\ No newline at end of file
index 9ad024529ab2c29be0231354d8a229a4a901f30b..95fa88f585dcdb1d51b4d4d4c606ae0093b98507 100644 (file)
@@ -11,6 +11,9 @@
 #include "zypp/Url.h"
 #include "zypp/TmpPath.h"
 
+#include "WebServer.h"
+#include <boost/thread.hpp>
+
 using std::cout;
 using std::endl;
 using std::string;