Imported Upstream version 17.22.1
[platform/upstream/libzypp.git] / tests / repo / DUdata_test.cc
1 #include <iostream>
2 #include <sstream>
3 #include <fstream>
4 #include <list>
5 #include <string>
6
7 #include <boost/test/unit_test.hpp>
8
9 #include "zypp/base/Logger.h"
10 #include "zypp/base/Exception.h"
11 #include "zypp/RepoManager.h"
12 #include "zypp/ResPool.h"
13 #include "zypp/sat/Pool.h"
14 #include "zypp/PoolQuery.h"
15
16 #include "KeyRingTestReceiver.h"
17 #include "TestSetup.h"
18
19 using boost::unit_test::test_case;
20
21 using namespace std;
22 using namespace zypp;
23 using namespace zypp::repo;
24 using namespace zypp::filesystem;
25
26 #define TEST_DIR TESTS_SRC_DIR "/repo/susetags/data/dudata"
27
28 PoolItem piFind( const std::string & name_r, const std::string & ver_r, bool installed_r = false )
29 {
30   PoolQuery q;
31   q.addDependency( sat::SolvAttr::name, name_r, Rel::EQ, Edition(ver_r) );
32   q.setStatusFilterFlags( installed_r ? PoolQuery::INSTALLED_ONLY : PoolQuery::UNINSTALLED_ONLY );
33   if ( q.size() != 1 )
34     ZYPP_THROW(Exception(q.size()?"Ambiguous!":"Missing!"));
35   return PoolItem( *q.begin() );
36 }
37
38 typedef std::pair<ByteCount,ByteCount> ByteSet;
39 namespace std
40 {
41   inline std::ostream & operator<<( std::ostream & str, const ByteSet & obj )
42   { return str << "<" << obj.first << "," << obj.second << ">"; }
43 }
44
45 inline ByteSet mkByteSet( const DiskUsageCounter::MountPointSet & mps_r )
46 { return ByteSet( mps_r.begin()->commitDiff(), (++mps_r.begin())->commitDiff() ); }
47
48 inline ByteSet mkByteSet( int grow_r , int norm_r )
49 { return ByteSet( ByteCount( grow_r, ByteCount::K ), ByteCount( norm_r, ByteCount::K ) ); }
50
51 inline ByteSet getSize( const DiskUsageCounter & duc_r, const PoolItem & pi_r )
52 { return mkByteSet( duc_r.disk_usage( pi_r ) ); }
53
54 inline ByteSet getSize( const DiskUsageCounter & duc_r, const ResPool & pool_r )
55 { return mkByteSet( duc_r.disk_usage( pool_r ) ); }
56
57 inline void XLOG( const DiskUsageCounter & duc_r, const ResPool & pool_r )
58 {
59   for( const auto & pi : pool_r )
60   {
61     USR << pi << endl;
62   }
63   WAR << duc_r.disk_usage( pool_r ) << endl;
64 }
65
66 BOOST_AUTO_TEST_CASE(dudata)
67 {
68   //KeyRingTestReceiver rec;
69   // rec.answerAcceptUnknownKey(true);
70   //rec.answerAcceptUnsignedFile(true);
71   // rec.answerImportKey(true);
72
73   Pathname repodir( TEST_DIR );
74   TestSetup test( Arch_x86_64 );
75   test.loadTargetRepo( repodir/"system" );
76   test.loadRepo( repodir/"repo", "repo" );
77
78   ResPool pool( ResPool::instance() );
79   PoolItem ins( piFind( "dutest", "1.0", true ) );
80   PoolItem up1( piFind( "dutest", "1.0" ) );
81   PoolItem up2( piFind( "dutest", "2.0" ) );
82   PoolItem up3( piFind( "dutest", "3.0" ) );
83
84   DiskUsageCounter duc( { DiskUsageCounter::MountPoint( "/grow", DiskUsageCounter::MountPoint::Hint_growonly ),
85                           DiskUsageCounter::MountPoint( "/norm" ) } );
86   //XLOG( duc, pool );
87
88   BOOST_CHECK_EQUAL( getSize( duc, ins ), mkByteSet(  5,  5 ) );
89   BOOST_CHECK_EQUAL( getSize( duc, up1 ), mkByteSet( 15, 15 ) );
90   BOOST_CHECK_EQUAL( getSize( duc, up2 ), mkByteSet( 45, 45 ) );
91   BOOST_CHECK_EQUAL( getSize( duc, up3 ), mkByteSet(  0,  0 ) );
92
93   // delete installed           size  5                g   n
94   ins.status().setTransact( true, ResStatus::USER );
95   BOOST_CHECK_EQUAL( getSize( duc, pool ), mkByteSet(  0, -5 ) );
96   ins.status().setTransact( false, ResStatus::USER );
97
98   // install known DU           size 15                g   n
99   up1.status().setTransact( true, ResStatus::USER );
100   BOOST_CHECK_EQUAL( getSize( duc, pool ), mkByteSet( 15, 15 ) );       // (multi)install (old stays)
101   ins.status().setTransact( true, ResStatus::USER );
102   BOOST_CHECK_EQUAL( getSize( duc, pool ), mkByteSet( 15, 10 ) );       // update (old goes)
103   ins.status().setTransact( false, ResStatus::USER );
104   up1.status().setTransact( false, ResStatus::USER );
105
106   // install unknown DU         size 0/installed       g   n
107   up3.status().setTransact( true, ResStatus::USER );
108   BOOST_CHECK_EQUAL( getSize( duc, pool ), mkByteSet(  5,  0 ) );       // (multi)install (n could be 5 too, but satsolver does not know about multinstall)
109   ins.status().setTransact( true, ResStatus::USER );
110   BOOST_CHECK_EQUAL( getSize( duc, pool ), mkByteSet(  5,  0 ) );       // update (old goes)
111   ins.status().setTransact( false, ResStatus::USER );
112   up3.status().setTransact( false, ResStatus::USER );
113 }