- implement "does file exist" using curl command line in the aria backend
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 20 Feb 2009 13:48:23 +0000 (14:48 +0100)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 20 Feb 2009 13:48:23 +0000 (14:48 +0100)
- fix testcases by using webserver class once per test
- switch tests back to mongoose embeded

PENDING:
- handling curl errors more granular
- enable aria backend by default, allow using ZYPP_ARIA2C=0 to disable

tests/lib/WebServer.cc
tests/zypp/Fetcher_test.cc
tests/zypp/MediaSetAccess_test.cc
zypp/media/MediaAccess.cc
zypp/media/MediaAria2c.cc

index 15d9430..030c6f0 100644 (file)
@@ -25,7 +25,7 @@ static inline string hostname()
     return result;
 }
 
-#define WEBRICK 1
+#define WEBRICK 0
 
 class WebServer::Impl
 {
@@ -88,7 +88,7 @@ public:
             
         stringstream strlog(_log);
 
-        string webrick_code = str::form("require 'webrick'; s = WEBrick::HTTPServer.new(:Port => %d, :DocumentRoot    => '%s'); trap('INT'){ s.shutdown }; trap('SIGKILL') { s.shutdown }; s.start;", _port, _docroot.c_str());
+        string webrick_code = str::form("require \"webrick\"; s = WEBrick::HTTPServer.new(:Port => %d, :DocumentRoot    => \"%s\"); trap(\"INT\"){ s.shutdown }; trap(\"SIGKILL\") { s.shutdown }; s.start;", _port, _docroot.c_str());
     
         const char* argv[] =
         {
@@ -163,10 +163,17 @@ public:
         MIL << "Starting shttpd (mongoose)" << endl;
         _log.clear();
         _ctx = mg_start();
-        if ( ! mg_set_option(_ctx, "ports", str::form("%d", _port).c_str()) )
-            ZYPP_THROW(Exception("Failed to set port"));
+
+        int ret = 0;
+        ret = mg_set_option(_ctx, "ports", str::form("%d", _port).c_str());
+        if (  ret != 1 )
+            ZYPP_THROW(Exception(str::form("Failed to set port: %d", ret)));
         
-        mg_set_option(_ctx, "root", _docroot.c_str());
+        MIL << "Setting root directory to : '" << _docroot << "'" << endl;
+        ret = mg_set_option(_ctx, "root", _docroot.c_str());
+        if (  ret != 1 )
+            ZYPP_THROW(Exception(str::form("Failed to set docroot: %d", ret)));
+
         _stopped = false;
     }
 
@@ -175,7 +182,7 @@ public:
         return _port;
     }
 
