Skip to content

Commit

Permalink
add genObjectStreamLocation tests
Browse files Browse the repository at this point in the history
Signed-off-by: kevindiu <kevindiujp@gmail.com>
  • Loading branch information
kevindiu committed Jun 20, 2022
1 parent 1ab93c5 commit f228b3a
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/test/data/request/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func GenObjectLocations(num int, name string, ipAddr string) *payload.Object_Loc
return result
}

// GenObjectStreamLocation generate ObjectStreamLocations payload with multiple name and ip with generated uuid.
func GenObjectStreamLocation(num int, name string, ipAddr string) []*payload.Object_StreamLocation {
result := make([]*payload.Object_StreamLocation, num)

Expand Down
141 changes: 141 additions & 0 deletions internal/test/data/request/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package request

import (
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -151,3 +152,143 @@ func TestGenObjectLocations(t *testing.T) {
})
}
}

func TestGenObjectStreamLocation(t *testing.T) {
type args struct {
num int
name string
ipAddr string
}
type want struct {
want []*payload.Object_StreamLocation
}
type test struct {
name string
args args
want want
checkFunc func(want, []*payload.Object_StreamLocation) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, got []*payload.Object_StreamLocation) error {
if !reflect.DeepEqual(got, w.want) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want)
}
return nil
}
tests := []test{
{
name: "success to generate 1 object stream location",
args: args{
num: 1,
name: "vald-agent-01",
ipAddr: "127.0.0.1",
},
want: want{
want: []*payload.Object_StreamLocation{
{
Payload: &payload.Object_StreamLocation_Location{
Location: &payload.Object_Location{
Name: "vald-agent-01",
Uuid: "uuid-1",
Ips: []string{"127.0.0.1"},
},
},
},
},
},
},
{
name: "success to generate 5 object stream location",
args: args{
num: 5,
name: "vald-agent-01",
ipAddr: "127.0.0.1",
},
want: want{
want: []*payload.Object_StreamLocation{
{
Payload: &payload.Object_StreamLocation_Location{
Location: &payload.Object_Location{
Name: "vald-agent-01",
Uuid: "uuid-1",
Ips: []string{"127.0.0.1"},
},
},
},
{
Payload: &payload.Object_StreamLocation_Location{
Location: &payload.Object_Location{
Name: "vald-agent-01",
Uuid: "uuid-2",
Ips: []string{"127.0.0.1"},
},
},
},
{
Payload: &payload.Object_StreamLocation_Location{
Location: &payload.Object_Location{
Name: "vald-agent-01",
Uuid: "uuid-3",
Ips: []string{"127.0.0.1"},
},
},
},
{
Payload: &payload.Object_StreamLocation_Location{
Location: &payload.Object_Location{
Name: "vald-agent-01",
Uuid: "uuid-4",
Ips: []string{"127.0.0.1"},
},
},
},
{
Payload: &payload.Object_StreamLocation_Location{
Location: &payload.Object_Location{
Name: "vald-agent-01",
Uuid: "uuid-5",
Ips: []string{"127.0.0.1"},
},
},
},
},
},
},
{
name: "success to generate 0 object stream location",
args: args{
num: 0,
name: "vald-agent-01",
ipAddr: "127.0.0.1",
},
want: want{
want: []*payload.Object_StreamLocation{},
},
},
}

for _, tc := range tests {
test := tc
t.Run(test.name, func(tt *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}
checkFunc := test.checkFunc
if test.checkFunc == nil {
checkFunc = defaultCheckFunc
}

got := GenObjectStreamLocation(test.args.num, test.args.name, test.args.ipAddr)
if err := checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}

})
}
}

0 comments on commit f228b3a

Please sign in to comment.