Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Test invokation via the Manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
tealeg committed Feb 11, 2019
1 parent 869a41d commit 8ca040d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
22 changes: 22 additions & 0 deletions manifest.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package pink

import (
"context"
"encoding/json"
"os"

"github.com/docker/docker/client"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -59,3 +61,23 @@ func validateManifest(m *Manifest) error {

return nil
}

//
func (m *Manifest) Invoke(ctx context.Context, pluginDir string, args []string) error {
var inv Invoker
switch m.Invoker {
case "executable":
inv = &ExecutableInvoker{
PluginDir: pluginDir,
}
case "docker":
client, err := client.NewEnvClient()
if err != nil {
return err
}
inv = NewDockerInvoker(client, os.Stdout, os.Stderr)
default:
return errors.Errorf("unsupported invoker '%s', only 'executable' is currently supported", m.Invoker)
}
return inv.Invoke(ctx, m, &InvokerConfig{Args: args, Env: os.Environ()})
}
48 changes: 48 additions & 0 deletions manifest_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package pink

import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -54,3 +59,46 @@ func TestValidateManifest(t *testing.T) {
require.Equal(t, test.returnsError, err != nil)
}
}

// Invoke defers to the correct invoker
func TestInvoke(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)

t.Run("executable", func(t *testing.T) {
execCommandContext = func(ctx context.Context, command string, args ...string) *exec.Cmd {
require.Equal(t, path.Join(wd, "a/b/somepath"), command)
return &exec.Cmd{Path: "/bin/echo"}
}
defer func() { execCommandContext = exec.CommandContext }()

m := Manifest{Command: []string{"a", "b"}, Invoker: "executable", Exec: "somepath"}

require.NoError(t, err)
err = m.Invoke(context.Background(), wd, nil)
require.NoError(t, err)

})

tmpDir, err := ioutil.TempDir("", "")
require.NoError(t, err)
tmpPath := filepath.Join(tmpDir, "stdout")
tmpFile, err := os.Create(tmpPath)
require.NoError(t, err)
origStdout := os.Stdout
os.Stdout = tmpFile
defer func() { os.Stdout = origStdout }()

m := Manifest{Invoker: "docker", Docker: DockerConfig{ImageURL: "alpine"}}
err = m.Invoke(context.Background(), wd, []string{"echo", "hello", "world"})
require.NoError(t, err)

tmpFile.Sync()
tmpFile.Close()

b, err := ioutil.ReadFile(tmpPath)
require.NoError(t, err)

require.Equal(t, "hello world", strings.TrimSpace(string(b)))

}

0 comments on commit 8ca040d

Please sign in to comment.