Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions pkg/functions/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

const (
defaultRunHost = "127.0.0.1" // TODO allow to be altered via a runOpt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is still an outstanding TODO

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arent we allowing the possibility to alter host with this flag? Or was the TODO for something more specific?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @lkingland , but now if a user will give his own Host then we have a function to adapt that host , so indirectly this todo has been solved .btw what do you think here ?

defaultRunHost = "127.0.0.1"
defaultRunPort = "8080"
readinessEndpoint = "/health/readiness"
)
Expand All @@ -26,6 +26,24 @@ type defaultRunner struct {
err io.Writer
}

func ParseAddress(val string) (host, port string, explicitPort bool) {
if val == "" {
return defaultRunHost, defaultRunPort, false
}

if h, p, err := net.SplitHostPort(val); err == nil {
if h == "" {
h = defaultRunHost
}
if p == "" {
return h, defaultRunPort, false
}
return h, p, true
}

return val, defaultRunPort, false
}

func newDefaultRunner(client *Client, out, err io.Writer) *defaultRunner {
return &defaultRunner{
client: client,
Expand All @@ -41,17 +59,7 @@ func (r *defaultRunner) Run(ctx context.Context, f Function, address string, sta
)

// Parse address if provided, otherwise use defaults
host := defaultRunHost
port := defaultRunPort
explicitPort := address != ""

if address != "" {
var err error
host, port, err = net.SplitHostPort(address)
if err != nil {
return nil, fmt.Errorf("invalid address format '%s': %w", address, err)
}
}
host, port, explicitPort := ParseAddress(address)

port, err = choosePort(host, port, explicitPort)
if err != nil {
Expand Down Expand Up @@ -95,15 +103,7 @@ func getRunFunc(ctx context.Context, job *Job) (runFn func() error, err error) {
runFn = func() error { return runGo(ctx, job) }
case "python":
runFn = func() error { return runPython(ctx, job) }
case "springboot":
err = ErrRunnerNotImplemented{runtime}
case "node":
err = ErrRunnerNotImplemented{runtime}
case "typescript":
err = ErrRunnerNotImplemented{runtime}
case "rust":
err = ErrRunnerNotImplemented{runtime}
case "quarkus":
case "springboot", "node", "typescript", "rust", "quarkus":
err = ErrRunnerNotImplemented{runtime}
default:
err = ErrRuntimeNotRecognized{runtime}
Expand Down
90 changes: 90 additions & 0 deletions pkg/functions/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,93 @@ func TestGetRunFuncErrors(t *testing.T) {
})
}
}

func TestParseAddress(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

every case which has implicit defaulting should expect the defaultX variable value instead of hardcoded string so the test doesnt have to be re-written if the default changes

tests := []struct {
name string
input string
expectedHost string
expectedPort string
explicitPort bool
}{
{
name: "empty value",
input: "",
expectedHost: defaultRunHost,
expectedPort: defaultRunPort,
explicitPort: false,
},
{
name: "host-only hostname",
input: "localhost",
expectedHost: "localhost",
expectedPort: defaultRunPort,
explicitPort: false,
},
{
name: "host-only ipv4",
input: "127.0.0.2",
expectedHost: "127.0.0.2",
expectedPort: defaultRunPort,
explicitPort: false,
},
{
name: "host-only ipv6",
input: "::1",
expectedHost: "::1",
expectedPort: defaultRunPort,
explicitPort: false,
},
{
name: "hostname with explicit port",
input: "localhost:5000",
expectedHost: "localhost",
expectedPort: "5000",
explicitPort: true,
},
{
name: "ipv4 with explicit port",
input: "127.0.0.2:5000",
expectedHost: "127.0.0.2",
expectedPort: "5000",
explicitPort: true,
},
{
name: "ipv6 with explicit port",
input: "[::1]:5000",
expectedHost: "::1",
expectedPort: "5000",
explicitPort: true,
},
{
name: "ipv4 with empty port",
input: "127.0.0.1:",
expectedHost: "127.0.0.1",
expectedPort: defaultRunPort,
explicitPort: false,
},
{
name: "empty host with explicit port",
input: ":5000",
expectedHost: defaultRunHost,
expectedPort: "5000",
explicitPort: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h, p, explicit := ParseAddress(tt.input)

if h != tt.expectedHost {
t.Errorf("host = %v, want %v", h, tt.expectedHost)
}
if p != tt.expectedPort {
t.Errorf("port = %v, want %v", p, tt.expectedPort)
}
if explicit != tt.explicitPort {
t.Errorf("explicitPort = %v, want %v", explicit, tt.explicitPort)
}
})
}
}
Loading