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:
parent
c21be64e1a
commit
572498be56
|
@ -8,7 +8,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/ioutils"
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -109,7 +108,7 @@ func GetSpecific(path string) (*File, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
hash, err := ioutils.HashData(bytes.NewReader(resolv))
|
hash, err := hashData(bytes.NewReader(resolv))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -127,7 +126,7 @@ func GetIfChanged() (*File, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
newHash, err := ioutils.HashData(bytes.NewReader(resolv))
|
newHash, err := hashData(bytes.NewReader(resolv))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -173,7 +172,7 @@ func FilterResolvDNS(resolvConf []byte, ipv6Enabled bool) (*File, error) {
|
||||||
}
|
}
|
||||||
cleanedResolvConf = append(cleanedResolvConf, []byte("\n"+strings.Join(dns, "\n"))...)
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,6 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/ioutils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGet(t *testing.T) {
|
func TestGet(t *testing.T) {
|
||||||
|
@ -21,7 +19,7 @@ func TestGet(t *testing.T) {
|
||||||
if string(resolvConfUtils.Content) != string(resolvConfSystem) {
|
if string(resolvConfUtils.Content) != string(resolvConfSystem) {
|
||||||
t.Fatalf("/etc/resolv.conf and GetResolvConf have different content.")
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,8 +2,6 @@ package ioutils // import "github.com/docker/docker/pkg/ioutils"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/hex"
|
|
||||||
"io"
|
"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
|
// OnEOFReader wraps an io.ReadCloser and a function
|
||||||
// the function will run at the end of file or close the file.
|
// the function will run at the end of file or close the file.
|
||||||
type OnEOFReader struct {
|
type OnEOFReader struct {
|
||||||
|
|
|
@ -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{}
|
type perpetualReader struct{}
|
||||||
|
|
||||||
func (p *perpetualReader) Read(buf []byte) (n int, err error) {
|
func (p *perpetualReader) Read(buf []byte) (n int, err error) {
|
||||||
|
|
Loading…
Reference in New Issue