From 6277d8f6b9edd3a382da68b06be378c82cecac18 Mon Sep 17 00:00:00 2001 From: apostasie Date: Sat, 10 Aug 2024 23:08:06 -0700 Subject: [PATCH] Move projectloader to testutil Signed-off-by: apostasie --- pkg/composer/projectloader/projectloader.go | 56 ------------------- pkg/composer/serviceparser/build_test.go | 3 +- .../serviceparser/serviceparser_test.go | 15 +++-- pkg/testutil/compose.go | 33 +++++++++++ 4 files changed, 41 insertions(+), 66 deletions(-) delete mode 100644 pkg/composer/projectloader/projectloader.go diff --git a/pkg/composer/projectloader/projectloader.go b/pkg/composer/projectloader/projectloader.go deleted file mode 100644 index 680eb2842d6..00000000000 --- a/pkg/composer/projectloader/projectloader.go +++ /dev/null @@ -1,56 +0,0 @@ -/* - Copyright The containerd Authors. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package projectloader - -import ( - "context" - "os" - "path/filepath" - - "github.com/compose-spec/compose-go/v2/loader" - compose "github.com/compose-spec/compose-go/v2/types" -) - -// Load is used only for unit testing. -// TODO: Remove -func Load(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) - } -} diff --git a/pkg/composer/serviceparser/build_test.go b/pkg/composer/serviceparser/build_test.go index 55d2ecc2e87..0c63343bc2b 100644 --- a/pkg/composer/serviceparser/build_test.go +++ b/pkg/composer/serviceparser/build_test.go @@ -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" ) @@ -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") diff --git a/pkg/composer/serviceparser/serviceparser_test.go b/pkg/composer/serviceparser/serviceparser_test.go index b3aa2008f99..0e178b791d1 100644 --- a/pkg/composer/serviceparser/serviceparser_test.go +++ b/pkg/composer/serviceparser/serviceparser_test.go @@ -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" @@ -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") @@ -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") @@ -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") @@ -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") @@ -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") @@ -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"} { @@ -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 { diff --git a/pkg/testutil/compose.go b/pkg/testutil/compose.go index 2d1a28e17f3..2e5b55b056d 100644 --- a/pkg/testutil/compose.go +++ b/pkg/testutil/compose.go @@ -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 { @@ -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) + } +}