Skip to content

Commit

Permalink
feat(amt): add execute support for redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
zaidusmani26 committed Oct 11, 2023
1 parent 5ec768a commit 8239680
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 16 deletions.
79 changes: 75 additions & 4 deletions pkg/amt/redirection/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,37 @@
package redirection

import (
"encoding/xml"

"github.com/open-amt-cloud-toolkit/go-wsman-messages/internal/message"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/amt/actions"
"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"`
Redirection Redirection `xml:"AMT_RedirectionService`

EnumerateResponse common.EnumerateResponse
}
Redirection struct {
CreationClassName string
ElementName string
EnabledState int
ListenerEnabled bool
Name string
SystemCreationClassName string
SystemName string
}
)
type RedirectionResponse struct {
AMT_RedirectionService RedirectionService
}
Expand Down Expand Up @@ -59,6 +86,7 @@ const (

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

func NewRedirectionService(wsmanMessageCreator *message.WSManMessageCreator) Service {
Expand All @@ -67,14 +95,57 @@ func NewRedirectionService(wsmanMessageCreator *message.WSManMessageCreator) Ser
}
}

func NewRedirectionServiceWithClient(wsmanMessageCreator *message.WSManMessageCreator, client wsman.WSManClient) Service {
return Service{
base: message.NewBaseWithClient(wsmanMessageCreator, AMT_RedirectionService, client),
client: client,
}
}
// Get retrieves the representation of the instance
func (RedirectionService Service) Get() string {
return RedirectionService.base.Get(nil)
func (s Service) Get() (response Response, err error) {

response = Response{
Message: &wsman.Message{
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 (RedirectionService Service) Enumerate() string {
return RedirectionService.base.Enumerate()
func (s Service) 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
70 changes: 58 additions & 12 deletions pkg/amt/redirection/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,91 @@
package redirection

import (
"encoding/xml"
"testing"

"github.com/stretchr/testify/assert"

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

func TestAMT_RedirectionService(t *testing.T) {
messageID := 0
resourceUriBase := "http://intel.com/wbem/wscim/1/amt-schema/1/"
wsmanMessageCreator := message.NewWSManMessageCreator(resourceUriBase)
elementUnderTest := NewRedirectionService(wsmanMessageCreator)
//client := MockClient{} // wsman.NewClient("http://localhost:16992/wsman", "admin", "P@ssw0rd", true)
//elementUnderTest := NewServiceWithClient(wsmanMessageCreator, &client)
// enumerationId := ""
client := wsman.NewClient("http://localhost:16992/wsman", "admin", "Intel123!", true)
elementUnderTest := NewRedirectionServiceWithClient(wsmanMessageCreator, client)

t.Run("amt_* Tests", func(t *testing.T) {
tests := []struct {
name string
method string
action string
body string
responseFunc func() string
responseFunc func() (Response, error)
expectedResponse interface{}
}{
//GETS
{"should create a valid AMT_RedirectionService Get wsman message", "AMT_RedirectionService", wsmantesting.GET, "", elementUnderTest.Get},
{
"should create a valid AMT_RedirectionService Get wsman message",
"AMT_RedirectionService",
wsmantesting.GET,
"",
func() (Response, error) {
//currentMessage = "Get"
return elementUnderTest.Get()
},
Body{
XMLName: xml.Name{Space: "http://www.w3.org/2003/05/soap-envelope", Local: "Body"},
Redirection: Redirection{
CreationClassName: "",
ElementName: "",
EnabledState: 0,
ListenerEnabled: false,
Name: "",
SystemCreationClassName: "",
SystemName: "",
},
},
},
//ENUMERATES
{"should create a valid AMT_RedirectionService Enumerate wsman message", "AMT_RedirectionService", wsmantesting.ENUMERATE, wsmantesting.ENUMERATE_BODY, elementUnderTest.Enumerate},
/*{
"should create a valid AMT_RedirectionService Enumerate wsman message",
"AMT_RedirectionService",
wsmantesting.ENUMERATE,
wsmantesting.ENUMERATE_BODY,
func() (Response, error) {
//client.CurrentMessage = "Enumerate"
return elementUnderTest.Enumerate()
},
Body{
XMLName: xml.Name{Space: "", Local: ""},
EnumerateResponse: common.EnumerateResponse{
EnumerationContext: "",
},
},
},*/
//PULLS
{"should create a valid AMT_RedirectionService Pull wsman message", "AMT_RedirectionService", wsmantesting.PULL, wsmantesting.PULL_BODY, func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }},
//{"should create a valid AMT_RedirectionService Pull wsman message", "AMT_RedirectionService", wsmantesting.PULL, wsmantesting.PULL_BODY, func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }},
}

for _, test := range tests {
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)

})
}

})
}

0 comments on commit 8239680

Please sign in to comment.