blob: 4f93bb87bf1d3546696e6899cc31d70d9c76f830 [file] [log] [blame]
Matteo Scandolo27428702019-10-11 16:21:16 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//https://quii.gitbook.io/learn-go-with-tests/go-fundamentals/mocking
18package devices
19
20import (
21 "errors"
Matteo Scandolo90d08f62020-10-29 12:06:55 -070022 "github.com/google/gopacket"
23 "github.com/google/gopacket/layers"
24 "github.com/opencord/voltha-protos/v4/go/openolt"
25 "github.com/stretchr/testify/assert"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010026 "testing"
Matteo Scandolo27428702019-10-11 16:21:16 -070027)
28
Matteo Scandolo90d08f62020-10-29 12:06:55 -070029func TestCreateNNI(t *testing.T) {
30 olt := OltDevice{
31 ID: 0,
Matteo Scandolo27428702019-10-11 16:21:16 -070032 }
Matteo Scandolo90d08f62020-10-29 12:06:55 -070033 nni, err := CreateNNI(&olt)
34
35 assert.Nil(t, err)
36 assert.Equal(t, "nni", nni.Type)
37 assert.Equal(t, uint32(0), nni.ID)
38 assert.Equal(t, "down", nni.OperState.Current())
Matteo Scandolo27428702019-10-11 16:21:16 -070039}
40
Matteo Scandolo90d08f62020-10-29 12:06:55 -070041func TestSendNniPacket(t *testing.T) {
42
43 stream := &mockStream{
44 CallCount: 0,
45 Calls: make(map[int]*openolt.Indication),
46 fail: false,
47 channel: make(chan int, 10),
Matteo Scandolo27428702019-10-11 16:21:16 -070048 }
Matteo Scandolo90d08f62020-10-29 12:06:55 -070049
50 dhcpServer := &mockDhcpServer{
51 callCount: 0,
52 fail: false,
53 }
54
55 nni := NniPort{
56 Olt: &OltDevice{
57 OpenoltStream: stream,
58 dhcpServer: dhcpServer,
59 },
60 ID: 12,
61 }
62
63 // the DHCP server is mocked, so we don't really care about the packet we send in
64 pkt := createTestDhcpPacket(t)
65 err := nni.handleNniPacket(pkt)
66 assert.Nil(t, err)
67 assert.Equal(t, stream.CallCount, 1)
68 indication := stream.Calls[1].GetPktInd()
69 assert.Equal(t, "nni", indication.IntfType)
70 assert.Equal(t, nni.ID, indication.IntfId)
71 assert.Equal(t, pkt.Data(), indication.Pkt)
Matteo Scandolo27428702019-10-11 16:21:16 -070072}
73
Matteo Scandolo90d08f62020-10-29 12:06:55 -070074type mockDhcpServer struct {
75 callCount int
76 fail bool
Matteo Scandolo27428702019-10-11 16:21:16 -070077}
78
Matteo Scandolo90d08f62020-10-29 12:06:55 -070079// being a Mock I just return the same packet I got
80func (s mockDhcpServer) HandleServerPacket(pkt gopacket.Packet) (gopacket.Packet, error) {
81 if s.fail {
82 return nil, errors.New("mocked-error")
Matteo Scandolo27428702019-10-11 16:21:16 -070083 }
Matteo Scandolo90d08f62020-10-29 12:06:55 -070084 return pkt, nil
85}
86
87func createTestDhcpPacket(t *testing.T) gopacket.Packet {
88 dhcp := &layers.DHCPv4{
89 Operation: layers.DHCPOpRequest,
90 }
91
92 buffer := gopacket.NewSerializeBuffer()
93 opts := gopacket.SerializeOptions{FixLengths: true}
94 err := gopacket.SerializeLayers(buffer, opts, dhcp)
95 if err != nil {
96 t.Fatal(err)
97 }
98
99 return gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeDHCPv4, gopacket.DecodeOptions{})
Matteo Scandolo27428702019-10-11 16:21:16 -0700100}