-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add unit tests for iac/plm and iac/lcs
- Loading branch information
1 parent
e43ec2d
commit c33e4df
Showing
4 changed files
with
376 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package main_test | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/pulumi/pulumi/sdk/v3/go/auto" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLocalProject(t *testing.T) { | ||
t.Parallel() | ||
ctx := context.Background() | ||
|
||
t.Run("Test_new_stack_local_source_secrets", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
stackName := "testLocalSourceSecrets" | ||
workDir := filepath.Join(".", "localproject") | ||
s, err := auto.NewStackLocalSource(ctx, stackName, workDir) | ||
require.NoError(t, err) | ||
require.NotNil(t, s) | ||
|
||
defer func() { | ||
err := os.Unsetenv("FOO") | ||
require.NoError(t, err) | ||
|
||
err = s.Workspace().RemoveStack(ctx, s.Name()) | ||
require.NoError(t, err) | ||
}() | ||
|
||
f := os.Getenv("FOO") | ||
b := os.Getenv("BAZ") | ||
s.Workspace().SetEnvVars(map[string]string{ | ||
"FOO": f, | ||
"BAZ": b, | ||
}) | ||
|
||
envvars := s.Workspace().GetEnvVars() | ||
require.Equal(t, f, envvars["FOO"]) | ||
require.Equal(t, b, envvars["BAZ"]) | ||
|
||
b = os.Getenv("BAR_TOKEN") | ||
err = s.SetAllConfig(ctx, auto.ConfigMap{ | ||
"bar:token": auto.ConfigValue{ | ||
Value: b, | ||
Secret: true, | ||
}, | ||
"buzz:owner": auto.ConfigValue{ | ||
Value: "xyz", | ||
Secret: true, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
values, err := s.GetAllConfig(ctx) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, b, values["bar:token"].Value) | ||
require.True(t, values["bar:token"].Secret) | ||
|
||
require.Equal(t, "xyz", values["buzz:owner"].Value) | ||
require.True(t, values["buzz:owner"].Secret) | ||
}) | ||
t.Run("Test_new_stack_local_source_config", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
stackName := "testLocalSourceConfig" | ||
workDir := filepath.Join(".", "localproject") | ||
s, err := auto.NewStackLocalSource(ctx, stackName, workDir) | ||
require.NoError(t, err) | ||
require.NotNil(t, s) | ||
|
||
defer func() { | ||
err = s.Workspace().RemoveStack(ctx, s.Name()) | ||
require.NoError(t, err) | ||
}() | ||
|
||
require.Equal(t, stackName, s.Name()) | ||
|
||
err = s.SetAllConfig(ctx, auto.ConfigMap{ | ||
"bar:token": auto.ConfigValue{ | ||
Value: "abc", | ||
Secret: true, | ||
}, | ||
"buzz:owner": auto.ConfigValue{ | ||
Value: "xyz", | ||
Secret: true, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
values, err := s.GetAllConfig(ctx) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, "abc", values["bar:token"].Value) | ||
require.True(t, values["bar:token"].Secret) | ||
|
||
require.Equal(t, "xyz", values["buzz:owner"].Value) | ||
require.True(t, values["buzz:owner"].Secret) | ||
}) | ||
t.Run("Test_local_source_workspace_env_vars", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
stackName := "testLocalSourceWorkspaceEnvVars" | ||
workDir := filepath.Join(".", "localproject") | ||
s, err := auto.NewStackLocalSource(ctx, stackName, workDir) | ||
require.NoError(t, err) | ||
require.NotNil(t, s) | ||
|
||
defer func() { | ||
err = s.Workspace().RemoveStack(ctx, s.Name()) | ||
require.NoError(t, err) | ||
}() | ||
|
||
require.Equal(t, stackName, s.Name()) | ||
|
||
err = s.Workspace().SetEnvVars(map[string]string{ | ||
"FOO": "BAR", | ||
"BAZ": "QUX", | ||
}) | ||
require.NoError(t, err) | ||
|
||
envvars := s.Workspace().GetEnvVars() | ||
require.Equal(t, "BAR", envvars["FOO"]) | ||
require.Equal(t, "QUX", envvars["BAZ"]) | ||
|
||
s.Workspace().UnsetEnvVar("FOO") | ||
s.Workspace().UnsetEnvVar("BAZ") | ||
|
||
envvars = s.Workspace().GetEnvVars() | ||
require.NotContains(t, envvars, "FOO") | ||
require.NotContains(t, envvars, "BAZ") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package main_test | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"os" | ||
"path/filepath" | ||
"plm/util" | ||
"testing" | ||
|
||
"dagger.io/dagger" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var c *dagger.Client | ||
var id dagger.ContainerID | ||
|
||
func TestMain(m *testing.M) { | ||
flag.Parse() | ||
|
||
ctx := context.Background() | ||
|
||
c, _ = dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr)) | ||
defer c.Close() | ||
|
||
c = c.Pipeline("pulumi_local_test") | ||
|
||
id, _ = c. | ||
Container(). | ||
From("busybox:uclibc"). | ||
WithMountedTemp("/mountedtmp"). | ||
ID(ctx) | ||
|
||
code := m.Run() | ||
defer c.Close() | ||
os.Exit(code) | ||
} | ||
|
||
func TestPulumiLocal(t *testing.T) { | ||
t.Parallel() | ||
ctx := context.Background() | ||
|
||
t.Run("Test_pulumi_local", func(t *testing.T) { | ||
t.Parallel() | ||
c = c.Pipeline("test_pulumi_local") | ||
require.NotNil(t, c) | ||
|
||
dir, err := os.Getwd() | ||
p := filepath.Join(dir, "..", "lcs") | ||
require.NoError(t, err) | ||
require.NotEmpty(t, p) | ||
|
||
container := c. | ||
Container(). | ||
From("golang:alpine") | ||
require.NotNil(t, container) | ||
|
||
id, err := container. | ||
WithMountedDirectory("/mountedtmp", c.Host().Directory(p)). | ||
ID(ctx) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, id) | ||
|
||
id, err = util.PulumiInstall(c, id). | ||
WithWorkdir("/mountedtmp"). | ||
WithEnvVariable("PULUMI_SKIP_UPDATE_CHECK", "true"). | ||
WithEnvVariable("PULUMI_CONFIG_PASSPHRASE", ""). | ||
WithExec([]string{"pulumi", "login", "--local"}). | ||
ID(ctx) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, id) | ||
|
||
reMatching := "TestLocalProject" | ||
_, err = c.Container(dagger.ContainerOpts{ID: id}). | ||
Pipeline("pulumi_localproject_test"). | ||
WithExec([]string{"go", "test", "-v", "-run", reMatching}). | ||
Stdout(ctx) | ||
require.NoError(t, err) | ||
}) | ||
t.Run("Test_mounted_host_parent_directory", func(t *testing.T) { | ||
t.Parallel() | ||
c = c.Pipeline("test_mounted_host_parent_directory") | ||
require.NotNil(t, c) | ||
|
||
dir, err := os.Getwd() | ||
p := filepath.Join(dir, "..") | ||
require.NoError(t, err) | ||
require.NotEmpty(t, p) | ||
|
||
container := c. | ||
Container(). | ||
From("busybox:uclibc") | ||
require.NotNil(t, container) | ||
|
||
id, err := container. | ||
WithMountedDirectory("/mountedtmp", c.Host().Directory(p)). | ||
ID(ctx) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, id) | ||
|
||
out, err := c. | ||
Container(dagger.ContainerOpts{ID: id}). | ||
WithExec([]string{"ls", "/mountedtmp"}). | ||
Stdout(ctx) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, out) | ||
}) | ||
t.Run("Test_pulumi_install", func(t *testing.T) { | ||
t.Parallel() | ||
c = c.Pipeline("test_pulumi_install") | ||
require.NotNil(t, c) | ||
|
||
container := util.PulumiInstall(c, id) | ||
require.NotNil(t, container) | ||
out, err := container. | ||
WithExec([]string{"ls", "/usr/bin/"}). | ||
Stdout(ctx) | ||
require.NoError(t, err) | ||
require.Contains(t, out, "pulumi") | ||
require.Contains(t, out, "pulumi-language-go") | ||
}) | ||
t.Run("Test_container_id", func(t *testing.T) { | ||
t.Parallel() | ||
c = c.Pipeline("test_container_id") | ||
require.NotNil(t, c) | ||
|
||
out, err := c. | ||
Container(dagger.ContainerOpts{ID: id}). | ||
WithExec([]string{"busybox"}). | ||
Stdout(ctx) | ||
require.NoError(t, err) | ||
require.Contains(t, out, "BusyBox") | ||
}) | ||
t.Run("Test_error_message", func(t *testing.T) { | ||
t.Parallel() | ||
c = c.Pipeline("test_error_message") | ||
require.NotNil(t, c) | ||
|
||
_, err := c. | ||
Container(). | ||
From("fake.invalid"). | ||
ID(ctx) | ||
require.Error(t, err) | ||
require.ErrorContains(t, err, "not exist") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package util_test | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"os" | ||
"plm/util" | ||
"testing" | ||
|
||
"dagger.io/dagger" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var c *dagger.Client | ||
var id dagger.ContainerID | ||
|
||
func TestMain(m *testing.M) { | ||
flag.Parse() | ||
|
||
ctx := context.Background() | ||
|
||
c, _ = dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr)) | ||
defer c.Close() | ||
|
||
c = c.Pipeline("pulumi_util_test") | ||
|
||
id, _ = c. | ||
Container(). | ||
From("busybox:uclibc"). | ||
ID(ctx) | ||
|
||
code := m.Run() | ||
defer c.Close() | ||
os.Exit(code) | ||
} | ||
|
||
func TestPulumi(t *testing.T) { | ||
t.Parallel() | ||
ctx := context.Background() | ||
|
||
t.Run("Test_pulumi_login_local", func(t *testing.T) { | ||
t.Parallel() | ||
c = c.Pipeline("test_pulumi_login_local") | ||
require.NotNil(t, c) | ||
|
||
container := util.PulumiInstall(c, id) | ||
require.NotNil(t, container) | ||
out, err := container. | ||
WithExec([]string{"pulumi", "login", "--local"}). | ||
Stdout(ctx) | ||
require.NoError(t, err) | ||
require.Contains(t, out, "Logged in") | ||
}) | ||
t.Run("Test_pulumi_install", func(t *testing.T) { | ||
t.Parallel() | ||
c = c.Pipeline("test_pulumi_install") | ||
require.NotNil(t, c) | ||
|
||
container := util.PulumiInstall(c, id) | ||
require.NotNil(t, container) | ||
out, err := container. | ||
WithExec([]string{"ls", "/usr/bin/"}). | ||
Stdout(ctx) | ||
require.NoError(t, err) | ||
require.Contains(t, out, "pulumi") | ||
require.Contains(t, out, "pulumi-language-go") | ||
}) | ||
} |