#  Copyright (c) 2019. Vinci Consulting Corp. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
import datetime
import time
from asfframework import VppAsfTestCase

enable_print = False


class TestVpeApi(VppAsfTestCase):
    """TestVpeApi"""

    def test_log_dump_default(self):
        rv = self.vapi.cli("test log notice fib entry this is a test")
        rv = self.vapi.log_dump()
        if enable_print:
            print("\n".join([str(v) for v in rv]))
        self.assertTrue(rv)

    def test_log_dump_timestamp_0(self):
        rv = self.vapi.cli("test log notice fib entry this is a test")
        rv = self.vapi.log_dump(start_timestamp=0.0)
        if enable_print:
            print("\n".join([str(v) for v in rv]))
        self.assertTrue(rv)

    def test_log_dump_timestamp_future(self):
        rv = self.vapi.cli("test log debug fib entry test")
        rv = self.vapi.log_dump(start_timestamp=time.time() + 60.0)
        if enable_print:
            print("\n".join([str(v) for v in rv]))
        self.assertFalse(rv)

    def test_show_vpe_system_time(self):
        local_start_time = datetime.datetime.now()
        rv = self.vapi.show_vpe_system_time()
        self.assertTrue(
            rv.vpe_system_time > local_start_time - datetime.timedelta(hours=1.0),
            "system times differ by more than an hour.",
        )
        if enable_print:
            print("\n".join([str(v) for v in rv]))
            print("%r %s" % (rv.vpe_system_time, rv.vpe_system_time))
