Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpolation seems to have broken with latest upgrade #742

Open
alexellis opened this issue Feb 20, 2025 · 3 comments
Open

Interpolation seems to have broken with latest upgrade #742

alexellis opened this issue Feb 20, 2025 · 3 comments

Comments

@alexellis
Copy link

Having upgraded to the latest version of this Go library, our unit tests are now failing for string interpolation from environment variables:

These tests worked with the previous version being used, but are now broken, the resulting string comes out with the text {$ARCH_SUFFIX still in place.

Version in go.mod: github.com/compose-spec/compose-go v1.20.2

Is this a regression? What would you suggest?

package pkg

import (
	"os"
	"path"
	"reflect"
	"testing"

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

func Test_interpolateEnv_EmptyValue(t *testing.T) {
	env := map[string]string{
		"ARCH_SUFFIX": "",
	}

	wantImage := "docker.io/openfaas/basic-auth-plugin:0.18.17"

	composeFile := `version: "3.7"
services:
  basic-auth-plugin:
    image: docker.io/openfaas/basic-auth-plugin:0.18.17${ARCH_SUFFIX}
`

	config, err := loader.ParseYAML([]byte(composeFile))
	if err != nil {
		t.Fatalf("can't parse compose file: %s", err)
	}

	var files []compose.ConfigFile
	files = append(files, compose.ConfigFile{Filename: "docker-compose.yaml", Config: config})

	compose, err := loader.Load(compose.ConfigDetails{
		WorkingDir:  os.TempDir(),
		ConfigFiles: files,
		Environment: env,
	})

	if err != nil {
		t.Fatalf("can't parse compose file: %s", err)
	}

	if compose.Services[0].Image != wantImage {
		t.Fatalf("incorrect service Image:\n\twant: %s,\n\tgot: %s", wantImage, compose.Services[0].Image)
	}

}

func Test_interpolateEnv_NewValue(t *testing.T) {
	env := map[string]string{
		"ARCH_SUFFIX": "-arm64",
	}

	wantImage := "docker.io/openfaas/basic-auth-plugin:0.18.17-arm64"

	composeFile := `version: "3.7"
services:
  basic-auth-plugin:
    image: docker.io/openfaas/basic-auth-plugin:0.18.17${ARCH_SUFFIX}
`

	config, err := loader.ParseYAML([]byte(composeFile))
	if err != nil {
		t.Fatalf("can't parse compose file: %s", err)
	}

	var files []compose.ConfigFile
	files = append(files, compose.ConfigFile{Filename: "docker-compose.yaml", Config: config})

	compose, err := loader.Load(compose.ConfigDetails{
		WorkingDir:  os.TempDir(),
		ConfigFiles: files,
		Environment: env,
	})

	if err != nil {
		t.Fatalf("can't parse compose file: %s", err)
	}

	if compose.Services[0].Image != wantImage {
		t.Fatalf("incorrect service Image:\n\twant: %s,\n\tgot: %s", wantImage, compose.Services[0].Image)
	}

}
@alexellis
Copy link
Author

@ndeloof I created this on the spec repo in error and have closed that issue. Can you see anything that I may be doing wrong here?

@ndeloof
Copy link
Collaborator

ndeloof commented Feb 20, 2025

You should better rely on LoadWithContext. ParseYAML is legacy func and should better be deprecated/removed from compose-go codebase to avoid confusion

@ndeloof
Copy link
Collaborator

ndeloof commented Feb 20, 2025

By the way, stop using version: "3.7" - version was removed from the compose file format with Compose Spec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants