Skip to content

Commit

Permalink
Tests: Add test for Client.CreateSystem
Browse files Browse the repository at this point in the history
This commit also fixes the Windows test issues and fixes the implementation of "updateCobblerFields" to make the test pass.
  • Loading branch information
SchoolGuy committed Aug 20, 2024
1 parent 325386d commit 0ff47d4
Show file tree
Hide file tree
Showing 87 changed files with 1,918 additions and 145 deletions.
45 changes: 21 additions & 24 deletions cobblerclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ import (
"bytes"
"errors"
"fmt"
"github.com/go-viper/mapstructure/v2"
"github.com/kolo/xmlrpc"
"io"
"io/ioutil"
"net/http"
"reflect"
"strings"

"github.com/go-viper/mapstructure/v2"
"github.com/kolo/xmlrpc"
)

const bodyTypeXML = "text/xml"
Expand Down Expand Up @@ -343,23 +341,27 @@ func decodeCobblerItem(raw interface{}, result interface{}) (interface{}, error)
// updateCobblerFields updates all fields in a Cobbler Item structure.
func (c *Client) updateCobblerFields(what string, item reflect.Value, id string) error {
method := fmt.Sprintf("modify_%s", what)

typeOfT := item.Type()

// In Cobbler v3.3.0, if profile name isn't created first, an empty child gets written to the distro, which causes
// a ValueError: "calling find with no arguments" TO-DO: figure a more efficient way of targeting name.
for i := 0; i < item.NumField(); i++ {
v := item.Field(i)
fieldType := v.Type().Name()
tag := typeOfT.Field(i).Tag
field := tag.Get("mapstructure")
if method == "modify_profile" && field == "name" {
var value interface{}
switch v.Type().String() {
case "string", "bool", "int64", "int":
value = v.Interface()
case "[]string":
value = strings.Join(v.Interface().([]string), " ")

if fieldType == "Item" {
// Update embedded Item struct if present (should be present once on all items)
err := c.updateCobblerFields(what, reflect.ValueOf(v.Interface()), id)
if err != nil {
return err
}
_, err := c.Call(method, id, field, value, c.Token)
continue
}

if method == "modify_profile" && field == "name" {
_, err := c.Call(method, id, field, v.Interface(), c.Token)
if err != nil {
return err
}
Expand All @@ -369,33 +371,28 @@ func (c *Client) updateCobblerFields(what string, item reflect.Value, id string)
for i := 0; i < item.NumField(); i++ {
v := item.Field(i)
tag := typeOfT.Field(i).Tag
fieldType := v.Type().Name()
field := tag.Get("mapstructure")
cobblerTag := tag.Get("cobbler")

if cobblerTag == "noupdate" {
if cobblerTag == "noupdate" || fieldType == "Item" {
continue
}

if field == "" {
continue
}
var value interface{}
switch v.Type().String() {
case "string", "bool", "int64", "int":
value = v.Interface()
case "[]string":
value = strings.Join(v.Interface().([]string), " ")
}
if result, err := c.Call(method, id, field, value, c.Token); err != nil {

if result, err := c.Call(method, id, field, v.Interface(), c.Token); err != nil {
return err
} else {
if result.(bool) == false && value != false {
if result.(bool) == false && v.Interface() != false {
// It's possible this is a new field that isn't available on
// older versions.
if cobblerTag == "newfield" {
continue
}
return fmt.Errorf("error updating %s to %s", field, value)
return fmt.Errorf("error updating %s to %s", field, v.Interface())
}
}
}
Expand Down
38 changes: 0 additions & 38 deletions cobblerclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,9 @@ package cobblerclient

import (
"reflect"
"regexp"
"testing"
)

var config = ClientConfig{
URL: "http://localhost:8081/cobbler_api",
Username: "cobbler",
Password: "cobbler",
}

// createStubHTTPClient ...
func createStubHTTPClient(t *testing.T, fixtures []string) Client {
hc := NewStubHTTPClient(t)

for _, fixture := range fixtures {
if fixture != "" {
rawRequest, err := Fixture(fixture + "-req.xml")
FailOnError(t, err)
response, err := Fixture(fixture + "-res.xml")
FailOnError(t, err)

// flatten the request so it matches the kolo generated xml
r := regexp.MustCompile(`\s+<`)
expectedReq := []byte(r.ReplaceAllString(string(rawRequest), "<"))
hc.answers = append(hc.answers, APIResponsePair{
Expected: expectedReq,
Response: response,
})
}
}

c := NewClient(hc, config)
c.Token = "securetoken99"
return c
}

// createStubHTTPClientSingle ...
func createStubHTTPClientSingle(t *testing.T, fixture string) Client {
return createStubHTTPClient(t, []string{fixture})
}

func TestGenerateAutoinstall(t *testing.T) {
c := createStubHTTPClientSingle(t, "generate-autoinstall")

Expand Down
16 changes: 16 additions & 0 deletions fixtures/create-system-name-check-req.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>get_system</methodName>
<params>
<param>
<value>
<string>mytestsystem</string>
</value>
</param>
<param>
<value>
<string>securetoken99</string>
</value>
</param>
</params>
</methodCall>
10 changes: 10 additions & 0 deletions fixtures/create-system-name-check-res.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version='1.0'?>
<methodResponse>
<params>
<param>
<value>
<string>~</string>
</value>
</param>
</params>
</methodResponse>
16 changes: 16 additions & 0 deletions fixtures/new-system-get-req.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>get_system</methodName>
<params>
<param>
<value>
<string>mytestsystem</string>
</value>
</param>
<param>
<value>
<string>securetoken99</string>
</value>
</param>
</params>
</methodCall>
Loading

0 comments on commit 0ff47d4

Please sign in to comment.