-    
+   
     virtual string log() const
     {
         return _log;
index c7af0a4..0adcb6f 100644 (file)
@@ -320,14 +320,11 @@ BOOST_AUTO_TEST_CASE(enqueue_broken_content_noindex)
 
 BOOST_AUTO_TEST_CASE(enqueuedir_http)
 {
+    WebServer web((Pathname(TESTS_SRC_DIR) + "/zypp/data/Fetcher/remote-site").c_str(), 10001);
+    web.start();
+
   // at this point the key is already trusted
   {
-      // add the key as trusted
-      //getZYpp()->keyRing()->importKey(PublicKey(DATADIR + "/complexdir/subdir1/SHA1SUMS.key"), true);
-
-      WebServer web((Pathname(TESTS_SRC_DIR) + "/zypp/data/Fetcher/remote-site").c_str(), 10001);
-      web.start();
-
       MediaSetAccess media( web.url(), "/" );
       Fetcher fetcher;
       filesystem::TmpDir dest;
@@ -343,21 +340,10 @@ BOOST_AUTO_TEST_CASE(enqueuedir_http)
       BOOST_CHECK( PathInfo(dest.path() + "/complexdir/subdir2/subdir2-file1.txt").isExist() );
       BOOST_CHECK( PathInfo(dest.path() + "/complexdir/subdir1/subdir1-file1.txt").isExist() );
       BOOST_CHECK( PathInfo(dest.path() + "/complexdir/subdir1/subdir1-file2.txt").isExist() );
-
-      web.stop();
   }
-}
 
-BOOST_AUTO_TEST_CASE(enqueuedir_http_broken)
-{
-  // at this point the key is already trusted
+  // test broken tree
   {
-      // add the key as trusted
-      //getZYpp()->keyRing()->importKey(PublicKey(DATADIR + "/complexdir/subdir1/SHA1SUMS.key"), true);
-
-      WebServer web((Pathname(TESTS_SRC_DIR) + "/zypp/data/Fetcher/remote-site").c_str() , 10001 );
-      web.start();
-
       MediaSetAccess media( web.url(), "/" );
       Fetcher fetcher;
       filesystem::TmpDir dest;
@@ -378,15 +364,11 @@ BOOST_AUTO_TEST_CASE(enqueuedir_http_broken)
       BOOST_CHECK( ! PathInfo(dest.path() + "/complexdir-broken/subdir1/subdir1-file2.txt").isExist() );
 
       fetcher.reset();
-
-      web.stop();
-
-      MIL << web.log();
-
   }
+  
+  web.stop();
 }
 
-
 BOOST_AUTO_TEST_SUITE_END();
 
 // vim: set ts=2 sts=2 sw=2 ai et:
index c8f1b26..7889010 100644 (file)
@@ -92,7 +92,7 @@ BOOST_AUTO_TEST_CASE(msa_url_rewrite)
     Url("http://ftp.opensuse.org/pub/opensuse/distribution/SL-OSS-factory/inst-source").asString());
 }
 
