WebServer utility to test http scenarios.
+
+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 )
+++ /dev/null
-#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
--- /dev/null
+
+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)
--- /dev/null
+#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
--- /dev/null
+
+#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);
+}
+
+
--- /dev/null
+
+#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
Vendor2
)
+#TARGET_LINK_LIBRARIES(Fetcher_test boost_thread-mt)
\ No newline at end of file
#include "zypp/Url.h"
#include "zypp/TmpPath.h"
+#include "WebServer.h"
+#include <boost/thread.hpp>
+
using std::cout;
using std::endl;
using std::string;