예제 #1
0
 def test_check_thresholds(self):
     self.mock_svc1.check_thresholds.return_value = {
         'foo': 'bar',
         'baz': 'blam',
     }
     self.mock_svc2.check_thresholds.return_value = {}
     res = self.cls.check_thresholds()
     assert res == {
         'SvcFoo': {
             'foo': 'bar',
             'baz': 'blam',
         }
     }
     assert self.mock_ta.mock_calls == [
         call.update_limits(),
     ]
     assert self.mock_svc1.mock_calls == [
         call._update_service_quotas(),
         call.check_thresholds()
     ]
     assert self.mock_svc2.mock_calls == [
         call._update_limits_from_api(),
         call._update_service_quotas(),
         call.check_thresholds()
     ]
예제 #2
0
 def test_check_thresholds(self):
     self.mock_svc1.check_thresholds.return_value = {
         'foo': 'bar',
         'baz': 'blam',
     }
     self.mock_svc2.check_thresholds.return_value = {}
     res = self.cls.check_thresholds()
     assert res == {
         'SvcFoo': {
             'foo': 'bar',
             'baz': 'blam',
         }
     }
     assert self.mock_ta.mock_calls == [
         call.update_limits({
             'SvcFoo': self.mock_svc1,
             'SvcBar': self.mock_svc2
         }),
     ]
     assert self.mock_svc1.mock_calls == [
         call.check_thresholds()
     ]
     assert self.mock_svc2.mock_calls == [
         call._update_limits_from_api(),
         call.check_thresholds()
     ]
예제 #3
0
    def test_check_thresholds_crit(self):
        """only critical"""
        mock_limit1 = Mock(spec_set=AwsLimit)
        mock_limit1.get_warnings.return_value = []
        mock_c1 = Mock(spec_set=AwsLimitUsage)
        mock_c2 = Mock(spec_set=AwsLimitUsage)
        mock_limit1.get_criticals.return_value = [mock_c1, mock_c2]

        mock_checker = Mock(spec_set=AwsLimitChecker)
        mock_checker.check_thresholds.return_value = {
            'svc1': {
                'limit1': mock_limit1,
            },
        }

        self.cls.checker = mock_checker
        self.cls.skip_ta = True
        with patch('awslimitchecker.runner.Runner.print_issue',
                   autospec=True) as mock_print:
            mock_print.return_value = ('', '')
            with patch('awslimitchecker.runner.dict2cols') as mock_d2c:
                mock_d2c.return_value = 'd2cval'
            res = self.cls.check_thresholds()
        assert mock_checker.mock_calls == [
            call.check_thresholds(use_ta=False, service=None)
        ]
        assert mock_print.mock_calls == [
            call(self.cls, 'svc1', mock_limit1, [mock_c1, mock_c2], []),
        ]
        assert res == 2
예제 #4
0
    def test_check_thresholds_warn_one_service(self):
        """just warnings"""
        mock_limit1 = Mock(spec_set=AwsLimit)
        mock_w1 = Mock(spec_set=AwsLimitUsage)
        mock_w2 = Mock(spec_set=AwsLimitUsage)
        mock_limit1.get_warnings.return_value = [mock_w1, mock_w2]
        mock_limit1.get_criticals.return_value = []

        mock_limit2 = Mock(spec_set=AwsLimit)
        mock_w3 = Mock(spec_set=AwsLimitUsage)
        mock_limit2.get_warnings.return_value = [mock_w3]
        mock_limit2.get_criticals.return_value = []

        mock_checker = Mock(spec_set=AwsLimitChecker)
        mock_checker.check_thresholds.return_value = {
            'svc2': {
                'limit2': mock_limit2,
            },
        }

        self.cls.checker = mock_checker
        self.cls.service_name = 'svc2'
        with patch('awslimitchecker.runner.Runner.print_issue',
                   autospec=True) as mock_print:
            mock_print.return_value = ('', '')
            with patch('awslimitchecker.runner.dict2cols') as mock_d2c:
                mock_d2c.return_value = 'd2cval'
                res = self.cls.check_thresholds()
        assert mock_checker.mock_calls == [
            call.check_thresholds(use_ta=True, service='svc2')
        ]
        assert mock_print.mock_calls == [
            call(self.cls, 'svc2', mock_limit2, [], [mock_w3]),
        ]
        assert res == 1
