Skip to content

Commit

Permalink
fix: [#12] Cannot find Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
sbp-bvanb committed Dec 18, 2024
1 parent 3302238 commit dbd2819
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
19 changes: 15 additions & 4 deletions internal/oktamock/oktamock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions internal/pkg/constants/constants.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit dbd2819

Please sign in to comment.