blob: 9ee945e045fc053002e5cef475852a9afb1802ff [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 Scandolo401503a2019-12-11 14:48:14 -080022 "github.com/google/gopacket/pcap"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010023 "testing"
24
Matteo Scandolo27428702019-10-11 16:21:16 -070025 "github.com/opencord/bbsim/internal/bbsim/types"
26 "gotest.tools/assert"
Matteo Scandolo27428702019-10-11 16:21:16 -070027)
28
29func 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
39func 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
49func TestCreateNNIPair(t *testing.T) {
50
51 startDHCPServerCalled := false
52 _startDHCPServer := startDHCPServer
53 defer func() { startDHCPServer = _startDHCPServer }()
Zdravko Bozakov681364d2019-11-10 14:28:46 +010054 startDHCPServer = func(upstreamVeth string, dhcpServerIp string) error {
Matteo Scandolo27428702019-10-11 16:21:16 -070055 startDHCPServerCalled = true
56 return nil
57 }
58
59 listenOnVethCalled := false
60 _listenOnVeth := listenOnVeth
61 defer func() { listenOnVeth = _listenOnVeth }()
Matteo Scandolo401503a2019-12-11 14:48:14 -080062 listenOnVeth = func(vethName string) (chan *types.PacketMsg, *pcap.Handle, error) {
Matteo Scandolo27428702019-10-11 16:21:16 -070063 listenOnVethCalled = true
Matteo Scandolo401503a2019-12-11 14:48:14 -080064 return make(chan *types.PacketMsg, 1), nil, nil
Matteo Scandolo27428702019-10-11 16:21:16 -070065 }
66 spy := &ExecutorSpy{
67 failRun: false,
68 Calls: make(map[int][]string),
69 }
70
71 olt := OltDevice{}
Zdravko Bozakov681364d2019-11-10 14:28:46 +010072 nni := NniPort{}
Matteo Scandolo27428702019-10-11 16:21:16 -070073
Zdravko Bozakov681364d2019-11-10 14:28:46 +010074 err := createNNIPair(spy, &olt, &nni)
Matteo Scandolo401503a2019-12-11 14:48:14 -080075 olt.nniPktInChannel, olt.nniHandle, _ = nni.NewVethChan()
Matteo Scandolo27428702019-10-11 16:21:16 -070076
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
84type ExecutorSpy struct {
85 failRun bool
86
87 CommandCallCount int
88 RunCallCount int
89 Calls map[int][]string
90}
91
92func (s *ExecutorSpy) Command(name string, arg ...string) Runnable {
93 s.CommandCallCount++
94
95 s.Calls[s.CommandCallCount] = arg
96
97 return s
98}
99
100func (s *ExecutorSpy) Run() error {
101 s.RunCallCount++
102 if s.failRun {
103 return errors.New("fake-error")
104 }
105 return nil
106}