Completed
Push — master ( e271b6...fc740f )
by Egor
01:21
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.4286
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
    @is_private()
73
    def test_crashes(self):
74
        gpm['Crash__limit_size'] = 1
75
        crash_size = 10*1024*1023
76
        CrashFactory.create_batch(200, archive_size=crash_size, minidump_size=0)
77
        self.assertEqual(Crash.objects.all().count(), 200)
78
79
        del_count = 98
80
        deleted = list(Crash.objects.values_list('id', 'created', 'signature', 'userid', 'appid'))[:del_count]
81
        deleted = map(lambda x: dict(id=x[0], element_created=x[1].strftime("%d. %B %Y %I:%M%p"), signature=x[2],
82
                                      userid=x[3], appid=x[4]), deleted)
83
84
        result = delete_size_is_exceeded('crash', 'Crash')
85
86
        self.assertDictEqual(result, dict(count=del_count, size=del_count * crash_size, elements=deleted))
87
        self.assertEqual(Crash.objects.all().count(), 102)
88
89
    @is_private()
90
    def test_feedbacks(self):
91
        gpm['Feedback__limit_size'] = 1
92
        feedback_size = 10*1024*1023
93
        FeedbackFactory.create_batch(200, screenshot_size=feedback_size, system_logs_size=0, attached_file_size=0, blackbox_size=0)
94
        self.assertEqual(Feedback.objects.all().count(), 200)
95
96
        del_count = 98
97
        deleted = list(Feedback.objects.values_list('id', 'created'))
98
        deleted = map(lambda x: dict(id=x[0], element_created=x[1].strftime("%d. %B %Y %I:%M%p")), deleted)[:del_count]
99
100
        result = delete_size_is_exceeded('feedback', 'Feedback')
101
        self.assertDictEqual(result, dict(count=del_count, size=del_count * feedback_size, elements=deleted))
102
        self.assertEqual(Feedback.objects.all().count(), 102)
103
104
105
class DeleteDuplicateTest(TestCase):
106
    @is_private()
107
    def test_crashes(self):
108
        gpm['Crash__duplicate_number'] = 10
109
        CrashFactory.create_batch(25, signature='test1')
110
        self.assertEqual(Crash.objects.filter(signature='test1').count(), 25)
111
        CrashFactory.create_batch(9, signature='test2')
112
        self.assertEqual(Crash.objects.filter(signature='test2').count(), 9)
113
114
        deleted = list(Crash.objects.filter(signature='test1').values_list('id', 'created', 'signature', 'userid', 'appid'))[:15]
115
        deleted = map(lambda x: dict(id=x[0], element_created=x[1].strftime("%d. %B %Y %I:%M%p"), signature=x[2],
116
                                     userid=x[3], appid=x[4]), deleted)
117
118
        result = delete_duplicate_crashes()
119
120
        self.assertDictEqual(result, dict(count=15, size=0, elements=deleted))
121
        self.assertEqual(Crash.objects.filter(signature='test1').count(), gpm['Crash__duplicate_number'])
122
        self.assertEqual(Crash.objects.filter(signature='test2').count(), 9)
123
124
125
@override_settings(CACHES={
126
    'default': {
127
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
128
    }
129
})
130
class MonitoringTest(TestCase):
131
    cache_keys = ['omaha_version_size', 'sparkle_version_size', 'crashes_size', 'feedbacks_size', 'symbols_size']
132
133
    def setUp(self):
134
        for key in self.cache_keys:
135
            cache.delete(key)
136
137
    @is_private()
138
    def test_monitoring(self):
139
        for key in self.cache_keys:
140
            self.assertEqual(cache.get(key, 0), 0)
141
142
        VersionFactory.create(file_size=100)
143
        SparkleVersionFactory.create(file_size=100)
144
        Crash.objects.create(archive_size=80, minidump_size=20)
145
        Symbols.objects.create(file_size=100)
146
        Feedback.objects.create(screenshot_size=25, blackbox_size=25, attached_file_size=25, system_logs_size=25)
147
148
        monitoring_size()
149
150
        for key in self.cache_keys:
151
            self.assertEqual(cache.get(key), 100)
152