Skip to content

Commit bdb2536

Browse files
author
Michael Ben-Ami
committed
add Move action
1 parent b96e948 commit bdb2536

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

ovs/action.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ var (
6161
// errTooManyDimensions is returned when the specified dimension exceeds the total dimension
6262
// in a conjunction action.
6363
errDimensionTooLarge = errors.New("dimension number exceeds total number of dimensions")
64+
65+
// errMoveEmpty is returned when Move is called with src and/or dst set to the empty string.
66+
errMoveEmpty = errors.New("src and/or dst field for action move are empty")
6467
)
6568

6669
// Action strings in lower case, as those are compared to the lower case letters
@@ -580,6 +583,34 @@ func (a *setTunnelAction) MarshalText() ([]byte, error) {
580583
return bprintf("set_tunnel:%#x", a.tunnelID), nil
581584
}
582585

586+
// Move sets the value of the destination field to the value of the source field.
587+
func Move(src, dst string) Action {
588+
return &moveAction{
589+
src: src,
590+
dst: dst,
591+
}
592+
}
593+
594+
// A moveAction is an Action used by Move.
595+
type moveAction struct {
596+
src string
597+
dst string
598+
}
599+
600+
// GoString implements Action.
601+
func (a *moveAction) GoString() string {
602+
return fmt.Sprintf("ovs.Move(%q, %q)", a.src, a.dst)
603+
}
604+
605+
// MarshalText implements Action.
606+
func (a *moveAction) MarshalText() ([]byte, error) {
607+
if a.src == "" || a.dst == "" {
608+
return nil, errMoveEmpty
609+
}
610+
611+
return bprintf("move:%s->%s", a.src, a.dst), nil
612+
}
613+
583614
// validARPOP indicates if an ARP OP is out of range. It should be in the range
584615
// 1-4.
585616
func validARPOP(op uint16) bool {

ovs/action_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,54 @@ func TestConjunction(t *testing.T) {
579579
}
580580
}
581581

582+
func TestMove(t *testing.T) {
583+
var tests = []struct {
584+
desc string
585+
a Action
586+
action string
587+
err error
588+
}{
589+
{
590+
desc: "move OK",
591+
a: Move("nw_src", "nw_dst"),
592+
action: "move:nw_src->nw_dst",
593+
},
594+
{
595+
desc: "both empty",
596+
a: Move("", ""),
597+
err: errMoveEmpty,
598+
},
599+
{
600+
desc: "src empty",
601+
a: Move("", "nw_dst"),
602+
err: errMoveEmpty,
603+
},
604+
{
605+
desc: "dst empty",
606+
a: Move("nw_src", ""),
607+
err: errMoveEmpty,
608+
},
609+
}
610+
611+
for _, tt := range tests {
612+
t.Run(tt.desc, func(t *testing.T) {
613+
action, err := tt.a.MarshalText()
614+
615+
if want, got := tt.err, err; want != got {
616+
t.Fatalf("unexpected error:\n- want: %v\n- got: %v",
617+
want, got)
618+
}
619+
if err != nil {
620+
return
621+
}
622+
623+
if want, got := tt.action, string(action); want != got {
624+
t.Fatalf("unexpected Action:\n- want: %q\n- got: %q",
625+
want, got)
626+
}
627+
})
628+
}
629+
}
582630
func TestActionGoString(t *testing.T) {
583631
tests := []struct {
584632
a Action
@@ -652,6 +700,10 @@ func TestActionGoString(t *testing.T) {
652700
a: Conjunction(123, 1, 2),
653701
s: `ovs.Conjunction(123, 1, 2)`,
654702
},
703+
{
704+
a: Move("nw_src", "nw_dst"),
705+
s: `ovs.Move("nw_src", "nw_dst")`,
706+
},
655707
}
656708

657709
for _, tt := range tests {

0 commit comments

Comments
 (0)