Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee1afaa

Browse files
Vicente-Chengbk201
authored andcommitted
ci: add test for duplicated disk
- Also add some helper function to handle the XML file for libvirt Disk Signed-off-by: Vicente Cheng <[email protected]>
1 parent fc5a2cd commit ee1afaa

3 files changed

Lines changed: 108 additions & 1 deletion

File tree

ci/scripts/upgrade_ndm.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ sleep 10 # wait 10 seconds for ndm start to respwan pods
6060

6161
wait_ndm_ready
6262
# check image
63-
pod_name=$(kubectl get pods -n harvester-system |grep ^harvester-node-disk-manager|head -n1 |awk '{print $1}')
63+
pod_name=$(kubectl get pods -n harvester-system |grep Running |grep ^harvester-node-disk-manager|head -n1 |awk '{print $1}')
6464
container_img=$(kubectl get pods ${pod_name} -n harvester-system -o yaml |yq -e .spec.containers[0].image |tr ":" \n)
6565
yaml_img=$(yq -e .image.repository ndm-override.yaml)
6666
if grep -q ${yaml_img} <<< ${container_img}; then

pkg/utils/virt.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package utils
2+
3+
import (
4+
"encoding/xml"
5+
"fmt"
6+
"os"
7+
)
8+
9+
type Disk struct {
10+
XMLName xml.Name `xml:"disk"`
11+
Type string `xml:"type,attr"`
12+
Device string `xml:"device,attr"`
13+
Driver Driver `xml:"driver"`
14+
Source Source `xml:"source"`
15+
Target Target `xml:"target"`
16+
WWN string `xml:"wwn"`
17+
}
18+
19+
type Driver struct {
20+
Name string `xml:"name,attr"`
21+
Type string `xml:"type,attr"`
22+
}
23+
24+
type Source struct {
25+
File string `xml:"file,attr"`
26+
}
27+
28+
type Target struct {
29+
Dev string `xml:"dev,attr"`
30+
Bus string `xml:"bus,attr"`
31+
}
32+
33+
// DiskXMLReader can read the libvirt disk xml file and return a Disk struct
34+
func DiskXMLReader(filePath string) (Disk, error) {
35+
f, err := os.Open(filePath)
36+
if err != nil {
37+
return Disk{}, fmt.Errorf("open file(%s) error: %v", filePath, err)
38+
}
39+
40+
defer f.Close()
41+
42+
var disk Disk
43+
err = xml.NewDecoder(f).Decode(&disk)
44+
if err != nil {
45+
return Disk{}, fmt.Errorf("decode XML Error: %v", err)
46+
}
47+
48+
return disk, nil
49+
}
50+
51+
// XMLWriter write XML to target file, make sure your xmlData should valid
52+
func XMLWriter(targetFilePath string, xmlData any) error {
53+
// Create a new file for writing
54+
targetFile, err := os.Create(targetFilePath)
55+
if err != nil {
56+
return fmt.Errorf("create file(%s) error: %v", targetFilePath, err)
57+
}
58+
defer targetFile.Close()
59+
60+
// Encode the disk data and write it to the output file
61+
encoder := xml.NewEncoder(targetFile)
62+
err = encoder.Encode(xmlData)
63+
if err != nil {
64+
return fmt.Errorf("encod XML Error: %v", err)
65+
}
66+
67+
return nil
68+
}

tests/integration/test_1_disk_hotplug_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
diskv1 "github.com/harvester/node-disk-manager/pkg/apis/harvesterhci.io/v1beta1"
2222
clientset "github.com/harvester/node-disk-manager/pkg/generated/clientset/versioned"
23+
"github.com/harvester/node-disk-manager/pkg/utils"
2324
)
2425

2526
/*
@@ -44,6 +45,7 @@ type HotPlugTestSuite struct {
4445
clientSet *clientset.Clientset
4546
targetNodeName string
4647
targetDiskName string
48+
curBusPath string // to make sure which path we deployed
4749
}
4850

4951
func (s *HotPlugTestSuite) SetupSuite() {
@@ -148,6 +150,43 @@ func (s *HotPlugTestSuite) Test_2_HotPlugAddDisk() {
148150
require.Equal(s.T(), err, nil, "Get Blockdevices should not get error")
149151

150152
require.Equal(s.T(), curBlockdevice.Status.State, diskv1.BlockDeviceActive, "Disk status should be inactive after we add disk")
153+
s.curBusPath = curBlockdevice.Status.DeviceStatus.Details.BusPath
154+
}
155+
156+
func (s *HotPlugTestSuite) Test_3_AddDuplicatedWWNDsik() {
157+
// create another another disk raw file and xml
158+
const (
159+
originalDeviceRaw = "/tmp/hotplug_disks/node1-sda.qcow2"
160+
duplicatedDeviceXML = "/tmp/hotplug_disks/node1-sdb.xml"
161+
duplicatedDeviceRaw = "/tmp/hotplug_disks/node1-sdb.qcow2"
162+
)
163+
cmdCpyRawFile := fmt.Sprintf("cp %s %s", originalDeviceRaw, duplicatedDeviceRaw)
164+
_, _, err := doCommand(cmdCpyRawFile)
165+
require.Equal(s.T(), err, nil, "Running command `cp the raw device file` should not get error")
166+
167+
disk, err := utils.DiskXMLReader(hotplugDiskXMLFileName)
168+
require.Equal(s.T(), err, nil, "Read xml file should not get error")
169+
disk.Source.File = duplicatedDeviceRaw
170+
disk.Target.Dev = "sdb"
171+
err = utils.XMLWriter(duplicatedDeviceXML, disk)
172+
require.Equal(s.T(), err, nil, "Write xml file should not get error")
173+
174+
cmd := fmt.Sprintf("virsh attach-device --domain %s --file %s --live", hotplugTargetNodeName, duplicatedDeviceXML)
175+
_, _, err = doCommand(cmd)
176+
require.Equal(s.T(), err, nil, "Running command `virsh attach-device` should not get error")
177+
178+
// wait for controller handling
179+
time.Sleep(5 * time.Second)
180+
181+
// check disk status
182+
require.NotEqual(s.T(), s.targetDiskName, "", "target disk name should not be empty before we start hotplug (add) test")
183+
bdi := s.clientSet.HarvesterhciV1beta1().BlockDevices("longhorn-system")
184+
blockdeviceList, err := bdi.List(context.TODO(), v1.ListOptions{})
185+
require.Equal(s.T(), err, nil, "Get BlockdevicesList should not get error")
186+
require.Equal(s.T(), 1, len(blockdeviceList.Items), "We should have one disks because duplicated wwn should not added")
187+
curBlockdevice, err := bdi.Get(context.TODO(), s.targetDiskName, v1.GetOptions{})
188+
require.Equal(s.T(), err, nil, "Get Blockdevices should not get error")
189+
require.Equal(s.T(), s.curBusPath, curBlockdevice.Status.DeviceStatus.Details.BusPath, "Disk path should not replace by duplicated wwn disk")
151190

152191
}
153192

0 commit comments

Comments
 (0)