예제 #5
0
    def test_check_thresholds_crit(self):
        """only critical"""
        mock_limit1 = Mock(spec_set=AwsLimit)
        mock_limit1.get_warnings.return_value = []
        mock_c1 = Mock(spec_set=AwsLimitUsage)
        mock_c2 = Mock(spec_set=AwsLimitUsage)
        mock_limit1.get_criticals.return_value = [mock_c1, mock_c2]

        mock_checker = Mock(spec_set=AwsLimitChecker)
        mock_checker.check_thresholds.return_value = {
            'svc1': {
                'limit1': mock_limit1,
            },
        }

        self.cls.checker = mock_checker
        self.cls.skip_ta = True
        with patch('awslimitchecker.runner.Runner.print_issue',
                   autospec=True) as mock_print:
            mock_print.return_value = ('', '')
            with patch('awslimitchecker.runner.dict2cols') as mock_d2c:
                mock_d2c.return_value = 'd2cval'
            res = self.cls.check_thresholds()
        assert mock_checker.mock_calls == [
            call.check_thresholds(use_ta=False, service=None)
        ]
        assert mock_print.mock_calls == [
            call(self.cls, 'svc1', mock_limit1, [mock_c1, mock_c2], []),
        ]
        assert res == 2
예제 #6
0
    def test_check_thresholds_warn_one_service(self):
        """just warnings"""
        mock_limit1 = Mock(spec_set=AwsLimit)
        mock_w1 = Mock(spec_set=AwsLimitUsage)
        mock_w2 = Mock(spec_set=AwsLimitUsage)
        mock_limit1.get_warnings.return_value = [mock_w1, mock_w2]
        mock_limit1.get_criticals.return_value = []

        mock_limit2 = Mock(spec_set=AwsLimit)
        mock_w3 = Mock(spec_set=AwsLimitUsage)
        mock_limit2.get_warnings.return_value = [mock_w3]
        mock_limit2.get_criticals.return_value = []

        mock_checker = Mock(spec_set=AwsLimitChecker)
        mock_checker.check_thresholds.return_value = {
            'svc2': {
                'limit2': mock_limit2,
            },
        }

        self.cls.checker = mock_checker
        self.cls.service_name = 'svc2'
        with patch('awslimitchecker.runner.Runner.print_issue',
                   autospec=True) as mock_print:
            mock_print.return_value = ('', '')
            with patch('awslimitchecker.runner.dict2cols') as mock_d2c:
                mock_d2c.return_value = 'd2cval'
                res = self.cls.check_thresholds()
        assert mock_checker.mock_calls == [
            call.check_thresholds(use_ta=True, service='svc2')
        ]
        assert mock_print.mock_calls == [
            call(self.cls, 'svc2', mock_limit2, [], [mock_w3]),
        ]
        assert res == 1
예제 #7
0
 def test_check_thresholds_no_ta(self):
     self.mock_svc1.check_thresholds.return_value = {
         'foo': 'bar',
         'baz': 'blam',
     }
     self.mock_svc2.check_thresholds.return_value = {}
     self.cls.use_ta = False
     res = self.cls.check_thresholds(use_ta=False)
     assert res == {
         'SvcFoo': {
             'foo': 'bar',
             'baz': 'blam',
         }
     }
     assert self.mock_ta.mock_calls == []
     assert self.mock_svc1.mock_calls == [call.check_thresholds()]
     assert self.mock_svc2.mock_calls == [
         call._update_limits_from_api(),
         call.check_thresholds()
     ]
