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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors