Skip to content

Commit 56e8160

Browse files
mregmimdlayher
authored andcommitted
ovs: add get-controller which is used to get the current controller address (#47)
Applications use this to get the current controller address set in ovs. this is needed if user wants to verify if the ovs has the address currently support, if not user can overwrite it with the supported one. Also add the unit test.
1 parent 2d0e0bd commit 56e8160

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

ovs/vswitch.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ func (v *VSwitchService) SetController(bridge string, address string) error {
126126
return err
127127
}
128128

129+
// GetController gets the controller address for this bridge.
130+
func (v *VSwitchService) GetController(bridge string) (string, error) {
131+
address, err := v.exec("get-controller", bridge)
132+
if err != nil {
133+
return "", err
134+
}
135+
return strings.TrimSpace(string(address)), err
136+
}
137+
129138
// exec executes an ExecFunc using 'ovs-vsctl'.
130139
func (v *VSwitchService) exec(args ...string) ([]byte, error) {
131140
return v.c.exec("ovs-vsctl", args...)

ovs/vswitch_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,36 @@ func TestClientVSwitchSetControllerOK(t *testing.T) {
167167
}
168168
}
169169

170+
func TestClientVSwitchGetControllerOK(t *testing.T) {
171+
bridge := "br0"
172+
address := "pssl:6653:127.0.0.1"
173+
174+
// Apply Timeout option to verify arguments
175+
c := testClient([]OptionFunc{Timeout(1)}, func(cmd string, args ...string) ([]byte, error) {
176+
// Verify correct command and arguments passed, including option flags
177+
if want, got := "ovs-vsctl", cmd; want != got {
178+
t.Fatalf("incorrect command:\n- want: %v\n- got: %v",
179+
want, got)
180+
}
181+
182+
wantArgs := []string{"--timeout=1", "get-controller", string(bridge)}
183+
if want, got := wantArgs, args; !reflect.DeepEqual(want, got) {
184+
t.Fatalf("incorrect arguments\n- want: %v\n- got: %v",
185+
want, got)
186+
}
187+
188+
return []byte(address), nil
189+
})
190+
191+
gotAddress, err := c.VSwitch.GetController(bridge)
192+
if err != nil {
193+
t.Fatalf("unexpected error for Client.VSwitch.GetController: %v", err)
194+
}
195+
if gotAddress != address {
196+
t.Fatalf("Controller address missmatch\n- got: %v\n- want: %v", gotAddress, address)
197+
}
198+
}
199+
170200
func TestClientVSwitchListPorts(t *testing.T) {
171201
tests := []struct {
172202
name string

0 commit comments

Comments
 (0)