Skip to content

Commit 9e9411a

Browse files
committed
chore: add e2e tests
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 55afe5b commit 9e9411a

File tree

4 files changed

+204
-3
lines changed

4 files changed

+204
-3
lines changed

.github/workflows/ci.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,11 @@ jobs:
109109
run: sudo bin/finch-daemon --debug --socket-owner $UID &
110110
- name: Run e2e test
111111
run: sudo make test-e2e
112+
- name: Clean up Daemon socket
113+
run: sudo rm /var/run/finch.sock && sudo rm /run/finch.pid
114+
- name: Verify Rego file presence
115+
run: ls -l ${{ github.workspace }}/sample.rego
116+
- name: Start finch-daemon with opa Authz
117+
run: sudo bin/finch-daemon --debug --enable-opa --rego-file ${{ github.workspace }}/sample.rego --socket-owner $UID &
118+
- name: Run opa e2e tests
119+
run: sudo make test-e2e-opa

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ test-e2e: linux
114114
TEST_E2E=1 \
115115
$(GINKGO) $(GFLAGS) ./e2e/...
116116

117+
.PHONY: test-e2e-opa
118+
test-e2e-opa: linux
119+
DOCKER_HOST="unix:///run/finch.sock" \
120+
DOCKER_API_VERSION="v1.43" \
121+
OPA_E2E=1 \
122+
TEST_E2E=1 \
123+
$(GINKGO) $(GFLAGS) ./e2e/...
124+
117125
.PHONY: licenses
118126
licenses:
119127
PATH=$(BIN):$(PATH) go-licenses report --template="scripts/third-party-license.tpl" --ignore github.com/runfinch ./... > THIRD_PARTY_LICENSES

e2e/e2e_test.go

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package e2e
55

