Skip to content

Commit

Permalink
lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
idsulik committed Oct 6, 2024
1 parent 18ca016 commit 9695d57
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
25 changes: 13 additions & 12 deletions pkg/compose/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/docker/compose/v2/pkg/utils"
"time"

"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/pkg/utils"
"github.com/opencontainers/go-digest"
)

Expand All @@ -38,16 +38,16 @@ func ServiceHash(o types.ServiceConfig) (string, error) {
}
o.DependsOn = nil

bytes, err := json.Marshal(o)
data, err := json.Marshal(o)
if err != nil {
return "", err
}
return digest.SHA256.FromBytes(bytes).Encoded(), nil
return digest.SHA256.FromBytes(data).Encoded(), nil
}

// ServiceConfigsHash computes the configuration hash for service configs.
func ServiceConfigsHash(project *types.Project, serviceConfig types.ServiceConfig) (string, error) {
bytes := make([]byte, 0)
data := make([]byte, 0)
for _, config := range serviceConfig.Configs {
file := project.Configs[config.Source]
b, err := createTarForConfig(project, types.FileReferenceConfig(config), types.FileObjectConfig(file))
Expand All @@ -56,15 +56,15 @@ func ServiceConfigsHash(project *types.Project, serviceConfig types.ServiceConfi
return "", err
}

bytes = append(bytes, b.Bytes()...)
data = append(data, b.Bytes()...)
}

return digest.SHA256.FromBytes(bytes).Encoded(), nil
return digest.SHA256.FromBytes(data).Encoded(), nil
}

// ServiceSecretsHash computes the configuration hash for service secrets.
func ServiceSecretsHash(project *types.Project, serviceConfig types.ServiceConfig) (string, error) {
bytes := make([]byte, 0)
data := make([]byte, 0)
for _, secret := range serviceConfig.Secrets {
file := project.Secrets[secret.Source]
b, err := createTarForConfig(project, types.FileReferenceConfig(secret), types.FileObjectConfig(file))
Expand All @@ -73,10 +73,10 @@ func ServiceSecretsHash(project *types.Project, serviceConfig types.ServiceConfi
return "", err
}

bytes = append(bytes, b.Bytes()...)
data = append(data, b.Bytes()...)
}

return digest.SHA256.FromBytes(bytes).Encoded(), nil
return digest.SHA256.FromBytes(data).Encoded(), nil
}