예제 #8
0
 def test_check_thresholds_service(self):
     self.mock_svc1.check_thresholds.return_value = {'foo': 'bar'}
     self.mock_svc2.check_thresholds.return_value = {'baz': 'blam'}
     res = self.cls.check_thresholds(service=['SvcFoo'])
     assert res == {
         'SvcFoo': {
             'foo': 'bar',
         }
     }
     assert self.mock_ta.mock_calls == [call.update_limits()]
     assert self.mock_svc1.mock_calls == [call.check_thresholds()]
     assert self.mock_svc2.mock_calls == []
예제 #9
0
 def test_check_thresholds_no_ta(self):
     self.mock_svc1.check_thresholds.return_value = {
         'foo': 'bar',
         'baz': 'blam',
     }
     self.mock_svc2.check_thresholds.return_value = {}
     self.cls.use_ta = False
     res = self.cls.check_thresholds(use_ta=False)
     assert res == {
         'SvcFoo': {
             'foo': 'bar',
             'baz': 'blam',
         }
     }
     assert self.mock_ta.mock_calls == []
     assert self.mock_svc1.mock_calls == [
         call.check_thresholds()
     ]
     assert self.mock_svc2.mock_calls == [
         call._update_limits_from_api(),
         call.check_thresholds()
     ]
예제 #10
0
 def test_check_thresholds_find_usage(self):
     cls = AwsServiceTester(1, 2)
     mock_limit1 = Mock(spec_set=AwsLimit)
     mock_limit1.check_thresholds.return_value = False
     cls.limits['foo'] = mock_limit1
     mock_limit2 = Mock(spec_set=AwsLimit)
     mock_limit2.check_thresholds.return_value = True
     cls.limits['foo2'] = mock_limit2
     mock_limit3 = Mock(spec_set=AwsLimit)
     mock_limit3.check_thresholds.return_value = True
     cls.limits['foo3'] = mock_limit3
     mock_limit4 = Mock(spec_set=AwsLimit)
     mock_limit4.check_thresholds.return_value = False
     cls.limits['foo4'] = mock_limit4
     mock_find_usage = Mock()
     with patch.object(AwsServiceTester, 'find_usage', mock_find_usage):
         res = cls.check_thresholds()
     assert mock_limit1.mock_calls == [call.check_thresholds()]
     assert mock_limit2.mock_calls == [call.check_thresholds()]
     assert mock_limit3.mock_calls == [call.check_thresholds()]
     assert mock_limit4.mock_calls == [call.check_thresholds()]
     assert res == {'foo': mock_limit1, 'foo4': mock_limit4}
     assert mock_find_usage.mock_calls == [call()]
예제 #11
0
 def test_check_thresholds_ok(self, capsys):
     """no problems, return 0 and print nothing"""
     mock_checker = Mock(spec_set=AwsLimitChecker)
     mock_checker.check_thresholds.return_value = {}
     self.cls.checker = mock_checker
     with patch('awslimitchecker.runner.dict2cols') as mock_d2c:
         mock_d2c.return_value = ''
         res = self.cls.check_thresholds()
     out, err = capsys.readouterr()
     assert out == '\n'
     assert mock_checker.mock_calls == [
         call.check_thresholds(use_ta=True, service=None)
     ]
     assert res == 0
예제 #12
0
 def test_check_thresholds_ok(self, capsys):
     """no problems, return 0 and print nothing"""
     mock_checker = Mock(spec_set=AwsLimitChecker)
     mock_checker.check_thresholds.return_value = {}
     self.cls.checker = mock_checker
     with patch('awslimitchecker.runner.dict2cols') as mock_d2c:
         mock_d2c.return_value = ''
         res = self.cls.check_thresholds()
     out, err = capsys.readouterr()
     assert out == '\n'
     assert mock_checker.mock_calls == [
         call.check_thresholds(use_ta=True, service=None)
     ]
     assert res == 0
