/
mocks.go
114 lines (89 loc) · 2.63 KB
/
mocks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// (C) Copyright 2020-2022 Intel Corporation.
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
package test
import (
"fmt"
"math"
"net"
"strings"
"github.com/google/uuid"
)
var hostAddrs = make(map[int32]*net.TCPAddr)
// MockListPoolsResult mocks list pool results.
type MockListPoolsResult struct {
Status int32
Err error
}
// GetIndex return suitable index value for auto generating mocks.
func GetIndex(varIdx ...int32) int32 {
if len(varIdx) == 0 {
varIdx = append(varIdx, 1)
}
return varIdx[0]
}
// MockUUID returns mock UUID values for use in tests.
func MockUUID(varIdx ...int32) string {
idx := GetIndex(varIdx...)
return fmt.Sprintf("%08d-%04d-%04d-%04d-%012d", idx, idx, idx, idx, idx)
}
// MockPoolUUID returns mock pool UUID values for use in tests.
func MockPoolUUID(varIdx ...int32) uuid.UUID {
idx := GetIndex(varIdx...)
return uuid.MustParse(fmt.Sprintf("%08d-%04d-%04d-%04d-%012d", idx, idx, idx, idx, idx))
}
// MockHostAddr returns mock tcp addresses for use in tests.
func MockHostAddr(varIdx ...int32) *net.TCPAddr {
idx := GetIndex(varIdx...)
if _, exists := hostAddrs[idx]; !exists {
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("10.0.0.%d:10001", idx))
if err != nil {
panic(err)
}
hostAddrs[idx] = addr
}
return hostAddrs[idx]
}
// MockPCIAddr returns mock PCIAddr values for use in tests.
func MockPCIAddr(varIdx ...int32) string {
idx := GetIndex(varIdx...)
return fmt.Sprintf("0000:%02d:00.0", idx)
}
// MockPCIAddrs returns slice of mock PCIAddr values for use in tests.
func MockPCIAddrs(idxs ...int) (addrs []string) {
for _, i := range idxs {
addrs = append(addrs, MockPCIAddr(int32(i)))
}
return
}
// MockVMDPCIAddr returns mock PCIAddr values for use in tests.
// VMD PCI address domains start at 0x10000 to avoid overlapping with standard ACPI addresses.
func MockVMDPCIAddr(dom int32, varIdx ...int32) string {
idx := GetIndex(varIdx...)
return fmt.Sprintf("%06x:%02x:00.0", (math.MaxUint16+1)*dom, idx)
}
// MockVMDPCIAddrs returns slice of mock VMD PCIAddr values for use in tests.
func MockVMDPCIAddrs(dom int, idxs ...int) (addrs []string) {
for _, i := range idxs {
addrs = append(addrs, MockVMDPCIAddr(int32(dom), int32(i)))
}
return
}
// MockWriter is a mock io.Writer that can be used to inject errors and check
// values written.
type MockWriter struct {
builder strings.Builder
WriteErr error
}
func (w *MockWriter) Write(p []byte) (int, error) {
if w.WriteErr != nil {
return 0, w.WriteErr
}
return w.builder.Write(p)
}
// GetWritten gets the string value written using Write.
func (w *MockWriter) GetWritten() string {
return w.builder.String()
}