|
| 1 | +package container |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/localstack/lstk/internal/config" |
| 10 | + "github.com/localstack/lstk/internal/runtime" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "go.uber.org/mock/gomock" |
| 14 | +) |
| 15 | + |
| 16 | +func TestAnyRunning_ReturnsTrueWhenConfiguredContainerIsRunning(t *testing.T) { |
| 17 | + initTestConfig(t, ` |
| 18 | +[[containers]] |
| 19 | +type = "aws" |
| 20 | +tag = "latest" |
| 21 | +port = "4566" |
| 22 | +`) |
| 23 | + |
| 24 | + ctrl := gomock.NewController(t) |
| 25 | + mockRT := runtime.NewMockRuntime(ctrl) |
| 26 | + mockRT.EXPECT().IsRunning(gomock.Any(), "localstack-aws").Return(true, nil) |
| 27 | + |
| 28 | + running, err := AnyRunning(context.Background(), mockRT) |
| 29 | + |
| 30 | + require.NoError(t, err) |
| 31 | + assert.True(t, running) |
| 32 | +} |
| 33 | + |
| 34 | +func TestAnyRunning_ReturnsFalseWhenConfiguredContainerIsNotRunning(t *testing.T) { |
| 35 | + initTestConfig(t, ` |
| 36 | +[[containers]] |
| 37 | +type = "aws" |
| 38 | +tag = "latest" |
| 39 | +port = "4566" |
| 40 | +`) |
| 41 | + |
| 42 | + ctrl := gomock.NewController(t) |
| 43 | + mockRT := runtime.NewMockRuntime(ctrl) |
| 44 | + mockRT.EXPECT().IsRunning(gomock.Any(), "localstack-aws").Return(false, nil) |
| 45 | + |
| 46 | + running, err := AnyRunning(context.Background(), mockRT) |
| 47 | + |
| 48 | + require.NoError(t, err) |
| 49 | + assert.False(t, running) |
| 50 | +} |
| 51 | + |
| 52 | +func initTestConfig(t *testing.T, content string) { |
| 53 | + t.Helper() |
| 54 | + |
| 55 | + dir := t.TempDir() |
| 56 | + path := filepath.Join(dir, "config.toml") |
| 57 | + err := os.WriteFile(path, []byte(content), 0o600) |
| 58 | + require.NoError(t, err) |
| 59 | + require.NoError(t, config.InitFromPath(path)) |
| 60 | + t.Cleanup(func() { |
| 61 | + require.NoError(t, config.InitFromPath(path)) |
| 62 | + }) |
| 63 | +} |
0 commit comments