Skip to content

Commit 33620e1

Browse files
committed
Build image only once in e2e tests
1 parent f7eff59 commit 33620e1

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

e2e/e2e_test.go

+42-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"os"
99
"os/exec"
10+
"sync"
1011
"testing"
1112
"time"
1213

@@ -16,16 +17,48 @@ import (
1617
"github.com/stretchr/testify/require"
1718
)
1819

19-
func TestE2E(t *testing.T) {
20-
e2eServerToken := os.Getenv("GITHUB_MCP_SERVER_E2E_TOKEN")
21-
if e2eServerToken == "" {
22-
t.Fatalf("GITHUB_MCP_SERVER_E2E_TOKEN environment variable is not set")
23-
}
20+
var (
21+
// Shared variables and sync.Once instances to ensure one-time execution
22+
getTokenOnce sync.Once
23+
e2eToken string
2424

25-
// Build the Docker image for the MCP server.
26-
buildDockerImage(t)
25+
buildOnce sync.Once
26+
buildError error
27+
)
2728

28-
t.Setenv("GITHUB_PERSONAL_ACCESS_TOKEN", e2eServerToken) // The MCP Client merges the existing environment.
29+
// getE2EToken ensures the environment variable is checked only once and returns the token
30+
func getE2EToken(t *testing.T) string {
31+
getTokenOnce.Do(func() {
32+
e2eToken = os.Getenv("GITHUB_MCP_SERVER_E2E_TOKEN")
33+
if e2eToken == "" {
34+
t.Fatalf("GITHUB_MCP_SERVER_E2E_TOKEN environment variable is not set")
35+
}
36+
})
37+
return e2eToken
38+
}
39+
40+
// ensureDockerImageBuilt makes sure the Docker image is built only once across all tests
41+
func ensureDockerImageBuilt(t *testing.T) {
42+
buildOnce.Do(func() {
43+
t.Log("Building Docker image for e2e tests...")
44+
cmd := exec.Command("docker", "build", "-t", "github/e2e-github-mcp-server", ".")
45+
cmd.Dir = ".." // Run this in the context of the root, where the Dockerfile is located.
46+
output, err := cmd.CombinedOutput()
47+
buildError = err
48+
if err != nil {
49+
t.Logf("Docker build output: %s", string(output))
50+
}
51+
})
52+
53+
// Check if the build was successful
54+
require.NoError(t, buildError, "expected to build Docker image successfully")
55+
}
56+
57+
func TestE2E(t *testing.T) {
58+
token := getE2EToken(t)
59+
ensureDockerImageBuilt(t)
60+
61+
t.Setenv("GITHUB_PERSONAL_ACCESS_TOKEN", token) // The MCP Client merges the existing environment.
2962
args := []string{
3063
"docker",
3164
"run",
@@ -81,20 +114,11 @@ func TestE2E(t *testing.T) {
81114

82115
// Then the login in the response should match the login obtained via the same
83116
// token using the GitHub API.
84-
client := github.NewClient(nil).WithAuthToken(e2eServerToken)
117+
client := github.NewClient(nil).WithAuthToken(token)
85118
user, _, err := client.Users.Get(context.Background(), "")
86119
require.NoError(t, err, "expected to get user successfully")
87120
require.Equal(t, trimmedContent.Login, *user.Login, "expected login to match")
88121
})
89122

90123
require.NoError(t, client.Close(), "expected to close client successfully")
91124
}
92-
93-
func buildDockerImage(t *testing.T) {
94-
t.Log("Building Docker image for e2e tests...")
95-
96-
cmd := exec.Command("docker", "build", "-t", "github/e2e-github-mcp-server", ".")
97-
cmd.Dir = ".." // Run this in the context of the root, where the Dockerfile is located.
98-
output, err := cmd.CombinedOutput()
99-
require.NoError(t, err, "expected to build Docker image successfully, output: %s", string(output))
100-
}

0 commit comments

Comments
 (0)