forked from gomods/athens
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
\gomods#1500 - prototype for using testcontainers-go for dependencies
- Loading branch information
1 parent
4530a58
commit 2891354
Showing
9 changed files
with
424 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
// +build e2etests | ||
|
||
package e2etests | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/gobuffalo/envy" | ||
"github.com/stretchr/testify/suite" | ||
"github.com/testcontainers/testcontainers-go" | ||
) | ||
|
||
type E2eSuite struct { | ||
suite.Suite | ||
goBinaryPath string | ||
env []string | ||
tmpDir string | ||
sampleRepoPath string | ||
stopAthens context.CancelFunc | ||
athensContainer testcontainers.Container | ||
} | ||
|
||
type catalogRes struct { | ||
Modules []struct { | ||
Module string `json:"module"` | ||
Version string `json:"version"` | ||
} `json:"modules"` | ||
} | ||
|
||
func (m *E2eSuite) SetupSuite() { | ||
var err error | ||
m.tmpDir, err = ioutil.TempDir("/tmp", "gopath") | ||
if err != nil { | ||
m.Fail("Failed to make temp dir", err) | ||
} | ||
|
||
m.sampleRepoPath, err = ioutil.TempDir("/tmp", "repopath") | ||
if err != nil { | ||
m.Fail("Failed to make temp dir for sample repo", err) | ||
} | ||
|
||
m.goBinaryPath = envy.Get("GO_BINARY_PATH", "go") | ||
|
||
athensBin, err := buildAthens(m.goBinaryPath, m.tmpDir, m.env) | ||
if err != nil { | ||
m.Fail("Failed to build athens ", err) | ||
} | ||
// ignoring error as if no athens is running it fails. | ||
|
||
ctx := context.Background() | ||
ctx, m.stopAthens = context.WithCancel(ctx) | ||
|
||
if m.athensContainer != nil { | ||
err = stopAthens(ctx, m.athensContainer) | ||
if err != nil { | ||
m.Fail(err.Error()) | ||
} | ||
} | ||
|
||
c, err := runAthensAndWait(ctx, athensBin, m.getEnv()) | ||
if err != nil { | ||
m.Fail(err.Error()) | ||
} | ||
|
||
m.athensContainer = c | ||
|
||
setupTestRepo(m.sampleRepoPath, "https://github.com/athens-artifacts/happy-path.git") | ||
} | ||
|
||
func (m *E2eSuite) TearDownSuite() { | ||
if m.athensContainer == nil { | ||
err := stopAthens(context.Background(), m.athensContainer) | ||
if err != nil { | ||
m.Fail(err.Error()) | ||
} | ||
} | ||
m.stopAthens() | ||
} | ||
|
||
func TestE2E(t *testing.T) { | ||
suite.Run(t, &E2eSuite{}) | ||
} | ||
|
||
func (m *E2eSuite) SetupTest() { | ||
chmodR(m.tmpDir, 0777) | ||
err := cleanGoCache(m.getEnv()) | ||
if err != nil { | ||
m.Fail("Failed to clear go cache", err) | ||
} | ||
} | ||
|
||
func (m *E2eSuite) TestNoGoProxy() { | ||
cmd := exec.Command("go", "run", ".") | ||
cmd.Env = m.env | ||
cmd.Dir = m.sampleRepoPath | ||
|
||
err := cmd.Run() | ||
if err != nil { | ||
m.Fail("go run failed on test repo", err) | ||
} | ||
} | ||
|
||
func (m *E2eSuite) TestGoProxy() { | ||
|
||
// TESTCONTAINERS - sub for docker container | ||
ep, err := m.athensContainer.Endpoint(context.Background(), "http") | ||
if err != nil { | ||
m.Fail(err.Error()) | ||
} | ||
|
||
cmd := exec.Command("go", "run", ".") | ||
cmd.Env = m.getEnvGoProxy(ep) | ||
cmd.Dir = m.sampleRepoPath | ||
err = cmd.Run() | ||
|
||
if err != nil { | ||
m.Fail("go run failed on test repo", err) | ||
} | ||
|
||
resp, err := http.Get(fmt.Sprintf("%s/catalog", ep)) | ||
if err != nil { | ||
m.Fail("failed to read catalog", err) | ||
} | ||
|
||
var catalog catalogRes | ||
err = json.NewDecoder(resp.Body).Decode(&catalog) | ||
if err != nil { | ||
m.Fail("failed to decode catalog res", err) | ||
} | ||
m.Assert().Equal(len(catalog.Modules), 1) | ||
m.Assert().Equal(catalog.Modules[0].Module, "github.com/athens-artifacts/no-tags") | ||
} | ||
|
||
func (m *E2eSuite) TestWrongGoProxy() { | ||
cmd := exec.Command("go", "run", ".") | ||
cmd.Env = m.getEnvWrongGoProxy(m.tmpDir) | ||
cmd.Dir = m.sampleRepoPath | ||
err := cmd.Run() | ||
m.Assert().NotNil(err, "Wrong proxy should fail") | ||
} | ||
|
||
func (m *E2eSuite) getEnv() []string { | ||
res := []string{ | ||
fmt.Sprintf("GOPATH=%s", m.tmpDir), | ||
"GO111MODULE=on", | ||
fmt.Sprintf("PATH=%s", os.Getenv("PATH")), | ||
fmt.Sprintf("GOCACHE=%s", filepath.Join(m.tmpDir, "cache")), | ||
} | ||
return res | ||
} | ||
|
||
func (m *E2eSuite) getEnvGoProxy(goproxyOverride string) []string { | ||
goProxy := "http://localhost:3000" | ||
if goproxyOverride != "" { | ||
goProxy = goproxyOverride | ||
} | ||
|
||
res := m.getEnv() | ||
res = append(res, fmt.Sprintf("GOPROXY=%s", goProxy)) | ||
return res | ||
} | ||
|
||
func (m *E2eSuite) getEnvWrongGoProxy(gopath string) []string { | ||
res := m.getEnv() | ||
res = append(res, "GOPROXY=http://localhost:3001") | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// +build e2etests | ||
|
||
package e2etests | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os/exec" | ||
"path" | ||
"path/filepath" | ||
"time" | ||
|
||
testcontainers "github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/wait" | ||
) | ||
|
||
func buildAthens(goBin, destPath string, env []string) (string, error) { | ||
target := path.Join(destPath, "athens-proxy") | ||
binFolder, err := filepath.Abs("../cmd/proxy") | ||
if err != nil { | ||
return "", fmt.Errorf("Failed to get athens source path %v", err) | ||
} | ||
|
||
cmd := exec.Command(goBin, "build", "-o", target, binFolder) | ||
cmd.Env = env | ||
output, err := cmd.CombinedOutput() | ||
if err != nil { | ||
return "", fmt.Errorf("Failed to build athens: %v - %s", err, string(output)) | ||
} | ||
return target, nil | ||
} | ||
|
||
func stopAthens(ctx context.Context, athensContainer testcontainers.Container) error { | ||
err := athensContainer.Terminate(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func runAthensAndWait(ctx context.Context, athensBin string, env []string) (testcontainers.Container, error) { | ||
req := testcontainers.ContainerRequest{ | ||
FromDockerfile: testcontainers.FromDockerfile{ | ||
Dockerfile: "cmd/proxy/Dockerfile", | ||
Context: "./..", | ||
}, | ||
ExposedPorts: []string{"3000/tcp"}, | ||
WaitingFor: wait.ForLog("Starting application at port :3000").WithStartupTimeout(time.Minute * 2), | ||
Cmd: []string{"athens-proxy", "-config_file=/config/config.toml"}, | ||
AutoRemove: true, | ||
} | ||
|
||
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ | ||
ContainerRequest: req, | ||
Started: true, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to run athens: %s", err) | ||
} | ||
|
||
return c, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.