Skip to content

Commit

Permalink
core: Add test for local imports across sessions (dagger#5471)
Browse files Browse the repository at this point in the history
We need to make sure that separate clients/sessions that happen to be
loading the same path (whether they are on the same host or not) load
independently rather than hitting the same cache just because the paths
happen to be the same.

Signed-off-by: Erik Sipsma <erik@dagger.io>
  • Loading branch information
sipsma committed Jul 17, 2023
1 parent e9d557a commit 7d2d263
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions core/integration/local_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package core

import (
"context"
"os"
"path/filepath"
"strings"
"testing"

"dagger.io/dagger"
"github.com/stretchr/testify/require"
)

func TestLocalImportsAcrossSessions(t *testing.T) {
ctx := context.Background()

tmpdir := t.TempDir()

c1, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
require.NoError(t, err)
t.Cleanup(func() { c1.Close() })

fileName := "afile"
err = os.WriteFile(filepath.Join(tmpdir, fileName), []byte("1"), 0644)
require.NoError(t, err)

hostDir1 := c1.Host().Directory(tmpdir)

out1, err := c1.Container().From("alpine:3.16.2").
WithMountedDirectory("/mnt", hostDir1).
WithExec([]string{"cat", "/mnt/" + fileName}).
Stdout(ctx)
require.NoError(t, err)
out1 = strings.TrimSpace(out1)
require.Equal(t, "1", out1)

// repeat with new session but overwrite the host file before it's loaded

err = os.WriteFile(filepath.Join(tmpdir, fileName), []byte("2"), 0644)
require.NoError(t, err)
// just do a sanity check the file contents are what we just wrote
contents, err := os.ReadFile(filepath.Join(tmpdir, fileName))
require.NoError(t, err)
require.Equal(t, "2", string(contents))

c2, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
require.NoError(t, err)
t.Cleanup(func() { c2.Close() })

hostDir2 := c2.Host().Directory(tmpdir)

out2, err := c2.Container().From("alpine:3.18").
WithMountedDirectory("/mnt", hostDir2).
WithExec([]string{"cat", "/mnt/" + fileName}).
Stdout(ctx)
require.NoError(t, err)
out2 = strings.TrimSpace(out2)
require.Equal(t, "2", out2)
}

0 comments on commit 7d2d263

Please sign in to comment.