Skip to content

Commit

Permalink
test: Add unit tests for iac/plm and iac/lcs
Browse files Browse the repository at this point in the history
  • Loading branch information
softwaredevelop committed Mar 20, 2024
1 parent e43ec2d commit c33e4df
Show file tree
Hide file tree
Showing 4 changed files with 376 additions and 2 deletions.
27 changes: 25 additions & 2 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
- main
paths:
- "cc/**/*_test.go"
- "iac/**/*_test.go"

concurrency:
group: ${{ github.ref }}-${{ github.workflow }}
Expand Down Expand Up @@ -40,10 +41,32 @@ jobs:
with:
go-version: stable

- name: Download dependencies
- name: Download dependencies for cc
run: go mod download
working-directory: cc

- name: Run unit tests on code quality functions
- name: Run unit tests on code quality functions in cc
run: go test -v ./...
working-directory: cc

unit_tests_in_iac:
runs-on: ubuntu-latest
if: contains(github.event.pull_request.head.repo.full_name, 'iac/**/*_test.go')
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Go latest
uses: actions/setup-go@v5
with:
go-version: stable

- name: Download dependencies for iac/plm
run: go mod download
working-directory: iac/plm

- name: Run unit tests on code quality functions in iac/plm
run: go test -v .
working-directory: iac/plm
137 changes: 137 additions & 0 deletions iac/lcs/local_test.go
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")
})
}
146 changes: 146 additions & 0 deletions iac/plm/main_test.go
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")
})
}
68 changes: 68 additions & 0 deletions iac/plm/util/pulumi_test.go
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")
})
}

0 comments on commit c33e4df

Please sign in to comment.