summaryrefslogtreecommitdiffstats
path: root/tests/events_test.py
blob: e7745448ded4de6b0b070a801f30a7a144e7f0ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# -*- coding: utf-8 -*-
#
#    Copyright (C) 2016 Dr. Robert C. Green II.
#
#    This file is part of Combox.
#
#   Combox is free software: you can redistribute it and/or modify it
#   under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   Combox is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with Combox (see COPYING).  If not, see
#   <http://www.gnu.org/licenses/>.

import os
import time
import yaml

from filecmp import cmp
from glob import glob
from os import path, remove
from shutil import copyfile
from threading import Lock

from nose.tools import *
from watchdog.observers import Observer

from combox.config import get_nodedirs
from combox.crypto import (decrypt_and_glue, split_and_encrypt,
                           encrypt_shards)
from combox.events import ComboxDirMonitor, NodeDirMonitor
from combox.file import (relative_path, purge_dir, hash_file,
                         read_file, write_file, move_shards,
                         rm_shards, mk_nodedir, rm_nodedir,
                         move_nodedir, node_paths, rm_path,
                         split_data, write_shards)

from combox.silo import ComboxSilo
from tests.utils import (get_config, shardedp, dirp, renamedp,
                         path_deletedp, rm_nodedirs, rm_configdir,
                         purge_nodedirs, purge, not_shardedp)


