-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from gubernator-io/thrawn/optional-config-file
Fixed regression, gubernator should start without a config file
- Loading branch information
Showing
3 changed files
with
160 additions
and
28 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
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,117 @@ | ||
package main_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/tls" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"net" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"syscall" | ||
"testing" | ||
"time" | ||
|
||
cli "github.com/gubernator-io/gubernator/v2/cmd/gubernator" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/net/proxy" | ||
) | ||
|
||
var cliRunning = flag.Bool("test_cli_running", false, "True if running as a child process; used by TestCLI") | ||
|
||
func TestCLI(t *testing.T) { | ||
if *cliRunning { | ||
if err := cli.Main(context.Background()); err != nil { | ||
//if !strings.Contains(err.Error(), "context deadline exceeded") { | ||
// log.Print(err.Error()) | ||
// os.Exit(1) | ||
//} | ||
fmt.Print(err.Error()) | ||
os.Exit(1) | ||
} | ||
os.Exit(0) | ||
} | ||
|
||
tests := []struct { | ||
args []string | ||
env []string | ||
name string | ||
contains string | ||
}{ | ||
{ | ||
name: "Should start with no config provided", | ||
env: []string{ | ||
"GUBER_GRPC_ADDRESS=localhost:8080", | ||
"GUBER_HTTP_ADDRESS=localhost:8081", | ||
}, | ||
args: []string{}, | ||
contains: "HTTP Gateway Listening on", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := exec.Command(os.Args[0], append([]string{"--test.run=TestCLI", "--test_cli_running"}, tt.args...)...) | ||
var out bytes.Buffer | ||
c.Stdout = &out | ||
c.Stderr = &out | ||
c.Env = tt.env | ||
|
||
if err := c.Start(); err != nil { | ||
t.Fatal("failed to start child process: ", err) | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||
defer cancel() | ||
|
||
waitCh := make(chan struct{}) | ||
go func() { | ||
_ = c.Wait() | ||
close(waitCh) | ||
}() | ||
|
||
err := waitForConnect(ctx, "localhost:8080", nil) | ||
assert.NoError(t, err) | ||
time.Sleep(time.Second * 1) | ||
|
||
err = c.Process.Signal(syscall.SIGTERM) | ||
require.NoError(t, err) | ||
|
||
<-waitCh | ||
assert.Contains(t, out.String(), tt.contains) | ||
}) | ||
} | ||
} | ||
|
||
// waitForConnect waits until the passed address is accepting connections. | ||
// It will continue to attempt a connection until context is canceled. | ||
func waitForConnect(ctx context.Context, address string, cfg *tls.Config) error { | ||
if address == "" { | ||
return fmt.Errorf("waitForConnect() requires a valid address") | ||
} | ||
|
||
var errs []string | ||
for { | ||
var d proxy.ContextDialer | ||
if cfg != nil { | ||
d = &tls.Dialer{Config: cfg} | ||
} else { | ||
d = &net.Dialer{} | ||
} | ||
conn, err := d.DialContext(ctx, "tcp", address) | ||
if err == nil { | ||
_ = conn.Close() | ||
return nil | ||
} | ||
errs = append(errs, err.Error()) | ||
if ctx.Err() != nil { | ||
errs = append(errs, ctx.Err().Error()) | ||
return errors.New(strings.Join(errs, "\n")) | ||
} | ||
time.Sleep(time.Millisecond * 100) | ||
continue | ||
} | ||
} |