#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#
# -*- mode: python; -*-

import platform
import os

Import('BuildEnv')
env = BuildEnv.Clone()

env.Append(LIBPATH = env['TOP'] + '/base')
env.Append(LIBPATH = env['TOP'] + '/bgp')
env.Append(LIBPATH = env['TOP'] + '/control-node')
env.Append(LIBPATH = env['TOP'] + '/db')
env.Append(LIBPATH = env['TOP'] + '/ifmap')
env.Append(LIBPATH = env['TOP'] + '/io')
env.Append(LIBPATH = env['TOP'] + '/ifmap/client')
env.Append(CPPPATH = env['TOP'] + '/config-client-mgr')
env.Append(LIBPATH = env['TOP'] + '/xml')
env.Append(LIBPATH = env['TOP'] + '/xmpp')
env.Append(LIBPATH = env['TOP'] + '/http/client')

etcd_incl = os.environ.get('CONTRAIL_ETCD_INCL');

if etcd_incl:
    env.Prepend(LIBS = ['etcdql', 'protobuf', 'grpc++', 'grpc', 'z'])

env.Prepend(LIBS = ['ifmap_server', 'config_client_mgr', 'ifmapio', 'xmpp',
                    'peer_sandesh', 'sandesh', 'http', 'http_parser', 'httpc',
                    'curl', 'sandeshvns', 'process_info', 'io', 'control_node',
                    'ifmap_common', 'pugixml', 'xml', 'db', 'base', 'gunit',
                    'crypto', 'ssl', 'boost_regex', 'boost_chrono',
                    'cassandra_cql', 'SimpleAmqpClient', 'rabbitmq',
                    'cassandra', 'gendb', 'ifmapio',
                    'boost_program_options', 'libbgp_schema', 'boost_chrono'])

if platform.system() != 'Darwin':
    env.Append(LIBS = ['rt'])

env.Library('ifmap_test_util',
            ['ifmap_test_util.cc'])
env.Library('ifmap_test_util_server',
            ['ifmap_client_mock.cc', 'ifmap_test_util_server.cc',
             'ifmap_xmpp_client_mock.cc'])
env.Library('ifmap_test_util_agent',
            ['ifmap_test_util_agent.cc'])

env.Append(LIBPATH = '.')
env.Prepend(LIBS = ['ifmap_test_util', 'ifmap_test_util_server'])
env.Append(LIBS = ['ifmap_test_util', 'ifmap_test_util_server', 'ifmap_server'])
env.Append(LIBPATH = env['TOP'] + '/base/test')
env.Prepend(LIBS = ['task_test'])

env.Append(CPPPATH = [env['TOP']])

test_suite = []
flaky_test_suite = []

def PrependLibraries(env, libs):
    for lib in libs:
        (path, libname) = lib.rsplit('/', 1)
        env.Append(LIBPATH = env['TOP'] + '/' + path)
        env.Prepend(LIBS = libname)

def AppendLibraries(env, libs):
    for lib in libs:
        (path, libname) = lib.rsplit('/', 1)
        env.Append(LIBPATH = env['TOP'] + '/' + path)
        env.Append(LIBS = libname)

def BuildTest(env, testname, sources, prepend_libs, append_libs,
              ts = test_suite):
    build_env = env.Clone()
    PrependLibraries(build_env, prepend_libs)
    AppendLibraries(build_env, append_libs)
    target = build_env.UnitTest(testname, sources)
    ts.append(target)
    env.Alias('src/ifmap:' + testname, target)
    env.Alias('controller/src/ifmap:' + testname, target)
    return target

BuildTest(env, 'ifmap_exporter_test', ['ifmap_exporter_test.cc'], [],
          ['schema/ifmap_vnc', 'schema/bgp_schema', 'schema/ifmapio'])

BuildTest(env, 'ifmap_graph_walker_test', ['ifmap_graph_walker_test.cc'], [],
          ['schema/ifmap_vnc', 'schema/bgp_schema', 'schema/ifmapio'])

BuildTest(env, 'ifmap_server_test', ['ifmap_server_test.cc'],
          [], ['schema/ifmap_vnc', 'schema/ifmapio', 'schema/bgp_schema'])

BuildTest(env, 'ifmap_server_table_test', ['ifmap_server_table_test.cc'],
          ['schema/ifmap_vnc', 'schema/ifmapio', 'schema/bgp_schema',
           'xml/xml'], [])

BuildTest(env, 'ifmap_uuid_mapper_test', ['ifmap_uuid_mapper_test.cc'],
          ['schema/ifmap_vnc', 'schema/bgp_schema', 'schema/ifmapio'],
          [])

BuildTest(env, 'ifmap_update_queue_test', ['ifmap_update_queue_test.cc'],
          ['schema/ifmap_vnc', 'schema/bgp_schema', 'schema/ifmapio',
           'xml/xml'], [])

BuildTest(env, 'ifmap_update_sender_test', ['ifmap_update_sender_test.cc'],
          ['schema/ifmap_vnc', 'schema/bgp_schema', 'schema/ifmapio',
           'xml/xml'], [], flaky_test_suite)

BuildTest(env, 'ifmap_xmpp_test', ['ifmap_xmpp_test.cc'],
          ['schema/ifmap_vnc', 'schema/bgp_schema', 'schema/ifmapio',
           'xml/xml'], [], flaky_test_suite)

BuildTest(env, 'ifmap_stress_test', ['ifmap_stress_test.cc'],
          ['schema/ifmap_vnc', 'schema/bgp_schema', 'schema/ifmapio',
           'xml/xml'], [], flaky_test_suite)

test = env.TestSuite('ifmap-test', test_suite)
flaky_test = env.TestSuite('ifmap-flaky-test', flaky_test_suite)
env.Alias('controller/src/ifmap:test', test)
env.Alias('controller/src/ifmap:flaky-test', flaky_test)
Return('test_suite')
