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