-#define DATADIR (string("dir:") + string(TESTS_SRC_DIR) + string("/zypp/data/mediasetaccess"))
+#define DATADIR (Pathname(TESTS_SRC_DIR) / "/zypp/data/mediasetaccess")
 
 /*
  *
@@ -121,8 +121,7 @@ BOOST_AUTO_TEST_CASE(msa_url_rewrite)
  */
 BOOST_AUTO_TEST_CASE(msa_provide_files_set)
 {
-  string urlstr = DATADIR + "/src1/cd1";
-  Url url(urlstr);
+  Url url = (DATADIR + "/src1/cd1").asUrl();
   MediaSetAccess setaccess(url);
 
   Pathname file1 = setaccess.provideFile("/test.txt", 1);
@@ -140,8 +139,7 @@ BOOST_AUTO_TEST_CASE(msa_provide_files_set)
  */
 BOOST_AUTO_TEST_CASE(msa_provide_files_set_verified)
 {
-  string urlstr = DATADIR + "/src1/cd1";
-  Url url(urlstr);
+  Url url = (DATADIR + "/src1/cd1").asUrl();
   MediaSetAccess setaccess(url);
 
   setaccess.setVerifier(1, media::MediaVerifierRef(new SimpleVerifier("media1")));
@@ -166,8 +164,7 @@ BOOST_AUTO_TEST_CASE(msa_provide_files_set_verified)
  */
 BOOST_AUTO_TEST_CASE(msa_provide_files_single)
 {
-  string urlstr = DATADIR + "/src2";
-  Url url(urlstr);
+  Url url = (DATADIR + "/src2").asUrl();
   MediaSetAccess setaccess(url);
   setaccess.setVerifier(1, media::MediaVerifierRef(new SimpleVerifier("media")));
 
@@ -186,8 +183,8 @@ BOOST_AUTO_TEST_CASE(msa_provide_files_single)
  */
 BOOST_AUTO_TEST_CASE(msa_provide_dir)
 {
-  string urlstr = DATADIR + "/src1/cd1";
-  Url url(urlstr);
+  Url url = (DATADIR + "/src1/cd1").asUrl();
+
   MediaSetAccess setaccess(url);
 
   Pathname dir = setaccess.provideDir("/dir", false, 1);
@@ -217,8 +214,7 @@ BOOST_AUTO_TEST_CASE(msa_provide_dir)
  */
 BOOST_AUTO_TEST_CASE(msa_provide_dirtree)
 {
-  string urlstr = DATADIR + "/src1/cd1";
-  Url url(urlstr);
+  Url url = (DATADIR + "/src1/cd1").asUrl(); 
   MediaSetAccess setaccess(url);
 
   Pathname dir = setaccess.provideDir("/dir", true, 1);
@@ -238,7 +234,8 @@ BOOST_AUTO_TEST_CASE(msa_provide_dirtree)
  */
 BOOST_AUTO_TEST_CASE(msa_provide_optional_file)
 {
-  MediaSetAccess setaccess(Url(DATADIR + "/src1/cd1"));
+  Url url = (DATADIR + "/src1/cd1").asUrl(); 
+  MediaSetAccess setaccess(url);
 
   // must not throw
   BOOST_CHECK(setaccess.provideOptionalFile("/foo", 1).empty() == true);
@@ -254,7 +251,8 @@ BOOST_AUTO_TEST_CASE(msa_provide_optional_file)
  */
 BOOST_AUTO_TEST_CASE(msa_file_exist_local)
 {
-  MediaSetAccess setaccess(Url(DATADIR + "/src1/cd1"));
+  Url url = (DATADIR + "/src1/cd1").asUrl(); 
+  MediaSetAccess setaccess(url);
 
   BOOST_CHECK(setaccess.doesFileExist("/test.txt"));
   BOOST_CHECK(!setaccess.doesFileExist("/testBADNAME.txt"));
@@ -265,12 +263,13 @@ BOOST_AUTO_TEST_CASE(msa_file_exist_local)
  */
 BOOST_AUTO_TEST_CASE(msa_file_exist_remote)
 {
-  WebServer web( Pathname(DATADIR) / "/src1/cd1", 10001 );
+  WebServer web( DATADIR / "/src1/cd1", 10002 );
   web.start();
   MediaSetAccess setaccess( web.url(), "/" );
 
-  BOOST_CHECK(setaccess.doesFileExist("/test.txt"));
   BOOST_CHECK(!setaccess.doesFileExist("/testBADNAME.txt"));
+  BOOST_CHECK(setaccess.doesFileExist("/test.txt"));
+
   web.stop();
 }
 
index 85ac5ea..1030222 100644 (file)
@@ -128,7 +128,29 @@ MediaAccess::open (const Url& url, const Pathname & preferred_attach_point)
     else if (scheme == "cifs")
         _handler = new MediaCIFS (url,preferred_attach_point);
     else if (scheme == "ftp" || scheme == "http" || scheme == "https")
-        _handler = new MediaAria2c (url,preferred_attach_point);
+    {
+        // Another good idea would be activate MediaAria2c handler via external var
+        bool use_aria = true;
+        const char *ariaenv = getenv( "ZYPP_ARIA2C" );
+        // if user disabled it manually
+        if ( ariaenv && ( strcmp(ariaenv, "0" ) == 0 ) )
+        {
+            WAR << "aria2c manually disabled. Falling back to curl";
+            use_aria = false;
+        }
+        
+        // disable if it does not exist
+        if ( ! MediaAria2c::existsAria2cmd() )
+        {
+            WAR << "aria2c not found. Falling back to curl";
+            use_aria = false;
+        }
+        
+        if ( use_aria )
+            _handler = new MediaAria2c (url,preferred_attach_point);
+        else
+            _handler = new MediaCurl (url,preferred_attach_point);
+    }
     else
     {
        ZYPP_THROW(MediaUnsupportedUrlSchemeException(url));
index a5de650..5f34f2c 100644 (file)
@@ -628,11 +628,25 @@ bool MediaAria2c::doGetDoesFileExist( const Pathname & filename ) const
               return false;
 
           if ( str::contains(curlResponse, "200 OK") )
-              return false;
+              return true;
       }
 
       int code = curl.close();
 
+      switch (code)
+      {
+      case 0: break;
+          // connection problems
+          return true;
+      case 1:
+      case 2:
+      case 3:
+      case 7:
+      default:
+          ZYPP_THROW(MediaException(_url.asString()));
+      }
+      
+
       report->finish( _url ,  zypp::media::DownloadProgressReport::NO_ERROR, "");
       retry = false;
     }