Skip to content

Commit

Permalink
ACM-15078: Created new function extractCmdlineParams and updated unit…
Browse files Browse the repository at this point in the history
… test case for the same

Signed-off-by: DAMISETTI-VEERABHADRARAO <[email protected]>
  • Loading branch information
veera-damisetti authored and openshift-cherrypick-robot committed Feb 6, 2025
1 parent 443238e commit f1125ca
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/commands/actions/reboot_for_reclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (a *rebootForReclaim) Run() (stdout, stderr string, exitCode int) {

if runtime.GOARCH == "s390x" {
var options string
cmdline_params := make(map[string]string)
var requiredCmdline string

stdout, stderr, exitCode = util.Execute("cat", "/boot/loader/entries/00-assisted-discovery.conf")
Expand Down Expand Up @@ -61,17 +60,7 @@ func (a *rebootForReclaim) Run() (stdout, stderr string, exitCode int) {
"rd.dasd",
}

for _, param := range paramsToExtract {
regex := regexp.MustCompile(fmt.Sprintf(`\b%s=([^\s]+)`, param))
match := regex.FindStringSubmatch(stdout)
if len(match) > 1 {
cmdline_params[param] = match[1]
}
}

for key, value := range cmdline_params {
requiredCmdline += fmt.Sprintf("%s=%s ", key, value)
}
requiredCmdline = extractCmdlineParams(stdout, paramsToExtract)

unshareCommand := "unshare"
unshareArgs := []string{
Expand All @@ -93,6 +82,30 @@ func (a *rebootForReclaim) Run() (stdout, stderr string, exitCode int) {
return util.Execute("systemctl", "reboot")
}

// Returns the paramsToExtract parameters which are present in cmdlineOutput, if no paramter matched then returns any empty string ''

func extractCmdlineParams(cmdlineOutput string, paramsToExtract []string) string {
cmdlineParams := make(map[string]string)
var requiredCmdline string

// Matches the exact param from paramsToExtract followed by an = sign, and then capture the non-whitespace value after the =
for _, param := range paramsToExtract {
regex := regexp.MustCompile(fmt.Sprintf(`\b%s=([^\s]+)`, param))
match := regex.FindStringSubmatch(cmdlineOutput)
if len(match) > 1 {
cmdlineParams[param] = match[1]
}
}

// Convert the key value pairs in map to string with predefined order which is in paramsToExtract
for _, key := range paramsToExtract {
if value, exists := cmdlineParams[key]; exists {
requiredCmdline += fmt.Sprintf("%s=%s ", key, value)
}
}
return requiredCmdline
}

// Unused, but required as part of ActionInterface
func (a *rebootForReclaim) Command() string {
return "systemctl"
Expand Down
21 changes: 21 additions & 0 deletions src/commands/actions/reboot_for_reclaim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,25 @@ var _ = Describe("reboot_for_reclaim", func() {
It("fails when given bad input", func() {
badParamsCommonTests(models.StepTypeRebootForReclaim, []string{})
})

Context("extractCmdlineParams", func() {
It("returns the expected parameters when valid cmdline output is given", func() {
cmdlineOutput := "ip=192.168.1.1 nameserver=8.8.8.8 rd.znet=enabled zfcp.allow_lun_scan=0 rd.zfcp=xyz rd.dasd=abc"
paramsToExtract := []string{"ip", "nameserver", "rd.znet", "zfcp.allow_lun_scan", "rd.zfcp", "rd.dasd"}

expectedCmdline := "ip=192.168.1.1 nameserver=8.8.8.8 rd.znet=enabled zfcp.allow_lun_scan=0 rd.zfcp=xyz rd.dasd=abc "
requiredCmdline := extractCmdlineParams(cmdlineOutput, paramsToExtract)

Expect(requiredCmdline).To(Equal(expectedCmdline))
})

It("returns an empty string when no parameters match", func() {
cmdlineOutput := "other_param=value"
paramsToExtract := []string{"ip", "nameserver"}

requiredCmdline := extractCmdlineParams(cmdlineOutput, paramsToExtract)

Expect(requiredCmdline).To(Equal(""))
})
})
})

0 comments on commit f1125ca

Please sign in to comment.