Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 1 | /* |
| 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 |
| 18 | package devices |
| 19 | |
| 20 | import ( |
| 21 | "errors" |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 22 | "github.com/google/gopacket/pcap" |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 23 | "testing" |
| 24 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 25 | "github.com/opencord/bbsim/internal/bbsim/types" |
| 26 | "gotest.tools/assert" |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | func TestSetVethUpSuccess(t *testing.T) { |
| 30 | spy := &ExecutorSpy{ |
| 31 | Calls: make(map[int][]string), |
| 32 | } |
| 33 | err := setVethUp(spy, "test_veth") |
| 34 | assert.Equal(t, spy.CommandCallCount, 1) |
| 35 | assert.Equal(t, spy.Calls[1][2], "test_veth") |
| 36 | assert.Equal(t, err, nil) |
| 37 | } |
| 38 | |
| 39 | func TestSetVethUpFail(t *testing.T) { |
| 40 | spy := &ExecutorSpy{ |
| 41 | failRun: true, |
| 42 | Calls: make(map[int][]string), |
| 43 | } |
| 44 | err := setVethUp(spy, "test_veth") |
| 45 | assert.Equal(t, spy.CommandCallCount, 1) |
| 46 | assert.Equal(t, err.Error(), "fake-error") |
| 47 | } |
| 48 | |
| 49 | func TestCreateNNIPair(t *testing.T) { |
| 50 | |
| 51 | startDHCPServerCalled := false |
| 52 | _startDHCPServer := startDHCPServer |
| 53 | defer func() { startDHCPServer = _startDHCPServer }() |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 54 | startDHCPServer = func(upstreamVeth string, dhcpServerIp string) error { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 55 | startDHCPServerCalled = true |
| 56 | return nil |
| 57 | } |
| 58 | |
| 59 | listenOnVethCalled := false |
| 60 | _listenOnVeth := listenOnVeth |
| 61 | defer func() { listenOnVeth = _listenOnVeth }() |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 62 | listenOnVeth = func(vethName string) (chan *types.PacketMsg, *pcap.Handle, error) { |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 63 | listenOnVethCalled = true |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 64 | return make(chan *types.PacketMsg, 1), nil, nil |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 65 | } |
| 66 | spy := &ExecutorSpy{ |
| 67 | failRun: false, |
| 68 | Calls: make(map[int][]string), |
| 69 | } |
| 70 | |
| 71 | olt := OltDevice{} |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 72 | nni := NniPort{} |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 73 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 74 | err := createNNIPair(spy, &olt, &nni) |
Matteo Scandolo | 401503a | 2019-12-11 14:48:14 -0800 | [diff] [blame] | 75 | olt.nniPktInChannel, olt.nniHandle, _ = nni.NewVethChan() |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 76 | |
| 77 | assert.Equal(t, spy.CommandCallCount, 3) |
| 78 | assert.Equal(t, startDHCPServerCalled, true) |
| 79 | assert.Equal(t, listenOnVethCalled, true) |
| 80 | assert.Equal(t, err, nil) |
| 81 | assert.Assert(t, olt.nniPktInChannel != nil) |
| 82 | } |
| 83 | |
| 84 | type ExecutorSpy struct { |
| 85 | failRun bool |
| 86 | |
| 87 | CommandCallCount int |
| 88 | RunCallCount int |
| 89 | Calls map[int][]string |
| 90 | } |
| 91 | |
| 92 | func (s *ExecutorSpy) Command(name string, arg ...string) Runnable { |
| 93 | s.CommandCallCount++ |
| 94 | |
| 95 | s.Calls[s.CommandCallCount] = arg |
| 96 | |
| 97 | return s |
| 98 | } |
| 99 | |
| 100 | func (s *ExecutorSpy) Run() error { |
| 101 | s.RunCallCount++ |
| 102 | if s.failRun { |
| 103 | return errors.New("fake-error") |
| 104 | } |
| 105 | return nil |
| 106 | } |