Skip to content

Commit

Permalink
more script test tweaks and additions (#211)
Browse files Browse the repository at this point in the history
* more script test fixes

* move to prevent cached test after modification

* fix comment

* add more tests

* add proxy integration tests (#212)
  • Loading branch information
jakecoffman authored Dec 22, 2023
1 parent 8cd2a4f commit c7aa6e7
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 213 deletions.
48 changes: 48 additions & 0 deletions cmd/dependabot/dependabot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"context"
"os"
"os/exec"
"path/filepath"
"rsc.io/script"
"rsc.io/script/scripttest"
"testing"
"time"
)

func TestDependabot(t *testing.T) {
err := exec.Command("go", "build", "dependabot.go").Run()
if err != nil {
t.Fatal("failed to build dependabot")
}
t.Cleanup(func() {
os.Remove("dependabot")
})

ctx := context.Background()
engine := &script.Engine{
Conds: scripttest.DefaultConds(),
Cmds: Commands(),
Quiet: !testing.Verbose(),
}
env := []string{
"PATH=" + os.Getenv("PATH"),
}
scripttest.Test(t, ctx, engine, env, "../../testdata/scripts/*.txt")
}

// Commands returns the commands that can be used in the scripts.
// Each line of the scripts are <command> <args...>
// When you use "echo" in the scripts it's actually running script.Echo
// not the echo binary on your system.
func Commands() map[string]script.Cmd {
commands := scripttest.DefaultCmds()
wd, _ := os.Getwd()
dependabot := filepath.Join(wd, "dependabot")

// additional Dependabot commands
commands["dependabot"] = script.Program(dependabot, nil, 100*time.Millisecond)

return commands
}
4 changes: 4 additions & 0 deletions cmd/dependabot/internal/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -91,6 +92,9 @@ func NewUpdateCommand() *cobra.Command {
Writer: writer,
ApiUrl: flags.apiUrl,
}); err != nil {
if errors.Is(err, context.DeadlineExceeded) {
log.Fatalf("update timed out after %s", flags.timeout)
}
log.Fatalf("failed to run updater: %v", err)
}

Expand Down
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
2 changes: 2 additions & 0 deletions testdata/scripts/basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ dependabot update go_modules dependabot/cli --updater-image dummy-updater
stderr 'bin/run arguments: fetch_files'
stderr 'bin/run arguments: update_files'

exec docker rmi -f dummy-updater

-- Dockerfile --
FROM ubuntu:22.04

Expand Down
8 changes: 8 additions & 0 deletions testdata/scripts/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ stderr '"repo":"dependabot/cli"'
dependabot update go_modules dependabot/cli --commit 1278c8d7503f9881eb969959446e2c3a5a0cce2d --updater-image input-verify-updater
stderr '"commit":"1278c8d7503f9881eb969959446e2c3a5a0cce2d"'

dependabot update go_modules dependabot/cli --branch cool-branch --updater-image input-verify-updater
stderr '"branch":"cool-branch"'

! dependabot update go_modules dependabot/cli --commit unknown --updater-image input-verify-updater
stderr 'commit must be a SHA, or not provided'

dependabot update go_modules dependabot/cli --dep golang.org/x/image --updater-image input-verify-updater
stderr '"allowed-updates":\[\{"dependency-name":"golang.org/x/image"\}\]'

dependabot update go_modules dependabot/cli --directory /code --updater-image input-verify-updater
stderr '"directory":"\/code"'

exec docker rmi -f input-verify-updater

-- Dockerfile --
FROM ubuntu:22.04

Expand Down
2 changes: 2 additions & 0 deletions testdata/scripts/local.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ dependabot update go_modules dependabot-fixtures/go-modules-lib --updater-image
stderr \.git
stderr hello.txt

exec docker rmi -f local-updater

-- Dockerfile --
FROM ubuntu:22.04

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"
24 changes: 24 additions & 0 deletions testdata/scripts/timeout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
exec docker build -t sleepy-updater .

! dependabot update go_modules dependabot/cli --timeout 1s --updater-image sleepy-updater
stderr 'update timed out after 1s'

exec docker rmi -f sleepy-updater

-- 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

echo "Updated those certificates for ya"

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

sleep 10
Loading

0 comments on commit c7aa6e7

Please sign in to comment.