def test_seconds_until_renew(self): raw = make_record( holder="me", duration=30, acquire="2018-12-18T12:32:22Z", renew=datetime.utcnow() + timedelta(seconds=20) ) record = ElectionRecordAnnotation("endpoints", raw) assert record.seconds_until_renew > 19 assert record.seconds_until_renew < 21 raw = make_record( holder="me", duration=30, acquire="2018-12-18T12:32:22Z", renew=datetime.utcnow() - timedelta(seconds=5) ) record = ElectionRecordAnnotation("endpoints", raw) assert record.seconds_until_renew > -6 assert record.seconds_until_renew < -4
def test_parse_annotation(self): record = ElectionRecordAnnotation("endpoints", RAW_VALID_RECORD) valid, reason = record.validate() assert valid is True assert reason is None assert record.leader_name == "dd-cluster-agent-568f458dd6-kj6vt" assert record.lease_duration == 60 assert record.transitions == 7 assert record.renew_time > record.acquire_time assert record.seconds_until_renew < 0 assert record.summary == ( "Leader: dd-cluster-agent-568f458dd6-kj6vt " "since 2018-12-17 11:53:07+00:00, " "next renew 2018-12-18 12:32:22+00:00" )
def mock_leader(): # Inject a fake object in the leader-election monitoring logic with mock.patch( 'datadog_checks.kube_controller_manager.KubeControllerManagerCheck._get_record', return_value=ElectionRecordAnnotation( "endpoints", '{"holderIdentity":"pod1","leaseDurationSeconds":15,"leaderTransitions":3,' + '"acquireTime":"2018-12-19T18:23:24Z","renewTime":"2019-01-02T16:30:07Z"}', ), ): yield
def mock_leader(): # Inject a fake object in the leader-election monitoring logic # don't forget to update the [testenv] in tox.ini with the 'kube' dependency with mock.patch( 'datadog_checks.kube_scheduler.KubeSchedulerCheck._get_record', return_value=ElectionRecordAnnotation( "endpoints", '{"holderIdentity":"pod1","leaseDurationSeconds":15,"leaderTransitions":3,' + '"acquireTime":"2018-12-19T18:23:24Z","renewTime":"2019-01-02T16:30:07Z"}', ), ): yield
def test_validation(self): cases = { make_record(): "Invalid record: no current leader recorded", make_record(holder="me"): "Invalid record: no lease duration set", make_record(holder="me", duration=30): "Invalid record: no renew time set", make_record( holder="me", duration=30, renew="2018-12-18T12:32:22Z" ): "Invalid record: no acquire time recorded", make_record(holder="me", duration=30, renew=datetime.utcnow(), acquire="2018-12-18T12:32:22Z"): None, make_record( holder="me", duration=30, renew="invalid", acquire="2018-12-18T12:32:22Z" ): "Invalid record: bad format for renewTime field", make_record( holder="me", duration=30, renew="2018-12-18T12:32:22Z", acquire="0000-12-18T12:32:22Z" ): "Invalid record: bad format for acquireTime field", } for raw, expected_reason in iteritems(cases): valid, reason = ElectionRecordAnnotation("endpoints", raw).validate() assert reason == expected_reason if expected_reason is None: assert valid is True else: assert valid is False