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