Skip to content

Commit 572498b

Browse files
committed
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 <[email protected]>
1 parent c21be64 commit 572498b

6 files changed

Lines changed: 39 additions & 31 deletions

File tree

libnetwork/resolvconf/resolvconf.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strings"
99
"sync"
1010

11-
"github.com/docker/docker/pkg/ioutils"
1211
"github.com/sirupsen/logrus"
1312
)
1413

@@ -109,7 +108,7 @@ func GetSpecific(path string) (*File, error) {
109108
if err != nil {
110109
return nil, err
111110
}
112-
hash, err := ioutils.HashData(bytes.NewReader(resolv))
111+
hash, err := hashData(bytes.NewReader(resolv))
113112
if err != nil {
114113
return nil, err
115114
}
@@ -127,7 +126,7 @@ func GetIfChanged() (*File, error) {
127126
if err != nil {
128127
return nil, err
129128
}
130-
newHash, err := ioutils.HashData(bytes.NewReader(resolv))
129+
newHash, err := hashData(bytes.NewReader(resolv))
131130
if err != nil {
132131
return nil, err
133132
}
@@ -173,7 +172,7 @@ func FilterResolvDNS(resolvConf []byte, ipv6Enabled bool) (*File, error) {
173172
}
174173
cleanedResolvConf = append(cleanedResolvConf, []byte("\n"+strings.Join(dns, "\n"))...)
175174
}
176-
hash, err := ioutils.HashData(bytes.NewReader(cleanedResolvConf))
175+
hash, err := hashData(bytes.NewReader(cleanedResolvConf))
177176
if err != nil {
178177
return nil, err
179178
}
@@ -287,7 +286,7 @@ func Build(path string, dns, dnsSearch, dnsOptions []string) (*File, error) {
287286
}
288287
}
289288

290-
hash, err := ioutils.HashData(bytes.NewReader(content.Bytes()))
289+
hash, err := hashData(bytes.NewReader(content.Bytes()))
291290
if err != nil {
292291
return nil, err
293292
}

libnetwork/resolvconf/resolvconf_linux_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"io/ioutil"
66
"os"
77
"testing"
8-
9-
"github.com/docker/docker/pkg/ioutils"
108
)
119

1210
func TestGet(t *testing.T) {
@@ -21,7 +19,7 @@ func TestGet(t *testing.T) {
2119
if string(resolvConfUtils.Content) != string(resolvConfSystem) {
2220
t.Fatalf("/etc/resolv.conf and GetResolvConf have different content.")
2321
}
24-
hashSystem, err := ioutils.HashData(bytes.NewReader(resolvConfSystem))
22+
hashSystem, err := hashData(bytes.NewReader(resolvConfSystem))
2523
if err != nil {
2624
t.Fatal(err)
2725
}

libnetwork/resolvconf/utils.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package resolvconf
2+
3+
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
"io"
7+
)
8+
9+
// hashData returns the sha256 sum of src.
10+
func hashData(src io.Reader) (string, error) {
11+
h := sha256.New()
12+
if _, err := io.Copy(h, src); err != nil {
13+
return "", err
14+
}
15+
return "sha256:" + hex.EncodeToString(h.Sum(nil)), nil
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package resolvconf
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func TestHashData(t *testing.T) {
9+
reader := strings.NewReader("hash-me")
10+
actual, err := hashData(reader)
11+
if err != nil {
12+
t.Fatal(err)
13+
}
14+
expected := "sha256:4d11186aed035cc624d553e10db358492c84a7cd6b9670d92123c144930450aa"
15+
if actual != expected {
16+
t.Fatalf("Expecting %s, got %s", expected, actual)
17+
}
18+
}

pkg/ioutils/readers.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package ioutils // import "github.com/docker/docker/pkg/ioutils"
22

33
import (
44
"context"
5-
"crypto/sha256"
6-
"encoding/hex"
75
"io"
86
)
97

@@ -49,15 +47,6 @@ func NewReaderErrWrapper(r io.Reader, closer func()) io.Reader {
4947
}
5048
}
5149

52-
// HashData returns the sha256 sum of src.
53-
func HashData(src io.Reader) (string, error) {
54-
h := sha256.New()
55-
if _, err := io.Copy(h, src); err != nil {
56-
return "", err
57-
}
58-
return "sha256:" + hex.EncodeToString(h.Sum(nil)), nil
59-
}
60-
6150
// OnEOFReader wraps an io.ReadCloser and a function
6251
// the function will run at the end of file or close the file.
6352
type OnEOFReader struct {

pkg/ioutils/readers_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,6 @@ func TestReaderErrWrapperRead(t *testing.T) {
5858
}
5959
}
6060

61-
func TestHashData(t *testing.T) {
62-
reader := strings.NewReader("hash-me")
63-
actual, err := HashData(reader)
64-
if err != nil {
65-
t.Fatal(err)
66-
}
67-
expected := "sha256:4d11186aed035cc624d553e10db358492c84a7cd6b9670d92123c144930450aa"
68-
if actual != expected {
69-
t.Fatalf("Expecting %s, got %s", expected, actual)
70-
}
71-
}
72-
7361
type perpetualReader struct{}
7462

7563
func (p *perpetualReader) Read(buf []byte) (n int, err error) {

0 commit comments

Comments
 (0)