Skip to content

Commit

Permalink
Merge 7e49bdc into 9e7dad1
Browse files Browse the repository at this point in the history
  • Loading branch information
rpatali committed Aug 25, 2019
2 parents 9e7dad1 + 7e49bdc commit da6595d
Show file tree
Hide file tree
Showing 26 changed files with 1,544 additions and 148 deletions.
63 changes: 61 additions & 2 deletions codegen/client.go
Expand Up @@ -23,9 +23,9 @@ package codegen
import (
"path/filepath"

yaml "github.com/ghodss/yaml"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
validator "gopkg.in/validator.v2"
"gopkg.in/validator.v2"
)

type clientConfig interface {
Expand Down Expand Up @@ -231,6 +231,63 @@ type GRPCClientConfig struct {
Config *ClientIDLConfig `yaml:"config" json:"config" validate:"nonzero"`
}

func newGRPCClientConfig(raw []byte) (*GRPCClientConfig, error) {
config := &GRPCClientConfig{}
if errUnmarshal := yaml.Unmarshal(raw, config); errUnmarshal != nil {
return nil, errors.Wrap(
errUnmarshal, "could not parse gRPC client config data")
}

validator.SetValidationFunc("exposedMethods", validateExposedMethods)
if errValidate := validator.Validate(config); errValidate != nil {
return nil, errors.Wrap(
errValidate, "grpc client config validation failed")
}

return config, nil
}

func newGRPCClientSpec(
clientType string,
config *ClientIDLConfig,
instance *ModuleInstance,
h *PackageHelper,
) (*ClientSpec, error) {
protoFile := filepath.Join(h.ThriftIDLPath(), config.IDLFile)
protoSpec, err := NewProtoModuleSpec(protoFile, false)
if err != nil {
return nil, errors.Wrapf(
err, "could not build proto spec for proto file %s: ", protoFile,
)
}

cspec := &ClientSpec{
ModuleSpec: protoSpec,
YAMLFile: instance.YAMLFileName,
JSONFile: instance.JSONFileName,
ClientType: clientType,
ImportPackagePath: instance.PackageInfo.ImportPackagePath(),
ImportPackageAlias: instance.PackageInfo.ImportPackageAlias(),
ExportName: instance.PackageInfo.ExportName,
ExportType: instance.PackageInfo.ExportType,
ThriftFile: protoFile,
ClientID: instance.InstanceName,
ClientName: instance.PackageInfo.QualifiedInstanceName,
ExposedMethods: config.ExposedMethods,
SidecarRouter: config.SidecarRouter,
}

return cspec, nil
}

// NewClientSpec creates a client spec from a client module instance
func (c *GRPCClientConfig) NewClientSpec(
instance *ModuleInstance,
h *PackageHelper,
) (*ClientSpec, error) {
return newGRPCClientSpec(c.Type, c.Config, instance, h)
}

func clientType(raw []byte) (string, error) {
clientConfig := ClassConfigBase{}
if err := yaml.Unmarshal(raw, &clientConfig); err != nil {
Expand All @@ -252,6 +309,8 @@ func newClientConfig(raw []byte) (clientConfig, error) {
return newHTTPClientConfig(raw)
case "tchannel":
return newTChannelClientConfig(raw)
case "grpc":
return newGRPCClientConfig(raw)
case "custom":
return newCustomClientConfig(raw)
default:
Expand Down
113 changes: 109 additions & 4 deletions codegen/client_test.go
Expand Up @@ -72,6 +72,26 @@ config:
exposedMethods:
a: method
`
grpcClientYAML = `
name: test
type: grpc
dependencies:
client:
- a
- b
config:
idlFileSha: idlFileSha
idlFile: clients/echo/echo.proto
customImportPath: path
fixture:
importPath: import
scenarios:
scenario:
- s1
- s2
exposedMethods:
a: method
`

customClientYAML = `
name: test
Expand Down Expand Up @@ -107,6 +127,14 @@ func TestNewTChannelClientConfigUnmarshalFilure(t *testing.T) {
assert.Equal(t, expectedErr, err.Error())
}

func TestNewGRPCClientConfigUnmarshalFilure(t *testing.T) {
invalidYAML := "{{{"
_, err := newGRPCClientConfig([]byte(invalidYAML))
expectedErr := "could not parse gRPC client config data: error converting YAML to JSON: yaml: line 1: did not find expected node content"
assert.Error(t, err)
assert.Equal(t, expectedErr, err.Error())
}

func TestNewCustomClientConfigUnmarshalFilure(t *testing.T) {
invalidYAML := "{{{"
_, err := newCustomClientConfig([]byte(invalidYAML))
Expand Down Expand Up @@ -134,6 +162,7 @@ type: %s
func TestNewClientConfigSubConfigMissing(t *testing.T) {
doSubConfigMissingTest(t, "http")
doSubConfigMissingTest(t, "tchannel")
doSubConfigMissingTest(t, "grpc")
doSubConfigMissingTest(t, "custom")
}

Expand All @@ -157,6 +186,7 @@ config:
func TestThriftFileMissingValidation(t *testing.T) {
doThriftFileMissingTest(t, "http")
doThriftFileMissingTest(t, "tchannel")
doThriftFileMissingTest(t, "grpc")
}

func doThriftFileShaMissingTest(t *testing.T, clientType string) {
Expand All @@ -175,6 +205,7 @@ config:
func TestThriftFileShaMissingValidation(t *testing.T) {
doThriftFileShaMissingTest(t, "http")
doThriftFileShaMissingTest(t, "tchannel")
doThriftFileShaMissingTest(t, "grpc")
}

func TestCustomClientRequiresCustomImportPath(t *testing.T) {
Expand Down Expand Up @@ -215,6 +246,7 @@ config:
func TestNewClientConfigDuplicatedMethodsFailure(t *testing.T) {
doDuplicatedExposedMethodsTest(t, "http")
doDuplicatedExposedMethodsTest(t, "tchannel")
doDuplicatedExposedMethodsTest(t, "grpc")
}

func TestGetConfigTypeFailure(t *testing.T) {
Expand Down Expand Up @@ -321,6 +353,34 @@ func TestNewClientConfigGetTChannelClient(t *testing.T) {
assert.Equal(t, &expectedClient, client)
}

func TestNewClientConfigGetGRPCClient(t *testing.T) {
client, err := newClientConfig([]byte(grpcClientYAML))
expectedClient := GRPCClientConfig{
ClassConfigBase: ClassConfigBase{
Name: "test",
Type: "grpc",
},
Dependencies: Dependencies{
Client: []string{"a", "b"},
},
Config: &ClientIDLConfig{
ExposedMethods: map[string]string{
"a": "method",
},
IDLFileSha: "idlFileSha",
IDLFile: "clients/echo/echo.proto",
Fixture: &Fixture{
ImportPath: "import",
Scenarios: map[string][]string{
"scenario": {"s1", "s2"},
},
},
},
}
assert.NoError(t, err)
assert.Equal(t, &expectedClient, client)
}

func TestNewClientConfigGetCustomClient(t *testing.T) {
client, err := newClientConfig([]byte(customClientYAML))
expectedClient := CustomClientConfig{
Expand Down Expand Up @@ -371,21 +431,27 @@ func newTestPackageHelper(t *testing.T) *PackageHelper {

}

func TestHTTPClientNewClientSpecFailedWithThriftCompilation(t *testing.T) {
configYAML := `
func TestClientNewClientSpecFailedWithThriftCompilation(t *testing.T) {
testNewClientSpecFailedWithCompilation(t, "http")
testNewClientSpecFailedWithCompilation(t, "grpc")
}

func testNewClientSpecFailedWithCompilation(t *testing.T, clientType string) {
configYAML := fmt.Sprintf(`
name: test
type: http
type: %s
config:
idlFileSha: idlFileSha
idlFile: NOT_EXIST
exposedMethods:
a: method
`
`, clientType)
client, errClient := newClientConfig([]byte(configYAML))
assert.NoError(t, errClient)

h := newTestPackageHelper(t)
_, errSpec := client.NewClientSpec(nil /* ModuleInstance */, h)
fmt.Println(errSpec)
assert.Error(t, errSpec)
}

Expand Down Expand Up @@ -438,6 +504,45 @@ func TestTChannelClientNewClientSpec(t *testing.T) {
doNewClientSpecTest(t, []byte(tchannelClientYAML), "tchannel")
}

func TestGRPCClientNewClientSpec(t *testing.T) {
client, errClient := newClientConfig([]byte(grpcClientYAML))
assert.NoError(t, errClient)
instance := &ModuleInstance{
YAMLFileName: "YAMLFileName",
JSONFileName: "JSONFileName",
InstanceName: "InstanceName",
PackageInfo: &PackageInfo{
ExportName: "ExportName",
ExportType: "ExportType",
QualifiedInstanceName: "QualifiedInstanceName",
},
}
h := newTestPackageHelper(t)

idlFile := filepath.Join(h.ThriftIDLPath(), "clients/echo/echo.proto")
expectedSpec := &ClientSpec{
ModuleSpec: nil,
YAMLFile: instance.YAMLFileName,
JSONFile: instance.JSONFileName,
ClientType: "grpc",
ImportPackagePath: instance.PackageInfo.ImportPackagePath(),
ImportPackageAlias: instance.PackageInfo.ImportPackageAlias(),
ExportName: instance.PackageInfo.ExportName,
ExportType: instance.PackageInfo.ExportType,
ThriftFile: idlFile,
ClientID: instance.InstanceName,
ClientName: instance.PackageInfo.QualifiedInstanceName,
ExposedMethods: map[string]string{
"a": "method",
},
}

spec, errSpec := client.NewClientSpec(instance, h)
spec.ModuleSpec = nil // Not interested in ModuleSpec here
assert.NoError(t, errSpec)
assert.Equal(t, expectedSpec, spec)
}

func TestCustomClientNewClientSpec(t *testing.T) {
client, errClient := newClientConfig([]byte(customClientYAML))
assert.NoError(t, errClient)
Expand Down

0 comments on commit da6595d

Please sign in to comment.