blob: 348d40fda4956704b775d49faa827fa962c6d103 [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"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010022 "testing"
23
Matteo Scandolo27428702019-10-11 16:21:16 -070024 "github.com/opencord/bbsim/internal/bbsim/types"
25 "gotest.tools/assert"
Matteo Scandolo27428702019-10-11 16:21:16 -070026)
27
28func 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
38func 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
48func TestCreateNNIPair(t *testing.T) {
49
50 startDHCPServerCalled := false
51 _startDHCPServer := startDHCPServer
52 defer func() { startDHCPServer = _startDHCPServer }()
Zdravko Bozakov681364d2019-11-10 14:28:46 +010053 startDHCPServer = func(upstreamVeth string, dhcpServerIp string) error {
Matteo Scandolo27428702019-10-11 16:21:16 -070054 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 Bozakov681364d2019-11-10 14:28:46 +010071 nni := NniPort{}
Matteo Scandolo27428702019-10-11 16:21:16 -070072
Zdravko Bozakov681364d2019-11-10 14:28:46 +010073 err := createNNIPair(spy, &olt, &nni)
74 olt.nniPktInChannel, _ = nni.NewVethChan()
Matteo Scandolo27428702019-10-11 16:21:16 -070075
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
83type ExecutorSpy struct {
84 failRun bool
85
86 CommandCallCount int
87 RunCallCount int
88 Calls map[int][]string
89}
90
91func (s *ExecutorSpy) Command(name string, arg ...string) Runnable {
92 s.CommandCallCount++
93
94 s.Calls[s.CommandCallCount] = arg
95
96 return s
97}
98
99func (s *ExecutorSpy) Run() error {
100 s.RunCallCount++
101 if s.failRun {
102 return errors.New("fake-error")
103 }
104 return nil
105}