Completed
Push — master ( 7648fa...b7eb8c )
by Egor
01:22
created

SizeExceedTest.test_crashes()   A

Complexity

Conditions 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.4285
cc 2
1
# coding: utf8
2
3
"""
4
This software is licensed under the Apache 2 license, quoted below.
5
6
Copyright 2014 Crystalnix Limited
7
8
Licensed under the Apache License, Version 2.0 (the "License"); you may not
9
use this file except in compliance with the License. You may obtain a copy of
10
the License at
11
12
    http://www.apache.org/licenses/LICENSE-2.0
13
14
Unless required by applicable law or agreed to in writing, software
15
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
License for the specific language governing permissions and limitations under
18
the License.
19
"""
20
21
from django.test import TestCase, override_settings
22
from django.utils import timezone
23
from django.core.cache import cache
24
25
from crash.factories import CrashFactory
26
from crash.models import Crash, Symbols
27
from feedback.factories import FeedbackFactory
28
from feedback.models import Feedback
29
from omaha.dynamic_preferences_registry import global_preferences_manager as gpm
30
from omaha.limitation import delete_older_than, delete_size_is_exceeded, delete_duplicate_crashes
31
from omaha.limitation import monitoring_size
32
from omaha.factories import VersionFactory
33
from omaha_server.utils import is_private
34
from sparkle.factories import SparkleVersionFactory
35
36
class DeleteOldTest(TestCase):
37
    @is_private()
38
    def test_crashes(self):
39
        old_date = timezone.now() - timezone.timedelta(days=5)
40
        gpm['Crash__limit_storage_days'] = 2
41
        CrashFactory.create_batch(10, created=old_date)
42
        Crash.objects.update(created=old_date)
43
        self.assertEqual(Crash.objects.all().count(), 10)
44
45
        deleted = list(Crash.objects.values_list('id', 'created', 'signature', 'userid', 'appid'))
46
        deleted = map(lambda x: dict(id=x[0], element_created=x[1].strftime("%d. %B %Y %I:%M%p"), signature=x[2],
47
                                     userid=x[3], appid=x[4]), deleted)
48
49
        result = delete_older_than('crash', 'Crash')
50
51
        self.assertDictEqual(result, dict(count=10, size=0, elements=deleted))
52
        self.assertEqual(Crash.objects.all().count(), 0)
53
54
    @is_private()
55
    def test_feedbacks(self):
56
        old_date = timezone.now() - timezone.timedelta(days=5)
57
        gpm['Feedback__limit_storage_days'] = 2
58
        FeedbackFactory.create_batch(10, created=old_date)
59
        Feedback.objects.update(created=old_date)
60
        self.assertEqual(Feedback.objects.all().count(), 10)
61
62
        deleted = list(Feedback.objects.values_list('id', 'created'))
63
        deleted = map(lambda x: dict(id=x[0], element_created=x[1].strftime("%d. %B %Y %I:%M%p")), deleted)
64
65
        result = delete_older_than('feedback', 'Feedback')
66
67
        self.assertDictEqual(result, dict(count=10, size=0, elements=deleted))
68
        self.assertEqual(Feedback.objects.all().count(), 0)
69
70
71
class SizeExceedTest(TestCase):
72
    maxDiff = None
73
74
    @is_private()
75
    def test_crashes(self):
76
        gpm['Crash__limit_size'] = 1
77
        crash_size = 10*1024*1023
78
        CrashFactory.create_batch(200, archive_size=crash_size, minidump_size=0)
79
        self.assertEqual(Crash.objects.all().count(), 200)
80
81
        del_count = 98
82
        deleted = list(Crash.objects.values_list('id', 'created', 'signature', 'userid', 'appid'))[:del_count]
83
        deleted = map(lambda x: dict(id=x[0], element_created=x[1].strftime("%d. %B %Y %I:%M%p"), signature=x[2],
84
                                      userid=x[3], appid=x[4]), deleted)
85
86
        result = delete_size_is_exceeded('crash', 'Crash')
87
88
        self.assertDictEqual(result, dict(count=del_count, size=del_count * crash_size, elements=deleted))
89
        self.assertEqual(Crash.objects.all().count(), 102)
90
91
    @is_private()
92
    def test_feedbacks(self):
93
        gpm['Feedback__limit_size'] = 1
94
        feedback_size = 10*1024*1023
95
        FeedbackFactory.create_batch(200, screenshot_size=feedback_size, system_logs_size=0, attached_file_size=0, blackbox_size=0)
96
        self.assertEqual(Feedback.objects.all().count(), 200)
97
98
        del_count = 98
99
        deleted = list(Feedback.objects.values_list('id', 'created'))
100
        deleted = map(lambda x: dict(id=x[0], element_created=x[1].strftime("%d. %B %Y %I:%M%p")), deleted)[:del_count]
101
102
        result = delete_size_is_exceeded('feedback', 'Feedback')
103
        self.assertDictEqual(result, dict(count=del_count, size=del_count * feedback_size, elements=deleted))
104
        self.assertEqual(Feedback.objects.all().count(), 102)
105
106
107
class DeleteDuplicateTest(TestCase):
108
    @is_private()
109
    def test_crashes(self):
110
        gpm['Crash__duplicate_number'] = 10
111
        CrashFactory.create_batch(25, signature='test1')
112
        self.assertEqual(Crash.objects.filter(signature='test1').count(), 25)
113
        CrashFactory.create_batch(9, signature='test2')
114
        self.assertEqual(Crash.objects.filter(signature='test2').count(), 9)
115
116
        deleted = list(Crash.objects.filter(signature='test1').values_list('id', 'created', 'signature', 'userid', 'appid'))[:15]
117
        deleted = map(lambda x: dict(id=x[0], element_created=x[1].strftime("%d. %B %Y %I:%M%p"), signature=x[2],
118
                                     userid=x[3], appid=x[4]), deleted)
119
120
        result = delete_duplicate_crashes()
121
122
        self.assertDictEqual(result, dict(count=15, size=0, elements=deleted))
123
        self.assertEqual(Crash.objects.filter(signature='test1').count(), gpm['Crash__duplicate_number'])
124
        self.assertEqual(Crash.objects.filter(signature='test2').count(), 9)
125
126
127
@override_settings(CACHES={
128
    'default': {
129
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
130
    }
131
})
132
class MonitoringTest(TestCase):
133
    cache_keys = ['omaha_version_size', 'sparkle_version_size', 'crashes_size', 'feedbacks_size', 'symbols_size']
134
135
    def setUp(self):
136
        for key in self.cache_keys:
137
            cache.delete(key)
138
139
    @is_private()
140
    def test_monitoring(self):
141
        for key in self.cache_keys:
142
            self.assertEqual(cache.get(key, 0), 0)
143
144
        VersionFactory.create(file_size=100)
145
        SparkleVersionFactory.create(file_size=100)
146
        Crash.objects.create(archive_size=80, minidump_size=20)
147
        Symbols.objects.create(file_size=100)
148
        Feedback.objects.create(screenshot_size=25, blackbox_size=25, attached_file_size=25, system_logs_size=25)
149
150
        monitoring_size()
151
152
        for key in self.cache_keys:
153
            self.assertEqual(cache.get(key), 100)
154