Skip to content

Commit

Permalink
chore: fix ESLint errors (#1589)
Browse files Browse the repository at this point in the history
  • Loading branch information
heitortsergent authored May 21, 2024
1 parent 5d23e29 commit c98e8ec
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ To do so, follow these steps:

{{< code >}}

<!-- eslint-disable no-undef -->

```javascript
export function contacts() {
group('Contacts flow', function () {
Expand All @@ -128,6 +130,8 @@ To do so, follow these steps:
}
```

<!-- eslint-enable no-undef -->

{{< /code >}}

As is, this script won't work, since it has undeclared functions and variables.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ As an optional step, you can log the response body to the console to make sure y
{{< code >}}
<!-- eslint-disable no-undef -->
```javascript
export default function () {
...
// ...
const res = http.post(url, payload, params);
Expand All @@ -85,6 +87,8 @@ export default function () {
}
```
<!-- eslint-enable no-undef -->
{{< /code >}}
## Add response checks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,14 @@ To do this:
1. Add the `abortOnFail` property to `http_req_failed`.

```javascript
http_req_failed: [{ threshold: "rate<0.01", abortOnFail: true }], // http errors should be less than 1%, otherwise abort the test
export const options = {
// define thresholds
thresholds: {
http_req_failed: [{ threshold: 'rate<0.01', abortOnFail: true }], // http errors should be less than 1%, otherwise abort the test
http_req_duration: ['p(99)<1000'], // 99% of requests should be below 1s
},
// ...
};
```

1. Update the `scenarios` property to ramp the test up until it fails.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@ import { check } from 'k6';

We also create a grpc client and load the protobufs definitions for the [HelloService](https://github.com/moul/pb/blob/master/hello/hello.proto) service.

<!-- eslint-disable no-undef -->

```javascript
const client = new grpc.Client();
client.load(['pb'], 'hello.proto');
```

<!-- eslint-enable no-undef -->

## Test Load

The test load is generated by the `default` function, which connects to the `grpcbin` service using the IP and port obtained from the environment variable `GRPC_HOST` and invokes the `SayHello` method of the `hello.HelloService` service. Finally, The status code of the response is checked. When faults are injected, this check should fail.

<!-- eslint-disable no-undef -->

```javascript
export default function () {
client.connect(__ENV.GRPC_HOST, {
Expand All @@ -56,12 +62,16 @@ export default function () {
}
```

<!-- eslint-enable no-undef -->

## Fault injection

The `disrupt` function creates a `ServiceDisruptor` for the `grpcbin` service in the namespace `grpcbin`.

The gRPC faults are injected by calling the [ServiceDisruptor.injectGrpcFaults](https://grafana.com/docs/k6/<K6_VERSION>/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor/injectgrpcfaults) method using a fault definition that introduces a delay of `300ms` on each request and an error with status code `13` ("Internal error") in `10%` of the requests.

<!-- eslint-disable no-undef -->

```javascript
export function disrupt() {
if (__ENV.SKIP_FAULTS == '1') {
Expand All @@ -79,11 +89,16 @@ export function disrupt() {
}
```

<!-- eslint-enable no-undef -->

Notice the following code snippet in the `injectFaults` function above:

```javascript
if (__ENV.SKIP_FAULTS == '1') {
return;
export function disrupt() {
if (__ENV.SKIP_FAULTS == '1') {
return;
}
//...
}
```

Expand All @@ -94,24 +109,26 @@ This code makes the function return without injecting faults if the `SKIP_FAULTS
This test defines two [scenarios](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/scenarios) to be executed. The `load` scenario applies the test load to the `grpcpbin` application for `30s` invoking the `default` function. The `disrupt` scenario invokes the `disrupt` function to inject a fault in the gRPC requests to the target application.

```javascript
scenarios: {
load: {
executor: 'constant-arrival-rate',
rate: 100,
preAllocatedVUs: 10,
maxVUs: 100,
exec: "default",
startTime: '0s',
duration: "30s",
},
disrupt: {
executor: 'shared-iterations',
iterations: 1,
vus: 1,
exec: "disrupt",
startTime: "0s",
}
}
export const options = {
scenarios: {
load: {
executor: 'constant-arrival-rate',
rate: 100,
preAllocatedVUs: 10,
maxVUs: 100,
exec: 'default',
startTime: '0s',
duration: '30s',
},
disrupt: {
executor: 'shared-iterations',
iterations: 1,
vus: 1,
exec: 'disrupt',
startTime: '0s',
},
},
};
```

{{% admonition type="note" %}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ import http from 'k6/http';

The test load is generated by the `default` function, which executes a request to the `httpbin` pod using the IP obtained from the environment variable `SVC_IP`. The test makes requests to the endpoint `delay/0.1` which will return after `0.1` seconds (`100ms`).

<!-- eslint-disable no-undef -->

```javascript
export default function (data) {
http.get(`http://${data.SVC_IP}/delay/0.1`);
}
```

<!-- eslint-enable no-undef -->

{{% admonition type="note" %}}

The test uses the `delay` endpoint which return after the requested delay. It requests a `0.1s` (`100ms`) delay to ensure the baseline scenario (see scenarios below) has meaningful statistics for the request duration. If we were simply calling a locally deployed http server (for example `nginx`), the response time would exhibit a large variation between a few microseconds to a few milliseconds. Having `100ms` as baseline response time has proved to offer more consistent results.
Expand All @@ -47,6 +51,8 @@ The `disrupt` function creates a `PodDisruptor` using a selector that matches po

The http faults are injected by calling the [PodDisruptor.injectHTTPFaults](https://grafana.com/docs/k6/<K6_VERSION>/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor/injecthttpfaults) method using a fault definition that introduces a delay of `50ms` on each request and an error code `500` in `10%` of the requests.

<!-- eslint-disable no-undef -->

```javascript
export function disrupt(data) {
if (__ENV.SKIP_FAULTS == '1') {
Expand All @@ -71,11 +77,16 @@ export function disrupt(data) {
}
```

<!-- eslint-enable no-undef -->

Notice the following code snippet in the `injectFaults` function above:

```javascript
if (__ENV.SKIP_FAULTS == '1') {
return;
export function disrupt(data) {
if (__ENV.SKIP_FAULTS == '1') {
return;
}
//...
}
```

Expand All @@ -86,24 +97,26 @@ This code makes the function return without injecting faults if the `SKIP_FAULTS
This test defines two [scenarios](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/scenarios) to be executed. The `load` scenario applies the test load to the `httpbin` application for `30s` invoking the `default` function. The `disrupt` scenario invokes the `disrupt` function to inject a fault in the HTTP requests of the target application.

```javascript
scenarios: {
load: {
executor: 'constant-arrival-rate',
rate: 100,
preAllocatedVUs: 10,
maxVUs: 100,
exec: "default",
startTime: "0s",
duration: "30s",
},
disrupt: {
executor: 'shared-iterations',
iterations: 1,
vus: 1,
exec: "disrupt",
startTime: "0s",
},
}
export const options = {
scenarios: {
load: {
executor: 'constant-arrival-rate',
rate: 100,
preAllocatedVUs: 10,
maxVUs: 100,
exec: 'default',
startTime: '0s',
duration: '30s',
},
disrupt: {
executor: 'shared-iterations',
iterations: 1,
vus: 1,
exec: 'disrupt',
startTime: '0s',
},
},
};
```

{{% admonition type="note" %}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ weight: 01
It provides a Javascript [API](https://grafana.com/docs/k6/<K6_VERSION>/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/) to inject [faults](https://grafana.com/docs/k6/<K6_VERSION>/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/faults) such as errors and delays into HTTP and gRPC requests served by selected Kubernetes [Pods](https://grafana.com/docs/k6/<K6_VERSION>/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/poddisruptor) or [Services](https://grafana.com/docs/k6/<K6_VERSION>/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/servicedisruptor).

```javascript
import { ServiceDisruptor } from 'k6/x/disruptor';

export default function () {
// Create a new disruptor that targets a service
const disruptor = new ServiceDisruptor('app-service', 'app-namespace');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ The code below shows an example of combining a browser and HTTP test in a single

{{< code >}}

<!-- eslint-disable no-undef -->

```javascript
import http from 'k6/http';
import { check } from 'k6';
Expand Down Expand Up @@ -58,7 +60,7 @@ export const options = {
};

export function getPizza() {
let restrictions = {
const restrictions = {
maxCaloriesPerSlice: 500,
mustBeVegetarian: false,
excludedIngredients: ['pepperoni'],
Expand All @@ -67,7 +69,7 @@ export function getPizza() {
minNumberOfToppings: 2,
};

let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
const res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
headers: {
'Content-Type': 'application/json',
'X-User-ID': customers[Math.floor(Math.random() * customers.length)],
Expand Down Expand Up @@ -101,6 +103,8 @@ export async function checkFrontend() {
}
```

<!-- eslint-enable no-undef -->

{{< /code >}}

## Browser and failure injection test
Expand All @@ -117,6 +121,7 @@ To find out more information about injecting faults to your service, check out t
import http from 'k6/http';
import { check } from 'k6';
import { browser } from 'k6/experimental/browser';
import { ServiceDisruptor } from 'k6/x/disruptor';

const BASE_URL = __ENV.BASE_URL;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ To do so, follow these steps:

{{< code >}}

<!-- eslint-disable no-undef -->

```javascript
export function contacts() {
group('Contacts flow', function () {
Expand All @@ -128,6 +130,8 @@ To do so, follow these steps:
}
```

<!-- eslint-enable no-undef -->

{{< /code >}}

As is, this script won't work, since it has undeclared functions and variables.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ As an optional step, you can log the response body to the console to make sure y
{{< code >}}
<!-- eslint-disable no-undef -->
```javascript
export default function () {
...
// ...
const res = http.post(url, payload, params);
Expand All @@ -85,6 +87,8 @@ export default function () {
}
```
<!-- eslint-enable no-undef -->
{{< /code >}}
## Add response checks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,14 @@ To do this:
1. Add the `abortOnFail` property to `http_req_failed`.

```javascript
http_req_failed: [{ threshold: "rate<0.01", abortOnFail: true }], // http errors should be less than 1%, otherwise abort the test
export const options = {
// define thresholds
thresholds: {
http_req_failed: [{ threshold: 'rate<0.01', abortOnFail: true }], // http errors should be less than 1%, otherwise abort the test
http_req_duration: ['p(99)<1000'], // 99% of requests should be below 1s
},
// ...
};
```

1. Update the `scenarios` property to ramp the test up until it fails.
Expand Down
Loading

0 comments on commit c98e8ec

Please sign in to comment.