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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors