Skip to content

Commit 8346775

Browse files
committed
e2e: use parallel tests and use a unique container to e2e
1 parent 069d866 commit 8346775

File tree

8 files changed

+118
-221
lines changed

8 files changed

+118
-221
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ luac.out
3737
*.app
3838
*.i*86
3939
*.x86_64
40-
*.hex
40+
*.hex
41+
42+
#e2e temp files
43+
e2e/nginx/

Dockerfile.e2e

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM tsuru/nginx-tsuru:1.26.3-main
2+
3+
COPY ./e2e/container/nginx.conf /etc/nginx/nginx.conf
4+
COPY ./lib/resty/libjwt /usr/local/lib/lua/5.1/resty/libjwt
5+
RUN mkdir -p /etc/nginx/html
6+
RUN echo -n '{"message": "content by nginx"}' > /etc/nginx/html/index.html
7+
8+
COPY e2e/nginx/jwks_1.json /usr/share/tokens/jwks1.json
9+
COPY e2e/nginx/jwks_2.json /usr/share/tokens/jwks2.json
10+
11+
EXPOSE 8888
12+
CMD ["nginx", "-g", "daemon off;"]

e2e/container/container.go

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package container_test
22

33
import (
4-
"fmt"
5-
"io"
6-
"os"
74
"time"
85

96
"github.com/docker/go-connections/nat"
@@ -30,13 +27,7 @@ type File struct {
3027
}
3128

3229
type ContainerInterface interface {
33-
AddFiles(file []File) error
3430
GetProps() Props
35-
Clear() error
36-
Terminate() error
37-
NginxToConfigDefault() error
38-
ChangeNginxConfigReadFile(nginxPath string) error
39-
ChangeNginxConfig(nginxConfig []byte) error
4031
}
4132

4233
var _ ContainerInterface = (*ContainerTest)(nil)
@@ -71,71 +62,9 @@ func New(ctx context.Context, context string, dockerfile string) (*ContainerTest
7162
}
7263
container.Props.Port = port.Port()
7364
container.Container = nginxContainer
74-
if err := container.NginxToConfigDefault(); err != nil {
75-
return nil, err
76-
}
7765
return &container, nil
7866
}
7967

80-
func (c *ContainerTest) AddFiles(files []File) error {
81-
for _, file := range files {
82-
c.FilesPath = append(c.FilesPath, file.Path)
83-
err := c.Container.CopyToContainer(context.Background(), []byte(file.File), file.Path, 0644)
84-
if err != nil {
85-
return err
86-
}
87-
}
88-
return nil
89-
}
90-
91-
func (c *ContainerTest) Clear() error {
92-
for _, file := range c.FilesPath {
93-
_, _, err := c.Container.Exec(context.Background(), []string{"rm", file})
94-
if err != nil {
95-
return err
96-
}
97-
}
98-
return c.NginxToConfigDefault()
99-
}
100-
101-
func (c *ContainerTest) NginxToConfigDefault() error {
102-
srcFile, err := os.Open("./container/nginx.conf")
103-
if err != nil {
104-
return fmt.Errorf("error opening nginx.conf: %w", err)
105-
}
106-
defer srcFile.Close()
107-
fileContent, err := io.ReadAll(srcFile)
108-
if err != nil {
109-
return fmt.Errorf("error reading nginx.conf: %w", err)
110-
}
111-
return c.ChangeNginxConfig(fileContent)
112-
}
113-
114-
func (c *ContainerTest) ChangeNginxConfigReadFile(nginxPath string) error {
115-
nginxConf, err := os.ReadFile(nginxPath)
116-
if err != nil {
117-
return err
118-
}
119-
return c.ChangeNginxConfig(nginxConf)
120-
}
121-
122-
func (c *ContainerTest) ChangeNginxConfig(nginxConfig []byte) error {
123-
_, _, err := c.Container.Exec(context.Background(), []string{"sh", "-c", "rm ", "/usr/local/openresty/nginx/conf/nginx.conf"})
124-
if err != nil {
125-
return err
126-
}
127-
err = c.Container.CopyToContainer(context.Background(), nginxConfig, "/usr/local/openresty/nginx/conf/nginx.conf", 0644)
128-
if err != nil {
129-
return err
130-
}
131-
_, _, err = c.Container.Exec(context.Background(), []string{"sh", "-c", "pkill -HUP openresty || openresty"})
132-
return err
133-
}
134-
135-
func (c *ContainerTest) Terminate() error {
136-
return c.Container.Terminate(context.Background())
137-
}
138-
13968
func (c *ContainerTest) GetProps() Props {
14069
return c.Props
14170
}

e2e/container/nginx.conf

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
worker_processes 1;
22

3+
include modules/*.conf;
4+
35
events {
46
worker_connections 1024;
57
}
@@ -13,5 +15,39 @@ http {
1315
default_type application/json;
1416
return 200 '{"message": "content by nginx"}';
1517
}
18+
19+
location /public {
20+
default_type application/json;
21+
return 200 '{"message": "Hello, World!"}';
22+
}
23+
24+
location /private {
25+
content_by_lua_block {
26+
local libjwt = require("resty.libjwt")
27+
local cjson = require("cjson.safe")
28+
local token, err = libjwt.validate({
29+
jwks_files = {"/usr/share/tokens/jwks1.json", "/usr/share/tokens/jwks2.json"},
30+
})
31+
ngx.header.content_type = "application/json"
32+
if err and err ~= "" then
33+
ngx.status = ngx.HTTP_UNAUTHORIZED
34+
local response = {
35+
message = err
36+
}
37+
return ngx.say(cjson.encode(response))
38+
end
39+
if token then
40+
local claim_str = cjson.encode(token.claim) or "Invalid token"
41+
ngx.log(ngx.ERR, "Token Claims: " .. claim_str)
42+
ngx.status = ngx.HTTP_OK
43+
return ngx.say(claim_str)
44+
end
45+
ngx.status = ngx.HTTP_UNAUTHORIZED
46+
local response = {
47+
message = "Unauthorized"
48+
}
49+
return ngx.say(cjson.encode(response))
50+
}
51+
}
1652
}
1753
}

0 commit comments

Comments
 (0)