Skip to content

Commit

Permalink
dockercompose: add new_name tests + fix resourceDep bug from previous…
Browse files Browse the repository at this point in the history
… commit

Signed-off-by: Kinland <16787581+kinland@users.noreply.github.com>
  • Loading branch information
kinland committed Mar 13, 2024
1 parent 112a15c commit 6c37994
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/tiltfile/docker_compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strconv"
"strings"

"golang.org/x/exp/slices"

"github.com/compose-spec/compose-go/consts"
"github.com/compose-spec/compose-go/loader"
"github.com/compose-spec/compose-go/types"
Expand Down Expand Up @@ -350,7 +352,9 @@ func (s *tiltfileState) renameDCService(projectName, name, newName string, svc *
index = i
} else if sd, ok := services[n].ServiceConfig.DependsOn[name]; ok {
services[n].ServiceConfig.DependsOn[newName] = sd
services[n].Options.resourceDeps = []string{newName}
if rdIndex := slices.Index(services[n].Options.resourceDeps, name); rdIndex != -1 {
services[n].Options.resourceDeps[rdIndex] = newName
}
delete(services[n].ServiceConfig.DependsOn, name)
}
}
Expand Down
86 changes: 86 additions & 0 deletions internal/tiltfile/tiltfile_docker_compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ services:
- barprofile
`

const threeServiceConfig = `version: '3'
services:
db:
image: db-image
foo:
image: foo-image
command: sleep 100
ports:
- "12312:80"
depends_on:
- db
bar:
image: bar-image
expose:
- "3000"
depends_on:
- db
- foo
`

// YAML for Foo config looks a little different from the above after being read into
// a struct and YAML'd back out...
func (f *fixture) simpleConfigAfterParse() string {
Expand Down Expand Up @@ -445,6 +465,72 @@ docker_compose('docker-compose1.yml')`
f.loadErrString(`dc_resource named "foo" already exists`)
}

func TestDockerComposeNewNameWithDependencies(t *testing.T) {
for _, testCase := range []struct {
name string
renames map[string]string
}{
{
"default",
make(map[string]string),
},
{
"rename db",
map[string]string{"db": "db2"},
},
{
"rename foo",
map[string]string{"foo": "foo2"},
},
{
"rename bar",
map[string]string{"bar": "bar2"},
},
{
"rename foo + bar",
map[string]string{
"foo": "foo2",
"bar": "bar2",
},
},
{
"rename db + foo + bar",
map[string]string{
"db": "db2",
"foo": "foo2",
"bar": "bar2",
},
},
} {
t.Run(testCase.name, func(t *testing.T) {
f := newFixture(t)

f.file("docker-compose.yml", threeServiceConfig)

tf := "docker_compose('docker-compose.yml')\n"

allNames := map[string]string{
"db": "db",
"foo": "foo",
"bar": "bar",
}

for oldName, newName := range testCase.renames {
tf += fmt.Sprintf("dc_resource('%s', new_name='%s')\n", oldName, newName)
allNames[oldName] = newName
}

f.file("Tiltfile", tf)

f.load()

f.assertNextManifest(model.ManifestName(allNames["db"]), resourceDeps())
f.assertNextManifest(model.ManifestName(allNames["foo"]), resourceDeps(allNames["db"]))
f.assertNextManifest(model.ManifestName(allNames["bar"]), resourceDeps(allNames["db"], allNames["foo"]))
})
}
}

func TestMultipleDockerComposeSameDir(t *testing.T) {
f := newFixture(t)

Expand Down

0 comments on commit 6c37994

Please sign in to comment.