Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2020 the original author or authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package openflow |
| 18 | |
| 19 | import ( |
Jonathan Hart | 828908c | 2020-04-15 14:23:45 -0700 | [diff] [blame] | 20 | ofp "github.com/opencord/goloxi/of13" |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 21 | "github.com/stretchr/testify/assert" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
| 25 | type testRoleManager struct { |
| 26 | role ofp.ControllerRole |
| 27 | generationId uint64 |
| 28 | from string |
| 29 | |
| 30 | generateError bool |
| 31 | } |
| 32 | |
| 33 | func (trm *testRoleManager) UpdateRoles(from string, request *ofp.RoleRequest) bool { |
| 34 | trm.from = from |
| 35 | trm.role = request.Role |
| 36 | trm.generationId = request.GenerationId |
| 37 | |
| 38 | return !trm.generateError |
| 39 | } |
| 40 | |
| 41 | func createOFClient() *OFClient { |
| 42 | endpoints := []string{"e1", "e2", "e3"} |
| 43 | |
| 44 | // create a client object |
| 45 | ofclient := &OFClient{ |
| 46 | OFControllerEndPoints: endpoints, |
| 47 | connections: make(map[string]*OFConnection), |
| 48 | } |
| 49 | |
| 50 | // create a connection object for each endpoint |
| 51 | for _, ep := range endpoints { |
| 52 | conn := &OFConnection{OFControllerEndPoint: ep, role: ofcRoleNone} |
| 53 | ofclient.connections[ep] = conn |
| 54 | } |
| 55 | |
| 56 | return ofclient |
| 57 | } |
| 58 | |
| 59 | func createRoleRequest(role ofp.ControllerRole, genId uint64) *ofp.RoleRequest { |
| 60 | rr := ofp.NewRoleRequest() |
| 61 | rr.Role = role |
| 62 | rr.GenerationId = genId |
| 63 | return rr |
| 64 | } |
| 65 | |
| 66 | func TestRoleChange(t *testing.T) { |
| 67 | endpoints := []string{"e1", "e2", "e3"} |
| 68 | ofclient := &OFClient{ |
| 69 | OFControllerEndPoints: endpoints, connections: make(map[string]*OFConnection), |
| 70 | } |
| 71 | |
| 72 | for _, ep := range endpoints { |
| 73 | conn := &OFConnection{OFControllerEndPoint: ep, role: ofcRoleNone} |
| 74 | ofclient.connections[ep] = conn |
| 75 | } |
| 76 | |
| 77 | assert.Equal(t, ofclient.connections["e1"].role, ofcRoleNone) |
| 78 | |
| 79 | // change role of e1 to master |
| 80 | rr := createRoleRequest(ofp.OFPCRRoleMaster, 1) |
| 81 | |
| 82 | ok := ofclient.UpdateRoles("e1", rr) |
| 83 | assert.True(t, ok) |
| 84 | assert.Equal(t, ofclient.connections["e1"].role, ofcRoleMaster) |
| 85 | |
| 86 | // change role of e2 to master |
| 87 | ok = ofclient.UpdateRoles("e2", rr) |
| 88 | assert.True(t, ok) |
| 89 | assert.Equal(t, ofclient.connections["e2"].role, ofcRoleMaster) |
| 90 | // e1 should now have reverted to slave |
| 91 | assert.Equal(t, ofclient.connections["e1"].role, ofcRoleSlave) |
| 92 | |
| 93 | // change role of e2 to slave |
| 94 | rr = createRoleRequest(ofp.OFPCRRoleSlave, 1) |
| 95 | |
| 96 | ok = ofclient.UpdateRoles("e2", rr) |
| 97 | assert.True(t, ok) |
| 98 | assert.Equal(t, ofclient.connections["e2"].role, ofcRoleSlave) |
| 99 | } |
| 100 | |
| 101 | func TestStaleRoleRequest(t *testing.T) { |
| 102 | ofclient := createOFClient() |
| 103 | |
| 104 | rr1 := createRoleRequest(ofp.OFPCRRoleMaster, 2) |
| 105 | |
| 106 | ok := ofclient.UpdateRoles("e1", rr1) |
| 107 | assert.True(t, ok) |
| 108 | assert.Equal(t, ofclient.connections["e1"].role, ofcRoleMaster) |
| 109 | |
| 110 | // 'stale' role request |
| 111 | rr2 := createRoleRequest(ofp.OFPCRRoleSlave, 1) |
| 112 | |
| 113 | ok = ofclient.UpdateRoles("e1", rr2) |
| 114 | // should not have succeeded |
| 115 | assert.False(t, ok) |
| 116 | // role should remain master |
| 117 | assert.Equal(t, ofclient.connections["e1"].role, ofcRoleMaster) |
| 118 | } |
| 119 | |
| 120 | func TestHandleRoleRequest(t *testing.T) { |
| 121 | |
| 122 | trm := &testRoleManager{} |
| 123 | |
| 124 | connection := &OFConnection{ |
| 125 | OFControllerEndPoint: "e1", |
| 126 | roleManager: trm, |
| 127 | sendChannel: make(chan Message, 10), |
| 128 | } |
| 129 | |
| 130 | rr := createRoleRequest(ofp.OFPCRRoleMaster, 1) |
| 131 | |
| 132 | connection.handleRoleRequest(rr) |
| 133 | |
| 134 | assert.Equal(t, "e1", trm.from) |
| 135 | assert.EqualValues(t, ofp.OFPCRRoleMaster, trm.role) |
| 136 | assert.EqualValues(t, 1, trm.generationId) |
| 137 | |
| 138 | resp := (<-connection.sendChannel).(*ofp.RoleReply) |
| 139 | |
| 140 | assert.EqualValues(t, resp.Role, ofp.OFPCRRoleMaster) |
| 141 | } |
| 142 | |
| 143 | func TestHandleRoleRequestError(t *testing.T) { |
| 144 | |
| 145 | trm := &testRoleManager{generateError: true} |
| 146 | |
| 147 | connection := &OFConnection{ |
| 148 | OFControllerEndPoint: "e1", |
| 149 | roleManager: trm, |
| 150 | sendChannel: make(chan Message, 10), |
| 151 | } |
| 152 | |
| 153 | rr := createRoleRequest(ofp.OFPCRRoleMaster, 1) |
| 154 | |
| 155 | connection.handleRoleRequest(rr) |
| 156 | |
| 157 | resp := (<-connection.sendChannel).(*ofp.RoleRequestFailedErrorMsg) |
| 158 | |
| 159 | assert.EqualValues(t, resp.Code, ofp.OFPRRFCStale) |
| 160 | } |