Skip to content

Commit

Permalink
nasty hack for tls testrunner
Browse files Browse the repository at this point in the history
  • Loading branch information
jbohanon committed Dec 22, 2023
1 parent 1707727 commit c99488a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
28 changes: 28 additions & 0 deletions testutils/helper/test_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helper

import (
"context"
"fmt"
"io"
"time"

Expand All @@ -18,6 +19,7 @@ import (

type TestRunner interface {
Deploy(timeout time.Duration) error
DeployTLS(timeout time.Duration, crt, key []byte) error
Terminate() error
Exec(command ...string) (string, error)
TestRunnerAsync(args ...string) (io.Reader, chan struct{}, error)
Expand Down Expand Up @@ -122,7 +124,23 @@ func (t *testContainer) Terminate() error {
return errors.Wrapf(err, "deleting %s pod", t.echoName)
}
return nil
}

func (t *testContainer) DeleteService() error {
if err := testutils.Kubectl("delete", "service", "-n", t.namespace, t.echoName, "--grace-period=0"); err != nil {
return errors.Wrapf(err, "deleting %s service", t.echoName)
}
return nil
}

func (t *testContainer) TerminateAndDeleteService() error {
if err := t.Terminate(); err != nil {
return err
}
if err := t.DeleteService(); err != nil {
return err
}
return nil
}

// testContainer executes a command inside the testContainer container
Expand All @@ -131,6 +149,16 @@ func (t *testContainer) Exec(command ...string) (string, error) {
return testutils.KubectlOut(args...)
}

// Cp copies files into the testContainer container
func (t *testContainer) Cp(files map[string]string) error {
for k, v := range files {
if err := testutils.Kubectl("cp", k, fmt.Sprintf("%s/%s:%s", t.namespace, t.echoName, v)); err != nil {
return err
}
}
return nil
}

// TestContainerAsync executes a command inside the testContainer container
// returning a buffer that can be read from as it executes
func (t *testContainer) TestRunnerAsync(args ...string) (io.Reader, chan struct{}, error) {
Expand Down
53 changes: 53 additions & 0 deletions testutils/helper/testrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package helper

import (
"fmt"
"os"
"time"

"github.com/pkg/errors"
"github.com/solo-io/go-utils/log"
)

const (
defaultTestRunnerImage = "quay.io/solo-io/testrunner:v1.7.0-beta17"
TestrunnerName = "testrunner"
TestRunnerPort = 1234
TestRunnerHttpsPort = 1235

// This response is given by the testrunner when the SimpleServer is started
SimpleHttpResponse = `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
Expand Down Expand Up @@ -79,3 +82,53 @@ func (t *testRunner) Deploy(timeout time.Duration) error {
}()
return nil
}

func (t *testRunner) DeployTLS(timeout time.Duration, crt, key []byte) error {
if err := t.TerminateAndDeleteService(); err != nil {
return errors.Wrap(err, "terminating pod and deleting service")
}
if err := t.deploy(timeout); err != nil {
return errors.Wrap(err, "deploying pod")
}

os.MkdirAll("/tmp/testrunner_tls", os.ModePerm)
defer os.RemoveAll("/tmp/testrunner_tls")

if err := os.WriteFile("/tmp/testrunner_tls/cert.pem", crt, os.ModePerm); err != nil {
return errors.Wrap(err, "writing cert")
}
if err := os.WriteFile("/tmp/testrunner_tls/key.pem", key, os.ModePerm); err != nil {
return errors.Wrap(err, "writing key")
}
if err := os.WriteFile("/tmp/testrunner_tls/server.py", []byte(`
import BaseHTTPServer, SimpleHTTPServer
import ssl
httpd = BaseHTTPServer.HTTPServer(('0.0.0.0', 1234), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, keyfile="/tmp/key.pem", certfile='/tmp/cert.pem', server_side=True)
httpd.serve_forever()
`), os.ModePerm); err != nil {
return errors.Wrap(err, "writing server")
}
if err := t.Cp(map[string]string{
"/tmp/testrunner_tls/cert.pem": "/tmp/cert.pem",
"/tmp/testrunner_tls/key.pem": "/tmp/key.pem",
"/tmp/testrunner_tls/server.py": "/tmp/server.py",
}); err != nil {
return errors.Wrap(err, "kubectl cp")
}

go func() {
start := time.Now()
log.Printf("~~~STARTING SERVER~~~")
log.Debugf("starting https server listening on port %v", TestRunnerPort)
// This command starts an https SimpleHttpServer and blocks until the server terminates
if _, err := t.Exec("python", "/tmp/server.py"); err != nil {
// if an error happened after 5 seconds, it's probably not an error.. just the pod terminating.
if time.Now().Sub(start).Seconds() < 5.0 {
log.Warnf("failed to start HTTP Server in Test Runner: %v", err)
}
}
}()
return nil
}

0 comments on commit c99488a

Please sign in to comment.