diff --git a/internal/oktamock/oktamock.go b/internal/oktamock/oktamock.go index 80fd2c9..846821c 100644 --- a/internal/oktamock/oktamock.go +++ b/internal/oktamock/oktamock.go @@ -12,6 +12,7 @@ import ( "github.com/ory/dockertest/v3" "github.com/ory/dockertest/v3/docker" + "schubergphilis/mcvs-integrationtest-services/internal/pkg/constants" "schubergphilis/mcvs-integrationtest-services/internal/pkg/dockertestutils" ) @@ -45,13 +46,23 @@ func (r *Resource) WithLogger(writer io.Writer) *Resource { } // Start starts the resource with given run options. -func (r *Resource) Start(opts *dockertest.RunOptions, contextDir string, hcOpts ...func(*docker.HostConfig)) error { +func (r *Resource) Start(opts *dockertest.RunOptions, _ string, hcOpts ...func(*docker.HostConfig)) error { opts.Networks = append(opts.Networks, r.network) var err error + projectRoot, err := constants.FindProjectRoot() + if err != nil { + return fmt.Errorf("failed to determine the root of the project: %w", err) + } + buildArgs := []docker.BuildArg{ + { + Name: "APPLICATION", + Value: "oktamock", + }, + } r.resource, err = r.pool.BuildAndRunWithBuildOptions(&dockertest.BuildOptions{ - Dockerfile: "./okta/Dockerfile", - ContextDir: contextDir, - BuildArgs: []docker.BuildArg{}, + Dockerfile: "./Dockerfile", + ContextDir: projectRoot, + BuildArgs: buildArgs, }, opts, hcOpts...) if err != nil { return fmt.Errorf("unable to build okta mock server container: %w", err) diff --git a/internal/pkg/constants/constants.go b/internal/pkg/constants/constants.go new file mode 100644 index 0000000..0c41de8 --- /dev/null +++ b/internal/pkg/constants/constants.go @@ -0,0 +1,33 @@ +package constants + +import ( + "fmt" + "os" + "path/filepath" +) + +func FindProjectRoot() (string, error) { + currentDir, err := os.Getwd() + if err != nil { + return "", err + } + + for { + if fileExists(filepath.Join(currentDir, "go.mod")) { + return currentDir, nil + } + + parentDir := filepath.Dir(currentDir) + if currentDir == parentDir { + break + } + currentDir = parentDir + } + + return "", fmt.Errorf("project root not found") +} + +func fileExists(path string) bool { + _, err := os.Stat(path) + return !os.IsNotExist(err) +}