Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(amt): adds execute support for ethernet #98

Merged
merged 6 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 109 additions & 5 deletions pkg/amt/ethernetport/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,61 @@
package ethernetport

import (
"encoding/json"
"encoding/xml"

"github.com/open-amt-cloud-toolkit/go-wsman-messages/internal/message"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/cim/models"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/common"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsman"
)

type (
Response struct {
*wsman.Message
XMLName xml.Name `xml:"Envelope"`
Header message.Header `xml:"Header"`
Body Body `xml:"Body"`
}

Body struct {
XMLName xml.Name `xml:"Body"`
EthernetPort EthernetPort `xml:"AMT_EthernetPortSettings"`
EnumerateResponse common.EnumerateResponse
}

EthernetPort struct {
DHCPEnabled bool
DefaultGateway string
ElementName string
InstanceID string
IpSyncEnabled bool
LinkIsUp bool
LinkPolicy int
MACAddress string
PhysicalConnectionType int
PrimaryDNS string
SecondaryDNS string
SharedDynamicIP bool
SharedMAC bool
SharedStaticIp bool
SubnetMask string
}
)

/*
type Selector struct {
XMLName xml.Name `xml:"w:Selector,omitempty"`
Name string `xml:"Name,attr"`
Value string `xml:",chardata"`
}

type Selector_OUTPUT struct {
XMLName xml.Name `xml:"Selector,omitempty"`
Name string `xml:"Name,attr"`
Value string `xml:",chardata"`
}
*/
type EthernetPortSettings struct {
models.SettingData
VLANTag int
Expand All @@ -36,6 +87,14 @@ type EthernetPortSettings struct {

type LinkPolicyValues int

func (w *Response) JSON() string {
jsonOutput, err := json.Marshal(w.Body)
if err != nil {
return ""
}
return string(jsonOutput)
}

const AMT_EthernetPortSettings = "AMT_EthernetPortSettings"

const (
Expand Down Expand Up @@ -95,7 +154,15 @@ const (
)

type Settings struct {
base message.Base
base message.Base
client wsman.WSManClient
}

func NewEthernetPortSettingsWithClient(wsmanMessageCreator *message.WSManMessageCreator, client wsman.WSManClient) Settings {
return Settings{
base: message.NewBaseWithClient(wsmanMessageCreator, AMT_EthernetPortSettings, client),
client: client,
}
}

func NewEthernetPortSettings(wsmanMessageCreator *message.WSManMessageCreator) Settings {
Expand All @@ -105,13 +172,50 @@ func NewEthernetPortSettings(wsmanMessageCreator *message.WSManMessageCreator) S
}

// Get retrieves the representation of the instance
func (EthernetPortSettings Settings) Get() string {
return EthernetPortSettings.base.Get(nil)
// func (s Settings) Get(selector *Selector) (response Response, err error) {
func (s Settings) Get() (response Response, err error) {
response = Response{
Message: &wsman.Message{
//XMLInput: s.base.Get((*message.Selector)(selector)),
XMLInput: s.base.Get(nil),
},
}

// send the message to AMT
err = s.base.Execute(response.Message)
if err != nil {
return
}

// put the xml response into the go struct
err = xml.Unmarshal([]byte(response.XMLOutput), &response)
if err != nil {
return
}

return
}

// Enumerates the instances of this class
func (EthernetPortSettings Settings) Enumerate() string {
return EthernetPortSettings.base.Enumerate()
func (s Settings) Enumerate() (response Response, err error) {
response = Response{
Message: &wsman.Message{
XMLInput: s.base.Enumerate(),
},
}
// send the message to AMT
err = s.base.Execute(response.Message)
if err != nil {
return
}

// put the xml response into the go struct
err = xml.Unmarshal([]byte(response.XMLOutput), &response)
if err != nil {
return
}

return
}

// Pulls instances of this class, following an Enumerate operation
Expand Down
121 changes: 105 additions & 16 deletions pkg/amt/ethernetport/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,133 @@
package ethernetport

import (
"encoding/xml"
"fmt"
"io"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/open-amt-cloud-toolkit/go-wsman-messages/internal/message"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/common"
"github.com/stretchr/testify/assert"
//"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsman"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsmantesting"
)

type MockClient struct {
}

const (
EnvelopeResponse = `<a:Envelope xmlns:a="http://www.w3.org/2003/05/soap-envelope" x-mlns:b="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:c="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:d="http://schemas.xmlsoap.org/ws/2005/02/trust" xmlns:e="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:f="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd" xmlns:g="http://intel.com/wbem/wscim/1/amt-schema/1/AMT_EthernetPortSettings" xmlns:h="http://schemas.dmtf.org/wbem/wscim/1/common" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><a:Header><b:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</b:To><b:RelatesTo>0</b:RelatesTo><b:Action a:mustUnderstand="true">`
GetBody = `<g:AMT_EthernetPortSettings><g:CreationClassName>AMT_EthernetPortSettings</g:CreationClassName><g:ElementName>Intel(r) AMT Ethernet Port Settings</g:ElementName><g:Name>Intel(r) AMT Ethernet Port Settings</g:Name><g:SystemCreationClassName>CIM_ComputerSystem</g:SystemCreationClassName><g:SystemName>ManagedSystem</g:SystemName></g:AMT_EthernetPortSettings>`
)

var currentMessage = ""

func (c *MockClient) Post(msg string) ([]byte, error) {
// read an xml file from disk:
xmlFile, err := os.Open("../../wsmantesting/responses/amt/ethernetport/" + strings.ToLower(currentMessage) + ".xml")
if err != nil {
fmt.Println("Error opening file:", err)
return nil, err
}
defer xmlFile.Close()
// read file into string
xmlData, err := io.ReadAll(xmlFile)
if err != nil {
fmt.Println("Error reading file:", err)
return nil, err
}
// strip carriage returns and new line characters
xmlData = []byte(strings.ReplaceAll(string(xmlData), "\r\n", ""))

// Simulate a successful response for testing.
return []byte(xmlData), nil
}
func TestAMT_EthernetPortSettings(t *testing.T) {
messageID := 0
//selector := &Selector{Name: "InstanceID", Value: "Intel(r) AMT Ethernet Port Settings 0"}
resourceUriBase := "http://intel.com/wbem/wscim/1/amt-schema/1/"
wsmanMessageCreator := message.NewWSManMessageCreator(resourceUriBase)
elementUnderTest := NewEthernetPortSettings(wsmanMessageCreator)
client := MockClient{} // wsman.NewClient("http://localhost:16992/wsman", "admin", "P@ssw0rd", true)
//elementUnderTest := NewServiceWithClient(wsmanMessageCreator, &client)
// enumerationId := ""
//client := wsman.NewClient("http://localhost:1699/wsman", "admin", "Intel123!", true)
elementUnderTest := NewEthernetPortSettingsWithClient(wsmanMessageCreator, &client)

t.Run("amt_* Tests", func(t *testing.T) {
tests := []struct {
name string
method string
action string
body string
responseFunc func() string
name string
method string
action string
body string
responseFunc func() (Response, error)
expectedResponse interface{}
}{
//GETS
{"should create a valid AMT_EthernetPortSettings Get wsman message", "AMT_EthernetPortSettings", wsmantesting.GET, "", elementUnderTest.Get},
{
"should create a valid AMT_EthernetPortSettings Get wsman message",
"AMT_EthernetPortSettings",
wsmantesting.GET,
"",
func() (Response, error) {
currentMessage = "Get"
//return elementUnderTest.Get(selector)
return elementUnderTest.Get()
},
Body{
XMLName: xml.Name{Space: "http://www.w3.org/2003/05/soap-envelope", Local: "Body"},
EthernetPort: EthernetPort{
DHCPEnabled: true,
DefaultGateway: "192.168.0.1",
ElementName: "Intel(r) AMT Ethernet Port Settings",
InstanceID: "Intel(r) AMT Ethernet Port Settings 0",
IpSyncEnabled: true,
LinkIsUp: true,
LinkPolicy: 14,
MACAddress: "c8-d9-d2-7a-1e-33",
PhysicalConnectionType: 0,
PrimaryDNS: "68.105.28.11",
SecondaryDNS: "68.105.29.11",
SharedDynamicIP: true,
SharedMAC: true,
SharedStaticIp: false,
SubnetMask: "255.255.255.0",
},
},
},

//ENUMERATES
{"should create a valid AMT_EthernetPortSettings Enumerate wsman message", "AMT_EthernetPortSettings", wsmantesting.ENUMERATE, wsmantesting.ENUMERATE_BODY, elementUnderTest.Enumerate},
{
"should create a valid AMT_EthernetPortSettings Enumerate wsman message",
"AMT_EthernetPortSettings",
wsmantesting.ENUMERATE,
wsmantesting.ENUMERATE_BODY,
func() (Response, error) {
currentMessage = "Enumerate"
return elementUnderTest.Enumerate()
},
Body{
XMLName: xml.Name{Space: "http://www.w3.org/2003/05/soap-envelope", Local: "Body"},
EnumerateResponse: common.EnumerateResponse{
EnumerationContext: "7700000-0000-0000-0000-000000000000",
},
},
},
//PULLS
{"should create a valid AMT_EthernetPortSettings Pull wsman message", "AMT_EthernetPortSettings", wsmantesting.PULL, wsmantesting.PULL_BODY, func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }},
// {"should create a valid AMT_EthernetPortSettings Pull wsman message", "AMT_EthernetPortSettings", wsmantesting.PULL, wsmantesting.PULL_BODY, func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
correctResponse := wsmantesting.ExpectedResponse(messageID, resourceUriBase, test.method, test.action, "", test.body)
expectedXMLInput := wsmantesting.ExpectedResponse(messageID, resourceUriBase, test.method, test.action, "", test.body)
messageID++
response := test.responseFunc()
if response != correctResponse {
assert.Equal(t, correctResponse, response)
}
response, err := test.responseFunc()
//println(response.XMLOutput)
assert.NoError(t, err)
assert.Equal(t, expectedXMLInput, response.XMLInput)
assert.Equal(t, test.expectedResponse, response.Body)
})
}
})
Expand Down
14 changes: 10 additions & 4 deletions pkg/wsmantesting/responses/amt/ethernetport/enumerate.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?xml version= "1.0" encoding= "UTF-8"?>
<a:Envelope xmlns:a= "http://www.w3.org/2003/05/soap-envelope" xmlns:b= "http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:c= "http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:d= "http://schemas.xmlsoap.org/ws/2005/02/trust" xmlns:e= "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:f= "http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd" xmlns:g= "http://schemas.xmlsoap.org/ws/2004/09/enumeration"
<?xml version= "1.0" encoding= "UTF-8"?>
<a:Envelope xmlns:a= "http://www.w3.org/2003/05/soap-envelope"
xmlns:b= "http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:c= "http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
xmlns:d= "http://schemas.xmlsoap.org/ws/2005/02/trust"
xmlns:e= "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:f= "http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd"
xmlns:g= "http://schemas.xmlsoap.org/ws/2004/09/enumeration"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance">
<a:Header>
<b:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</b:To>
Expand All @@ -11,7 +17,7 @@
</a:Header>
<a:Body>
<g:EnumerateResponse>
<g:EnumerationContext>C5000000-0000-0000-0000-000000000000</g:EnumerationContext>
<g:EnumerationContext>7700000-0000-0000-0000-000000000000</g:EnumerationContext>
</g:EnumerateResponse>
</a:Body>
</a:Envelope>
</a:Envelope>
36 changes: 36 additions & 0 deletions pkg/wsmantesting/responses/amt/ethernetport/get.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<a:Envelope xmlns:a="http://www.w3.org/2003/05/soap-envelope"
xmlns:b="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:c="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
xmlns:d="http://schemas.xmlsoap.org/ws/2005/02/trust"
xmlns:e="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:f="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd"
xmlns:g="http://intel.com/wbem/wscim/1/amt-schema/1/AMT_EthernetPortSettings"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<a:Header>
<b:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</b:To>
<b:RelatesTo>0</b:RelatesTo>
<b:Action a:mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse</b:Action>
<b:MessageID>uuid:00000000-8086-8086-8086-00000000047B</b:MessageID>
<c:ResourceURI>http://intel.com/wbem/wscim/1/amt-schema/1/AMT_EthernetPortSettings</c:ResourceURI>
</a:Header>
<a:Body>
<g:AMT_EthernetPortSettings>
<g:DHCPEnabled>true</g:DHCPEnabled>
<g:DefaultGateway>192.168.0.1</g:DefaultGateway>
<g:ElementName>Intel(r) AMT Ethernet Port Settings</g:ElementName>
<g:InstanceID>Intel(r) AMT Ethernet Port Settings 0</g:InstanceID>
<g:IpSyncEnabled>true</g:IpSyncEnabled>
<g:LinkIsUp>true</g:LinkIsUp>
<g:LinkPolicy>14</g:LinkPolicy>
<g:MACAddress>c8-d9-d2-7a-1e-33</g:MACAddress>
<g:PhysicalConnectionType>0</g:PhysicalConnectionType>
<g:PrimaryDNS>68.105.28.11</g:PrimaryDNS>
<g:SecondaryDNS>68.105.29.11</g:SecondaryDNS>
<g:SharedDynamicIP>true</g:SharedDynamicIP>
<g:SharedMAC>true</g:SharedMAC>
<g:SharedStaticIp>false</g:SharedStaticIp>
<g:SubnetMask>255.255.255.0</g:SubnetMask>
</g:AMT_EthernetPortSettings>
</a:Body>
</a:Envelope>
Loading