@@ -19,6 +19,7 @@ import (
1919 "errors"
2020 "fmt"
2121 "reflect"
22+ "strconv"
2223 "strings"
2324 "testing"
2425)
@@ -501,6 +502,101 @@ func TestClientVSwitchSetBridgeProtocolsOK(t *testing.T) {
501502 }
502503}
503504
505+ func TestClientVSwitchGetPortOptionsOK (t * testing.T ) {
506+ const port = "bond0"
507+ vlanModeStr := "trunk"
508+ vlanMode := & vlanModeStr
509+ trunk := []int {1 , 2 , 3 , 4 , 5 }
510+
511+ c := testClient ([]OptionFunc {Timeout (1 )}, func (cmd string , args ... string ) ([]byte , error ) {
512+ if want , got := "ovs-vsctl" , cmd ; want != got {
513+ t .Fatalf ("incorrect command:\n - want: %v\n - got: %v" ,
514+ want , got )
515+ }
516+
517+ wantArgs := []string {
518+ "--timeout=1" ,
519+ "--format=json" ,
520+ "get" ,
521+ "port" ,
522+ port ,
523+ "tag" ,
524+ "vlan_mode" ,
525+ "trunk" ,
526+ }
527+ if want , got := wantArgs , args ; ! reflect .DeepEqual (want , got ) {
528+ t .Fatalf ("incorrect arguments\n - want: %v\n - got: %v" ,
529+ want , got )
530+ }
531+
532+ // Make the return value with newline to simulate
533+ // the ovs-vsctl output.
534+ data := "[]\n "
535+ data += fmt .Sprintf ("%s\n " , vlanModeStr )
536+ t , err := json .Marshal (& trunk )
537+ if err != nil {
538+ return nil , err
539+ }
540+ data += fmt .Sprintf ("%s\n " , string (t ))
541+ return []byte (fmt .Sprintln (data )), err
542+ })
543+
544+ got , err := c .VSwitch .Get .Port (port )
545+ if err != nil {
546+ t .Fatalf ("unexpected error for Client.VSwitch.Get.Port: %v" , err )
547+ }
548+ if got .Tag != nil {
549+ t .Fatalf ("unexpected tag for Client.VSwitch.Get.Port: %v" , * got .Tag )
550+ }
551+ if ! reflect .DeepEqual (* got .VLANMode , * vlanMode ) {
552+ t .Fatalf ("unexpected vlan_mode for Client.VSwitch.Get.Port: %v" , * got .VLANMode )
553+ }
554+ if ! reflect .DeepEqual (got .Trunk , trunk ) {
555+ t .Fatalf ("unexpected trunk for Client.VSwitch.Get.Port: %v" , got .Trunk )
556+ }
557+ }
558+
559+ func TestClientVSwitchSetPortOptionsOK (t * testing.T ) {
560+ const port = "bond0"
561+ vlanModeStr := "trunk"
562+ vlanMode := & vlanModeStr
563+ trunk := []int {1 , 2 , 3 , 4 , 5 }
564+
565+ c := testClient ([]OptionFunc {Timeout (1 )}, func (cmd string , args ... string ) ([]byte , error ) {
566+ if want , got := "ovs-vsctl" , cmd ; want != got {
567+ t .Fatalf ("incorrect command:\n - want: %v\n - got: %v" ,
568+ want , got )
569+ }
570+
571+ var trunkSequence string
572+ for _ , trunk := range trunk {
573+ trunkSequence += fmt .Sprintf ("%s," , strconv .Itoa (int (trunk )))
574+ }
575+ wantArgs := []string {
576+ "--timeout=1" ,
577+ "set" ,
578+ "port" ,
579+ port ,
580+ fmt .Sprintf ("vlan_mode=%s" , * vlanMode ),
581+ fmt .Sprintf ("trunk=%s" , trunkSequence ),
582+ }
583+ if want , got := wantArgs , args ; ! reflect .DeepEqual (want , got ) {
584+ t .Fatalf ("incorrect arguments\n - want: %v\n - got: %v" ,
585+ want , got )
586+ }
587+
588+ return nil , nil
589+ })
590+
591+ err := c .VSwitch .Set .Port (port , PortOptions {
592+ VLANMode : vlanMode ,
593+ Trunk : trunk ,
594+ })
595+ if err != nil {
596+ t .Fatalf ("unexpected error for Client.VSwitch.Set.Bridge: %v" , err )
597+ }
598+ }
599+
504600func TestClientVSwitchSetInterfaceTypeOK (t * testing.T ) {
505601 ifi := "bond0"
506602 ifiType := InterfaceTypePatch
0 commit comments