Skip to content

Commit

Permalink
change test files
Browse files Browse the repository at this point in the history
  • Loading branch information
DDH13 committed May 9, 2024
1 parent 4d8be88 commit 5ab8ad1
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 61 deletions.
160 changes: 136 additions & 24 deletions test/integration/integration/tests/grpc-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,162 @@
package tests

import (
"context"
"crypto/tls"
"github.com/wso2/apk/test/integration/integration/utils/generatedcode/student"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"testing"
"time"

"github.com/wso2/apk/test/integration/integration/utils/http"
"github.com/wso2/apk/test/integration/integration/utils/suite"
)

func init() {
IntegrationTests = append(IntegrationTests, GRPCAPI)
}

// DisableAPISecurity test
// GRPCAPI tests gRPC API
var GRPCAPI = suite.IntegrationTest{
ShortName: "GRPCAPI",
Description: "Tests gRPC API",
Manifests: []string{"tests/grpc-api.yaml"},
//Manifests: []string{"tests/grpc-api.yaml"},
Test: func(t *testing.T, suite *suite.IntegrationTestSuite) {
gwAddr := "grpc.test.gw.wso2.com:9095"
//token := http.GetTestToken(t)

testCases := []http.ExpectedResponse{
//{
// Request: http.Request{
// Host: "gql.test.gw.wso2.com",
// Path: "/gql/v1",
// Method: "POST",
// Headers: map[string]string{
// "Content-Type": "application/json",
// },
// Body: `{"query":"query{\n human(id:1000){\n id\n name\n }\n}","variables":{}}`,
// },
// ExpectedRequest: &http.ExpectedRequest{
// Request: http.Request{
// Method: ""},
// },
// Response: http.Response{StatusCode: 200},
//},

//testCases := []http.ExpectedResponse{
//{
// Request: http.Request{
// Host: "gql.test.gw.wso2.com",
// Path: "/gql/v1",
// Method: "POST",
// Headers: map[string]string{
// "Content-Type": "application/json",
// },
// Body: `{"query":"query{\n human(id:1000){\n id\n name\n }\n}","variables":{}}`,
// },
// ExpectedRequest: &http.ExpectedRequest{
// Request: http.Request{
// Method: ""},
// },
// Response: http.Response{StatusCode: 200},
//},
//}
testCases := []ExpectedResponse{
{
out: &student.StudentResponse{
Name: "Dineth",
Age: 10,
},
err: nil,
},
}
for i := range testCases {
tc := testCases[i]
t.Run(tc.GetTestCaseName(i), func(t *testing.T) {
t.Parallel()
http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, tc)
//t.Run(tc.GetTestCaseName(i), func(t *testing.T) {
// t.Parallel()
// http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, tc)
//})
t.Run("Invoke gRPC API", func(t *testing.T) {
out, err := invokeGRPCClientUntilSatisfied(gwAddr, t)
if err != nil {
if tc.err != nil {
t.Errorf("Err -> \nWant: %q\nGot: %q\n", tc.err, err)
}
} else {
if tc.out.Name != out.Name ||
tc.out.Age != out.Age {
t.Errorf("Out -> \nWant: %q\nGot : %q", tc.out, out)
}
}

})
}
},
}

func invokeGRPCClient(gwAddr string, t *testing.T) (*student.StudentResponse, error) {

t.Logf("Starting gRPC client...")

// Set up TLS credentials for the connection without enforcing server certificate validation.
t.Logf("Setting up TLS credentials without server certificate validation...")
config := &tls.Config{
InsecureSkipVerify: true, // CAUTION: This disables SSL certificate verification.
}
creds := credentials.NewTLS(config)

// Dial the server with the TLS credentials and a dial timeout.
dialCtx, dialCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer dialCancel()
t.Logf("Dialing to server at %s with timeout...", gwAddr)
conn, err := grpc.DialContext(dialCtx, gwAddr, grpc.WithTransportCredentials(creds), grpc.WithBlock())
if err != nil {
t.Fatalf("Failed to connect: %v", err)
}
defer conn.Close()
t.Log("Successfully connected to the server.")

c := student.NewStudentServiceClient(conn)

// Prepare the context with a timeout for the request.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

t.Log("Sending request to the server...")
// Create a StudentRequest message
r := &student.StudentRequest{Id: 1234} // Adjust the ID according to your actual implementation
response, err := c.GetStudent(ctx, r)
if err != nil {
t.Logf("Could not fetch student: %v", err)
}

t.Logf("Received response from server: %v\n", response)
t.Logf("Student Details: %v\n", response)
return response, nil
}

type ExpectedResponse struct {
out *student.StudentResponse
err error
}

func invokeGRPCClientUntilSatisfied(gwAddr string, t *testing.T) (*student.StudentResponse, error) {
var out *student.StudentResponse
var err error
attempt := 0
maxAttempts := 4

for attempt < maxAttempts {
t.Logf("Attempt %d to invoke gRPC client...", attempt+1)
out, err = invokeGRPCClient(gwAddr, t)

if err != nil {
t.Logf("Error on attempt %d: %v", attempt+1, err)
} else {
// Check if the response is satisfactory. This condition needs to be defined.
// For example, assuming a satisfactory condition is when out.Satisfied is true.
// This is a placeholder condition and should be replaced with your actual success criteria.
if out != nil && isResponseSatisfactory(out) {
t.Logf("Satisfactory response received: %+v", out)
return out, nil
}
}

if attempt < maxAttempts-1 {
t.Logf("Waiting 20 seconds before next attempt...")
time.Sleep(20 * time.Second)
}
attempt++
}

t.Logf("Failed to receive a satisfactory response after %d attempts", maxAttempts)
return out, err // Returning the last response and error, might need adjustment based on requirements.
}

func isResponseSatisfactory(response *student.StudentResponse) bool {
// Define the condition for a response to be considered satisfactory.
// This is a placeholder function and should contain actual logic to evaluate the response.
return false // Placeholder: assume every response is satisfactory.
}
14 changes: 7 additions & 7 deletions test/integration/integration/tests/resources/base/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ spec:
apiVersion: v1
kind: Service
metadata:
name: dineth-grpc-demo-server
name: grpc-backend-v1
namespace: gateway-integration-test-infra
spec:
selector:
app: dineth-grpc-demo-server
app: grpc-backend-v1
ports:
- protocol: TCP
port: 6565
Expand All @@ -127,22 +127,22 @@ spec:
apiVersion: apps/v1
kind: Deployment
metadata:
name: dineth-grpc-demo-server
name: grpc-backend-v1
namespace: gateway-integration-test-infra
labels:
app: dineth-grpc-demo-server
app: grpc-backend-v1
spec:
replicas: 1
selector:
matchLabels:
app: dineth-grpc-demo-server
app: grpc-backend-v1
template:
metadata:
labels:
app: dineth-grpc-demo-server
app: grpc-backend-v1
spec:
containers:
- name: dineth-grpc-demo-server
- name: grpc-backend-v1
image: ddh13/dineth-grpc-demo-server:1.0.0
imagePullPolicy: Always
env:
Expand Down
45 changes: 15 additions & 30 deletions test/integration/integration/tests/resources/tests/grpc-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,75 +17,66 @@
apiVersion: dp.wso2.com/v1alpha2
kind: API
metadata:
name: dineth-grpc-api
name: grpc-api
namespace: gateway-integration-test-infra
labels:
api-name: "dineth-grpc-api"
api-version: "v1"
organization: wso2-org
managed-by: "apk"
spec:
apiName: Dineth GRPC API
apiName: GRPC API
apiType: GRPC
apiVersion: v1
basePath: /dineth.grpc.api.v1
isDefaultVersion: false
production:
- routeRefs:
- dineth-grpc-route
- grpc-api-grpcroute
organization: wso2-org

---

apiVersion: gateway.networking.k8s.io/v1alpha2
kind: GRPCRoute
metadata:
name: dineth-grpc-route
name: grpc-api-grpcroute
namespace: gateway-integration-test-infra
labels:
api-name: "dineth-grpc-api"
api-version: "v1"
organization: "wso2-org"
managed-by: "apk"
spec:
hostnames:
- grpc.test.gw.wso2.com
parentRefs:
- group: gateway.networking.k8s.io
kind: Gateway
name: default
name: wso2-apk-default
namespace: apk-integration-test
sectionName: httpslistener
hostnames:
- grpc.test.gw.wso2.com
rules:
- matches:
- method:
service: student.StudentService
method: GetStudent
backendRefs:
- name: dineth-grpc-backend
- name: grpc-backend-v1
kind: Backend
port: 6565
- matches:
- method:
service: student.StudentService
method: SendStudentStream
backendRefs:
- name: dineth-grpc-backend
- name: grpc-backend-v1
kind: Backend
port: 6565
- matches:
- method:
service: student.StudentService
method: GetStudentStream
backendRefs:
- name: dineth-grpc-backend
- name: grpc-backend-v1
kind: Backend
port: 6565
- matches:
- method:
service: student.StudentService
method: SendAndGetStudentStream
backendRefs:
- name: dineth-grpc-backend
- name: grpc-backend-v1
kind: Backend
port: 6565

Expand All @@ -103,22 +94,16 @@ spec:
group: gateway.networking.k8s.io
kind: API
namespace: gateway-integration-test-infra
name: dineth-grpc-api
name: grpc-api
---

apiVersion: dp.wso2.com/v1alpha1
kind: Backend
metadata:
name: dineth-grpc-backend
name: grpc-backend-v1
namespace: gateway-integration-test-infra
labels:
api-name: "dineth-grpc-api"
api-version: "v1"
organization: wso2-org
managed-by: "apk"
spec:
services:
- host: grpc-backend.gateway-integration-test-infra
- host: grpc-backend-v1.gateway-integration-test-infra
port: 6565
basePath: ""
protocol: https
1 change: 1 addition & 0 deletions test/integration/scripts/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ sudo echo "$IP ratelimit-priority.test.gw.wso2.com" | sudo tee -a /etc/hosts
sudo echo "$IP different-endpoint-with-same-route.test.gw.wso2.com" | sudo tee -a /etc/hosts
sudo echo "$IP custom-auth-header.test.gw.wso2.com" | sudo tee -a /etc/hosts
sudo echo "$IP gql.test.gw.wso2.com" | sudo tee -a /etc/hosts
sudo echo "$IP grpc.test.gw.wso2.com" | sudo tee -a /etc/hosts
sudo echo "$IP api-level-jwt.test.gw.wso2.com" | sudo tee -a /etc/hosts
sudo echo "$IP resource-level-jwt.test.gw.wso2.com" | sudo tee -a /etc/hosts
sudo echo "255.255.255.255 broadcasthost" | sudo tee -a /etc/hosts
Expand Down
1 change: 1 addition & 0 deletions test/integration/scripts/setup-hosts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ sudo echo "$IP ratelimit-priority.test.gw.wso2.com" | sudo tee -a /etc/hosts
sudo echo "$IP different-endpoint-with-same-route.test.gw.wso2.com" | sudo tee -a /etc/hosts
sudo echo "$IP custom-auth-header.test.gw.wso2.com" | sudo tee -a /etc/hosts
sudo echo "$IP gql.test.gw.wso2.com" | sudo tee -a /etc/hosts
sudo echo "$IP grpc.test.gw.wso2.com" | sudo tee -a /etc/hosts
sudo echo "$IP api-level-jwt.test.gw.wso2.com" | sudo tee -a /etc/hosts
sudo echo "$IP resource-level-jwt.test.gw.wso2.com" | sudo tee -a /etc/hosts
sudo echo "255.255.255.255 broadcasthost" | sudo tee -a /etc/hosts
Expand Down

0 comments on commit 5ab8ad1

Please sign in to comment.