|
| 1 | +package collectors |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +func TestLSMODRunSuccess(t *testing.T) { |
| 13 | + assert := assert.New(t) |
| 14 | + |
| 15 | + var dataBlob KernMod |
| 16 | + dataBlob.KernModProfileID = "0da8174a9f9c4a1f195d9af9804f24ba29e8431467e8203ae5663683292481e4" |
| 17 | + dataBlob.KernModProfile = []string{"acpi_tad", "autofs4", "ccp", "dmi_sysfs", "efi_pstore", "hid_generic", "i2c_algo_bit", "i2c_piix4", "i2c_smbus", "input_leds", "ip_tables", "joydev", "k10temp", "lp", "mac_hid", "mc", "msr", "nfnetlink", "nvme", "nvme_core", "parport", "parport_pc", "ppdev", "psmouse", "r8169", "rc_core", "realtek", "sch_fq_codel", "snd_pci_acp3x", "snd_soc_acpi", "soundcore", "usbhid", "x_tables"} |
| 18 | + |
| 19 | + kernModTestData :=`rc_core 73728 1 cec |
| 20 | +snd_soc_acpi 16384 3 snd_sof_amd_acp,snd_acp_config,snd_pci_ps |
| 21 | +i2c_piix4 32768 0 |
| 22 | +mc 81920 5 videodev,snd_usb_audio,videobuf2_v4l2,uvcvideo,videobuf2_common |
| 23 | +k10temp 16384 0 |
| 24 | +i2c_algo_bit 16384 1 amdgpu |
| 25 | +i2c_smbus 20480 1 i2c_piix4 |
| 26 | +soundcore 16384 1 snd |
| 27 | +ccp 155648 1 kvm_amd |
| 28 | +snd_pci_acp3x 16384 0 |
| 29 | +input_leds 12288 0 |
| 30 | +joydev 32768 0 |
| 31 | +acpi_tad 20480 0 |
| 32 | +mac_hid 12288 0 |
| 33 | +sch_fq_codel 24576 4 |
| 34 | +msr 12288 0 |
| 35 | +parport_pc 53248 0 |
| 36 | +ppdev 24576 0 |
| 37 | +lp 28672 0 |
| 38 | +parport 73728 3 parport_pc,lp,ppdev |
| 39 | +efi_pstore 12288 0 |
| 40 | +nfnetlink 20480 5 nft_compat,nf_tables,ip_set |
| 41 | +dmi_sysfs 24576 0 |
| 42 | +ip_tables 32768 0 |
| 43 | +x_tables 65536 9 xt_conntrack,nft_compat,xt_tcpudp,xt_addrtype,xt_CHECKSUM,xt_set,ipt_REJECT,ip_tables,xt_MASQUERADE |
| 44 | +autofs4 57344 2 |
| 45 | +hid_generic 12288 0 |
| 46 | +usbhid 77824 0 |
| 47 | +nvme 61440 2 |
| 48 | +psmouse 217088 0 |
| 49 | +r8169 126976 0 |
| 50 | +nvme_core 225280 3 nvme |
| 51 | +realtek 49152 2 |
| 52 | +` |
| 53 | + |
| 54 | + mockUtilExecute(kernModTestData, nil) |
| 55 | + expected := Result{"KernModData": dataBlob} |
| 56 | + |
| 57 | + collector := LSMOD{} |
| 58 | + result, err := collector.run(ARCHITECTURE_X86_64) |
| 59 | + |
| 60 | + assert.Equal(expected, result) |
| 61 | + assert.Nil(err) |
| 62 | +} |
| 63 | + |
| 64 | +func TestLSMODRunFail(t *testing.T) { |
| 65 | + assert := assert.New(t) |
| 66 | + |
| 67 | + mockUtilExecute("", fmt.Errorf("forced error")) |
| 68 | + expected := Result{"KernModData": nil} |
| 69 | + |
| 70 | + collector := LSMOD{} |
| 71 | + result, err := collector.run(ARCHITECTURE_X86_64) |
| 72 | + |
| 73 | + assert.Equal(expected, result) |
| 74 | + assert.ErrorContains(err, "forced error") |
| 75 | +} |
| 76 | + |
| 77 | +func TestKernModChkSum(t *testing.T) { |
| 78 | + assert := assert.New(t) |
| 79 | + |
| 80 | + err := putKernModChecksum("/tmp/testPutCheckSum", "testthis") |
| 81 | + assert.Nil(err) |
| 82 | + content, err1 := os.ReadFile("/tmp/testPutCheckSum") |
| 83 | + assert.Nil(err1) |
| 84 | + assert.Equal("testthis", string(content)) |
| 85 | + |
| 86 | +} |
| 87 | + |
| 88 | +func TestGetKernModChkSum(t *testing.T) { |
| 89 | + assert := assert.New(t) |
| 90 | + mockUtilFileExists(true) |
| 91 | + KernModChecksumFile := "/etc/zypp/chksum-kernmoddata.txt" |
| 92 | + mockLocalOsReadfile(t, KernModChecksumFile , "TestThis") |
| 93 | + |
| 94 | + tstChkSum := getKernModChecksum(KernModChecksumFile) |
| 95 | + assert.Equal("TestThis", tstChkSum) |
| 96 | +} |
| 97 | + |
| 98 | +func TestGetKernModChkSumNoFile(t *testing.T) { |
| 99 | + assert := assert.New(t) |
| 100 | + mockUtilFileExists(false) |
| 101 | + |
| 102 | + tstChkSum := getKernModChecksum("/etc/zypp/chksum-kernmoddata.txt") |
| 103 | + assert.Equal("", tstChkSum) |
| 104 | +} |
0 commit comments