Skip to content

Commit

Permalink
Move projectloader to testutil
Browse files Browse the repository at this point in the history
Signed-off-by: apostasie <spam_blackhole@farcloser.world>
  • Loading branch information
apostasie committed Aug 11, 2024
1 parent 02cb8a9 commit 6277d8f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 66 deletions.
56 changes: 0 additions & 56 deletions pkg/composer/projectloader/projectloader.go

This file was deleted.

3 changes: 1 addition & 2 deletions pkg/composer/serviceparser/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package serviceparser
import (
"testing"

"github.com/containerd/nerdctl/v2/pkg/composer/projectloader"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"gotest.tools/v3/assert"
)
Expand Down Expand Up @@ -59,7 +58,7 @@ secrets:
comp := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp.CleanUp()

project, err := projectloader.Load(comp.YAMLFullPath(), comp.ProjectName(), nil)
project, err := testutil.LoadProject(comp.YAMLFullPath(), comp.ProjectName(), nil)
assert.NilError(t, err)

fooSvc, err := project.GetService("foo")
Expand Down
15 changes: 7 additions & 8 deletions pkg/composer/serviceparser/serviceparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"testing"

"github.com/compose-spec/compose-go/v2/types"
"github.com/containerd/nerdctl/v2/pkg/composer/projectloader"
"github.com/containerd/nerdctl/v2/pkg/strutil"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"gotest.tools/v3/assert"
Expand Down Expand Up @@ -141,7 +140,7 @@ volumes:
comp := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp.CleanUp()

project, err := projectloader.Load(comp.YAMLFullPath(), comp.ProjectName(), nil)
project, err := testutil.LoadProject(comp.YAMLFullPath(), comp.ProjectName(), nil)
assert.NilError(t, err)

wpSvc, err := project.GetService("wordpress")
Expand Down Expand Up @@ -212,7 +211,7 @@ services:
comp := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp.CleanUp()

project, err := projectloader.Load(comp.YAMLFullPath(), comp.ProjectName(), nil)
project, err := testutil.LoadProject(comp.YAMLFullPath(), comp.ProjectName(), nil)
assert.NilError(t, err)

fooSvc, err := project.GetService("foo")
Expand Down Expand Up @@ -273,7 +272,7 @@ services:
comp := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp.CleanUp()

project, err := projectloader.Load(comp.YAMLFullPath(), comp.ProjectName(), nil)
project, err := testutil.LoadProject(comp.YAMLFullPath(), comp.ProjectName(), nil)
assert.NilError(t, err)

fooSvc, err := project.GetService("foo")
Expand Down Expand Up @@ -346,7 +345,7 @@ services:
comp := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp.CleanUp()

project, err := projectloader.Load(comp.YAMLFullPath(), comp.ProjectName(), nil)
project, err := testutil.LoadProject(comp.YAMLFullPath(), comp.ProjectName(), nil)
assert.NilError(t, err)

fooSvc, err := project.GetService("foo")
Expand Down Expand Up @@ -378,7 +377,7 @@ services:
comp := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp.CleanUp()

project, err := projectloader.Load(comp.YAMLFullPath(), comp.ProjectName(), nil)
project, err := testutil.LoadProject(comp.YAMLFullPath(), comp.ProjectName(), nil)
assert.NilError(t, err)

fooSvc, err := project.GetService("foo")
Expand Down Expand Up @@ -438,7 +437,7 @@ configs:
comp := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp.CleanUp()

project, err := projectloader.Load(comp.YAMLFullPath(), comp.ProjectName(), nil)
project, err := testutil.LoadProject(comp.YAMLFullPath(), comp.ProjectName(), nil)
assert.NilError(t, err)

for _, f := range []string{"secret1", "secret2", "secret3", "config1", "config2"} {
Expand Down Expand Up @@ -482,7 +481,7 @@ services:
comp := testutil.NewComposeDir(t, dockerComposeYAML)
defer comp.CleanUp()

project, err := projectloader.Load(comp.YAMLFullPath(), comp.ProjectName(), nil)
project, err := testutil.LoadProject(comp.YAMLFullPath(), comp.ProjectName(), nil)
assert.NilError(t, err)

getContainersFromService := func(svcName string) []Container {
Expand Down
33 changes: 33 additions & 0 deletions pkg/testutil/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
package testutil

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/compose-spec/compose-go/v2/loader"
compose "github.com/compose-spec/compose-go/v2/types"
)

type ComposeDir struct {
Expand Down Expand Up @@ -63,3 +67,32 @@ func NewComposeDir(t testing.TB, dockerComposeYAML string) *ComposeDir {
cd.WriteFile(cd.yamlBasePath, dockerComposeYAML)
return cd
}

// Load is used only for unit testing.
func LoadProject(fileName, projectName string, envMap map[string]string) (*compose.Project, error) {
if envMap == nil {
envMap = make(map[string]string)
}
b, err := os.ReadFile(fileName)
if err != nil {
return nil, err
}

wd, err := filepath.Abs(filepath.Dir(fileName))
if err != nil {
return nil, err
}
var files []compose.ConfigFile
files = append(files, compose.ConfigFile{Filename: fileName, Content: b})
return loader.LoadWithContext(context.TODO(), compose.ConfigDetails{
WorkingDir: wd,
ConfigFiles: files,
Environment: envMap,
}, withProjectName(projectName))
}

func withProjectName(name string) func(*loader.Options) {
return func(lOpts *loader.Options) {
lOpts.SetProjectName(name, true)
}
}

0 comments on commit 6277d8f

Please sign in to comment.