testUfs.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #include "squid.h"
10 #include "DiskIO/DiskIOModule.h"
11 #include "fs/ufs/UFSSwapDir.h"
12 #include "globals.h"
13 #include "HttpHeader.h"
14 #include "HttpReply.h"
15 #include "MemObject.h"
16 #include "RequestFlags.h"
17 #include "SquidConfig.h"
18 #include "Store.h"
19 #include "store/Disks.h"
20 #include "testStoreSupport.h"
21 #include "testUfs.h"
22 #include "unitTestMain.h"
23 
24 #include <stdexcept>
25 
26 #define TESTDIR "testUfs_Store"
27 
29 
31 extern REMOVALPOLICYCREATE createRemovalPolicy_lru; /* XXX fails with --enable-removal-policies=heap */
32 
33 static void
35 {
39 }
40 
41 /* TODO make this a cbdata class */
42 
43 static bool cbcalled;
44 
45 static void
47 {
48  cbcalled = true;
49 }
50 
51 void
53 {
54  static bool inited = false;
55 
56  if (inited)
57  return;
58 
59  Config.Store.avgObjectSize = 1024;
61  Config.Store.maxObjectSize = 2048;
62 
63  Config.store_dir_select_algorithm = xstrdup("round-robin");
64 
66  Config.replPolicy->type = xstrdup("lru");
67 
68  Config.memShared.defaultTo(false);
69 
70  /* garh garh */
72 
73  Mem::Init();
74 
75  comm_init();
76 
77  httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
78 
79  inited = true;
80 }
81 
82 void
84 {
85  /* test sequence
86  * make a valid working ufs swapdir
87  * put two entries in it and sync logs
88  * search the ufs dir
89  * check the entries we find are what we want
90  */
91 
92  if (0 > system ("rm -rf " TESTDIR))
93  throw std::runtime_error("Failed to clean test work directory");
94 
95  Store::Init();
96 
97  MySwapDirPointer aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
98 
99  aStore->IO = new Fs::Ufs::UFSStrategy(DiskIOModule::Find("Blocking")->createStrategy());
100 
101  addSwapDir(aStore);
102 
103  commonInit();
105 
106  char *path=xstrdup(TESTDIR);
107 
108  char *config_line=xstrdup("100 1 1");
109 
110  visible_appname_string = xstrdup(PACKAGE "/" VERSION);
111 
112  ConfigParser::SetCfgLine(config_line);
113 
114  aStore->parse(0, path);
115  store_maxobjsize = 1024*1024*2;
116 
117  safe_free(path);
118 
119  safe_free(config_line);
120 
121  /* ok, ready to create */
122  aStore->create();
123 
124  /* ok, ready to use - inits store & hash too */
125  Store::Root().init();
126 
127  /* our swapdir must be scheduled to rebuild */
128  CPPUNIT_ASSERT_EQUAL(2, StoreController::store_dirs_rebuilding);
129 
130  /* rebuild is a scheduled event */
131  StockEventLoop loop;
132 
134  loop.runOnce();
135 
136  /* cannot use loop.run(); as the loop will never idle: the store-dir
137  * clean() scheduled event prevents it
138  */
139 
140  /* nothing left to rebuild */
141  CPPUNIT_ASSERT_EQUAL(0, StoreController::store_dirs_rebuilding);
142 
143  /* add an entry */
144  {
145  /* Create "vary" base object */
146  RequestFlags flags;
147  flags.cachable = true;
148  StoreEntry *pe = storeCreateEntry("dummy url", "dummy log url", flags, Http::METHOD_GET);
149  HttpReply *rep = (HttpReply *) pe->getReply(); // bypass const
150  rep->setHeaders(Http::scOkay, "dummy test object", "x-squid-internal/test", 0, -1, squid_curtime + 100000);
151 
152  pe->setPublicKey();
153 
154  pe->buffer();
155  pe->getReply()->packHeadersInto(pe);
156  pe->flush();
157  pe->timestampsSet();
158  pe->complete();
159  pe->swapOut();
160  CPPUNIT_ASSERT_EQUAL(0, pe->swap_dirn);
161  CPPUNIT_ASSERT_EQUAL(0, pe->swap_filen);
162  pe->unlock("testUfs::testUfsSearch vary");
163  }
164 
166 
167  /* here we cheat: we know that UFSSwapDirs search off disk. If we did an init call to a new
168  * swapdir instance, we'd not be testing a clean build.
169  */
170  StoreSearchPointer search = Store::Root().search(); /* search for everything in the store */
171 
172  /* nothing should be immediately available */
173 #if 0
174 
175  CPPUNIT_ASSERT_EQUAL(false, search->next());
176 #endif
177 
178  CPPUNIT_ASSERT_EQUAL(false, search->error());
179  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
180  CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
181 
182  /* trigger a callback */
183  cbcalled = false;
184  search->next(searchCallback, NULL);
185  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
186 
187  /* we should have access to a entry now, that matches the entry we had before */
188  //CPPUNIT_ASSERT_EQUAL(false, search->next());
189  CPPUNIT_ASSERT_EQUAL(false, search->error());
190  CPPUNIT_ASSERT_EQUAL(false, search->isDone());
191  CPPUNIT_ASSERT(search->currentItem() != NULL);
192 
193  /* trigger another callback */
194  cbcalled = false;
195  search->next(searchCallback, NULL);
196  CPPUNIT_ASSERT_EQUAL(true, cbcalled);
197 
198  /* now we should have no error, we should have finished and have no current item */
199  //CPPUNIT_ASSERT_EQUAL(false, search->next());
200  CPPUNIT_ASSERT_EQUAL(false, search->error());
201  CPPUNIT_ASSERT_EQUAL(true, search->isDone());
202  CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), search->currentItem());
203 
205 
207 
208  /* todo: here we should test a dirty rebuild */
209 
211  delete Config.replPolicy;
212 
213  if (0 > system ("rm -rf " TESTDIR))
214  throw std::runtime_error("Failed to clean test work directory");
215 }
216 
217 /* The UFS store should always configure an IO engine even if none is
218  * supplied on the configuration line.
219  */
220 void
222 {
223  /* boring common test boilerplate */
224  if (0 > system ("rm -rf " TESTDIR))
225  throw std::runtime_error("Failed to clean test work directory");
226 
227  // This assertion may fail if previous test cases fail.
228  // Apparently, CPPUNIT_ASSERT* failure may prevent destructors of local
229  // objects such as "StorePointer aRoot" from being called.
230  CPPUNIT_ASSERT(!store_table); // or StoreHashIndex ctor will abort below
231 
232  Store::Init();
233  MySwapDirPointer aStore (new Fs::Ufs::UFSSwapDir("ufs", "Blocking"));
234  addSwapDir(aStore);
235  commonInit();
237  Config.replPolicy->type = xstrdup("lru");
239 
240  char *path=xstrdup(TESTDIR);
241  char *config_line=xstrdup("100 1 1");
242  ConfigParser::SetCfgLine(config_line);
243  aStore->parse(0, path);
244  safe_free(path);
245  safe_free(config_line);
246  CPPUNIT_ASSERT(aStore->IO->io != NULL);
247 
251  delete Config.replPolicy;
252 
253  if (0 > system ("rm -rf " TESTDIR))
254  throw std::runtime_error("Failed to clean test work directory");
255 }
256 
RemovalPolicy * createRemovalPolicy(RemovalPolicySettings *settings)
Definition: store.cc:1799
sdirno swap_dirn
Definition: Store.h:179
void storeReplAdd(const char *type, REMOVALPOLICYCREATE *create)
Definition: store.cc:1773
void Init(Controller *root=nullptr)
initialize the storage module; a custom root is used by unit tests only
Definition: Controller.cc:626
virtual void next(void(callback)(void *cbdata), void *cbdata)=0
StoreSearch * search()
Definition: Controller.cc:205
static int store_dirs_rebuilding
the number of cache_dirs being rebuilt; TODO: move to Disks::Rebuilding
Definition: Controller.h:90
hash_table * store_table
int unlock(const char *context)
Definition: store.cc:485
Definition: cbdata.cc:60
virtual StoreEntry * currentItem()=0
virtual bool isDone() const =0
bool timestampsSet()
Definition: store.cc:1525
#define xstrdup
void setPublicKey(const KeyScope keyScope=ksDefault)
Definition: store.cc:623
RemovalPolicy * mem_policy
Definition: MemObject.cc:45
virtual void buffer()
Definition: store.cc:1707
RefCount< Fs::Ufs::UFSSwapDir > MySwapDirPointer
Definition: testUfs.cc:30
int64_t store_maxobjsize
#define safe_free(x)
Definition: xalloc.h:73
YesNoNone memShared
whether the memory cache is shared among workers
Definition: SquidConfig.h:79
virtual void flush()
Definition: store.cc:1718
struct SquidConfig::@110 Store
sfileno swap_filen
unique ID inside a cache_dir for swapped out entries; -1 for others
Definition: Store.h:177
void testUfsDefaultEngine()
Definition: testUfs.cc:221
Controller & Root()
safely access controller singleton
Definition: Controller.cc:619
int64_t maxObjectSize
Definition: SquidConfig.h:262
time_t squid_curtime
Definition: stub_time.cc:17
static void addSwapDir(MySwapDirPointer aStore)
Definition: testUfs.cc:34
RemovalPolicy * REMOVALPOLICYCREATE(wordlist *args)
Definition: RemovalPolicy.h:80
virtual void complete()
Definition: store.cc:1088
int64_t avgObjectSize
Definition: SquidConfig.h:261
static DiskIOModule * Find(char const *type)
static void SetCfgLine(char *line)
Set the configuration file line to parse.
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition: HttpReply.cc:151
StoreEntry * storeCreateEntry(const char *url, const char *logUrl, const RequestFlags &flags, const HttpRequestMethod &method)
Definition: store.cc:800
static void searchCallback(void *cbdata)
Definition: testUfs.cc:46
virtual HttpReply const * getReply() const
Definition: store.cc:1742
void Init()
Definition: old_api.cc:412
void comm_init(void)
Definition: comm.cc:1211
int storeDirWriteCleanLogs(int reopen)
Definition: Disks.cc:572
void free_cachedir(Store::DiskConfig *swap)
Definition: Disks.cc:677
int objectsPerBucket
Definition: SquidConfig.h:260
void allocate_new_swapdir(Store::DiskConfig *swap)
Definition: Disks.cc:660
static bool cbcalled
Definition: testUfs.cc:43
void httpHeaderInitModule(void)
Definition: HttpHeader.cc:119
RefCount< SwapDir > * swapDirs
Definition: SquidConfig.h:58
REMOVALPOLICYCREATE createRemovalPolicy_lru
DiskIOStrategy * io
Definition: UFSStrategy.h:53
void FreeMemory()
undo Init()
Definition: Controller.cc:632
RemovalPolicySettings * replPolicy
Definition: SquidConfig.h:89
Store::DiskConfig cacheSwap
Definition: SquidConfig.h:418
#define TESTDIR
Definition: testUfs.cc:26
#define VERSION
virtual void parse(int index, char *path) override
Definition: UFSSwapDir.cc:185
void packHeadersInto(Packable *p) const
Definition: HttpReply.cc:85
bool runOnce()
Definition: EventLoop.cc:89
virtual void init() override
Definition: Controller.cc:58
virtual bool error() const =0
Fs::Ufs::UFSStrategy * IO
Definition: UFSSwapDir.h:79
void swapOut()
CPPUNIT_TEST_SUITE_REGISTRATION(testUfs)
char const * visible_appname_string
C * getRaw() const
Definition: RefCount.h:74
class SquidConfig Config
Definition: SquidConfig.cc:12
#define NULL
Definition: types.h:166
void commonInit()
Definition: testUfs.cc:52
char * store_dir_select_algorithm
Definition: SquidConfig.h:503
virtual void create() override
create system resources needed for this store to operate in the future
Definition: UFSSwapDir.cc:305
void testUfsSearch()
Definition: testUfs.cc:83
void defaultTo(bool beSet)
enables or disables the option; updating to 'implicit' state
Definition: YesNoNone.h:58

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors