#  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
import unittest
from framework import VppTestCase

enable_print = False


class TestVpeApi(VppTestCase):
    """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))
