fix(libstack): fix compose run BE-11381 (#126)

pull/12272/merge
andres-portainer 2 weeks ago committed by GitHub
parent 2baae7072f
commit 47d428f3eb

@ -142,7 +142,7 @@ func withComposeService(
// Deploy creates and starts containers
func (c *ComposeDeployer) Deploy(ctx context.Context, filePaths []string, options libstack.DeployOptions) error {
return withComposeService(ctx, filePaths, options.Options, func(composeService api.Service, project *types.Project) error {
addServiceLabels(project)
addServiceLabels(project, false)
var opts api.UpOptions
if options.ForceRecreate {
@ -164,12 +164,17 @@ func (c *ComposeDeployer) Deploy(ctx context.Context, filePaths []string, option
func (c *ComposeDeployer) Run(ctx context.Context, filePaths []string, serviceName string, options libstack.RunOptions) error {
return withComposeService(ctx, filePaths, options.Options, func(composeService api.Service, project *types.Project) error {
addServiceLabels(project)
addServiceLabels(project, true)
if err := composeService.Create(ctx, project, api.CreateOptions{}); err != nil {
return err
}
opts := api.RunOptions{
AutoRemove: options.Remove,
Command: options.Args,
Detach: options.Detached,
Service: serviceName,
}
if _, err := composeService.RunOneOffContainer(ctx, project, opts); err != nil {
@ -235,7 +240,12 @@ func (c *ComposeDeployer) Config(ctx context.Context, filePaths []string, option
return payload, nil
}
func addServiceLabels(project *types.Project) {
func addServiceLabels(project *types.Project, oneOff bool) {
oneOffLabel := "False"
if oneOff {
oneOffLabel = "True"
}
for i, s := range project.Services {
s.CustomLabels = map[string]string{
api.ProjectLabel: project.Name,
@ -243,7 +253,7 @@ func addServiceLabels(project *types.Project) {
api.VersionLabel: api.ComposeVersion,
api.WorkingDirLabel: "/",
api.ConfigFilesLabel: strings.Join(project.ComposeFiles, ","),
api.OneoffLabel: "False",
api.OneoffLabel: oneOffLabel,
}
project.Services[i] = s
}

@ -35,17 +35,14 @@ services:
dir := t.TempDir()
filePathOriginal, err := createFile(dir, "docker-compose.yml", composeFileContent)
require.NoError(t, err)
filePathOverride, err := createFile(dir, "docker-compose-override.yml", overrideComposeFileContent)
require.NoError(t, err)
filePathOriginal := createFile(t, dir, "docker-compose.yml", composeFileContent)
filePathOverride := createFile(t, dir, "docker-compose-override.yml", overrideComposeFileContent)
filePaths := []string{filePathOriginal, filePathOverride}
ctx := context.Background()
err = w.Validate(ctx, filePaths, libstack.Options{ProjectName: projectName})
err := w.Validate(ctx, filePaths, libstack.Options{ProjectName: projectName})
require.NoError(t, err)
err = w.Pull(ctx, filePaths, libstack.Options{ProjectName: projectName})
@ -73,14 +70,33 @@ services:
require.False(t, containerExists(composeContainerName))
}
func createFile(dir, fileName, content string) (string, error) {
func TestRun(t *testing.T) {
w := NewComposeDeployer()
filePath := createFile(t, t.TempDir(), "docker-compose.yml", `
services:
updater:
image: alpine
`)
filePaths := []string{filePath}
serviceName := "updater"
err := w.Run(context.Background(), filePaths, serviceName, libstack.RunOptions{
Options: libstack.Options{
ProjectName: "project_name",
},
})
require.NoError(t, err)
}
func createFile(t *testing.T, dir, fileName, content string) string {
filePath := filepath.Join(dir, fileName)
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
return "", err
}
err := os.WriteFile(filePath, []byte(content), 0o644)
require.NoError(t, err)
return filePath, nil
return filePath
}
func containerExists(containerName string) bool {
@ -101,8 +117,7 @@ func Test_Validate(t *testing.T) {
dir := t.TempDir()
filePathOriginal, err := createFile(dir, "docker-compose.yml", invalidComposeFileContent)
require.NoError(t, err)
filePathOriginal := createFile(t, dir, "docker-compose.yml", invalidComposeFileContent)
filePaths := []string{filePathOriginal}
@ -110,7 +125,7 @@ func Test_Validate(t *testing.T) {
ctx := context.Background()
err = w.Validate(ctx, filePaths, libstack.Options{ProjectName: projectName})
err := w.Validate(ctx, filePaths, libstack.Options{ProjectName: projectName})
require.Error(t, err)
}
@ -308,13 +323,11 @@ networks:
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
composeFilePath, err := createFile(dir, "docker-compose.yml", tc.composeFileContent)
require.NoError(t, err)
composeFilePath := createFile(t, dir, "docker-compose.yml", tc.composeFileContent)
envFilePath := ""
if tc.envFileContent != "" {
envFilePath, err = createFile(dir, "stack.env", tc.envFileContent)
require.NoError(t, err)
envFilePath = createFile(t, dir, "stack.env", tc.envFileContent)
}
w := NewComposeDeployer()

Loading…
Cancel
Save