mirror of https://github.com/hashicorp/packer
parent
55d9cd2124
commit
53c3d330e4
@ -0,0 +1,62 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"net/rpc"
|
||||
)
|
||||
|
||||
// An implementation of packer.Artifact where the artifact is actually
|
||||
// available over an RPC connection.
|
||||
type artifact struct {
|
||||
client *rpc.Client
|
||||
}
|
||||
|
||||
// ArtifactServer wraps a packer.Artifact implementation and makes it
|
||||
// exportable as part of a Golang RPC server.
|
||||
type ArtifactServer struct {
|
||||
artifact packer.Artifact
|
||||
}
|
||||
|
||||
func Artifact(client *rpc.Client) *artifact {
|
||||
return &artifact{client}
|
||||
}
|
||||
|
||||
func (a *artifact) BuilderId() (result string) {
|
||||
a.client.Call("Artifact.BuilderId", new(interface{}), &result)
|
||||
return
|
||||
}
|
||||
|
||||
func (a *artifact) Files() (result []string) {
|
||||
a.client.Call("Artifact.Files", new(interface{}), &result)
|
||||
return
|
||||
}
|
||||
|
||||
func (a *artifact) Id() (result string) {
|
||||
a.client.Call("Artifact.Id", new(interface{}), &result)
|
||||
return
|
||||
}
|
||||
|
||||
func (a *artifact) String() (result string) {
|
||||
a.client.Call("Artifact.String", new(interface{}), &result)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *ArtifactServer) BuilderId(args *interface{}, reply *string) error {
|
||||
*reply = s.artifact.BuilderId()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ArtifactServer) Files(args *interface{}, reply *[]string) error {
|
||||
*reply = s.artifact.Files()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ArtifactServer) Id(args *interface{}, reply *string) error {
|
||||
*reply = s.artifact.Id()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *ArtifactServer) String(args *interface{}, reply *string) error {
|
||||
*reply = s.artifact.String()
|
||||
return nil
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"cgl.tideland.biz/asserts"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"net/rpc"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testArtifact struct{}
|
||||
|
||||
func (testArtifact) BuilderId() string {
|
||||
return "bid"
|
||||
}
|
||||
|
||||
func (testArtifact) Files() []string {
|
||||
return []string{"a", "b"}
|
||||
}
|
||||
|
||||
func (testArtifact) Id() string {
|
||||
return "id"
|
||||
}
|
||||
|
||||
func (testArtifact) String() string {
|
||||
return "string"
|
||||
}
|
||||
|
||||
func TestArtifactRPC(t *testing.T) {
|
||||
assert := asserts.NewTestingAsserts(t, true)
|
||||
|
||||
// Create the interface to test
|
||||
a := new(testArtifact)
|
||||
|
||||
// Start the server
|
||||
server := rpc.NewServer()
|
||||
RegisterArtifact(server, a)
|
||||
address := serveSingleConn(server)
|
||||
|
||||
// Create the client over RPC and run some methods to verify it works
|
||||
client, err := rpc.Dial("tcp", address)
|
||||
assert.Nil(err, "should be able to connect")
|
||||
aClient := Artifact(client)
|
||||
|
||||
// Test
|
||||
assert.Equal(aClient.BuilderId(), "bid", "should have correct builder ID")
|
||||
assert.Equal(aClient.Files(), []string{"a", "b"}, "should have correct builder ID")
|
||||
assert.Equal(aClient.Id(), "id", "should have correct builder ID")
|
||||
assert.Equal(aClient.String(), "string", "should have correct builder ID")
|
||||
}
|
||||
|
||||
func TestArtifact_Implements(t *testing.T) {
|
||||
assert := asserts.NewTestingAsserts(t, true)
|
||||
|
||||
var r packer.Artifact
|
||||
a := Artifact(nil)
|
||||
|
||||
assert.Implementor(a, &r, "should be an Artifact")
|
||||
}
|
||||
Loading…
Reference in new issue