예제 #13
0
 def test_check_thresholds_find_usage(self):
     cls = AwsServiceTester(1, 2)
     mock_limit1 = Mock(spec_set=AwsLimit)
     mock_limit1.check_thresholds.return_value = False
     cls.limits['foo'] = mock_limit1
     mock_limit2 = Mock(spec_set=AwsLimit)
     mock_limit2.check_thresholds.return_value = True
     cls.limits['foo2'] = mock_limit2
     mock_limit3 = Mock(spec_set=AwsLimit)
     mock_limit3.check_thresholds.return_value = True
     cls.limits['foo3'] = mock_limit3
     mock_limit4 = Mock(spec_set=AwsLimit)
     mock_limit4.check_thresholds.return_value = False
     cls.limits['foo4'] = mock_limit4
     mock_find_usage = Mock()
     with patch.object(AwsServiceTester, 'find_usage', mock_find_usage):
         res = cls.check_thresholds()
     assert mock_limit1.mock_calls == [call.check_thresholds()]
     assert mock_limit2.mock_calls == [call.check_thresholds()]
     assert mock_limit3.mock_calls == [call.check_thresholds()]
     assert mock_limit4.mock_calls == [call.check_thresholds()]
     assert res == {'foo': mock_limit1, 'foo4': mock_limit4}
     assert mock_find_usage.mock_calls == [call()]
예제 #14
0
 def test_check_thresholds_service(self):
     self.mock_svc1.check_thresholds.return_value = {'foo': 'bar'}
     self.mock_svc2.check_thresholds.return_value = {'baz': 'blam'}
     res = self.cls.check_thresholds(service='SvcFoo')
     assert res == {
         'SvcFoo': {
             'foo': 'bar',
         }
     }
     assert self.mock_ta.mock_calls == [
         call.update_limits({'SvcFoo': self.mock_svc1})
     ]
     assert self.mock_svc1.mock_calls == [
         call.check_thresholds()
     ]
     assert self.mock_svc2.mock_calls == []
예제 #15
0
 def test_check_thresholds_service_api(self):
     self.mock_svc1.check_thresholds.return_value = {'foo': 'bar'}
     self.mock_svc2.check_thresholds.return_value = {'baz': 'blam'}
     res = self.cls.check_thresholds(service='SvcBar')
     assert res == {
         'SvcBar': {
             'baz': 'blam',
         }
     }
     assert self.mock_ta.mock_calls == [
         call.update_limits({'SvcBar': self.mock_svc2})
     ]
     assert self.mock_svc1.mock_calls == []
     assert self.mock_svc2.mock_calls == [
         call._update_limits_from_api(),
         call.check_thresholds()
     ]
예제 #16
0
    def test_check_thresholds_many_problems(self):
        """lots of problems"""
        mock_limit1 = Mock(spec_set=AwsLimit)
        type(mock_limit1).name = 'limit1'
        mock_w1 = Mock(spec_set=AwsLimitUsage)
        mock_limit1.get_warnings.return_value = [mock_w1]
        mock_c1 = Mock(spec_set=AwsLimitUsage)
        mock_limit1.get_criticals.return_value = [mock_c1]

        mock_limit2 = Mock(spec_set=AwsLimit)
        type(mock_limit2).name = 'limit2'
        mock_w2 = Mock(spec_set=AwsLimitUsage)
        mock_limit2.get_warnings.return_value = [mock_w2]
        mock_limit2.get_criticals.return_value = []

        mock_limit3 = Mock(spec_set=AwsLimit)
        type(mock_limit3).name = 'limit3'
        mock_w3 = Mock(spec_set=AwsLimitUsage)
        mock_limit3.get_warnings.return_value = [mock_w3]
        mock_limit3.get_criticals.return_value = []

        mock_limit4 = Mock(spec_set=AwsLimit)
        type(mock_limit4).name = 'limit4'
        mock_limit4.get_warnings.return_value = []
        mock_c2 = Mock(spec_set=AwsLimitUsage)
        mock_limit4.get_criticals.return_value = [mock_c2]

        mock_checker = Mock(spec_set=AwsLimitChecker)
        mock_checker.check_thresholds.return_value = {
            'svc2': {
                'limit3': mock_limit3,
                'limit4': mock_limit4,
            },
            'svc1': {
                'limit1': mock_limit1,
                'limit2': mock_limit2,
            },
        }

        def se_print(cls, s, l, c, w):
            return ('{s}/{l}'.format(s=s, l=l.name), '')

        self.cls.checker = mock_checker
        with patch('awslimitchecker.runner.Runner.print_issue',
                   autospec=True) as mock_print:
            mock_print.side_effect = se_print
            with patch('awslimitchecker.runner.dict2cols') as mock_d2c:
                mock_d2c.return_value = 'd2cval'
                res = self.cls.check_thresholds()
        assert mock_checker.mock_calls == [
            call.check_thresholds(use_ta=True, service=None)
        ]
        assert mock_print.mock_calls == [
            call(self.cls, 'svc1', mock_limit1, [mock_c1], [mock_w1]),
            call(self.cls, 'svc1', mock_limit2, [], [mock_w2]),
            call(self.cls, 'svc2', mock_limit3, [], [mock_w3]),
            call(self.cls, 'svc2', mock_limit4, [mock_c2], []),
        ]
        assert mock_d2c.mock_calls == [
            call({
                'svc1/limit1': '',
                'svc1/limit2': '',
                'svc2/limit3': '',
                'svc2/limit4': '',
            })
        ]
        assert res == 2
