Skip to content

Commit

Permalink
fix(hash): add support for env/config file in configs
Browse files Browse the repository at this point in the history
Signed-off-by: Suleiman Dibirov <[email protected]>
  • Loading branch information
idsulik committed Jul 6, 2024
1 parent 0815ad2 commit f75d940
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
13 changes: 12 additions & 1 deletion pkg/compose/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package compose

import (
"encoding/json"
"os"

"github.com/compose-spec/compose-go/v2/types"
"github.com/opencontainers/go-digest"
Expand All @@ -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)...)
}
}
}

Expand Down
38 changes: 31 additions & 7 deletions pkg/compose/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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,
},
},
}
Expand Down
1 change: 1 addition & 0 deletions pkg/compose/testdata/config1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is 1 config file
1 change: 1 addition & 0 deletions pkg/compose/testdata/config2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is 2 config file

0 comments on commit f75d940

Please sign in to comment.