Skip to content

Commit

Permalink
feat(cli): Allow user to reference protobuf file path on Test yaml fr…
Browse files Browse the repository at this point in the history
…om CLI (#2620)

* Adding grpc e2e test

* Updating tests

* Adding feature of read protobuf file from path and injecting it on gRPC trigger

* Adding validation to the test

* Adding PR suggestion
  • Loading branch information
danielbdias committed May 31, 2023
1 parent d55b683 commit ab6d0a4
Show file tree
Hide file tree
Showing 10 changed files with 499 additions and 85 deletions.
55 changes: 55 additions & 0 deletions cli/actions/run_test_action.go
Expand Up @@ -112,6 +112,17 @@ func (a runTestAction) testFileToID(ctx context.Context, originalPath, filePath
return "", err
}

if t, err := f.Definition().Test(); err == nil && t.Trigger.Type == "grpc" {
newFile, err := getUpdatedTestWithGrpcTrigger(f, t)
if err != nil {
return "", err
}

if newFile != nil { // has new file, update it
f = *newFile
}
}

body, _, err := a.client.ApiApi.
UpsertDefinition(ctx).
TextDefinition(openapi.TextDefinition{
Expand Down Expand Up @@ -182,6 +193,17 @@ func (a runTestAction) runDefinitionFile(ctx context.Context, f file.File, param
}
}

if t, err := f.Definition().Test(); err == nil && t.Trigger.Type == "grpc" {
newFile, err := getUpdatedTestWithGrpcTrigger(f, t)
if err != nil {
return err
}

if newFile != nil { // has new file, update it
f = *newFile
}
}

variables := make([]openapi.EnvironmentValue, 0)
for name, value := range params.EnvironmentVariables {
variables = append(variables, openapi.EnvironmentValue{Key: openapi.PtrString(name), Value: openapi.PtrString(value)})
Expand Down Expand Up @@ -578,3 +600,36 @@ func getTestRunIDFromString(testRunIDAsString string) int32 {
func getTestRunID(testRun openapi.TestRun) int32 {
return getTestRunIDFromString(testRun.GetId())
}

func getUpdatedTestWithGrpcTrigger(f file.File, t yaml.Test) (*file.File, error) {
protobufFile := filepath.Join(f.AbsDir(), t.Trigger.GRPC.ProtobufFile)

if !utils.StringReferencesFile(protobufFile) {
return nil, nil
}

// referencing a file, keep the value
fileContent, err := utils.ReadFileContent(protobufFile)
if err != nil {
return nil, fmt.Errorf(`cannot read protobuf file: %w`, err)
}

t.Trigger.GRPC.ProtobufFile = fileContent

def := yaml.File{
Type: yaml.FileTypeTest,
Spec: t,
}

updated, err := def.Encode()
if err != nil {
return nil, fmt.Errorf(`cannot encode updated test: %w`, err)
}

f, err = file.New(f.Path(), updated)
if err != nil {
return nil, fmt.Errorf(`cannot recreate updated file: %w`, err)
}

return &f, nil
}
9 changes: 9 additions & 0 deletions cli/utils/common.go
Expand Up @@ -21,6 +21,15 @@ func IOReadCloserToString(r io.ReadCloser) string {
return string(b)
}

func ReadFileContent(filePath string) (string, error) {
fileContent, err := os.ReadFile(filePath)
if err != nil {
return "", err
}

return string(fileContent), nil
}

func StringReferencesFile(path string) bool {
// for the current working dir, check if the file exists
// by finding its absolute path and executing a stat command
Expand Down
@@ -0,0 +1,68 @@
version: '3'
services:
tracetest:
image: kubeshop/tracetest:${TAG:-latest}
volumes:
- type: bind
source: ./tracetest-config.yaml
target: /app/tracetest.yaml
- type: bind
source: ./tracetest-provision.yaml
target: /app/provision.yaml
command: --provisioning-file /app/provision.yaml
ports:
- 11633:11633
extra_hosts:
- "host.docker.internal:host-gateway"
depends_on:
postgres:
condition: service_healthy
otel-collector:
condition: service_started
healthcheck:
test: ["CMD", "wget", "--spider", "localhost:11633"]
interval: 1s
timeout: 3s
retries: 60
environment:
TRACETEST_DEV: ${TRACETEST_DEV}

postgres:
image: postgres:14
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
healthcheck:
test: pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"
interval: 1s
timeout: 5s
retries: 60
ports:
- 5432

otel-collector:
image: otel/opentelemetry-collector:0.54.0
command:
- "--config"
- "/otel-local-config.yaml"
volumes:
- ./collector.config.yaml:/otel-local-config.yaml
depends_on:
- jaeger
ports:
- 4317

jaeger:
image: jaegertracing/all-in-one:latest
restart: unless-stopped
healthcheck:
test: ["CMD", "wget", "--spider", "localhost:16686"]
interval: 1s
timeout: 3s
retries: 60
ports:
- 16685

networks:
default:
name: _default
@@ -0,0 +1,112 @@
services:
cache:
healthcheck:
test:
- CMD
- redis-cli
- ping
timeout: 3s
interval: 1s
retries: 60
image: redis:6
restart: unless-stopped

demo-api:
depends_on:
cache:
condition: service_healthy
postgres:
condition: service_healthy
queue:
condition: service_healthy
environment:
COLLECTOR_ENDPOINT: http://otel-collector:4317
DATABASE_URL: postgresql://postgres:postgres@postgres:5432/postgres?schema=public
NPM_RUN_COMMAND: api
POKE_API_BASE_URL: https://pokeapi.co/api/v2
RABBITMQ_HOST: queue
REDIS_URL: cache
healthcheck:
test:
- CMD
- wget
- --spider
- localhost:8081
timeout: 3s
interval: 1s
retries: 60
image: kubeshop/demo-pokemon-api:latest
ports:
- mode: ingress
target: 8081
published: 8081
protocol: tcp
pull_policy: always
restart: unless-stopped

demo-rpc:
depends_on:
cache:
condition: service_healthy
postgres:
condition: service_healthy
queue:
condition: service_healthy
environment:
COLLECTOR_ENDPOINT: http://otel-collector:4317
DATABASE_URL: postgresql://postgres:postgres@postgres:5432/postgres?schema=public
NPM_RUN_COMMAND: rpc
POKE_API_BASE_URL: https://pokeapi.co/api/v2
RABBITMQ_HOST: queue
REDIS_URL: cache
healthcheck:
test:
- CMD
- lsof
- -i
- "8082"
timeout: 3s
interval: 1s
retries: 60
image: kubeshop/demo-pokemon-api:latest
ports:
- mode: ingress
target: 8082
published: 8082
protocol: tcp
pull_policy: always
restart: unless-stopped

demo-worker:
depends_on:
cache:
condition: service_healthy
postgres:
condition: service_healthy
queue:
condition: service_healthy
environment:
COLLECTOR_ENDPOINT: http://otel-collector:4317
DATABASE_URL: postgresql://postgres:postgres@postgres:5432/postgres?schema=public
NPM_RUN_COMMAND: worker
POKE_API_BASE_URL: https://pokeapi.co/api/v2
RABBITMQ_HOST: queue
REDIS_URL: cache
image: kubeshop/demo-pokemon-api:latest
pull_policy: always
restart: unless-stopped

queue:
healthcheck:
test:
- CMD-SHELL
- rabbitmq-diagnostics -q check_running
timeout: 5s
interval: 1s
retries: 60
image: rabbitmq:3.8-management
restart: unless-stopped

networks:
default:
name: _default

This file was deleted.

0 comments on commit ab6d0a4

Please sign in to comment.