Merge pull request #269 from dagger/cli-tests

Cli tests
This commit is contained in:
Sam Alba 2021-04-02 17:45:34 -07:00 committed by GitHub
commit 45d98d9bf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 132 additions and 12 deletions

View File

@ -41,7 +41,7 @@ var computeCmd = &cobra.Command{
for _, input := range viper.GetStringSlice("input-string") {
parts := strings.SplitN(input, "=", 2)
k, v := parts[0], parts[1]
err := st.AddInput(k, dagger.TextInput(v))
err := st.SetInput(k, dagger.TextInput(v))
if err != nil {
lg.
Fatal().
@ -54,7 +54,7 @@ var computeCmd = &cobra.Command{
for _, input := range viper.GetStringSlice("input-dir") {
parts := strings.SplitN(input, "=", 2)
k, v := parts[0], parts[1]
err := st.AddInput(k, dagger.DirInput(v, []string{}))
err := st.SetInput(k, dagger.DirInput(v, []string{}))
if err != nil {
lg.
Fatal().
@ -67,7 +67,7 @@ var computeCmd = &cobra.Command{
for _, input := range viper.GetStringSlice("input-git") {
parts := strings.SplitN(input, "=", 2)
k, v := parts[0], parts[1]
err := st.AddInput(k, dagger.GitInput(v, "", ""))
err := st.SetInput(k, dagger.GitInput(v, "", ""))
if err != nil {
lg.
Fatal().
@ -98,7 +98,7 @@ var computeCmd = &cobra.Command{
lg.Fatal().Msg("invalid json")
}
err = st.AddInput("", dagger.JSONInput(string(content)))
err = st.SetInput("", dagger.JSONInput(string(content)))
if err != nil {
lg.Fatal().Err(err).Msg("failed to add input")
}
@ -121,7 +121,7 @@ var computeCmd = &cobra.Command{
content = plaintext
}
err = st.AddInput("", dagger.YAMLInput(string(content)))
err = st.SetInput("", dagger.YAMLInput(string(content)))
if err != nil {
lg.Fatal().Err(err).Msg("failed to add input")
}

View File

@ -34,7 +34,7 @@ func updateDeploymentInput(ctx context.Context, target string, input dagger.Inpu
}
st := common.GetCurrentDeploymentState(ctx, store)
st.AddInput(target, input)
st.SetInput(target, input)
if err := store.UpdateDeployment(ctx, st, nil); err != nil {
lg.Fatal().Err(err).Str("deploymentId", st.ID).Str("deploymentName", st.Name).Msg("cannot update deployment")

View File

@ -102,6 +102,7 @@ func getPlanSource(ctx context.Context) dagger.Input {
if planDir != "" {
checkFirstSet()
src = dagger.DirInput(planDir, []string{"*.cue", "cue.mod"})
}

View File

@ -31,7 +31,7 @@ var upCmd = &cobra.Command{
state := common.GetCurrentDeploymentState(ctx, store)
// TODO: Implement options: --no-cache
common.DeploymentUp(ctx, state, false)
common.DeploymentUp(ctx, state, true)
},
}

View File

@ -40,7 +40,15 @@ type inputKV struct {
Value Input `json:"value,omitempty"`
}
func (s *DeploymentState) AddInput(key string, value Input) error {
func (s *DeploymentState) SetInput(key string, value Input) error {
for i, inp := range s.Inputs {
if inp.Key != key {
continue
}
// Remove existing inputs with the same key
s.Inputs = append(s.Inputs[:i], s.Inputs[i+1:]...)
}
s.Inputs = append(s.Inputs, inputKV{Key: key, Value: value})
return nil
}

View File

@ -3,6 +3,7 @@ package dagger
import (
"encoding/json"
"fmt"
"path/filepath"
"dagger.io/go/dagger/compiler"
)
@ -65,6 +66,12 @@ func (i Input) Compile() (*compiler.Value, error) {
// An input artifact loaded from a local directory
func DirInput(path string, include []string) Input {
// resolve absolute path
path, err := filepath.Abs(path)
if err != nil {
panic(err)
}
return Input{
Type: InputTypeDir,
Dir: &dirInput{

View File

@ -10,13 +10,13 @@ func TestInputDir(t *testing.T) {
st := &DeploymentState{
PlanSource: DirInput("/tmp/source", []string{}),
}
require.NoError(t, st.AddInput("www.source", DirInput(".", []string{})))
require.NoError(t, st.SetInput("www.source", DirInput("/", []string{})))
deployment, err := NewDeployment(st)
require.NoError(t, err)
localdirs := deployment.LocalDirs()
require.Len(t, localdirs, 2)
require.Contains(t, localdirs, ".")
require.Contains(t, localdirs, "/")
require.Contains(t, localdirs, "/tmp/source")
}

View File

@ -62,7 +62,7 @@ func TestStoreLookupByPath(t *testing.T) {
st := &DeploymentState{
Name: "test",
}
require.NoError(t, st.AddInput("foo", DirInput("/test/path", []string{})))
require.NoError(t, st.SetInput("foo", DirInput("/test/path", []string{})))
require.NoError(t, store.CreateDeployment(ctx, st))
// Lookup by path
@ -72,7 +72,7 @@ func TestStoreLookupByPath(t *testing.T) {
require.Equal(t, st.ID, r.ID)
// Add a new path
require.NoError(t, st.AddInput("bar", DirInput("/test/anotherpath", []string{})))
require.NoError(t, st.SetInput("bar", DirInput("/test/anotherpath", []string{})))
require.NoError(t, store.UpdateDeployment(ctx, st, nil))
// Lookup by the previous path

25
tests/cli/input/main.cue Normal file
View File

@ -0,0 +1,25 @@
package testing
import (
"dagger.io/llb"
"dagger.io/dagger"
)
source: dagger.#Artifact
foo: "bar"
bar: {
string
#up: [
llb.#FetchContainer & {ref: "busybox"},
llb.#Exec & {
args: ["cp", "/source/testfile", "/out"]
mount: "/source": from: source
},
llb.#Export & {
format: "string"
source: "/out"
},
]
}

1
tests/cli/input/testdata/testfile vendored Normal file
View File

@ -0,0 +1 @@
thisisatest

View File

@ -9,7 +9,10 @@ test::cli() {
test::cli::list "$dagger"
test::cli::newdir "$dagger"
test::cli::newgit "$dagger"
test::cli::query "$dagger"
test::cli::plan "$dagger"
test::cli::input "$dagger"
}
test::cli::list() {
@ -54,6 +57,27 @@ test::cli::newdir() {
"$dagger" "${DAGGER_BINARY_ARGS[@]}" query -f cue -d "simple" -c
}
test::cli::newgit() {
local dagger="$1"
# Create temporary store
local DAGGER_STORE
DAGGER_STORE="$(mktemp -d -t dagger-store-XXXXXX)"
export DAGGER_STORE
test::one "CLI: new git: --plan-git" \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" new --plan-git https://github.com/samalba/dagger-test.git simple
test::one "CLI: new git: verify plan can be upped" \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" up -d "simple"
test::one "CLI: new git: verify we have the right plan" --stdout='{
foo: "value"
bar: "another value"
}' \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" query -f cue -d "simple" -c
}
test::cli::query() {
local dagger="$1"
@ -80,3 +104,57 @@ test::cli::query() {
test::one "CLI: query: non concrete" --exit=1 \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" query -f cue -d "nonconcrete" -c
}
test::cli::plan() {
local dagger="$1"
# Create temporary store
local DAGGER_STORE
DAGGER_STORE="$(mktemp -d -t dagger-store-XXXXXX)"
export DAGGER_STORE
test::one "CLI: new" \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" new --plan-dir "$d"/cli/simple simple
test::one "CLI: plan dir" \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" -d "simple" plan dir "$d"/cli/nonconcrete
test::one "CLI: plan dir: query non-concrete" --exit=1 \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" -d "simple" query -c
test::one "CLI: plan git" \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" -d "simple" plan git https://github.com/samalba/dagger-test.git
test::one "CLI: plan git: verify we have the right plan" --stdout='{
foo: "value"
bar: "another value"
}' \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" query -f cue -d "simple" -c
}
test::cli::input() {
local dagger="$1"
# Create temporary store
local DAGGER_STORE
DAGGER_STORE="$(mktemp -d -t dagger-store-XXXXXX)"
export DAGGER_STORE
test::one "CLI: new input" \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" new --plan-dir "$d"/cli/input "input"
test::one "CLI: up: missing input" \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" up -d "input" --stdout='{"foo":"bar"}'
test::one "CLI: input dir" \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" input -d "input" dir "source" ./tests/cli/input/testdata
test::one "CLI: up: input is set with input dir" \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" up -d "input" --stdout='{"bar":"thisisatest\n","foo":"bar","source":{}}'
test::one "CLI: input git" \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" input -d "input" git "source" https://github.com/samalba/dagger-test-simple.git
test::one "CLI: up: input is set with input git" \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" up -d "input" --stdout='{"bar":"testgit\n","foo":"bar","source":{}}'
}