예제 #17
0
    def test_check_thresholds_many_problems(self):
        """lots of problems"""
        mock_limit1 = Mock(spec_set=AwsLimit)
        type(mock_limit1).name = 'limit1'
        mock_w1 = Mock(spec_set=AwsLimitUsage)
        mock_limit1.get_warnings.return_value = [mock_w1]
        mock_c1 = Mock(spec_set=AwsLimitUsage)
        mock_limit1.get_criticals.return_value = [mock_c1]

        mock_limit2 = Mock(spec_set=AwsLimit)
        type(mock_limit2).name = 'limit2'
        mock_w2 = Mock(spec_set=AwsLimitUsage)
        mock_limit2.get_warnings.return_value = [mock_w2]
        mock_limit2.get_criticals.return_value = []

        mock_limit3 = Mock(spec_set=AwsLimit)
        type(mock_limit3).name = 'limit3'
        mock_w3 = Mock(spec_set=AwsLimitUsage)
        mock_limit3.get_warnings.return_value = [mock_w3]
        mock_limit3.get_criticals.return_value = []

        mock_limit4 = Mock(spec_set=AwsLimit)
        type(mock_limit4).name = 'limit4'
        mock_limit4.get_warnings.return_value = []
        mock_c2 = Mock(spec_set=AwsLimitUsage)
        mock_limit4.get_criticals.return_value = [mock_c2]

        mock_checker = Mock(spec_set=AwsLimitChecker)
        mock_checker.check_thresholds.return_value = {
            'svc2': {
                'limit3': mock_limit3,
                'limit4': mock_limit4,
            },
            'svc1': {
                'limit1': mock_limit1,
                'limit2': mock_limit2,
            },
        }

        def se_print(cls, s, l, c, w):
            return ('{s}/{l}'.format(s=s, l=l.name), '')

        self.cls.checker = mock_checker
        with patch('awslimitchecker.runner.Runner.print_issue',
                   autospec=True) as mock_print:
            mock_print.side_effect = se_print
            with patch('awslimitchecker.runner.dict2cols') as mock_d2c:
                mock_d2c.return_value = 'd2cval'
                res = self.cls.check_thresholds()
        assert mock_checker.mock_calls == [
            call.check_thresholds(use_ta=True, service=None)
        ]
        assert mock_print.mock_calls == [
            call(self.cls, 'svc1', mock_limit1, [mock_c1], [mock_w1]),
            call(self.cls, 'svc1', mock_limit2, [], [mock_w2]),
            call(self.cls, 'svc2', mock_limit3, [], [mock_w3]),
            call(self.cls, 'svc2', mock_limit4, [mock_c2], []),
        ]
        assert mock_d2c.mock_calls == [
            call({
                'svc1/limit1': '',
                'svc1/limit2': '',
                'svc2/limit3': '',
                'svc2/limit4': '',
            })
        ]
        assert res == 2