Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 3881b73 | 2022-12-27 07:55:37 -0500 | [diff] [blame^] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 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 |
| 18 | package devices |
| 19 | |
| 20 | import ( |
| 21 | "errors" |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 22 | "github.com/google/gopacket" |
| 23 | "github.com/google/gopacket/layers" |
David K. Bainbridge | c415efe | 2021-08-19 13:05:21 +0000 | [diff] [blame] | 24 | "github.com/opencord/voltha-protos/v5/go/openolt" |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 25 | "github.com/stretchr/testify/assert" |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 26 | "testing" |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 29 | func TestCreateNNI(t *testing.T) { |
| 30 | olt := OltDevice{ |
| 31 | ID: 0, |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 32 | } |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 33 | 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 Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 41 | func 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 Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 48 | } |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 49 | |
| 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 Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 74 | type mockDhcpServer struct { |
| 75 | callCount int |
| 76 | fail bool |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 79 | // being a Mock I just return the same packet I got |
| 80 | func (s mockDhcpServer) HandleServerPacket(pkt gopacket.Packet) (gopacket.Packet, error) { |
| 81 | if s.fail { |
| 82 | return nil, errors.New("mocked-error") |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 83 | } |
Matteo Scandolo | 90d08f6 | 2020-10-29 12:06:55 -0700 | [diff] [blame] | 84 | return pkt, nil |
| 85 | } |
| 86 | |
| 87 | func 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 Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 100 | } |