#!/usr/bin/env python 
#
# Copyright (c) 2014, Arista Networks, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#  - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#  - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#  - Neither the name of Arista Networks nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARISTA NETWORKS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
import unittest
import yaml

import ztpserver.topology  #pylint: disable=F0401

TEST_DIR = 'test/neighbordb'

class TestDefinition(unittest.TestCase):
    #pylint: disable=R0904

    def __init__(self, node):
        super(TestDefinition, self).__init__('run_test')
        self.node = node
        self.neighbordb_node = ztpserver.topology.create_node(node['details'])

    def run_test(self):
        print 'Checking node: %s' % self.node['node']
        result = ztpserver.topology.neighbordb.match_node(self.neighbordb_node)

        if self.node.get('matches', None):
            self.assertEqual(len(result), self.node['matches'])

        if self.node.get('matche_includes', None):
            matches = [x.name for x in result]
            self.assertEqual(matches, self.node['match_includes'])

        if self.node.get('matche_excludes', None):
            matches = [x.name for x in result]
            for match in self.node['match_excludes']:
                self.assertNotIn(match, matches)

def load_tests(loader, tests, pattern):            #pylint: disable=W0613
    suite = unittest.TestSuite()
    for test in [f for f in os.listdir(TEST_DIR) 
                 if os.path.join(TEST_DIR, f).endswith('_test')]:
        print 'Starting test %s' % test

        definition = yaml.load(open(os.path.join(TEST_DIR, test)))
        ztpserver.topology.clear()        
        ztpserver.topology.neighbordb.deserialize(definition['neighbordb'])

        for node in definition['nodes']:
            print 'Adding test: %s' % node['node']
            suite.addTest(TestDefinition(node))

    return suite

if __name__ == '__main__':
    unittest.main()

@@ -1746,19 +1746,37 @@
 (self, n
-ode
+ame, node, neighbordb
 ):%0A     
@@ -1827,16 +1827,41 @@
 _test')%0A
+        self.name = name%0A
         
@@ -1877,16 +1877,53 @@
  = node%0A
+        self.neighbordb = neighbordb%0A
         
@@ -1990,24 +1990,326 @@
 details'%5D)%0A%0A
+    def setUp(self):%0A        ztpserver.topology.clear()        %0A%0A        assert not ztpserver.topology.neighbordb.patterns%5B'globals'%5D%0A        assert not ztpserver.topology.neighbordb.patterns%5B'nodes'%5D%0A%0A        ztpserver.topology.neighbordb.deserialize(self.neighbordb)%0A        self.longMessage = True%0A%0A
     def run_
@@ -2454,16 +2454,102 @@
 b_node)%0A
+        result = %5Bx.name for x in result%5D%0A        print 'Result: %25s' %25 result%0A        
 %0A       
@@ -2642,24 +2642,64 @@
 e%5B'matches'%5D
+,%0A                             self.name
 )%0A%0A        i
@@ -2720,17 +2720,16 @@
 t('match
-e
 _include
@@ -2743,55 +2743,8 @@
 e):%0A
-            matches = %5Bx.name for x in result%5D%0A
     
@@ -2768,23 +2768,22 @@
 rtEqual(
-matches
+result
 , self.n
@@ -2803,16 +2803,56 @@
 cludes'%5D
+,%0A                             self.name
 )%0A%0A     
@@ -2877,17 +2877,16 @@
 t('match
-e
 _exclude
@@ -2900,55 +2900,8 @@
 e):%0A
-            matches = %5Bx.name for x in result%5D%0A
     
@@ -2990,23 +2990,66 @@
 (match, 
-matches
+result,%0A                                 self.name
 )%0A%0Adef l
@@ -3377,127 +3377,8 @@
 t)))
-%0A        ztpserver.topology.clear()        %0A        ztpserver.topology.neighbordb.deserialize(definition%5B'neighbordb'%5D)
 %0A%0A  
@@ -3508,20 +3508,52 @@
 inition(
-node
+test, node, definition%5B'neighbordb'%5D
 ))%0A%0A    

test fix