Merge pull request #559 from milosgajdos83/table-tests
Default routing table tests
This commit is contained in:
		| @@ -138,6 +138,12 @@ func (t *table) Update(r Route) error { | |||||||
| 		return ErrRouteNotFound | 		return ErrRouteNotFound | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	if _, ok := t.m[destAddr][sum]; !ok && r.Policy == AddIfNotExists { | ||||||
|  | 		t.m[destAddr][sum] = r | ||||||
|  | 		go t.sendEvent(&Event{Type: CreateEvent, Route: r}) | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	// if the route has been found update it | 	// if the route has been found update it | ||||||
| 	if _, ok := t.m[destAddr][sum]; ok { | 	if _, ok := t.m[destAddr][sum]; ok { | ||||||
| 		t.m[destAddr][sum] = r | 		t.m[destAddr][sum] = r | ||||||
| @@ -248,7 +254,12 @@ func (t *table) Size() int { | |||||||
| 	t.RLock() | 	t.RLock() | ||||||
| 	defer t.RUnlock() | 	defer t.RUnlock() | ||||||
|  |  | ||||||
| 	return len(t.m) | 	size := 0 | ||||||
|  | 	for dest, _ := range t.m { | ||||||
|  | 		size += len(t.m[dest]) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return size | ||||||
| } | } | ||||||
|  |  | ||||||
| // String returns debug information | // String returns debug information | ||||||
|   | |||||||
							
								
								
									
										185
									
								
								network/router/default_table_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										185
									
								
								network/router/default_table_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,185 @@ | |||||||
|  | package router | ||||||
|  |  | ||||||
|  | import "testing" | ||||||
|  |  | ||||||
|  | // creates routing table and test route | ||||||
|  | func testSetup() (Table, Route) { | ||||||
|  | 	table := NewTable() | ||||||
|  |  | ||||||
|  | 	route := Route{ | ||||||
|  | 		Destination: "dest.svc", | ||||||
|  | 		Gateway:     "dest.gw", | ||||||
|  | 		Router:      "dest.router", | ||||||
|  | 		Network:     "dest.network", | ||||||
|  | 		Metric:      10, | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return table, route | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestAdd(t *testing.T) { | ||||||
|  | 	table, route := testSetup() | ||||||
|  | 	testTableSize := table.Size() | ||||||
|  |  | ||||||
|  | 	if err := table.Add(route); err != nil { | ||||||
|  | 		t.Errorf("error adding route: %s", err) | ||||||
|  | 	} | ||||||
|  | 	testTableSize += 1 | ||||||
|  |  | ||||||
|  | 	// adds new route for the original destination | ||||||
|  | 	route.Gateway = "dest.gw2" | ||||||
|  |  | ||||||
|  | 	if err := table.Add(route); err != nil { | ||||||
|  | 		t.Errorf("error adding route: %s", err) | ||||||
|  | 	} | ||||||
|  | 	testTableSize += 1 | ||||||
|  |  | ||||||
|  | 	// overrides an existing route | ||||||
|  | 	// NOTE: the size of the table should not change | ||||||
|  | 	route.Metric = 100 | ||||||
|  | 	route.Policy = OverrideIfExists | ||||||
|  |  | ||||||
|  | 	if err := table.Add(route); err != nil { | ||||||
|  | 		t.Errorf("error adding route: %s", err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if table.Size() != testTableSize { | ||||||
|  | 		t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size()) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// dont add new route if it already exists | ||||||
|  | 	// NOTE: The size of the table should not change | ||||||
|  | 	route.Policy = IgnoreIfExists | ||||||
|  |  | ||||||
|  | 	if err := table.Add(route); err != nil { | ||||||
|  | 		t.Errorf("error adding route: %s", err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if table.Size() != testTableSize { | ||||||
|  | 		t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size()) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// adding the same route under AddIfNotExists policy must error | ||||||
|  | 	route.Policy = AddIfNotExists | ||||||
|  |  | ||||||
|  | 	if err := table.Add(route); err != ErrDuplicateRoute { | ||||||
|  | 		t.Errorf("error adding route. Expected error: %s, Given: %s", ErrDuplicateRoute, err) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestDelete(t *testing.T) { | ||||||
|  | 	table, route := testSetup() | ||||||
|  | 	testTableSize := table.Size() | ||||||
|  |  | ||||||
|  | 	if err := table.Add(route); err != nil { | ||||||
|  | 		t.Errorf("error adding route: %s", err) | ||||||
|  | 	} | ||||||
|  | 	testTableSize += 1 | ||||||
|  |  | ||||||
|  | 	// should fail to delete non-existant route | ||||||
|  | 	prevDest := route.Destination | ||||||
|  | 	route.Destination = "randDest" | ||||||
|  |  | ||||||
|  | 	if err := table.Delete(route); err != ErrRouteNotFound { | ||||||
|  | 		t.Errorf("error deleting route. Expected error: %s, given: %s", ErrRouteNotFound, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// we should be able to delete the existing route | ||||||
|  | 	route.Destination = prevDest | ||||||
|  |  | ||||||
|  | 	if err := table.Delete(route); err != nil { | ||||||
|  | 		t.Errorf("error deleting route: %s", err) | ||||||
|  | 	} | ||||||
|  | 	testTableSize -= 1 | ||||||
|  |  | ||||||
|  | 	if table.Size() != testTableSize { | ||||||
|  | 		t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size()) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestUpdate(t *testing.T) { | ||||||
|  | 	table, route := testSetup() | ||||||
|  | 	testTableSize := table.Size() | ||||||
|  |  | ||||||
|  | 	if err := table.Add(route); err != nil { | ||||||
|  | 		t.Errorf("error adding route: %s", err) | ||||||
|  | 	} | ||||||
|  | 	testTableSize += 1 | ||||||
|  |  | ||||||
|  | 	// change the metric of the original route | ||||||
|  | 	// NOTE: this should NOT change the size of the table | ||||||
|  | 	route.Metric = 200 | ||||||
|  |  | ||||||
|  | 	if err := table.Update(route); err != nil { | ||||||
|  | 		t.Errorf("error updating route: %s", err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if table.Size() != testTableSize { | ||||||
|  | 		t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size()) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// NOTE: routing table routes on <destination, gateway, network> | ||||||
|  | 	// this should add a new route | ||||||
|  | 	route.Destination = "new.dest" | ||||||
|  |  | ||||||
|  | 	if err := table.Update(route); err != nil { | ||||||
|  | 		t.Errorf("error updating route: %s", err) | ||||||
|  | 	} | ||||||
|  | 	testTableSize += 1 | ||||||
|  |  | ||||||
|  | 	// NOTE: default policy is AddIfNotExists so the new route will be added here | ||||||
|  | 	if table.Size() != testTableSize { | ||||||
|  | 		t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size()) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// NOTE: we are hashing routes on <destination, gateway, network> | ||||||
|  | 	// this should add a new route | ||||||
|  | 	route.Gateway = "new.gw" | ||||||
|  |  | ||||||
|  | 	if err := table.Update(route); err != nil { | ||||||
|  | 		t.Errorf("error updating route: %s", err) | ||||||
|  | 	} | ||||||
|  | 	testTableSize += 1 | ||||||
|  |  | ||||||
|  | 	if table.Size() != testTableSize { | ||||||
|  | 		t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size()) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// this should NOT add a new route as we are setting the policy to IgnoreIfExists | ||||||
|  | 	route.Destination = "rand.dest" | ||||||
|  | 	route.Policy = IgnoreIfExists | ||||||
|  |  | ||||||
|  | 	if err := table.Update(route); err != ErrRouteNotFound { | ||||||
|  | 		t.Errorf("error updating route. Expected error: %s, given: %s", ErrRouteNotFound, err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if table.Size() != 3 { | ||||||
|  | 		t.Errorf("invalid number of routes. expected: %d, given: %d", testTableSize, table.Size()) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestList(t *testing.T) { | ||||||
|  | 	table, route := testSetup() | ||||||
|  |  | ||||||
|  | 	dest := []string{"one.svc", "two.svc", "three.svc"} | ||||||
|  |  | ||||||
|  | 	for i := 0; i < len(dest); i++ { | ||||||
|  | 		route.Destination = dest[i] | ||||||
|  | 		if err := table.Add(route); err != nil { | ||||||
|  | 			t.Errorf("error adding route: %s", err) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	routes, err := table.List() | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Errorf("error listing routes: %s", err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if len(routes) != len(dest) { | ||||||
|  | 		t.Errorf("incorrect number of routes listed. Expected: %d, Given: %d", len(dest), len(routes)) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if len(routes) != table.Size() { | ||||||
|  | 		t.Errorf("mismatch number of routes and table size. Routes: %d, Size: %d", len(routes), table.Size()) | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @@ -18,10 +18,10 @@ var ( | |||||||
| type RoutePolicy int | type RoutePolicy int | ||||||
|  |  | ||||||
| const ( | const ( | ||||||
| 	// OverrideIfExists overrides route if it already exists |  | ||||||
| 	OverrideIfExists RoutePolicy = iota |  | ||||||
| 	// AddIfNotExist adds the route if it does not exist | 	// AddIfNotExist adds the route if it does not exist | ||||||
| 	AddIfNotExists | 	AddIfNotExists RoutePolicy = iota | ||||||
|  | 	// OverrideIfExists overrides route if it already exists | ||||||
|  | 	OverrideIfExists | ||||||
| 	// IgnoreIfExists instructs to not modify existing route | 	// IgnoreIfExists instructs to not modify existing route | ||||||
| 	IgnoreIfExists | 	IgnoreIfExists | ||||||
| ) | ) | ||||||
| @@ -29,10 +29,10 @@ const ( | |||||||
| // String returns human reprensentation of policy | // String returns human reprensentation of policy | ||||||
| func (p RoutePolicy) String() string { | func (p RoutePolicy) String() string { | ||||||
| 	switch p { | 	switch p { | ||||||
| 	case OverrideIfExists: |  | ||||||
| 		return "OVERRIDE_IF_EXISTS" |  | ||||||
| 	case AddIfNotExists: | 	case AddIfNotExists: | ||||||
| 		return "ADD_IF_NOT_EXISTS" | 		return "ADD_IF_NOT_EXISTS" | ||||||
|  | 	case OverrideIfExists: | ||||||
|  | 		return "OVERRIDE_IF_EXISTS" | ||||||
| 	case IgnoreIfExists: | 	case IgnoreIfExists: | ||||||
| 		return "IGNORE_IF_EXISTS" | 		return "IGNORE_IF_EXISTS" | ||||||
| 	default: | 	default: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user