Skip to content

Commit

Permalink
Extract ReleaseType from zypper output
Browse files Browse the repository at this point in the history
This is added to match original behavior for custom/OEM release types.
  • Loading branch information
skazi0 committed Nov 10, 2021
1 parent 349d901 commit 5fafecb
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/connect/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type Product struct {
IsBase bool `xml:"isbase,attr" json:"base"`

FriendlyName string `json:"friendly_name,omitempty"`
ReleaseType string `json:"release_type,omitempty"`
ReleaseType string `xml:"registerrelease,attr" json:"release_type,omitempty"`
ProductLine string `xml:"productline,attr"`
Available bool `json:"available"`
Free bool `json:"free"`
Recommended bool `json:"recommended"`
Expand Down
30 changes: 30 additions & 0 deletions internal/connect/zypper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package connect

import (
"encoding/xml"
"fmt"
"os"
"path/filepath"
"strings"
)

const (
zypperPath = "/usr/bin/zypper"
oemPath = "/var/lib/suseRegister/OEM"
)

const (
Expand Down Expand Up @@ -58,6 +62,26 @@ func installedProducts() ([]Product, error) {
return parseProductsXML(output)
}

// get first line of OEM file if present
func oemReleaseType(productLine string) (string, error) {
if productLine == "" {
return "", fmt.Errorf("empty productline")
}
oemFile := filepath.Join(CFG.FsRoot, oemPath, productLine)
if !fileExists(oemFile) {
return "", fmt.Errorf("OEM file not found: %v", oemFile)
}
data, err := os.ReadFile(oemFile)
if err != nil {
return "", err
}
lines := strings.Split(string(data), "\n")
if len(lines) == 0 {
return "", fmt.Errorf("empty OEM file: %v", oemFile)
}
return strings.TrimSpace(lines[0]), nil
}

// parseProductsXML returns products parsed from zypper XML
func parseProductsXML(xmlDoc []byte) ([]Product, error) {
var products struct {
Expand All @@ -66,6 +90,12 @@ func parseProductsXML(xmlDoc []byte) ([]Product, error) {
if err := xml.Unmarshal(xmlDoc, &products); err != nil {
return []Product{}, err
}
// override ProductType with OEM value if defined
for i, p := range products.Products {
if oemValue, err := oemReleaseType(p.ProductLine); err == nil {
products.Products[i].ReleaseType = oemValue
}
}
return products.Products, nil
}

Expand Down
36 changes: 36 additions & 0 deletions internal/connect/zypper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package connect

import (
"os"
"path/filepath"
"testing"
)

Expand All @@ -17,6 +19,40 @@ func TestParseProductsXML(t *testing.T) {
}
}

func TestParseProductsXML_ReleaseType(t *testing.T) {
CFG.FsRoot = t.TempDir()
// write test OEM file
testRT := "SLES-OEM-TEST"
oemDir := filepath.Join(CFG.FsRoot, oemPath)
if err := os.MkdirAll(oemDir, 0755); err != nil {
t.Fatalf("Error creating OEM dir: %v", err)
}
if err := os.WriteFile(filepath.Join(oemDir, "sles"), []byte(testRT), 0600); err != nil {
t.Fatalf("Error writing OEM file: %v", err)
}

products, err := parseProductsXML(readTestFile("products-rt.xml", t))
if err != nil {
t.Errorf("Unexpected error: %s", err)
}

if products[0].ReleaseType != "" {
t.Errorf("Expected empty ReleaseType Got %v", products[0].ReleaseType)
}
if products[1].ReleaseType != testRT {
t.Errorf("Expected ReleaseType=%v Got %v", testRT, products[1].ReleaseType)
}
if products[2].ReleaseType != testRT {
t.Errorf("Expected ReleaseType=%v Got %v", testRT, products[2].ReleaseType)
}
if products[3].ReleaseType != "rel2" {
t.Errorf("Expected ReleaseType=rel2 Got %v", products[3].ReleaseType)
}
if products[4].ReleaseType != "" {
t.Errorf("Expected empty ReleaseType Got %v", products[4].ReleaseType)
}
}

func TestParseServicesXML(t *testing.T) {
services, err := parseServicesXML(readTestFile("services.xml", t))
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions testdata/products-rt.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version='1.0'?>
<stream>
<product-list>
<product name="suse-openstack-cloud" version="8" release="0" arch="x86_64" productline="suse-openstack-cloud" registerrelease=""></product>
<product name="SLES" version="12.5" release="0" arch="x86_64" productline="sles" registerrelease=""></product>
<product name="SLES-OEM-test1" productline="sles" registerrelease="rel1"></product>
<product name="SLES-OEM-test2" productline="" registerrelease="rel2"></product>
<product name="SLES-OEM-test3" productline="" registerrelease=""></product>
</product-list>
</stream>

0 comments on commit 5fafecb

Please sign in to comment.