terraform/internal/cloudplugin/cloudplugin1/grpc_client_test.go
Sarah French 2e5b5dee5d
Some checks failed
build / Determine intended Terraform version (push) Has been cancelled
build / Determine Go toolchain version (push) Has been cancelled
Quick Checks / Unit Tests (push) Has been cancelled
Quick Checks / Race Tests (push) Has been cancelled
Quick Checks / End-to-end Tests (push) Has been cancelled
Quick Checks / Code Consistency Checks (push) Has been cancelled
build / Generate release metadata (push) Has been cancelled
build / Build for freebsd_386 (push) Has been cancelled
build / Build for linux_386 (push) Has been cancelled
build / Build for openbsd_386 (push) Has been cancelled
build / Build for windows_386 (push) Has been cancelled
build / Build for darwin_amd64 (push) Has been cancelled
build / Build for freebsd_amd64 (push) Has been cancelled
build / Build for linux_amd64 (push) Has been cancelled
build / Build for openbsd_amd64 (push) Has been cancelled
build / Build for solaris_amd64 (push) Has been cancelled
build / Build for windows_amd64 (push) Has been cancelled
build / Build for freebsd_arm (push) Has been cancelled
build / Build for linux_arm (push) Has been cancelled
build / Build for darwin_arm64 (push) Has been cancelled
build / Build for linux_arm64 (push) Has been cancelled
build / Build for windows_arm64 (push) Has been cancelled
build / Build Docker image for linux_386 (push) Has been cancelled
build / Build Docker image for linux_amd64 (push) Has been cancelled
build / Build Docker image for linux_arm (push) Has been cancelled
build / Build Docker image for linux_arm64 (push) Has been cancelled
build / Build e2etest for linux_386 (push) Has been cancelled
build / Build e2etest for windows_386 (push) Has been cancelled
build / Build e2etest for darwin_amd64 (push) Has been cancelled
build / Build e2etest for linux_amd64 (push) Has been cancelled
build / Build e2etest for windows_amd64 (push) Has been cancelled
build / Build e2etest for linux_arm (push) Has been cancelled
build / Build e2etest for darwin_arm64 (push) Has been cancelled
build / Build e2etest for linux_arm64 (push) Has been cancelled
build / Run e2e test for linux_386 (push) Has been cancelled
build / Run e2e test for windows_386 (push) Has been cancelled
build / Run e2e test for darwin_amd64 (push) Has been cancelled
build / Run e2e test for linux_amd64 (push) Has been cancelled
build / Run e2e test for windows_amd64 (push) Has been cancelled
build / Run e2e test for linux_arm (push) Has been cancelled
build / Run e2e test for linux_arm64 (push) Has been cancelled
build / Run terraform-exec test for linux amd64 (push) Has been cancelled
Upgrade protoc and protoc-gen-go-grpc versions to matching terraform-plugin-go (#37647)
* Update protoc version in downloader script

* go get google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1

This matched terraform-plugin-go

* make protobuf

* Run `make protobuf`

* Update generator to handle generic types from google.golang.org/grpc

Looks like this was added in v1.69.3 in https://github.com/grpc/grpc-go/pull/7057 ?

* Run `make generate`

* Fix "cannot infer Res" compile error - more usage of generics

* More fixing compile errors due to switching to use of a generic

* Make putting `google.golang.org/grpc` import into generated files conditional

* Run `make generate`

* Update more places where generics now need to be used

* Update generator to handle any types from google.golang.org/grpc in same switch case.
2025-10-22 14:46:18 +01:00

140 lines
3.7 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package cloudplugin1
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"testing"
"github.com/hashicorp/terraform/internal/cloudplugin/cloudproto1"
"github.com/hashicorp/terraform/internal/cloudplugin/mock_cloudproto1"
"go.uber.org/mock/gomock"
)
var mockError = "this is a mock error"
func testGRPCloudClient(t *testing.T, ctrl *gomock.Controller, client *mock_cloudproto1.MockCommandService_ExecuteClient[cloudproto1.CommandResponse], executeError error) *GRPCCloudClient {
t.Helper()
if client != nil && executeError != nil {
t.Fatal("one of client or executeError must be nil")
}
result := mock_cloudproto1.NewMockCommandServiceClient(ctrl)
result.EXPECT().Execute(
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(client, executeError)
return &GRPCCloudClient{
client: result,
context: context.Background(),
}
}
func Test_GRPCCloudClient_ExecuteError(t *testing.T) {
ctrl := gomock.NewController(t)
gRPCClient := testGRPCloudClient(t, ctrl, nil, errors.New(mockError))
buffer := bytes.Buffer{}
exitCode := gRPCClient.Execute([]string{"example"}, io.Discard, &buffer)
if exitCode != 1 {
t.Fatalf("expected exit %d, got %d", 1, exitCode)
}
if buffer.String() != mockError {
t.Errorf("expected error %q, got %q", mockError, buffer.String())
}
}
func Test_GRPCCloudClient_Execute_RecvError(t *testing.T) {
ctrl := gomock.NewController(t)
executeClient := mock_cloudproto1.NewMockCommandService_ExecuteClient[cloudproto1.CommandResponse](ctrl)
executeClient.EXPECT().Recv().Return(nil, errors.New(mockError))
gRPCClient := testGRPCloudClient(t, ctrl, executeClient, nil)
buffer := bytes.Buffer{}
exitCode := gRPCClient.Execute([]string{"example"}, io.Discard, &buffer)
if exitCode != 1 {
t.Fatalf("expected exit %d, got %d", 1, exitCode)
}
mockRecvError := fmt.Sprintf("Failed to receive command response from cloudplugin: %s", mockError)
if buffer.String() != mockRecvError {
t.Errorf("expected error %q, got %q", mockRecvError, buffer.String())
}
}
func Test_GRPCCloudClient_Execute_Invalid_Exit(t *testing.T) {
ctrl := gomock.NewController(t)
executeClient := mock_cloudproto1.NewMockCommandService_ExecuteClient[cloudproto1.CommandResponse](ctrl)
executeClient.EXPECT().Recv().Return(
&cloudproto1.CommandResponse{
Data: &cloudproto1.CommandResponse_ExitCode{
ExitCode: 3_000,
},
}, nil,
)
gRPCClient := testGRPCloudClient(t, ctrl, executeClient, nil)
exitCode := gRPCClient.Execute([]string{"example"}, io.Discard, io.Discard)
if exitCode != 255 {
t.Fatalf("expected exit %q, got %q", 255, exitCode)
}
}
func Test_GRPCCloudClient_Execute(t *testing.T) {
ctrl := gomock.NewController(t)
executeClient := mock_cloudproto1.NewMockCommandService_ExecuteClient[cloudproto1.CommandResponse](ctrl)
gomock.InOrder(
executeClient.EXPECT().Recv().Return(
&cloudproto1.CommandResponse{
Data: &cloudproto1.CommandResponse_Stdout{
Stdout: []byte("firstcall\n"),
},
}, nil,
),
executeClient.EXPECT().Recv().Return(
&cloudproto1.CommandResponse{
Data: &cloudproto1.CommandResponse_Stdout{
Stdout: []byte("secondcall\n"),
},
}, nil,
),
executeClient.EXPECT().Recv().Return(
&cloudproto1.CommandResponse{
Data: &cloudproto1.CommandResponse_ExitCode{
ExitCode: 99,
},
}, nil,
),
)
gRPCClient := testGRPCloudClient(t, ctrl, executeClient, nil)
stdoutBuffer := bytes.Buffer{}
exitCode := gRPCClient.Execute([]string{"example"}, &stdoutBuffer, io.Discard)
if exitCode != 99 {
t.Fatalf("expected exit %q, got %q", 99, exitCode)
}
if stdoutBuffer.String() != "firstcall\nsecondcall\n" {
t.Errorf("expected output %q, got %q", "firstcall\nsecondcall\n", stdoutBuffer.String())
}
}