Skip to content

Commit

Permalink
Update tests for exec
Browse files Browse the repository at this point in the history
  • Loading branch information
eveld committed Oct 30, 2023
1 parent 33fc133 commit 29aca54
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/config/resources/exec/resource.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package exec

import (
"fmt"
"strings"

"github.com/jumppad-labs/hclconfig/types"
Expand Down Expand Up @@ -46,6 +47,10 @@ func (e *Exec) Process() error {

// make sure line endings are linux
e.Script = strings.Replace(e.Script, "\r\n", "\n", -1)
} else {
if len(e.Networks) > 0 || len(e.Volumes) > 0 {
return fmt.Errorf("unable to create local exec with networks or volumes")
}
}

// do we have an existing resource in the state?
Expand Down
58 changes: 58 additions & 0 deletions pkg/config/resources/exec/resource_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package exec

import (
"os"
"testing"

"github.com/jumppad-labs/hclconfig/types"
"github.com/jumppad-labs/jumppad/pkg/config"
ctypes "github.com/jumppad-labs/jumppad/pkg/config/resources/container"
"github.com/jumppad-labs/jumppad/testutils"
"github.com/stretchr/testify/require"
)

func init() {
config.RegisterResource(TypeExec, &Exec{}, &Provider{})
}

func TestExecSetsOutputsFromState(t *testing.T) {
testutils.SetupState(t, `
{
Expand All @@ -33,3 +40,54 @@ func TestExecSetsOutputsFromState(t *testing.T) {

require.Equal(t, 42, c.PID)
}

func TestExecProcessSetsAbsolute(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)

c := &Exec{
ResourceMetadata: types.ResourceMetadata{File: "./"},
Image: &ctypes.Image{
Name: "test",
},
Volumes: []ctypes.Volume{
{
Source: "./",
Destination: "./",
},
},
}

c.Process()

require.Equal(t, wd, c.Volumes[0].Source)
}

func TestExecLocalWithVolumesReturnsError(t *testing.T) {
c := &Exec{
ResourceMetadata: types.ResourceMetadata{File: "./"},
Volumes: []ctypes.Volume{
{
Source: "./",
Destination: "./",
},
},
}

err := c.Process()
require.Error(t, err)
}

func TestExecLocalWithNetworksReturnsError(t *testing.T) {
c := &Exec{
ResourceMetadata: types.ResourceMetadata{File: "./"},
Networks: []ctypes.NetworkAttachment{
{
Name: "test",
},
},
}

err := c.Process()
require.Error(t, err)
}

0 comments on commit 29aca54

Please sign in to comment.