6. Test Annotations
import ballerina/test;
@test:Config {
before: "beforeTestBar",
after: "afterTestBar",
dependsOn: ["testFunctionPre1", "testFuncctionPre2"],
groups: ["group1"]
}
function testBar() {
// test logic for function bar()
}
7. More Test Annotations & Assertions
import ballerina/test;
@test:BeforeSuite {...} | @test:BeforeEach {...} | @test:AfterSuite
{...}
function testBar() {
// test logic for function bar()
test:assertEquals(actual,greetingExp,msg="string test failed");
test:assertTrue(value,msg="Assert True failed");
test:assertFail("Expected an exception");
}
12. Unit Test with Mock
@test:Mock { packageName: ".", functionName: "intAdd" }
function mockIntAdd(int a, int b)returns int)... { return (a - b); }
@test:Config
function testIntAdd() {
int answer = 0;
answer = intAdd(5, 3);
test:assertEquals(actual,2,msg="mock failed");
}
function intAdd(int a, int b)returns int)... { return (a + b); }
13. Service Test Setup
function startService(){
serviceStarted=test:startServices("hello-service");
}
@test:Config { before: "startService",after: "stopService" }
function testFunc() {
//...
}
function stopService() {
test:stopServices("hello-service");
}
14. Service Test Body
@test:Config { before: "startService",after: "stopService" }
function testFunc() {
endpoint http:Client httpEndpoint { url: "http://localhost:9090" };
test:assertTrue(serviceStarted, msg = "Unable to start the service");
...
match response {
...
test:assertEquals(res, response1);
}
error err => test:assertFail(msg = "Failed to call the endpoint:");
}
}
15. Testing Microservices
Function/ Method
Service REST
Services Package
Deploy Kubernetes
End-to-endLogs
Unit
Observability
updated from: "https://martinfowler.com/articles/microservice-testing"
21. Service Deployment - Docker
import ballerinax/docker;
@docker:Config {
registry:"ballerina.guides.io",
name:"travel_agency_service",
tag:"v1.0"
}
@docker:Expose{}
# Auto Generated Dockerfile
FROM ballerina/ballerina:0.975.1
LABEL maintainer="dev@ballerina.io"
COPY travel_agency.balx
/home/ballerina
EXPOSE 9090
CMD ballerina run travel_agency.balx
22. OSS Tools for Testing Containers
Sysdig - Falco for k8
● Uses rules
● Alerts on drift
● Includes container best practices
● Highly configurable
Google - Container Structure Tests
● Unit Tests
○ for Docker Images
● Command Tests
○ verifies output of running a
command inside container
● File Existence Tests
○ verifies existence of file at a
location
● File Content Tests
○ verifies file contents
● Metadata Test
○ verifies container configuration
23. Testing Microservices
Function/ Method
Service REST
Services Package
Deploy Kubernetes
End-to-endLogs
Unit
Observability
updated from: "https://martinfowler.com/articles/microservice-testing"
24. Modern Approaches to Testing Microservices
from "https://opensource.com/article/18/6/five-microservice-testing-strategies-startups"
Documentation First
AWS Testing
(own-account)
Full stack
in-a-box
(local)
Shared
Testing
Instance
Stubbed
services Lorem ipsum congue
tempus
Lorem ipsum
tempus
Lorem ipsum
congue tempus
Lorem
ipsum
tempus
Documentation First
AWS
own-account
Full stack
in-a-box
local
Shared
Testing
Instances
Stubbed
Services
25. Breaking Things on Purpose
○ Inject something harmful
○ in order to build immunity
○ Chaos engineering
○ "thoughtful, planned experiments
designed to reveal the weakness in our
systems"
○ Testing proactively
○ instead of waiting for an outage
○ "Engineering Chaos" = better name
image from: https://www.anshulpatel.in/post/chaos_engg_exprmnt_dsgn/
26.
27. Microservice Testing Reality
Unit Tests
Test functions
Test methods
Test services
Integration Tests
Test interactions within groups
Test interactions between groups
Test interactions outside groups
Deployment Tests
Test Container Configuration
Test Container Management
Configuration
Test Endpoints
Observability
Events, logs and alerts
Test in Production
Engineering Chaos
Modern Tests
Test Documentation
Full stack in-a-box
AWS own-account
Stubbed Services
05
01
02 03
04
29. How to get involved
Learn more
Open source
Get support
http://ballerina.io
http://github.com/ballerina-platform/
Stack Overflow #ballerina tag
Practice http://exercism.io/tracks/ballerina