class TestEvents(object):
    """
    Class that tests the events.py module.
    """

    @classmethod
    def setup_class(self):
        """Set things up."""

        self.silo_lock = Lock()
        self.monitor_lock = Lock()
        self.config = get_config()
        self.silo = ComboxSilo(self.config, self.silo_lock)

        self.FILES_DIR = self.config['combox_dir']
        self.NODE_DIR = get_nodedirs(self.config)[0]
        self.TEST_FILE = path.join(self.FILES_DIR, 'thgttg-21st.png')

        self.lorem = path.join(self.FILES_DIR, 'lorem.txt')
        self.ipsum = path.join(self.FILES_DIR, 'ipsum.txt')
        self.lorem_moved = path.join(self.FILES_DIR, 'lorem.moved.txt')
        self.lorem_ipsum = path.join(self.FILES_DIR, 'lorem.ipsum.txt')

        # files that have to be removed at the end of this test class
        # should be appended to this list.
        self.purge_list = []

    def test_CDM(self):
        """
        Tests the ComboxDirMonitor class.
        """

        event_handler = ComboxDirMonitor(self.config, self.silo_lock, self.monitor_lock)
        observer = Observer()
        observer.schedule(event_handler, self.FILES_DIR, recursive=True)
        observer.start()

        # Test - new file addition
        self.TEST_FILE_COPY_0 = "%s.mutant" % self.TEST_FILE
        copyfile(self.TEST_FILE, self.TEST_FILE_COPY_0)
        ## wait for ComboxDirMonitor to split and scatter the file in the
        ## node directories.
        time.sleep(2)
        ## check if the shards were created.
        shardedp(self.TEST_FILE_COPY_0)
        ## check if the new file's info is in silo
        silo = ComboxSilo(self.config, self.silo_lock)
        assert silo.exists(self.TEST_FILE_COPY_0)

        # Test - tmp file creation.
        # combox must ignore it.
        self.TEST_TMP_FILE = "%s~" % self.TEST_FILE
        copyfile(self.TEST_FILE, self.TEST_TMP_FILE)
        # wait for a second.
        time.sleep(2)
        ## confirm that shards were not created.
        not_shardedp(self.TEST_TMP_FILE)
        ## confirm that it did not get registered in the silo.
        assert not silo.exists(self.TEST_TMP_FILE)
        # purge it later.
        self.purge_list.append(self.TEST_TMP_FILE)

        # Test - File deletion.
        remove(self.TEST_FILE_COPY_0)
        time.sleep(1)
        path_deletedp(self.TEST_FILE_COPY_0)
        ## check if the new file's info is removed from silo
        silo = ComboxSilo(self.config, self.silo_lock)
        assert not silo.exists(self.TEST_FILE_COPY_0)

        # Test - directory creation
        self.TEST_DIR_0 = path.join(self.FILES_DIR, 'foo')
        os.mkdir(self.TEST_DIR_0)
        time.sleep(2)
        ## check if TEST_DIR_0 is created under node directories.
        dirp(self.TEST_DIR_0)

        self.TEST_DIR_1 = path.join(self.TEST_DIR_0, 'bar')
        os.mkdir(self.TEST_DIR_1)
        time.sleep(2)
        ## check if TEST_DIR_1 is created under node directories.
        dirp(self.TEST_DIR_1)

        # Test - new file in a nested directory
        self.TEST_FILE_COPY_1 = path.join(self.TEST_DIR_1,
                                          path.basename(self.TEST_FILE))
        copyfile(self.TEST_FILE, self.TEST_FILE_COPY_1)
        time.sleep(2)
        shardedp(self.TEST_FILE_COPY_1)

        # Test - dir rename
        self.TEST_DIR_1_NEW = path.join(path.dirname(self.TEST_DIR_1),
                                        'snafu')
        self.TEST_FILE_COPY_1_NEW = path.join(self.TEST_DIR_1_NEW,
                                         path.basename(self.TEST_FILE))

        os.rename(self.TEST_DIR_1, self.TEST_DIR_1_NEW)
        time.sleep(3)
        renamedp(self.TEST_DIR_1, self.TEST_DIR_1_NEW)
        renamedp(self.TEST_FILE_COPY_1, self.TEST_FILE_COPY_1_NEW)
        ## check if the new file's info is updated in silo
        silo = ComboxSilo(self.config, self.silo_lock)
        assert not silo.exists(self.TEST_FILE_COPY_1)
        assert silo.exists(self.TEST_FILE_COPY_1_NEW)

        # Test directory & file deletion
        purge_dir(self.TEST_DIR_0)
        # remove the directory itself.
        os.rmdir(self.TEST_DIR_0)
        time.sleep(2)
        path_deletedp(self.TEST_FILE_COPY_1_NEW)
        path_deletedp(self.TEST_DIR_1, True)
        path_deletedp(self.TEST_DIR_0, True)


        # Test - file modification
        self.lorem_file = path.join(self.FILES_DIR, 'lorem.txt')
        self.lorem_file_copy = "%s.copy" % self.lorem_file
        # this will shard lorem.txt.copy in the node directories.
        copyfile(self.lorem_file, self.lorem_file_copy)
        time.sleep(2)
        shardedp(self.lorem_file_copy)
        ## check if the lorem_file_copy's info is stored in silo
        silo = ComboxSilo(self.config, self.silo_lock)
        lorem_file_copy_hash = silo.db.get(self.lorem_file_copy)

        self.ipsum_file = path.join(self.FILES_DIR, 'ipsum.txt')
        ipsum_content = read_file(self.ipsum_file)
        lorem_copy_content = read_file(self.lorem_file_copy)
        lorem_copy_content = "%s\n%s" % (lorem_copy_content, ipsum_content)

        # write lorem's new content to  lorem_file_copy
        write_file(self.lorem_file_copy, lorem_copy_content)
        time.sleep(2)
        ## check if the lorem_file_copy's info is updated in silo
        silo = ComboxSilo(self.config, self.silo_lock)
        assert lorem_file_copy_hash != silo.db.get(self.lorem_file_copy)


        # decrypt_and_glue will decrypt the file shards, glues them and
        # writes it to the respective file
        decrypt_and_glue(self.lorem_file_copy, self.config)
        time.sleep(2)

        lorem_content_from_disk = read_file(self.lorem_file_copy)
        assert lorem_copy_content == lorem_content_from_disk

        # remove lorem_file_copy and confirm that its shards are deleted
        # in the node directories.
        remove(self.lorem_file_copy)
        time.sleep(2)
        path_deletedp(self.lorem_file_copy)
        ## check if the lorem_file_copy's info is deleted from silo
        silo = ComboxSilo(self.config, self.silo_lock)
        assert not silo.exists(self.lorem_file_copy)

        observer.stop()
        observer.join()


    def test_CDM_housekeep(self):
        """ComboxDirMonitor's housekeep method test."""

        # test file deletion and addition
        os.rename(self.lorem, self.lorem_moved)

        cdm = ComboxDirMonitor(self.config, self.silo_lock, self.monitor_lock)
        cdm.housekeep()

        silo = ComboxSilo(self.config, self.silo_lock)
        assert not silo.exists(self.lorem)
        assert silo.exists(self.lorem_moved)
        shardedp(self.lorem_moved)

        ##1
        os.rename(self.lorem_moved, self.lorem)
        rm_shards(self.lorem_moved, self.config)
        silo.remove(self.lorem_moved)

        # test file modification
        copyfile(self.lorem, self.lorem_ipsum)
        assert path.exists(self.lorem_ipsum)

        cdm = ComboxDirMonitor(self.config, self.silo_lock, self.monitor_lock)
        cdm.housekeep()

        silo = ComboxSilo(self.config, self.silo_lock)
        assert silo.exists(self.lorem_ipsum)

        ipsum_content = read_file(self.ipsum)

        lorem_ipsum_content = read_file(self.lorem_ipsum)
        lorem_ipsum_content = "%s\n%s" % (lorem_ipsum_content, ipsum_content)
        write_file(self.lorem_ipsum, lorem_ipsum_content)

        cdm.housekeep()

        silo = ComboxSilo(self.config, self.silo_lock)
        assert not silo.stale(self.lorem_ipsum)


    def test_NDM_numnodes(self):
        """Tests whether the NodeDirMonitor's num_nodes variable has the
        right value.

        """
        nmonitor = NodeDirMonitor(self.config, self.silo_lock,
                                  self.monitor_lock)
        assert_equal(2, nmonitor.num_nodes)


    def test_NDM_oncreated(self):
        """Testing on_created method in NodeDirMonitor"""
        nodes =  get_nodedirs(self.config)
        num_nodes =  len(get_nodedirs(self.config))

        nmonitors = []
        observers = []

        # create an observer for each node directory and make it
        # monitor them.
        for node in nodes:
            nmonitor = NodeDirMonitor(self.config, self.silo_lock,
                                      self.monitor_lock)
            observer = Observer()
            observer.schedule(nmonitor, node, recursive=True)
            observer.start()

            nmonitors.append(nmonitor)
            observers.append(observer)

        # Test - new file addition, when shard is created in node_dirs
        self.TEST_FILE_MUTANT = "%s.mutant" % self.TEST_FILE

        fmutant_content = read_file(self.TEST_FILE)

        split_and_encrypt(self.TEST_FILE_MUTANT, self.config,
                          fmutant_content)
        ## wait for NodeDirMonitor to reconstruct the shards and put
        ## it in combox directory
        time.sleep(2)
        assert fmutant_content == read_file(self.TEST_FILE_MUTANT)
        ## check if the new file's info is in silo
        assert self.silo.exists(self.TEST_FILE_MUTANT)
        assert_equal(None, self.silo.node_get('file_created',
                                              self.TEST_FILE_MUTANT))

        self.purge_list.append(self.TEST_FILE_MUTANT)

        # Test - directory creation
        self.FOO_DIR = path.join(self.FILES_DIR, 'foo')
        mk_nodedir(self.FOO_DIR, self.config)
        time.sleep(2)
        ## check if FOO_DIR is created under the combox directory
        assert path.isdir(self.FOO_DIR)
        assert_equal(None, self.silo.node_get('file_created',
                                              self.FOO_DIR))
        self.BAR_DIR = path.join(self.FOO_DIR, 'bar')
        mk_nodedir(self.BAR_DIR, self.config)
        time.sleep(2)
        ## check if BAR_DIR is created under the combox directory.
        assert path.isdir(self.BAR_DIR)
        assert_equal(None, self.silo.node_get('file_created',
                                              self.BAR_DIR))

        self.purge_list.append(self.FOO_DIR)

        # stop the zarking observers.
        for i in range(num_nodes):
            observers[i].stop()
            observers[i].join()


    def test_NDM_onmodified(self):
        """Testing on_modified method in NodeDirMonitor"""
        nodes =  get_nodedirs(self.config)
        num_nodes =  len(get_nodedirs(self.config))

        nmonitors = []
        observers = []

        # create an observer for each node directory and make it
        # monitor them.
        for node in nodes:
            nmonitor = NodeDirMonitor(self.config, self.silo_lock,
                                      self.monitor_lock)
            observer = Observer()
            observer.schedule(nmonitor, node, recursive=True)
            observer.start()

            nmonitors.append(nmonitor)
            observers.append(observer)

        # Test - shard modification
        self.lorem_file = path.join(self.FILES_DIR, 'lorem.txt')
        lorem_content = read_file(self.lorem_file)
        self.lorem_file_copy = "%s.copy" % self.lorem_file

        copyfile(self.lorem_file, self.lorem_file_copy)
        split_and_encrypt(self.lorem_file_copy, self.config,
                          lorem_content)

        time.sleep(2)
        self.silo.update(self.lorem_file_copy)
        shardedp(self.lorem_file_copy)

        self.silo.reload()
        lorem_file_copy_hash = self.silo.db.get(self.lorem_file_copy)

        self.ipsum_file = path.join(self.FILES_DIR, 'ipsum.txt')
        ipsum_content = read_file(self.ipsum_file)
        lorem_copy_content = "%s\n%s" % (lorem_content, ipsum_content)

        split_and_encrypt(self.lorem_file_copy, self.config,
                          lorem_copy_content)
        time.sleep(2)
        assert lorem_copy_content == read_file(self.lorem_file_copy)

        ## check if the lorem_file_copy's info is updated in silo
        self.silo.reload()
        assert lorem_file_copy_hash != self.silo.db.get(self.lorem_file_copy)
        assert_equal(None, self.silo.node_get('file_modified',
                                              self.lorem_file_copy))

        self.purge_list.append(self.lorem_file_copy)

        # stop the zarking observers.
        for i in range(num_nodes):
            observers[i].stop()
            observers[i].join()


    def test_NDM_ondeleted(self):
        """Testing on_delete method in NodeDirMonitor"""
        nodes =  get_nodedirs(self.config)
        num_nodes =  len(get_nodedirs(self.config))

        nmonitors = []
        observers = []

        # create an observer for each node directory and make it
        # monitor them.
        for node in nodes:
            nmonitor = NodeDirMonitor(self.config, self.silo_lock,
                                      self.monitor_lock)
            observer = Observer()
            observer.schedule(nmonitor, node, recursive=True)
            observer.start()

            nmonitors.append(nmonitor)
            observers.append(observer)

        BAR_DIR = path.join(self.FILES_DIR, 'bar')
        mk_nodedir(BAR_DIR, self.config)
        # wait for the `bar' directory to be created inside combox
        # directory.
        time.sleep(2)

        # Test - directory deletion inside node directory.
        rm_nodedir(BAR_DIR, self.config)
        time.sleep(2)
        assert not path.exists(BAR_DIR)

        the_guide = path.join(self.FILES_DIR, 'the.guide')
        split_and_encrypt(the_guide, self.config,
                          read_file(self.TEST_FILE))
        time.sleep(2)
        assert path.exists(the_guide)

        # Test - Shard deletion.
        rm_shards(the_guide, self.config)
        time.sleep(5)
        assert not path.exists(the_guide)

        ## check if the new file's info is removed from silo
        silo = ComboxSilo(self.config, self.silo_lock)
        assert not silo.exists(the_guide)

        self.purge_list.append(BAR_DIR)
        self.purge_list.append(the_guide)

        # stop the zarking observers.
        for i in range(num_nodes):
            observers[i].stop()
            observers[i].join()


    def test_GoogleDrive_file_modify(self):
        """Simulates Google Drive client's file modification behavior and
        checks if combox is interpreting it properly.
        """

        nodes =  get_nodedirs(self.config)
        num_nodes =  len(get_nodedirs(self.config))

        nmonitors = []
        observers = []

        # create an observer for each node directory and make it
        # monitor them.
        for node in nodes:
            nmonitor = NodeDirMonitor(self.config, self.silo_lock,
                                      self.monitor_lock)
            observer = Observer()
            observer.schedule(nmonitor, node, recursive=True)
            observer.start()

            nmonitors.append(nmonitor)
            observers.append(observer)

        # Test - shard modification
        lorem_content = read_file(self.lorem)
        self.lorem_copy = "%s.copy" % self.lorem

        copyfile(self.lorem, self.lorem_copy)
        split_and_encrypt(self.lorem_copy, self.config,
                          lorem_content)
        self.silo.update(self.lorem_copy)
        shardedp(self.lorem_copy)

        lorem_copy_hash = self.silo.db.get(self.lorem_copy)

        ipsum_content = read_file(self.ipsum)
        lorem_copy_content = "%s\n%s" % (lorem_content, ipsum_content)

        time.sleep(2)

        # Modify shards in the first n-1 node directories in the usual
        # way. For the nth node directory simulate Google Drive
        # official client's way of modifiying the shard.

        rel_path = relative_path(self.lorem_copy, self.config)

        # no. of shards = no. of nodes.
        SHARDS = len(self.config['nodes_info'].keys())

        f_shards = split_data(lorem_copy_content, SHARDS)

        # encrypt shards
        ciphered_shards = encrypt_shards(f_shards, self.config['topsecret'])

        # write ciphered shards to disk
        f_basename =  rel_path
        # gets the list of node' directories.
        nodes = get_nodedirs(self.config)
        last_node_index = len(nodes) - 1

        nodes_subset = nodes[:last_node_index]
        last_node = nodes[last_node_index]

        # write n-1 shards to the first n-1 node directories
        write_shards(ciphered_shards, nodes_subset, f_basename)


        # now for the nth shard, simulate Google Drive's client
        # behavior.
        last_shard_path = "%s.shard%d" % (path.join(last_node, f_basename),
                                          last_node_index)
        # remove the shard first
        rm_path(last_shard_path)
        # write the latest version of the shard
        write_file(last_shard_path, ciphered_shards[last_node_index])
        time.sleep(3)

        self.silo.reload()
        assert lorem_copy_content == read_file(self.lorem_copy)

        ## check if the lorem_copy's info is updated in silo
        assert lorem_copy_hash != self.silo.db.get(self.lorem_copy)
        assert_equal(None, self.silo.node_get('file_modified',
                                                  self.lorem_copy))

        self.purge_list.append(self.lorem_copy)

        # stop the zarking observers.
        for i in range(num_nodes):
            observers[i].stop()
            observers[i].join()


    def untest_NDM(self):
        """
        Tests the NodeDirMonitor class.
        """

        event_handler = NodeDirMonitor(self.config, self.silo_lock,
                                       self.monitor_lock)
        observer = Observer()
        observer.schedule(event_handler, self.NODE_DIR, recursive=True)
        observer.start()

        ####
        # Test - new file addition, when shard is created in node_dirs
        self.TEST_FILE_MUTANT = "%s.mutant" % self.TEST_FILE

        fmutant_content = read_file(self.TEST_FILE)

        split_and_encrypt(self.TEST_FILE_MUTANT, self.config,
                          fmutant_content)
        ## wait for NodeDirMonitor to reconstruct the shards and put
        ## it in combox directory
        time.sleep(1)
        assert fmutant_content == read_file(self.TEST_FILE_MUTANT)
        ## check if the new file's info is in silo
        silo = ComboxSilo(self.config, self.silo_lock)
        assert silo.exists(self.TEST_FILE_MUTANT)
        ####

        # Test - Shard deletion.
        rm_shards(self.TEST_FILE_MUTANT, self.config)
        time.sleep(1)
        assert not path.exists(self.TEST_FILE_MUTANT)

        ## check if the new file's info is removed from silo
        silo = ComboxSilo(self.config, self.silo_lock)
        assert not silo.exists(self.TEST_FILE_MUTANT)

        # Test - directory deletion inside node directory
        rm_nodedir(self.BAR_DIR, self.config)
        time.sleep(1)
        assert not path.exists(self.BAR_DIR)

        # Test - shard modification
        self.lorem_file = path.join(self.FILES_DIR, 'lorem.txt')
        lorem_content = read_file(self.lorem_file)
        self.lorem_file_copy = "%s.copy" % self.lorem_file

        copyfile(self.lorem_file, self.lorem_file_copy)
        split_and_encrypt(self.lorem_file_copy, self.config,
                          lorem_content)

        silo = ComboxSilo(self.config, self.silo_lock)
        silo.update(self.lorem_file_copy)
        shardedp(self.lorem_file_copy)

        silo = ComboxSilo(self.config, self.silo_lock)
        lorem_file_copy_hash = silo.db.get(self.lorem_file_copy)

        self.ipsum_file = path.join(self.FILES_DIR, 'ipsum.txt')
        ipsum_content = read_file(self.ipsum_file)
        lorem_copy_content = "%s\n%s" % (lorem_content, ipsum_content)

        split_and_encrypt(self.lorem_file_copy, self.config,
                          lorem_copy_content)
        time.sleep(1)
        ## check if the lorem_file_copy's info is updated in silo
        silo = ComboxSilo(self.config, self.silo_lock)

        assert lorem_copy_content == read_file(self.lorem_file_copy)
        assert lorem_file_copy_hash != silo.db.get(self.lorem_file_copy)

        self.purge_list.append(self.lorem_file_copy)

        observer.stop()
        observer.join()


    def test_NDM_onmoved(self):
        """Testing on_moved method in NodeDirMonitor"""

        nodes =  get_nodedirs(self.config)
        num_nodes =  len(get_nodedirs(self.config))

        nmonitors = []
        observers = []

        # create an observer for each node directory and make it
        # monitor them.
        for node in nodes:
            nmonitor = NodeDirMonitor(self.config, self.silo_lock,
                                      self.monitor_lock)
            observer = Observer()
            observer.schedule(nmonitor, node, recursive=True)
            observer.start()

            nmonitors.append(nmonitor)
            observers.append(observer)

        # event_handler = NodeDirMonitor(self.config, self.silo_lock,
        #                                self.monitor_lock)
        # observer = Observer()
        # observer.schedule(event_handler, self.NODE_DIR, recursive=True)
        # observer.start()

        self.testf = "%s.onm" % self.TEST_FILE
        copyfile(self.TEST_FILE, self.testf)

        silo = ComboxSilo(self.config, self.silo_lock)
        silo.update(self.testf)

        split_and_encrypt(self.testf, self.config)
        time.sleep(1)

        self.testf_moved = "%s.onm.moved" % self.TEST_FILE

        # test file move/rename
        move_shards(self.testf, self.testf_moved, self.config)
        time.sleep(1)
        assert path.isfile(self.testf_moved)
        assert silo.exists(self.testf_moved)
        assert not silo.exists(self.testf)

        # test directory move/rename
        dirt = path.join(self.FILES_DIR, "fom")
        os.mkdir(dirt)
        mk_nodedir(dirt, self.config)

        dirt_lorem = path.join(dirt, "lorem.txt")
        copyfile(self.lorem, dirt_lorem)
        split_and_encrypt(dirt_lorem, self.config)
        time.sleep(1)

        silo.update(dirt_lorem)

        dirt_m = path.join(self.FILES_DIR, "mof")
        dirt_m_lorem = path.join(dirt_m, "lorem.txt")
        move_nodedir(dirt, dirt_m, self.config)
        time.sleep(1)

        assert path.isdir(dirt_m)
        assert path.isfile(dirt_m_lorem)
        assert not path.isdir(dirt)
        assert not path.isdir(dirt_lorem)

        assert silo.exists(dirt_m_lorem)
        assert not silo.exists(dirt_lorem)

        self.purge_list.append(self.testf_moved)
        self.purge_list.append(dirt_m)

        # stop the zarking observers.
        for i in range(num_nodes):
            observers[i].stop()
            observers[i].join()


    def test_NDM_housekeep_delete(self):
        """Testing NodeDirMonitor's housekeep method's delete functionality."""
        # files for testing deletion.
        testf1 = path.join(self.FILES_DIR, 'hitchhikers.png')
        testf2 = path.join(self.FILES_DIR, 'lorem.housekeep')
        copyfile(self.TEST_FILE, testf1)
        copyfile(self.lorem, testf2)

        split_and_encrypt(testf2, self.config)
        testf2_shard0 = node_paths(testf2, self.config, True)[0]
        remove(testf2_shard0)

        silo = ComboxSilo(self.config, self.silo_lock)
        silo.update(testf1)
        silo.update(testf2)

        ndm = NodeDirMonitor(self.config, self.silo_lock,
                             self.monitor_lock)
        ndm.housekeep()

        assert not path.exists(testf1)
        assert path.exists(testf2)

        self.purge_list.append(testf1)
        self.purge_list.append(testf2)


    def test_NDM_housekeep_create(self):
        """Testing NodeDirMonitor's housekeep method's create functionality."""

        # test shard creation
        hmutant = "%s.mutant" % self.TEST_FILE
        hmutant_content = read_file(self.TEST_FILE)
        split_and_encrypt(hmutant, self.config,
                          hmutant_content)

        lorem_c = "%s.c" % self.lorem
        split_and_encrypt(lorem_c, self.config,
                          read_file(self.lorem))
        # delete lorem_c's shard0
        remove(node_paths(lorem_c, self.config, True)[0])

        ndm = NodeDirMonitor(self.config, self.silo_lock,
                             self.monitor_lock)
        ndm.housekeep()

        assert path.exists(hmutant)
        assert hmutant_content == read_file(hmutant)

        assert not path.exists(lorem_c)
        assert_equal(len(get_nodedirs(self.config))-1,
                         self.silo.node_get('file_created', lorem_c))

        self.purge_list.append(hmutant)
        self.purge_list.append(lorem_c)


    def untest_NDM_housekeep(self):
        """Testing NodeDirMonitor's housekeep method."""

        # files for testing deletion.
        testf1 = path.join(self.FILES_DIR, 'hitchhikers.png')
        testf2 = path.join(self.FILES_DIR, 'lorem.housekeep')
        copyfile(self.TEST_FILE, testf1)
        copyfile(self.lorem, testf2)

        silo = ComboxSilo(self.config, self.silo_lock)
        silo.update(testf1)
        silo.update(testf2)

        ndm = NodeDirMonitor(self.config, self.silo_lock,
                             self.monitor_lock)
        ndm.housekeep()

        assert not path.exists(testf1)
        assert not path.exists(testf2)

        self.purge_list.append(testf1)
        self.purge_list.append(testf2)

        # test shard creation
        hmutant = "%s.mutant" % self.TEST_FILE
        hmutant_content = read_file(self.TEST_FILE)

        split_and_encrypt(hmutant, self.config,
                          hmutant_content)

        ndm = NodeDirMonitor(self.config, self.silo_lock,
                             self.monitor_lock)
        ndm.housekeep()

        assert path.exists(hmutant)
        assert hmutant_content == read_file(hmutant)

        self.purge_list.append(hmutant)

        # test shard modification
        lcopy = "%s.copy" % self.lorem
        copyfile(self.lorem, lcopy)
        lcopy_content = read_file(lcopy)
        split_and_encrypt(lcopy, self.config,
                          lcopy_content)

        silo = ComboxSilo(self.config, self.silo_lock)
        silo.update(lcopy)
        shardedp(lcopy)

        silo = ComboxSilo(self.config, self.silo_lock)
        lcopy_hash = silo.db.get(lcopy)

        ipsum_content = read_file(self.ipsum)
        lcopy_content = "%s\n%s" % (lcopy_content, ipsum_content)

        split_and_encrypt(lcopy, self.config,
                          lcopy_content)

        ndm = NodeDirMonitor(self.config, self.silo_lock,
                             self.monitor_lock)
        ndm.housekeep()

        ## check if the lorem_file_copy's info is updated in silo
        silo = ComboxSilo(self.config, self.silo_lock)

        assert lcopy_content == read_file(lcopy)
        assert hash_file(lcopy, lcopy_content) == silo.db.get(lcopy)

        self.purge_list.append(lcopy)


    def test_NDM_shardp(self):
        """Testing shardp method in NodeDirMonitor class"""
        shard = 'some.shard0'
        not_shard = 'some.extension'
        ndm = NodeDirMonitor(self.config, self.silo_lock,
                             self.monitor_lock)

        assert_equal(True, ndm.shardp(shard))
        assert_equal(False, ndm.shardp(not_shard))


    def teardown(self):
        """Cleans up things after each test in this class"""

        purge_nodedirs(self.config)
        self.silo.db.deldb()
        purge(self.purge_list)


    @classmethod
    def teardown_class(self):
        """Purge the mess created by this test"""

        rm_shards(self.TEST_FILE, self.config)
        rm_shards(self.lorem, self.config)
        rm_shards(self.ipsum, self.config)

        rm_path(self.lorem_ipsum)

        rm_nodedirs(self.config)
        rm_configdir()