Skip to content

Commit f6d37d1

Browse files
committed
Add scale test
Signed-off-by: Ulysses Souza <[email protected]>
1 parent acc50a5 commit f6d37d1

File tree

9 files changed

+109
-10
lines changed

9 files changed

+109
-10
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ check: ## Checks the environment before running any command
88
.PHONY: images
99
images: ## Build the test images
1010
docker build server -t test-server
11+
docker build client -t test-client
1112

1213
.PHONY: test
1314
test: check images ## Run tests

client/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM golang:1.13.3 AS dev
2+
WORKDIR /
3+
COPY main.go /
4+
RUN go mod init github.com/compose-spec/compatibility-test-suite/client
5+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main /main.go
6+
7+
FROM scratch AS run
8+
COPY --from=dev main /
9+
ENTRYPOINT ["/main"]

client/main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"os"
6+
"time"
7+
)
8+
9+
const targetHost = "server"
10+
11+
func main() {
12+
time.Sleep(time.Second)
13+
value := os.Getenv("HOSTNAME")
14+
resp, err := http.Get("http://" + targetHost + ":8080/scalechecker?value=" + value)
15+
if err != nil {
16+
panic(err)
17+
}
18+
if resp.StatusCode != http.StatusOK {
19+
panic(resp.Status)
20+
}
21+
}

commands/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name: "docker-composeV1"
22
command: "docker-compose"
33
ps_command: "docker ps -aq"
44
global_opts:
5+
- name: "--compatibility"
56
- name: "-f"
67
value: "compose.yaml"
78
up:

compliance_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77
"time"
88

9+
"gopkg.in/yaml.v2"
910
"gotest.tools/v3/assert"
1011
)
1112

@@ -19,6 +20,8 @@ const (
1920
volumeUrl = volumefileEntrypoint + "?filename="
2021

2122
udpEntrypoint = "http://" + localhost + ":8080/udp"
23+
24+
scaleEntrypoint = "http://" + localhost + ":8080/scalechecker"
2225
)
2326

2427
func TestSimpleLifecycle(t *testing.T) {
@@ -136,3 +139,19 @@ func TestUdpPort(t *testing.T) {
136139
h.Check(expected, actual)
137140
})
138141
}
142+
143+
func TestScaling(t *testing.T) {
144+
h := TestHelper{
145+
T: t,
146+
testDir: "scaling",
147+
skipCommands: []string{"compose-ref"},
148+
specRef: "Networks-top-level-element",
149+
}
150+
h.TestUpDown(func() {
151+
actual := h.getHttpBody(scaleEntrypoint)
152+
responseArray := Response{}
153+
err := yaml.Unmarshal([]byte(actual), &responseArray)
154+
assert.NilError(h.T, err)
155+
h.Check(responseArray.Response, "3")
156+
})
157+
}

server/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ COPY main.go /
44
RUN go mod init github.com/compose-spec/compatibility-test-suite/server
55
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main /main.go
66

7-
FROM ubuntu AS run
7+
FROM scratch AS run
88
COPY --from=dev main /
99
ENTRYPOINT ["/main"]

server/main.go

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,45 @@ func udpHandler(c echo.Context) error {
107107
)
108108
}
109109

110+
var scaleValues []string
111+
112+
type ScaleValue struct {
113+
Value string `json:"value"`
114+
}
115+
116+
func scaleCheckHandler(c echo.Context) error {
117+
s := new(ScaleValue)
118+
if err := c.Bind(s); err != nil {
119+
c.Error(err)
120+
return err
121+
}
122+
if s.Value != "" && !containsString(scaleValues, s.Value) {
123+
scaleValues = append(scaleValues, s.Value)
124+
}
125+
return c.JSON(
126+
http.StatusOK,
127+
getMapResponse(fmt.Sprintf("%d", len(scaleValues))),
128+
)
129+
}
130+
131+
func containsString(ss []string, s string) bool {
132+
for _, v := range ss {
133+
if v == s {
134+
return true
135+
}
136+
}
137+
return false
138+
}
139+
140+
func checkError(err error, exitOnError bool) {
141+
if err != nil {
142+
fmt.Println("Error: ", err)
143+
if exitOnError {
144+
os.Exit(0)
145+
}
146+
}
147+
}
148+
110149
func main() {
111150
go udpServer()
112151

@@ -125,14 +164,6 @@ func main() {
125164
e.GET("/ping", pingHandler)
126165
e.GET("/volumefile", fileHandler)
127166
e.GET("/udp", udpHandler)
167+
e.GET("/scalechecker", scaleCheckHandler)
128168
e.Logger.Fatal(e.StartServer(s))
129169
}
130-
131-
func checkError(err error, exitOnError bool) {
132-
if err != nil {
133-
fmt.Println("Error: ", err)
134-
if exitOnError {
135-
os.Exit(0)
136-
}
137-
}
138-
}

tests/scaling/compose.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3.7'
2+
3+
services:
4+
server:
5+
image: test-server
6+
build: ../../server
7+
ports:
8+
- 8080:8080
9+
client:
10+
image: test-client
11+
build: ../../client
12+
deploy:
13+
replicas: 3

tests_helper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ type TestHelper struct {
4545
specRef string
4646
}
4747

48+
type Response struct {
49+
Response string `yaml:"response"`
50+
}
51+
4852
func (h TestHelper) TestUpDown(fun func()) {
4953
assert.Assert(h, fun != nil, "Test function cannot be `nil`")
5054
for _, f := range h.listFiles(commandsDir) {

0 commit comments

Comments
 (0)