66
import (
7+
"fmt"
78
"os"
89
"testing"
910

@@ -15,11 +16,104 @@ import (
1516
"github.com/runfinch/finch-daemon/e2e/tests"
1617
)
1718

19+
// func TestRun(t *testing.T) {
20+
// if os.Getenv("TEST_E2E") != "1" {
21+
// t.Skip("E2E tests skipped. Set TEST_E2E=1 to run these tests")
22+
// }
23+
// // TODO : Make this configurable
24+
// runtimeExe := "nerdctl"
25+
// opt, _ := option.New([]string{runtimeExe, "-n", "finch"})
26+
27+
// ginkgo.SynchronizedBeforeSuite(func() []byte {
28+
// tests.SetupLocalRegistry(opt)
29+
// return nil
30+
// }, func(bytes []byte) {})
31+
32+
// ginkgo.SynchronizedAfterSuite(func() {
33+
// tests.CleanupLocalRegistry(opt)
34+
// // clean up everything after the local registry is cleaned up
35+
// command.RemoveAll(opt)
36+
// }, func() {})
37+
38+
// const description = "Finch Daemon Functional test"
39+
// ginkgo.Describe(description, func() {
40+
// // functional test for container APIs
41+
// tests.ContainerCreate(opt)
42+
// tests.ContainerStart(opt)
43+
// tests.ContainerStop(opt)
44+
// tests.ContainerRestart(opt)
45+
// tests.ContainerRemove(opt)
46+
// tests.ContainerList(opt)
47+
// tests.ContainerRename(opt)
48+
// tests.ContainerStats(opt)
49+
// tests.ContainerAttach(opt)
50+
// tests.ContainerLogs(opt)
51+
// tests.ContainerKill(opt)
52+
53+
// // functional test for volume APIs
54+
// tests.VolumeList(opt)
55+
// tests.VolumeInspect(opt)
56+
// tests.VolumeRemove(opt)
57+
58+
// // functional test for network APIs
59+
// tests.NetworkCreate(opt)
60+
// tests.NetworkRemove(opt)
61+
// tests.NetworkList(opt)
62+
// tests.NetworkInspect(opt)
63+
64+
// // functional test for image APIs
65+
// tests.ImageRemove(opt)
66+
// tests.ImagePush(opt)
67+
// tests.ImagePull(opt)
68+
69+
// // functional test for system api
70+
// tests.SystemVersion(opt)
71+
// tests.SystemEvents(opt)
72+
73+
// // functional test for distribution api
74+
// tests.DistributionInspect(opt)
75+
// })
76+
77+
// gomega.RegisterFailHandler(ginkgo.Fail)
78+
// ginkgo.RunSpecs(t, description)
79+
// }
80+
1881
func TestRun(t *testing.T) {
19-
if os.Getenv("TEST_E2E") != "1" {
20-
t.Skip("E2E tests skipped. Set TEST_E2E=1 to run these tests")
82+
if os.Getenv("OPA_E2E") == "1" {
83+
runOPATests(t)
84+
} else if os.Getenv("TEST_E2E") == "1" {
85+
runE2ETests(t)
86+
} else {
87+
t.Skip("E2E tests skipped. Set TEST_E2E=1 to run regular E2E tests or OPA_E2E=1 to run OPA tests")
2188
}
22-
// TODO : Make this configurable
89+
}
90+
91+
func runOPATests(t *testing.T) {
92+
runtimeExe := "nerdctl"
93+
opt, _ := option.New([]string{runtimeExe, "-n", "finch"})
94+
95+
ginkgo.SynchronizedBeforeSuite(func() []byte {
96+
tests.SetupLocalRegistry(opt)
97+
return nil
98+
}, func(bytes []byte) {})
99+
100+
ginkgo.SynchronizedAfterSuite(func() {
101+
tests.CleanupLocalRegistry(opt)
102+
// clean up everything after the local registry is cleaned up
103+
command.RemoveAll(opt)
104+
}, func() {})
105+
106+
const description = "Finch Daemon OPA E2E Tests"
107+
ginkgo.Describe(description, func() {
108+
tests.OpaMiddlewareTest(opt)
109+
fmt.Print(opt)
110+
})
111+
112+
gomega.RegisterFailHandler(ginkgo.Fail)
113+
ginkgo.RunSpecs(t, description)
114+
}
115+
116+
func runE2ETests(t *testing.T) {
23117
runtimeExe := "nerdctl"
24118
opt, _ := option.New([]string{runtimeExe, "-n", "finch"})
25119

e2e/tests/opa_middleware.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package tests
5+
6+
import (
7+
"bytes"
8+
"encoding/json"
9+
"fmt"
10+
"net/http"
11+
12+
. "github.com/onsi/ginkgo/v2"
13+
. "github.com/onsi/gomega"
14+
"github.com/runfinch/common-tests/command"
15+
"github.com/runfinch/common-tests/option"
16+
17+
"github.com/runfinch/finch-daemon/api/types"
18+
"github.com/runfinch/finch-daemon/e2e/client"
19+
)
20+
21+
// OpaMiddlewareTest tests the OPA functionality.
22+
func OpaMiddlewareTest(opt *option.Option) {
23+
Describe("test opa middleware functionality", func() {
24+
var (
25+
uClient *http.Client
26+
version string
27+
wantContainerName string
28+
options types.ContainerCreateRequest
29+
createUrl string
30+
)
31+
BeforeEach(func() {
32+
// create a custom client to use http over unix sockets
33+
uClient = client.NewClient(GetDockerHostUrl())
34+
// get the docker api version that will be tested
35+
version = GetDockerApiVersion()
36+
wantContainerName = fmt.Sprintf("/%s", testContainerName)
37+
// set default container options
38+
options = types.ContainerCreateRequest{}
39+
options.Image = defaultImage
40+
createUrl = client.ConvertToFinchUrl(version, "/containers/create")
41+
})
42+
AfterEach(func() {
43+
command.RemoveAll(opt)
44+
})
45+
It("should allow GET version API request", func() {
46+
res, err := uClient.Get(client.ConvertToFinchUrl("", "/version"))
47+
Expect(err).ShouldNot(HaveOccurred())
48+
jd := json.NewDecoder(res.Body)
49+
var v types.VersionInfo
50+
err = jd.Decode(&v)
51+
Expect(err).ShouldNot(HaveOccurred())
52+
Expect(v.Version).ShouldNot(BeNil())
53+
Expect(v.ApiVersion).Should(Equal("1.43"))
54+
fmt.Println(version)
55+
})
56+
57+
It("shold allow GET containers API request", func() {
58+
id := command.StdoutStr(opt, "run", "-d", "--name", testContainerName, defaultImage, "sleep", "infinity")
59+
want := []types.ContainerListItem{
60+
{
61+
Id: id[:12],
62+
Names: []string{wantContainerName},
63+
},
64+
}
65+
66+
res, err := uClient.Get(client.ConvertToFinchUrl(version, "/containers/json"))
67+
Expect(err).Should(BeNil())
68+
Expect(res.StatusCode).Should(Equal(http.StatusOK))
69+
var got []types.ContainerListItem
70+
err = json.NewDecoder(res.Body).Decode(&got)
71+
Expect(err).Should(BeNil())
72+
Expect(len(got)).Should(Equal(2))
73+
got = filterContainerList(got)
74+
Expect(got).Should(ContainElements(want))
75+
})
76+
77+
It("shold disallow POST containers/create API request", func() {
78+
79+
options.Cmd = []string{"echo", "hello world"}
80+
81+
reqBody, err := json.Marshal(options)
82+
Expect(err).Should(BeNil())
83+
84+
fmt.Println("createUrl = ", createUrl)
85+
res, _ := uClient.Post(createUrl, "application/json", bytes.NewReader(reqBody))
86+
87+
Expect(res.StatusCode).Should(Equal(http.StatusForbidden))
88+
})
89+
90+
})
91+
}

0 commit comments

Comments
 (0)