github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/rpc/command_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"github.com/mitchellh/packer/packer"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  type TestCommand struct {
    10  	runArgs   []string
    11  	runCalled bool
    12  	runEnv    packer.Environment
    13  }
    14  
    15  func (tc *TestCommand) Help() string {
    16  	return "bar"
    17  }
    18  
    19  func (tc *TestCommand) Run(env packer.Environment, args []string) int {
    20  	tc.runCalled = true
    21  	tc.runArgs = args
    22  	tc.runEnv = env
    23  	return 0
    24  }
    25  
    26  func (tc *TestCommand) Synopsis() string {
    27  	return "foo"
    28  }
    29  
    30  func TestRPCCommand(t *testing.T) {
    31  	// Create the command
    32  	command := new(TestCommand)
    33  
    34  	// Start the server
    35  	client, server := testClientServer(t)
    36  	defer client.Close()
    37  	defer server.Close()
    38  	server.RegisterCommand(command)
    39  	commClient := client.Command()
    40  
    41  	//Test Help
    42  	help := commClient.Help()
    43  	if help != "bar" {
    44  		t.Fatalf("bad: %s", help)
    45  	}
    46  
    47  	// Test run
    48  	runArgs := []string{"foo", "bar"}
    49  	testEnv := &testEnvironment{}
    50  	exitCode := commClient.Run(testEnv, runArgs)
    51  	if !reflect.DeepEqual(command.runArgs, runArgs) {
    52  		t.Fatalf("bad: %#v", command.runArgs)
    53  	}
    54  	if exitCode != 0 {
    55  		t.Fatalf("bad: %d", exitCode)
    56  	}
    57  
    58  	if command.runEnv == nil {
    59  		t.Fatal("runEnv should not be nil")
    60  	}
    61  
    62  	// Test Synopsis
    63  	synopsis := commClient.Synopsis()
    64  	if synopsis != "foo" {
    65  		t.Fatalf("bad: %#v", synopsis)
    66  	}
    67  }
    68  
    69  func TestCommand_Implements(t *testing.T) {
    70  	var _ packer.Command = new(command)
    71  }