Files
coder/provisionersdk/agent_test.go
Kyle Carberry a06821c103 feat: Update Coder Terraform Provider to v0.2.1 (#563)
This update exposes the workspace name and owner, and changes
authentication methods to be explicit. Implicit authentication
added unnecessary complexity and introduced inconsistency.
2022-03-25 16:34:45 +00:00

57 lines
1.5 KiB
Go

//go:build !windows
// +build !windows
// There isn't a portable Windows binary equivalent to "echo".
// This can be tested, but it's a bit harder.
package provisionersdk_test
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/exec"
"runtime"
"strings"
"testing"
"github.com/go-chi/render"
"github.com/stretchr/testify/require"
"github.com/coder/coder/provisionersdk"
)
func TestAgentScript(t *testing.T) {
t.Parallel()
t.Run("Run", func(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
echoPath, err := exec.LookPath("echo")
require.NoError(t, err)
content, err := os.ReadFile(echoPath)
require.NoError(t, err)
render.Status(r, http.StatusOK)
render.Data(rw, r, content)
}))
t.Cleanup(srv.Close)
srvURL, err := url.Parse(srv.URL)
require.NoError(t, err)
script, exists := provisionersdk.AgentScriptEnv()[fmt.Sprintf("CODER_AGENT_SCRIPT_%s_%s", runtime.GOOS, runtime.GOARCH)]
if !exists {
t.Skip("Agent not supported...")
return
}
script = strings.ReplaceAll(script, "${ACCESS_URL}", srvURL.String()+"/")
script = strings.ReplaceAll(script, "${AUTH_TYPE}", "token")
output, err := exec.Command("sh", "-c", script).CombinedOutput()
t.Log(string(output))
require.NoError(t, err)
// Because we use the "echo" binary, we should expect the arguments provided
// as the response to executing our script.
require.Equal(t, "workspaces agent", strings.TrimSpace(string(output)))
})
}