Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: export config BE handler for transformer, and unstarted http test server #374

Merged
merged 4 commits into from
Mar 15, 2024
Merged
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
16 changes: 12 additions & 4 deletions testhelper/docker/resource/transformer/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/samber/lo"

dockerTestHelper "github.com/rudderlabs/rudder-go-kit/testhelper/docker"
dockertesthelper "github.com/rudderlabs/rudder-go-kit/testhelper/docker"

"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
Expand Down Expand Up @@ -56,21 +56,29 @@ func (c *config) setBackendConfigURL(url string) {
// })
func WithUserTransformations(transformations map[string]string, cleaner resource.Cleaner) func(*config) {
return func(conf *config) {
backendConfigSvc := NewTestBackendConfigServer(transformations)
backendConfigSvc := newTestBackendConfigServer(transformations)

conf.setBackendConfigURL(dockerTestHelper.ToInternalDockerHost(backendConfigSvc.URL))
conf.setBackendConfigURL(dockertesthelper.ToInternalDockerHost(backendConfigSvc.URL))
conf.extraHosts = append(conf.extraHosts, "host.docker.internal:host-gateway")
cleaner.Cleanup(func() {
backendConfigSvc.Close()
})
}
}

// WithConnectionToHostEnabled lets transformer container connect with the host machine
// i.e. transformer container will be able to access localhost of the host machine
func WithConnectionToHostEnabled() func(*config) {
return func(conf *config) {
conf.extraHosts = append(conf.extraHosts, "host.docker.internal:host-gateway")
}
}

// WithConfigBackendURL lets transformer use custom backend config server for transformations
// WithConfigBackendURL should not be used with WithUserTransformations option
func WithConfigBackendURL(url string) func(*config) {
return func(conf *config) {
conf.setBackendConfigURL(url)
conf.setBackendConfigURL(dockertesthelper.ToInternalDockerHost(url))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ const (
versionIDKey = "versionId"
)

func NewTestBackendConfigServer(transformations map[string]string) *kithttptest.Server {
func newTestBackendConfigServer(transformations map[string]string) *kithttptest.Server {
return kithttptest.NewServer(NewTransformerBackendConfigHandler(transformations))
}

// NewTransformerBackendConfigHandler returns http request handler to handle all backend config requests by transformer
func NewTransformerBackendConfigHandler(transformations map[string]string) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc(getByVersionIdEndPoint, getByVersionIdHandler(transformations))
return kithttptest.NewServer(mux)
return mux
}

func getByVersionIdHandler(transformations map[string]string) func(http.ResponseWriter, *http.Request) {
Expand Down
4 changes: 2 additions & 2 deletions testhelper/httptest/httptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// NewServer starts a new httptest server that listens on all interfaces, contrary to the standard net/httptest.Server that listens only on localhost.
// This is useful when you want to access the test http server from within a docker container.
func NewServer(handler http.Handler) *Server {
ts := newUnStartedServer(handler)
ts := NewUnStartedServer(handler)
ts.start()
return ts
}
Expand All @@ -29,7 +29,7 @@ func (s *Server) start() {
s.URL = fmt.Sprintf("http://%s:%s", "localhost", port)
}

func newUnStartedServer(handler http.Handler) *Server {
func NewUnStartedServer(handler http.Handler) *Server {
return &Server{&nethttptest.Server{
Listener: newListener(),
Config: &http.Server{Handler: handler},
Expand Down
34 changes: 34 additions & 0 deletions testhelper/httptest/httptest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,37 @@ func TestServer(t *testing.T) {
require.Equal(t, http.StatusOK, statusCode)
require.Equal(t, "Hello, world!", string(body))
}

func TestUnStartedServer(t *testing.T) {
// create a server which is not started
httpUnStartedServer := kithttptest.NewUnStartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("Hello, world!"))
}))

var (
body []byte
statusCode int
)
require.Never(t, func() bool {
resp, err := http.Get("http://" + httpUnStartedServer.Listener.Addr().String())
defer func() { httputil.CloseResponse(resp) }()
return err == nil
}, 5*time.Second, 10*time.Millisecond, "connected to an un-started server")

// start the server now
httpUnStartedServer.Start()
defer httpUnStartedServer.Close()

require.Eventually(t, func() bool {
resp, err := http.Get(httpUnStartedServer.URL)
defer func() { httputil.CloseResponse(resp) }()
if err == nil {
statusCode = resp.StatusCode
body, err = io.ReadAll(resp.Body)
}
return err == nil
}, 2*time.Second, 100*time.Millisecond, "failed to connect to server")

require.Equal(t, http.StatusOK, statusCode)
require.Equal(t, "Hello, world!", string(body))
}
Loading