github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/not-internal/plugin/grpc_provisioner_test.go (about)

     1  package plugin
     2  
     3  import (
     4  	"io"
     5  	"testing"
     6  
     7  	"github.com/golang/mock/gomock"
     8  	"github.com/google/go-cmp/cmp"
     9  	"github.com/google/go-cmp/cmp/cmpopts"
    10  	"github.com/muratcelep/terraform/not-internal/configs/hcl2shim"
    11  	"github.com/muratcelep/terraform/not-internal/provisioners"
    12  	proto "github.com/muratcelep/terraform/not-internal/tfplugin5"
    13  	"github.com/zclconf/go-cty/cty"
    14  
    15  	mockproto "github.com/muratcelep/terraform/not-internal/plugin/mock_proto"
    16  )
    17  
    18  var _ provisioners.Interface = (*GRPCProvisioner)(nil)
    19  
    20  var (
    21  	equateEmpty   = cmpopts.EquateEmpty()
    22  	typeComparer  = cmp.Comparer(cty.Type.Equals)
    23  	valueComparer = cmp.Comparer(cty.Value.RawEquals)
    24  )
    25  
    26  func mockProvisionerClient(t *testing.T) *mockproto.MockProvisionerClient {
    27  	ctrl := gomock.NewController(t)
    28  	client := mockproto.NewMockProvisionerClient(ctrl)
    29  
    30  	// we always need a GetSchema method
    31  	client.EXPECT().GetSchema(
    32  		gomock.Any(),
    33  		gomock.Any(),
    34  	).Return(provisionerProtoSchema(), nil)
    35  
    36  	return client
    37  }
    38  
    39  func provisionerProtoSchema() *proto.GetProvisionerSchema_Response {
    40  	return &proto.GetProvisionerSchema_Response{
    41  		Provisioner: &proto.Schema{
    42  			Block: &proto.Schema_Block{
    43  				Attributes: []*proto.Schema_Attribute{
    44  					{
    45  						Name:     "attr",
    46  						Type:     []byte(`"string"`),
    47  						Required: true,
    48  					},
    49  				},
    50  			},
    51  		},
    52  	}
    53  }
    54  
    55  func TestGRPCProvisioner_GetSchema(t *testing.T) {
    56  	p := &GRPCProvisioner{
    57  		client: mockProvisionerClient(t),
    58  	}
    59  
    60  	resp := p.GetSchema()
    61  	checkDiags(t, resp.Diagnostics)
    62  }
    63  
    64  func TestGRPCProvisioner_ValidateProvisionerConfig(t *testing.T) {
    65  	client := mockProvisionerClient(t)
    66  	p := &GRPCProvisioner{
    67  		client: client,
    68  	}
    69  
    70  	client.EXPECT().ValidateProvisionerConfig(
    71  		gomock.Any(),
    72  		gomock.Any(),
    73  	).Return(&proto.ValidateProvisionerConfig_Response{}, nil)
    74  
    75  	cfg := hcl2shim.HCL2ValueFromConfigValue(map[string]interface{}{"attr": "value"})
    76  	resp := p.ValidateProvisionerConfig(provisioners.ValidateProvisionerConfigRequest{Config: cfg})
    77  	checkDiags(t, resp.Diagnostics)
    78  }
    79  
    80  func TestGRPCProvisioner_ProvisionResource(t *testing.T) {
    81  	ctrl := gomock.NewController(t)
    82  	client := mockproto.NewMockProvisionerClient(ctrl)
    83  
    84  	// we always need a GetSchema method
    85  	client.EXPECT().GetSchema(
    86  		gomock.Any(),
    87  		gomock.Any(),
    88  	).Return(provisionerProtoSchema(), nil)
    89  
    90  	stream := mockproto.NewMockProvisioner_ProvisionResourceClient(ctrl)
    91  	stream.EXPECT().Recv().Return(&proto.ProvisionResource_Response{
    92  		Output: "provisioned",
    93  	}, io.EOF)
    94  
    95  	client.EXPECT().ProvisionResource(
    96  		gomock.Any(),
    97  		gomock.Any(),
    98  	).Return(stream, nil)
    99  
   100  	p := &GRPCProvisioner{
   101  		client: client,
   102  	}
   103  
   104  	rec := &provisionRecorder{}
   105  
   106  	resp := p.ProvisionResource(provisioners.ProvisionResourceRequest{
   107  		Config: cty.ObjectVal(map[string]cty.Value{
   108  			"attr": cty.StringVal("value"),
   109  		}),
   110  		Connection: cty.EmptyObjectVal,
   111  		UIOutput:   rec,
   112  	})
   113  
   114  	if resp.Diagnostics.HasErrors() {
   115  		t.Fatal(resp.Diagnostics.Err())
   116  	}
   117  
   118  	if len(rec.output) == 0 || rec.output[0] != "provisioned" {
   119  		t.Fatalf("expected %q, got %q", []string{"provisioned"}, rec.output)
   120  	}
   121  }
   122  
   123  type provisionRecorder struct {
   124  	output []string
   125  }
   126  
   127  func (r *provisionRecorder) Output(s string) {
   128  	r.output = append(r.output, s)
   129  }
   130  
   131  func TestGRPCProvisioner_Stop(t *testing.T) {
   132  	ctrl := gomock.NewController(t)
   133  	client := mockproto.NewMockProvisionerClient(ctrl)
   134  	p := &GRPCProvisioner{
   135  		client: client,
   136  	}
   137  
   138  	client.EXPECT().Stop(
   139  		gomock.Any(),
   140  		gomock.Any(),
   141  	).Return(&proto.Stop_Response{}, nil)
   142  
   143  	err := p.Stop()
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  }