Skip to content

Commit

Permalink
add proxy integration tests (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakecoffman authored Dec 22, 2023
1 parent 8c44f9d commit 28dd685
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 131 deletions.
26 changes: 19 additions & 7 deletions internal/infra/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func NewProxy(ctx context.Context, cli *client.Client, params *RunParams, nets *
}

hostCfg := &container.HostConfig{
AutoRemove: true,
ExtraHosts: []string{
"host.docker.internal:host-gateway",
},
Expand Down Expand Up @@ -169,13 +168,26 @@ func (p *Proxy) TailLogs(ctx context.Context, cli *client.Client) {
_, _ = stdcopy.StdCopy(w, w, out)
}

func (p *Proxy) Close() error {
func (p *Proxy) Close() (err error) {
defer func() {
removeErr := p.cli.ContainerRemove(context.Background(), p.containerID, types.ContainerRemoveOptions{Force: true})
if removeErr != nil {
err = fmt.Errorf("failed to remove proxy container: %w", removeErr)
}
}()

// Check the error code if the container has already exited, so we can pass it along to the caller. If the proxy
//crashes we want the CLI to error out. Unlike the Updater it should never stop on its own.
containerInfo, inspectErr := p.cli.ContainerInspect(context.Background(), p.containerID)
if inspectErr != nil {
return fmt.Errorf("failed to inspect proxy container: %w", inspectErr)
}
if containerInfo.State.ExitCode != 0 {
return fmt.Errorf("proxy container exited with non-zero exit code: %d", containerInfo.State.ExitCode)
}

timeout := 5
_ = p.cli.ContainerStop(context.Background(), p.containerID, container.StopOptions{Timeout: &timeout})

err := p.cli.ContainerRemove(context.Background(), p.containerID, types.ContainerRemoveOptions{Force: true})
if err != nil {
return fmt.Errorf("failed to remove proxy container: %w", err)
}
return nil
return err
}
117 changes: 0 additions & 117 deletions internal/infra/proxy_test.go

This file was deleted.

18 changes: 11 additions & 7 deletions internal/infra/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ import (
"syscall"
"time"

"github.com/dependabot/cli/internal/model"
"github.com/dependabot/cli/internal/server"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/archive"
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"

"github.com/dependabot/cli/internal/model"
"github.com/dependabot/cli/internal/server"
"github.com/docker/docker/api/types"
"github.com/moby/moby/api/types/registry"
"github.com/moby/moby/client"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -330,8 +329,9 @@ func generateIgnoreConditions(params *RunParams, actual *model.Scenario) error {
return nil
}

func runContainers(ctx context.Context, params RunParams) error {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
func runContainers(ctx context.Context, params RunParams) (err error) {
var cli *client.Client
cli, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return fmt.Errorf("failed to create Docker client: %w", err)
}
Expand Down Expand Up @@ -365,7 +365,11 @@ func runContainers(ctx context.Context, params RunParams) error {
if err != nil {
return err
}
defer prox.Close()
defer func() {
if proxyErr := prox.Close(); proxyErr != nil {
err = proxyErr
}
}()

// proxy logs interfere with debugging output
if !params.Debug {
Expand Down
58 changes: 58 additions & 0 deletions testdata/scripts/proxy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Tests related to running the Proxy

exec docker build -qt proxy-updater .
exec docker build -qt dummy-proxy -f Dockerfile.proxy .

dependabot update go_modules dependabot/cli --updater-image proxy-updater --proxy-image dummy-proxy
stderr 'proxy \| Proxy is running'
stderr 'updater \| Updater is running'
! stderr 'proxy \| custom-ca-cert\.crt'

dependabot update go_modules dependabot/cli --proxy-cert my-cert --updater-image proxy-updater --proxy-image dummy-proxy
stderr 'proxy \| custom-ca-cert\.crt'
stderr 'proxy \| I am a certificate'

# Test that the CLI exits with non-zero if the proxy does too.
! dependabot update go_modules dependabot/cli --proxy-cert crash --updater-image proxy-updater --proxy-image dummy-proxy --proxy-username user --proxy-password pass

exec docker rmi -f proxy-updater dummy-proxy

-- crash --
crash

-- my-cert --
I am a certificate

-- Dockerfile.proxy --
FROM ubuntu:22.04

COPY --chmod=755 update-ca-certificates /usr/bin/update-ca-certificates
COPY --chmod=755 update-job-proxy /update-job-proxy

-- update-job-proxy --
#!/usr/bin/env bash

echo "Proxy is running"
echo "$(</config.json)"

-- Dockerfile --
FROM ubuntu:22.04

RUN useradd dependabot

COPY --chown=dependabot --chmod=755 update-ca-certificates /usr/bin/update-ca-certificates
COPY --chown=dependabot --chmod=755 run bin/run

-- update-ca-certificates --
#!/usr/bin/env bash

ls /usr/local/share/ca-certificates || true
cat /usr/local/share/ca-certificates/custom-ca-cert.crt || true

# signal to cause the proxy to exit with a non-zero code
grep crash /usr/local/share/ca-certificates/custom-ca-cert.crt && exit 1 || true

-- run --
#!/usr/bin/env bash

echo "Updater is running"

0 comments on commit 28dd685

Please sign in to comment.