diff --git a/pkg/compose/hash.go b/pkg/compose/hash.go index dba127f1a15..d75b67568de 100644 --- a/pkg/compose/hash.go +++ b/pkg/compose/hash.go @@ -18,6 +18,7 @@ package compose import ( "encoding/json" + "os" "github.com/compose-spec/compose-go/v2/types" "github.com/opencontainers/go-digest" @@ -40,7 +41,17 @@ func ServiceHash(project *types.Project, o types.ServiceConfig) (string, error) for _, serviceConfig := range o.Configs { if projectConfig, ok := project.Configs[serviceConfig.Source]; ok { - bytes = append(bytes, []byte(projectConfig.Content)...) + if projectConfig.Content != "" { + bytes = append(bytes, []byte(projectConfig.Content)...) + } else if projectConfig.File != "" { + content, err := os.ReadFile(projectConfig.File) + if err != nil { + return "", err + } + bytes = append(bytes, content...) + } else if projectConfig.Environment != "" { + bytes = append(bytes, []byte(projectConfig.Environment)...) + } } } diff --git a/pkg/compose/hash_test.go b/pkg/compose/hash_test.go index 04b448ce161..0ae121cd4e4 100644 --- a/pkg/compose/hash_test.go +++ b/pkg/compose/hash_test.go @@ -24,9 +24,9 @@ import ( ) func TestServiceHashWithAllValuesTheSame(t *testing.T) { - hash1, err := ServiceHash(projectConfig("a", "b"), serviceConfig("myContext1", "always", 1)) + hash1, err := ServiceHash(projectConfig("a", "b", "c", ""), serviceConfig("myContext1", "always", 1)) assert.NilError(t, err) - hash2, err := ServiceHash(projectConfig("a", "b"), serviceConfig("myContext1", "always", 1)) + hash2, err := ServiceHash(projectConfig("a", "b", "c", ""), serviceConfig("myContext1", "always", 1)) assert.NilError(t, err) assert.Equal(t, hash1, hash2) } @@ -39,19 +39,43 @@ func TestServiceHashWithIgnorableValues(t *testing.T) { assert.Equal(t, hash1, hash2) } -func TestServiceHashWithChangedConfig(t *testing.T) { - hash1, err := ServiceHash(projectConfig("myConfigSource", "a"), serviceConfig("myContext1", "always", 1)) +func TestServiceHashWithChangedConfigContent(t *testing.T) { + hash1, err := ServiceHash(projectConfig("myConfigSource", "a", "", ""), serviceConfig("myContext1", "always", 1)) assert.NilError(t, err) - hash2, err := ServiceHash(projectConfig("myConfigSource", "b"), serviceConfig("myContext2", "never", 2)) + hash2, err := ServiceHash(projectConfig("myConfigSource", "b", "", ""), serviceConfig("myContext2", "never", 2)) assert.NilError(t, err) assert.Assert(t, hash1 != hash2) } -func projectConfig(configName, configContent string) *types.Project { +func TestServiceHashWithChangedConfigEnvironment(t *testing.T) { + hash1, err := ServiceHash(projectConfig("myConfigSource", "", "a", ""), serviceConfig("myContext1", "always", 1)) + assert.NilError(t, err) + hash2, err := ServiceHash(projectConfig("myConfigSource", "", "b", ""), serviceConfig("myContext2", "never", 2)) + assert.NilError(t, err) + assert.Assert(t, hash1 != hash2) +} + +func TestServiceHashWithChangedConfigFile(t *testing.T) { + hash1, err := ServiceHash( + projectConfig("myConfigSource", "", "", "./testdata/config1.txt"), + serviceConfig("myContext1", "always", 1), + ) + assert.NilError(t, err) + hash2, err := ServiceHash( + projectConfig("myConfigSource", "", "", "./testdata/config2.txt"), + serviceConfig("myContext2", "never", 2), + ) + assert.NilError(t, err) + assert.Assert(t, hash1 != hash2) +} + +func projectConfig(configName, configContent, configEnvironment, configFile string) *types.Project { return &types.Project{ Configs: types.Configs{ configName: types.ConfigObjConfig{ - Content: configContent, + Content: configContent, + Environment: configEnvironment, + File: configFile, }, }, } diff --git a/pkg/compose/testdata/config1.txt b/pkg/compose/testdata/config1.txt new file mode 100644 index 00000000000..c8a618fe3ee --- /dev/null +++ b/pkg/compose/testdata/config1.txt @@ -0,0 +1 @@ +This is 1 config file diff --git a/pkg/compose/testdata/config2.txt b/pkg/compose/testdata/config2.txt new file mode 100644 index 00000000000..51f7bc363de --- /dev/null +++ b/pkg/compose/testdata/config2.txt @@ -0,0 +1 @@ +This is 2 config file