move pkg/ioutils.HashData() to libnetwork/resolvconf

It's the only location it's used, so we might as well move it there.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2021-08-18 13:23:06 +02:00
parent c21be64e1a
commit 572498be56
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
6 changed files with 39 additions and 31 deletions

View File

@ -8,7 +8,6 @@ import (
"strings"
"sync"
"github.com/docker/docker/pkg/ioutils"
"github.com/sirupsen/logrus"
)
@ -109,7 +108,7 @@ func GetSpecific(path string) (*File, error) {
if err != nil {
return nil, err
}
hash, err := ioutils.HashData(bytes.NewReader(resolv))
hash, err := hashData(bytes.NewReader(resolv))
if err != nil {
return nil, err
}
@ -127,7 +126,7 @@ func GetIfChanged() (*File, error) {
if err != nil {
return nil, err
}
newHash, err := ioutils.HashData(bytes.NewReader(resolv))
newHash, err := hashData(bytes.NewReader(resolv))
if err != nil {
return nil, err
}
@ -173,7 +172,7 @@ func FilterResolvDNS(resolvConf []byte, ipv6Enabled bool) (*File, error) {
}
cleanedResolvConf = append(cleanedResolvConf, []byte("\n"+strings.Join(dns, "\n"))...)
}
hash, err := ioutils.HashData(bytes.NewReader(cleanedResolvConf))
hash, err := hashData(bytes.NewReader(cleanedResolvConf))
if err != nil {
return nil, err
}
@ -287,7 +286,7 @@ func Build(path string, dns, dnsSearch, dnsOptions []string) (*File, error) {
}
}
hash, err := ioutils.HashData(bytes.NewReader(content.Bytes()))
hash, err := hashData(bytes.NewReader(content.Bytes()))
if err != nil {
return nil, err
}

View File

@ -5,8 +5,6 @@ import (
"io/ioutil"
"os"
"testing"
"github.com/docker/docker/pkg/ioutils"
)
func TestGet(t *testing.T) {
@ -21,7 +19,7 @@ func TestGet(t *testing.T) {
if string(resolvConfUtils.Content) != string(resolvConfSystem) {
t.Fatalf("/etc/resolv.conf and GetResolvConf have different content.")
}
hashSystem, err := ioutils.HashData(bytes.NewReader(resolvConfSystem))
hashSystem, err := hashData(bytes.NewReader(resolvConfSystem))
if err != nil {
t.Fatal(err)
}

View File

@ -0,0 +1,16 @@
package resolvconf
import (
"crypto/sha256"
"encoding/hex"
"io"
)
// hashData returns the sha256 sum of src.
func hashData(src io.Reader) (string, error) {
h := sha256.New()
if _, err := io.Copy(h, src); err != nil {
return "", err
}
return "sha256:" + hex.EncodeToString(h.Sum(nil)), nil
}

View File

@ -0,0 +1,18 @@
package resolvconf
import (
"strings"
"testing"
)
func TestHashData(t *testing.T) {
reader := strings.NewReader("hash-me")
actual, err := hashData(reader)
if err != nil {
t.Fatal(err)
}
expected := "sha256:4d11186aed035cc624d553e10db358492c84a7cd6b9670d92123c144930450aa"
if actual != expected {
t.Fatalf("Expecting %s, got %s", expected, actual)
}
}

View File

@ -2,8 +2,6 @@ package ioutils // import "github.com/docker/docker/pkg/ioutils"
import (
"context"
"crypto/sha256"
"encoding/hex"
"io"
)
@ -49,15 +47,6 @@ func NewReaderErrWrapper(r io.Reader, closer func()) io.Reader {
}
}
// HashData returns the sha256 sum of src.
func HashData(src io.Reader) (string, error) {
h := sha256.New()
if _, err := io.Copy(h, src); err != nil {
return "", err
}
return "sha256:" + hex.EncodeToString(h.Sum(nil)), nil
}
// OnEOFReader wraps an io.ReadCloser and a function
// the function will run at the end of file or close the file.
type OnEOFReader struct {

View File

@ -58,18 +58,6 @@ func TestReaderErrWrapperRead(t *testing.T) {
}
}
func TestHashData(t *testing.T) {
reader := strings.NewReader("hash-me")
actual, err := HashData(reader)
if err != nil {
t.Fatal(err)
}
expected := "sha256:4d11186aed035cc624d553e10db358492c84a7cd6b9670d92123c144930450aa"
if actual != expected {
t.Fatalf("Expecting %s, got %s", expected, actual)
}
}
type perpetualReader struct{}
func (p *perpetualReader) Read(buf []byte) (n int, err error) {