diff options
author | Ondrej Fabry <ofabry@cisco.com> | 2019-08-01 06:19:33 +0200 |
---|---|---|
committer | Ondrej Fabry <ofabry@cisco.com> | 2019-08-01 06:19:33 +0200 |
commit | 639870b5083a1e66f4584ec7a5f30492fcdb7168 (patch) | |
tree | 0f2ee2c2572eba9cc91d0013a2387d2729b748fc | |
parent | 572c0840e77d2c34077a054548e7559cb3670308 (diff) |
Fix unit tests
Change-Id: I49e0ec8420cf2563b77ec2b5a9cf9674286d5e5d
Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
-rw-r--r-- | cmd/binapi-generator/generate_test.go | 21 | ||||
-rw-r--r-- | cmd/binapi-generator/main.go | 14 | ||||
-rw-r--r-- | core/channel_test.go | 143 |
3 files changed, 35 insertions, 143 deletions
diff --git a/cmd/binapi-generator/generate_test.go b/cmd/binapi-generator/generate_test.go index e68b54a..bff5406 100644 --- a/cmd/binapi-generator/generate_test.go +++ b/cmd/binapi-generator/generate_test.go @@ -16,6 +16,7 @@ package main import ( "bufio" + "io/ioutil" "os" "testing" @@ -24,7 +25,7 @@ import ( func TestGetInputFiles(t *testing.T) { RegisterTestingT(t) - result, err := getInputFiles("testdata") + result, err := getInputFiles("testdata", 1) Expect(err).ShouldNot(HaveOccurred()) Expect(result).To(HaveLen(3)) for _, file := range result { @@ -34,7 +35,7 @@ func TestGetInputFiles(t *testing.T) { func TestGetInputFilesError(t *testing.T) { RegisterTestingT(t) - result, err := getInputFiles("nonexisting_directory") + result, err := getInputFiles("nonexisting_directory", 1) Expect(err).Should(HaveOccurred()) Expect(result).To(BeNil()) } @@ -112,9 +113,9 @@ func TestGetContextInterfaceJson(t *testing.T) { func TestReadJson(t *testing.T) { RegisterTestingT(t) - inputData, err := readFile("testdata/af_packet.api.json") + inputData, err := ioutil.ReadFile("testdata/af_packet.api.json") Expect(err).ShouldNot(HaveOccurred()) - result, err := parseJSON(inputData) + result, err := parseInputJSON(inputData) Expect(err).ShouldNot(HaveOccurred()) Expect(result).ToNot(BeNil()) Expect(result.Len()).To(BeEquivalentTo(5)) @@ -122,9 +123,9 @@ func TestReadJson(t *testing.T) { func TestReadJsonError(t *testing.T) { RegisterTestingT(t) - inputData, err := readFile("testdata/input-read-json-error.json") + inputData, err := ioutil.ReadFile("testdata/input-read-json-error.json") Expect(err).ShouldNot(HaveOccurred()) - result, err := parseJSON(inputData) + result, err := parseInputJSON(inputData) Expect(err).Should(HaveOccurred()) Expect(result).To(BeNil()) } @@ -136,9 +137,9 @@ func TestGeneratePackage(t *testing.T) { testCtx.packageName = "test-package-name" // prepare input/output output files - inputData, err := readFile("testdata/ip.api.json") + inputData, err := ioutil.ReadFile("testdata/ip.api.json") Expect(err).ShouldNot(HaveOccurred()) - jsonRoot, err := parseJSON(inputData) + jsonRoot, err := parseInputJSON(inputData) Expect(err).ShouldNot(HaveOccurred()) testCtx.packageData, err = parsePackage(testCtx, jsonRoot) Expect(err).ShouldNot(HaveOccurred()) @@ -161,9 +162,9 @@ func TestGenerateMessageType(t *testing.T) { testCtx.packageName = "test-package-name" // prepare input/output output files - inputData, err := readFile("testdata/ip.api.json") + inputData, err := ioutil.ReadFile("testdata/ip.api.json") Expect(err).ShouldNot(HaveOccurred()) - jsonRoot, err := parseJSON(inputData) + jsonRoot, err := parseInputJSON(inputData) Expect(err).ShouldNot(HaveOccurred()) outDir := "test_output_directory" outFile, err := os.Create(outDir) diff --git a/cmd/binapi-generator/main.go b/cmd/binapi-generator/main.go index 89a2b2d..d46001d 100644 --- a/cmd/binapi-generator/main.go +++ b/cmd/binapi-generator/main.go @@ -116,6 +116,14 @@ func getInputFiles(inputDir string, deep int) (files []string, err error) { return files, nil } +func parseInputJSON(inputData []byte) (*jsongo.JSONNode, error) { + jsonRoot := new(jsongo.JSONNode) + if err := json.Unmarshal(inputData, jsonRoot); err != nil { + return nil, fmt.Errorf("unmarshalling JSON failed: %v", err) + } + return jsonRoot, nil +} + // generateFromFile generates Go package from one input JSON file func generateFromFile(inputFile, outputDir string) error { // create generator context @@ -142,9 +150,9 @@ func generateFromFile(inputFile, outputDir string) error { return fmt.Errorf("reading input file %s failed: %v", ctx.inputFile, err) } // parse JSON data into objects - jsonRoot := new(jsongo.JSONNode) - if err := json.Unmarshal(ctx.inputData, jsonRoot); err != nil { - return fmt.Errorf("unmarshalling JSON failed: %v", err) + jsonRoot, err := parseInputJSON(ctx.inputData) + if err != nil { + return fmt.Errorf("parsing JSON input failed: %v", err) } ctx.packageData, err = parsePackage(ctx, jsonRoot) if err != nil { diff --git a/core/channel_test.go b/core/channel_test.go index ba761b3..7e721cd 100644 --- a/core/channel_test.go +++ b/core/channel_test.go @@ -18,14 +18,13 @@ import ( "testing" "time" + . "github.com/onsi/gomega" + "git.fd.io/govpp.git/adapter/mock" + "git.fd.io/govpp.git/api" "git.fd.io/govpp.git/examples/binapi/interfaces" "git.fd.io/govpp.git/examples/binapi/memif" - "git.fd.io/govpp.git/examples/binapi/tap" "git.fd.io/govpp.git/examples/binapi/vpe" - - "git.fd.io/govpp.git/api" - . "github.com/onsi/gomega" ) type testCtx struct { @@ -56,93 +55,6 @@ func (ctx *testCtx) teardownTest() { ctx.conn.Disconnect() } -func TestRequestReplyTapConnect(t *testing.T) { - ctx := setupTest(t) - defer ctx.teardownTest() - - // mock reply - ctx.mockVpp.MockReply(&tap.TapConnectReply{ - SwIfIndex: 1, - }) - - request := &tap.TapConnect{ - TapName: []byte("test-tap-name"), - UseRandomMac: 1, - } - reply := &tap.TapConnectReply{} - - err := ctx.ch.SendRequest(request).ReceiveReply(reply) - Expect(err).ShouldNot(HaveOccurred()) - Expect(reply.Retval).To(BeEquivalentTo(0), - "Incorrect Retval value for TapConnectReply") - Expect(reply.SwIfIndex).To(BeEquivalentTo(1), - "Incorrect SwIfIndex value for TapConnectReply") -} - -func TestRequestReplyTapModify(t *testing.T) { - ctx := setupTest(t) - defer ctx.teardownTest() - - // mock reply - ctx.mockVpp.MockReply(&tap.TapModifyReply{ - SwIfIndex: 2, - }) - - request := &tap.TapModify{ - TapName: []byte("test-tap-modify"), - UseRandomMac: 1, - CustomDevInstance: 1, - } - reply := &tap.TapModifyReply{} - - err := ctx.ch.SendRequest(request).ReceiveReply(reply) - Expect(err).ShouldNot(HaveOccurred()) - Expect(reply.Retval).To(BeEquivalentTo(0), - "Incorrect Retval value for TapModifyReply") - Expect(reply.SwIfIndex).To(BeEquivalentTo(2), - "Incorrect SwIfIndex value for TapModifyReply") -} - -func TestRequestReplyTapDelete(t *testing.T) { - ctx := setupTest(t) - defer ctx.teardownTest() - - // mock reply - ctx.mockVpp.MockReply(&tap.TapDeleteReply{}) - - request := &tap.TapDelete{ - SwIfIndex: 3, - } - reply := &tap.TapDeleteReply{} - - err := ctx.ch.SendRequest(request).ReceiveReply(reply) - Expect(err).ShouldNot(HaveOccurred()) - Expect(reply.Retval).To(BeEquivalentTo(0), - "Incorrect Retval value for TapDeleteReply") -} - -func TestRequestReplySwInterfaceTapDump(t *testing.T) { - ctx := setupTest(t) - defer ctx.teardownTest() - - // mock reply - byteName := []byte("dev-name-test") - ctx.mockVpp.MockReply(&tap.SwInterfaceTapDetails{ - SwIfIndex: 25, - DevName: byteName, - }) - - request := &tap.SwInterfaceTapDump{} - reply := &tap.SwInterfaceTapDetails{} - - err := ctx.ch.SendRequest(request).ReceiveReply(reply) - Expect(err).ShouldNot(HaveOccurred()) - Expect(reply.SwIfIndex).To(BeEquivalentTo(25), - "Incorrect SwIfIndex value for SwInterfaceTapDetails") - Expect(reply.DevName).ToNot(BeEquivalentTo(byteName), - "Incorrect DevName value for SwInterfaceTapDetails") -} - func TestRequestReplyMemifCreate(t *testing.T) { ctx := setupTest(t) defer ctx.teardownTest() @@ -208,35 +120,6 @@ func TestRequestReplyMemifDetails(t *testing.T) { "Incorrect Role value for MemifDetails") } -func TestMultiRequestReplySwInterfaceTapDump(t *testing.T) { - ctx := setupTest(t) - defer ctx.teardownTest() - - // mock reply - var msgs []api.Message - for i := 1; i <= 10; i++ { - msgs = append(msgs, &tap.SwInterfaceTapDetails{ - SwIfIndex: uint32(i), - DevName: []byte("dev-name-test"), - }) - } - ctx.mockVpp.MockReply(msgs...) - ctx.mockVpp.MockReply(&ControlPingReply{}) - - reqCtx := ctx.ch.SendMultiRequest(&tap.SwInterfaceTapDump{}) - cnt := 0 - for { - msg := &tap.SwInterfaceTapDetails{} - stop, err := reqCtx.ReceiveReply(msg) - if stop { - break - } - Expect(err).ShouldNot(HaveOccurred()) - cnt++ - } - Expect(cnt).To(BeEquivalentTo(10)) -} - func TestMultiRequestReplySwInterfaceMemifDump(t *testing.T) { ctx := setupTest(t) defer ctx.teardownTest() @@ -479,16 +362,16 @@ func TestReceiveReplyAfterTimeout(t *testing.T) { }, // normal reply for next request mock.MsgWithContext{ - Msg: &tap.TapConnectReply{}, + Msg: &interfaces.SwInterfaceSetFlagsReply{}, SeqNum: 3, }, ) - req := &tap.TapConnect{ - TapName: []byte("test-tap-name"), - UseRandomMac: 1, + req := &interfaces.SwInterfaceSetFlags{ + SwIfIndex: 1, + AdminUpDown: 1, } - reply := &tap.TapConnectReply{} + reply := &interfaces.SwInterfaceSetFlagsReply{} // should succeed err = ctx.ch.SendRequest(req).ReceiveReply(reply) @@ -554,13 +437,13 @@ func TestReceiveReplyAfterTimeoutMultiRequest(t *testing.T) { ctx.mockVpp.MockReplyWithContext(msgs...) // normal reply for next request - ctx.mockVpp.MockReplyWithContext(mock.MsgWithContext{Msg: &tap.TapConnectReply{}, SeqNum: 3}) + ctx.mockVpp.MockReplyWithContext(mock.MsgWithContext{Msg: &interfaces.SwInterfaceSetFlagsReply{}, SeqNum: 3}) - req := &tap.TapConnect{ - TapName: []byte("test-tap-name"), - UseRandomMac: 1, + req := &interfaces.SwInterfaceSetFlags{ + SwIfIndex: 1, + AdminUpDown: 1, } - reply := &tap.TapConnectReply{} + reply := &interfaces.SwInterfaceSetFlagsReply{} // should succeed err = ctx.ch.SendRequest(req).ReceiveReply(reply) |