From c8428439d66cbce1fef7849971093191e2353be8 Mon Sep 17 00:00:00 2001
From: Oliver Herms <oliver.herms@exaring.de>
Date: Sat, 23 Jun 2018 22:59:23 +0200
Subject: [PATCH] Adding test

---
 routingtable/adjRIBOut/path_id_manager.go     |   1 +
 .../adjRIBOut/path_id_manager_test.go         | 231 ++++++++++++++++++
 2 files changed, 232 insertions(+)
 create mode 100644 routingtable/adjRIBOut/path_id_manager_test.go

diff --git a/routingtable/adjRIBOut/path_id_manager.go b/routingtable/adjRIBOut/path_id_manager.go
index 377ce9b3..b13c6674 100644
--- a/routingtable/adjRIBOut/path_id_manager.go
+++ b/routingtable/adjRIBOut/path_id_manager.go
@@ -61,6 +61,7 @@ func (fm *pathIDManager) releasePath(p *route.Path) (uint32, error) {
 		delete(fm.ids, fm.idByPath[*p.BGPPath])
 		delete(fm.idByPath, *p.BGPPath)
 	}
+	fm.used--
 
 	return id, nil
 }
diff --git a/routingtable/adjRIBOut/path_id_manager_test.go b/routingtable/adjRIBOut/path_id_manager_test.go
new file mode 100644
index 00000000..9f3ca112
--- /dev/null
+++ b/routingtable/adjRIBOut/path_id_manager_test.go
@@ -0,0 +1,231 @@
+package adjRIBOut
+
+import (
+	"testing"
+
+	"github.com/bio-routing/bio-rd/route"
+	"github.com/stretchr/testify/assert"
+)
+
+func TestAddPath(t *testing.T) {
+	tests := []struct {
+		name     string
+		maxIDs   uint32
+		count    int
+		wantFail bool
+	}{
+		{
+			name:     "Out of path IDs",
+			maxIDs:   10,
+			count:    11,
+			wantFail: true,
+		},
+		{
+			name:     "Success",
+			maxIDs:   10,
+			count:    10,
+			wantFail: false,
+		},
+	}
+
+X:
+	for _, test := range tests {
+		maxUint32 = test.maxIDs
+		m := newPathIDManager()
+		for i := 0; i < test.count; i++ {
+			_, err := m.addPath(&route.Path{BGPPath: &route.BGPPath{LocalPref: uint32(i)}})
+			if err != nil {
+				if test.wantFail {
+					continue X
+				}
+
+				t.Errorf("Unexpected failure for test %q: %v", test.name, err)
+				continue X
+			}
+		}
+
+		if test.wantFail {
+			t.Errorf("Unexpected success for test %q", test.name)
+			continue
+		}
+	}
+
+}
+
+func TestReleasePath(t *testing.T) {
+	tests := []struct {
+		name     string
+		pm       *pathIDManager
+		release  *route.Path
+		expected *pathIDManager
+		wantFail bool
+	}{
+		{
+			name: "Release existent",
+			pm: &pathIDManager{
+				ids: map[uint32]uint64{
+					0: 1,
+					1: 1,
+					2: 1,
+				},
+				idByPath: map[route.BGPPath]uint32{
+					route.BGPPath{
+						LocalPref: 0,
+					}: 0,
+					route.BGPPath{
+						LocalPref: 1,
+					}: 1,
+					route.BGPPath{
+						LocalPref: 2,
+					}: 2,
+				},
+				last: 2,
+				used: 3,
+			},
+			release: &route.Path{BGPPath: &route.BGPPath{
+				LocalPref: 2,
+			}},
+			expected: &pathIDManager{
+				ids: map[uint32]uint64{
+					0: 1,
+					1: 1,
+				},
+				idByPath: map[route.BGPPath]uint32{
+					route.BGPPath{
+						LocalPref: 0,
+					}: 0,
+					route.BGPPath{
+						LocalPref: 1,
+					}: 1,
+				},
+				last: 2,
+				used: 2,
+			},
+		},
+		{
+			name: "Release non-existent",
+			pm: &pathIDManager{
+				ids: map[uint32]uint64{
+					0: 1,
+					1: 1,
+					2: 1,
+				},
+				idByPath: map[route.BGPPath]uint32{
+					route.BGPPath{
+						LocalPref: 0,
+					}: 0,
+					route.BGPPath{
+						LocalPref: 1,
+					}: 1,
+					route.BGPPath{
+						LocalPref: 2,
+					}: 2,
+				},
+				last: 2,
+				used: 3,
+			},
+			release: &route.Path{BGPPath: &route.BGPPath{
+				LocalPref: 4,
+			}},
+			expected: &pathIDManager{
+				ids: map[uint32]uint64{
+					0: 1,
+					1: 1,
+					2: 1,
+				},
+				idByPath: map[route.BGPPath]uint32{
+					route.BGPPath{
+						LocalPref: 0,
+					}: 0,
+					route.BGPPath{
+						LocalPref: 1,
+					}: 1,
+					route.BGPPath{
+						LocalPref: 2,
+					}: 2,
+				},
+				last: 2,
+				used: 3,
+			},
+			wantFail: true,
+		},
+	}
+
+	for _, test := range tests {
+		_, err := test.pm.releasePath(test.release)
+		if err != nil {
+			if test.wantFail {
+				continue
+			}
+
+			t.Errorf("Unexpected failure for test %q: %v", test.name, err)
+			continue
+		}
+
+		if test.wantFail {
+			t.Errorf("Unexpected success for test %q", test.name)
+			continue
+		}
+
+		assert.Equalf(t, test.expected, test.pm, "%s", test.name)
+	}
+}
+
+/*func TestReleaseID(t *testing.T) {
+	tests := []struct {
+		name     string
+		pm       *pathIDManager
+		release  uint32
+		expected *pathIDManager
+	}{
+		{
+			name: "Release existent",
+			pm: &pathIDManager{
+				ids: map[uint32]struct{}{
+					0: struct{}{},
+					1: struct{}{},
+					2: struct{}{},
+				},
+				last: 2,
+				used: 3,
+			},
+			release: 1,
+			expected: &pathIDManager{
+				ids: map[uint32]struct{}{
+					0: struct{}{},
+					2: struct{}{},
+				},
+				last: 2,
+				used: 2,
+			},
+		},
+		{
+			name: "Release non-existent",
+			pm: &pathIDManager{
+				ids: map[uint32]struct{}{
+					0: struct{}{},
+					1: struct{}{},
+					2: struct{}{},
+				},
+				last: 2,
+				used: 3,
+			},
+			release: 3,
+			expected: &pathIDManager{
+				ids: map[uint32]struct{}{
+					0: struct{}{},
+					1: struct{}{},
+					2: struct{}{},
+				},
+				last: 2,
+				used: 3,
+			},
+		},
+	}
+
+	for _, test := range tests {
+		test.pm.releaseID(test.release)
+		assert.Equalf(t, test.expected, test.pm, "%s", test.name)
+	}
+}
+*/
-- 
GitLab