Skip to content

Commit 9dfa5dc

Browse files
committed
Unit test: MachineInfo Clone() method
Add a unit test that ensures that Clone() method clones all fields and that the cloned struct equals the original. To avoid missing fields in the future, the test fails if one of the struct's fields is populated with a zero-value. The unit test uses reflection to achieve that. Signed-off-by: Itamar Holder <[email protected]>
1 parent 587b475 commit 9dfa5dc

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

info/v1/machine_test.go

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright 2023 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package v1
16+
17+
import (
18+
"reflect"
19+
"testing"
20+
"time"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestMachineInfoClone(t *testing.T) {
26+
machineInfo := getFakeMachineInfo()
27+
28+
// Make sure all fields are populated
29+
machineInfoReflection := reflect.ValueOf(machineInfo)
30+
for i := 0; i < machineInfoReflection.NumField(); i++ {
31+
assert.Falsef(t, machineInfoReflection.Field(i).IsZero(), "field %s is not populated", machineInfoReflection.Type().Field(i).Name)
32+
}
33+
34+
clonedMachineInfo := *machineInfo.Clone()
35+
assert.Equal(t, machineInfo, clonedMachineInfo, "cloned machine info should be equal to the original")
36+
}
37+
38+
func getFakeMachineInfo() MachineInfo {
39+
return MachineInfo{
40+
Timestamp: time.Now(),
41+
CPUVendorID: "fake-id",
42+
NumCores: 1,
43+
NumPhysicalCores: 2,
44+
NumSockets: 3,
45+
CpuFrequency: 4,
46+
MemoryCapacity: 5,
47+
SwapCapacity: 6,
48+
MemoryByType: map[string]*MemoryInfo{
49+
"fake": {Capacity: 2, DimmCount: 3},
50+
},
51+
NVMInfo: NVMInfo{
52+
MemoryModeCapacity: 2,
53+
AppDirectModeCapacity: 6,
54+
AvgPowerBudget: 3,
55+
},
56+
HugePages: []HugePagesInfo{{
57+
PageSize: 512,
58+
NumPages: 343,
59+
}},
60+
MachineID: "fake-machine-id",
61+
SystemUUID: "fake-uuid",
62+
BootID: "fake-boot-id",
63+
Filesystems: []FsInfo{{
64+
Device: "dev",
65+
DeviceMajor: 1,
66+
DeviceMinor: 2,
67+
Capacity: 3,
68+
Type: "fake-type",
69+
Inodes: 4,
70+
HasInodes: true,
71+
}},
72+
DiskMap: map[string]DiskInfo{"fake-disk": {
73+
Name: "fake",
74+
Major: 1,
75+
Minor: 2,
76+
Size: 3,
77+
Scheduler: "sched",
78+
}},
79+
NetworkDevices: []NetInfo{{
80+
Name: "fake-net-info",
81+
MacAddress: "123",
82+
Speed: 2,
83+
Mtu: 3,
84+
}},
85+
Topology: []Node{{
86+
Id: 1,
87+
Memory: 2,
88+
}},
89+
CloudProvider: "fake-provider",
90+
InstanceType: "fake-instance-type",
91+
InstanceID: "fake-instance-id",
92+
}
93+
}

0 commit comments

Comments
 (0)