func createTarForConfig(
Expand All @@ -91,9 +91,10 @@ func createTarForConfig(
serviceConfig.Target = "/" + serviceConfig.Source
}

if file.Content != "" {
switch {
case file.Content != "":
return bytes.NewBuffer([]byte(file.Content)), nil
} else if file.Environment != "" {
case file.Environment != "":
env, ok := project.Environment[file.Environment]
if !ok {
return nil, fmt.Errorf(
Expand All @@ -103,7 +104,7 @@ func createTarForConfig(
)
}
return bytes.NewBuffer([]byte(env)), nil
} else if file.File != "" {
case file.File != "":
return utils.CreateTarByPath(file.File, modTime)
}

Expand Down
40 changes: 20 additions & 20 deletions pkg/compose/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,82 +40,82 @@ func TestServiceHashWithIgnorableValues(t *testing.T) {
}

func TestServiceConfigsHashWithoutChangesContent(t *testing.T) {
hash1, err := ServiceConfigsHash(projectWithConfigs("myConfigSource", "a", "", ""), serviceConfig("myContext1", "always", 1))
hash1, err := ServiceConfigsHash(projectWithConfigs("a", "", ""), serviceConfig("myContext1", "always", 1))
assert.NilError(t, err)
hash2, err := ServiceConfigsHash(projectWithConfigs("myConfigSource", "a", "", ""), serviceConfig("myContext2", "never", 2))
hash2, err := ServiceConfigsHash(projectWithConfigs("a", "", ""), serviceConfig("myContext2", "never", 2))
assert.NilError(t, err)
assert.Assert(t, hash1 == hash2)
}

func TestServiceConfigsHashWithChangedConfigContent(t *testing.T) {
hash1, err := ServiceConfigsHash(projectWithConfigs("myConfigSource", "a", "", ""), serviceConfig("myContext1", "always", 1))
hash1, err := ServiceConfigsHash(projectWithConfigs("a", "", ""), serviceConfig("myContext1", "always", 1))
assert.NilError(t, err)
hash2, err := ServiceConfigsHash(projectWithConfigs("myConfigSource", "b", "", ""), serviceConfig("myContext2", "never", 2))
hash2, err := ServiceConfigsHash(projectWithConfigs("b", "", ""), serviceConfig("myContext2", "never", 2))
assert.NilError(t, err)
assert.Assert(t, hash1 != hash2)
}

func TestServiceConfigsHashWithChangedConfigEnvironment(t *testing.T) {
hash1, err := ServiceConfigsHash(projectWithConfigs("myConfigSource", "", "a", ""), serviceConfig("myContext1", "always", 1))
hash1, err := ServiceConfigsHash(projectWithConfigs("", "a", ""), serviceConfig("myContext1", "always", 1))
assert.NilError(t, err)
hash2, err := ServiceConfigsHash(projectWithConfigs("myConfigSource", "", "b", ""), serviceConfig("myContext2", "never", 2))
hash2, err := ServiceConfigsHash(projectWithConfigs("", "b", ""), serviceConfig("myContext2", "never", 2))
assert.NilError(t, err)
assert.Assert(t, hash1 != hash2)
}

func TestServiceConfigsHashWithChangedConfigFile(t *testing.T) {
hash1, err := ServiceConfigsHash(
projectWithConfigs("myConfigSource", "", "", "./testdata/config1.txt"),
projectWithConfigs("", "", "./testdata/config1.txt"),
serviceConfig("myContext1", "always", 1),
)
assert.NilError(t, err)
hash2, err := ServiceConfigsHash(
projectWithConfigs("myConfigSource", "", "", "./testdata/config2.txt"),
projectWithConfigs("", "", "./testdata/config2.txt"),
serviceConfig("myContext2", "never", 2),
)
assert.NilError(t, err)
assert.Assert(t, hash1 != hash2)
}

func TestServiceSecretsHashWithoutChangesContent(t *testing.T) {
hash1, err := ServiceSecretsHash(projectWithSecrets("mySecretSource", "a", "", ""), serviceConfig("myContext1", "always", 1))
hash1, err := ServiceSecretsHash(projectWithSecrets("a", "", ""), serviceConfig("myContext1", "always", 1))
assert.NilError(t, err)
hash2, err := ServiceSecretsHash(projectWithSecrets("mySecretSource", "a", "", ""), serviceConfig("myContext2", "never", 2))
hash2, err := ServiceSecretsHash(projectWithSecrets("a", "", ""), serviceConfig("myContext2", "never", 2))
assert.NilError(t, err)
assert.Assert(t, hash1 == hash2)
}

func TestServiceSecretsHashWithChangedSecretContent(t *testing.T) {
hash1, err := ServiceSecretsHash(projectWithSecrets("mySecretSource", "a", "", ""), serviceConfig("myContext1", "always", 1))
hash1, err := ServiceSecretsHash(projectWithSecrets("a", "", ""), serviceConfig("myContext1", "always", 1))
assert.NilError(t, err)
hash2, err := ServiceSecretsHash(projectWithSecrets("mySecretSource", "b", "", ""), serviceConfig("myContext2", "never", 2))
hash2, err := ServiceSecretsHash(projectWithSecrets("b", "", ""), serviceConfig("myContext2", "never", 2))
assert.NilError(t, err)
assert.Assert(t, hash1 != hash2)
}

func TestServiceSecretsHashWithChangedSecretEnvironment(t *testing.T) {
hash1, err := ServiceSecretsHash(projectWithSecrets("mySecretSource", "", "a", ""), serviceConfig("myContext1", "always", 1))
hash1, err := ServiceSecretsHash(projectWithSecrets("", "a", ""), serviceConfig("myContext1", "always", 1))
assert.NilError(t, err)
hash2, err := ServiceSecretsHash(projectWithSecrets("mySecretSource", "", "b", ""), serviceConfig("myContext2", "never", 2))
hash2, err := ServiceSecretsHash(projectWithSecrets("", "b", ""), serviceConfig("myContext2", "never", 2))
assert.NilError(t, err)
assert.Assert(t, hash1 != hash2)
}

func TestServiceSecretsHashWithChangedSecretFile(t *testing.T) {
hash1, err := ServiceSecretsHash(
projectWithSecrets("mySecretSource", "", "", "./testdata/config1.txt"),
projectWithSecrets("", "", "./testdata/config1.txt"),
serviceConfig("myContext1", "always", 1),
)
assert.NilError(t, err)
hash2, err := ServiceSecretsHash(
projectWithSecrets("mySecretSource", "", "", "./testdata/config2.txt"),
projectWithSecrets("", "", "./testdata/config2.txt"),
serviceConfig("myContext2", "never", 2),
)
assert.NilError(t, err)
assert.Assert(t, hash1 != hash2)
}

func projectWithConfigs(configName, configContent, configEnvironmentValue, configFile string) *types.Project {
func projectWithConfigs(configContent, configEnvironmentValue, configFile string) *types.Project {
envName := "myEnv"

if configEnvironmentValue == "" {
Expand All @@ -127,7 +127,7 @@ func projectWithConfigs(configName, configContent, configEnvironmentValue, confi
envName: configEnvironmentValue,
},
Configs: types.Configs{
configName: types.ConfigObjConfig{
"myConfigSource": types.ConfigObjConfig{
Content: configContent,
Environment: envName,
File: configFile,
Expand All @@ -136,7 +136,7 @@ func projectWithConfigs(configName, configContent, configEnvironmentValue, confi
}
}

func projectWithSecrets(secretName, secretContent, secretEnvironmentValue, secretFile string) *types.Project {
func projectWithSecrets(secretContent, secretEnvironmentValue, secretFile string) *types.Project {
envName := "myEnv"

if secretEnvironmentValue == "" {
Expand All @@ -148,7 +148,7 @@ func projectWithSecrets(secretName, secretContent, secretEnvironmentValue, secre
envName: secretEnvironmentValue,
},
Secrets: types.Secrets{
secretName: types.SecretConfig{
"mySecretSource": types.SecretConfig{
Content: secretContent,
Environment: envName,
File: secretFile,
Expand Down
8 changes: 6 additions & 2 deletions pkg/utils/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ func CreateTar(content []byte, config types.FileReferenceConfig, modTime time.Ti
func CreateTarByPath(path string, modTime time.Time) (*bytes.Buffer, error) {
b := new(bytes.Buffer)
tw := tar.NewWriter(b)
defer tw.Close()
defer func() {
_ = tw.Close()
}()

// Walk the directory or file tree at the given path
err := filepath.Walk(path, func(file string, fi os.FileInfo, err error) error {
Expand Down Expand Up @@ -110,7 +112,9 @@ func CreateTarByPath(path string, modTime time.Time) (*bytes.Buffer, error) {
if err != nil {
return err
}
defer f.Close()
defer func() {
_ = f.Close()
}()

if _, err := io.Copy(tw, f); err != nil {
return err
Expand Down

0 comments on commit 9695d57

Please sign in to comment.