aboutsummaryrefslogtreecommitdiffstats
path: root/formats/packfile/read_recaller_impl_test.go
blob: f89171d32fbbd018c7e90cc2529a2b0e79a98edc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package packfile

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"

	"gopkg.in/src-d/go-git.v4/core"

	. "gopkg.in/check.v1"
)

type ReadRecallerImplSuite struct{}

var _ = Suite(&ReadRecallerImplSuite{})

type implFn func([]byte) ReadRecaller

func newStream(data []byte) ReadRecaller {
	buf := bytes.NewBuffer(data)
	return NewStream(buf)
}

func newSeekable(data []byte) ReadRecaller {
	buf := bytes.NewReader(data)
	return NewSeekable(buf)
}

func (s *ReadRecallerImplSuite) TestRead(c *C) {
	for _, impl := range []struct {
		id    string
		newFn implFn
	}{
		{id: "stream", newFn: newStream},
		{id: "seekable", newFn: newSeekable},
	} {
		com := Commentf("implementation %s", impl.id)
		data := []byte{0, 1, 2, 3, 4, 5, 7, 8, 9, 10}
		sr := impl.newFn(data)
		all := make([]byte, 0, len(data))

		for len(all) < len(data) {
			tmp := make([]byte, 3)
			nr, err := sr.Read(tmp)
			c.Assert(err, IsNil, com)
			all = append(all, tmp[:nr]...)
		}
		c.Assert(data, DeepEquals, all, com)
	}
}

func (s *ReadRecallerImplSuite) TestReadbyte(c *C) {
	for _, impl := range []struct {
		id    string
		newFn implFn
	}{
		{id: "stream", newFn: newStream},
		{id: "seekable", newFn: newSeekable},
	} {
		com := Commentf("implementation %s", impl.id)
		data := []byte{0, 1, 2, 3, 4, 5, 7, 8, 9, 10}
		sr := impl.newFn(data)
		all := make([]byte, 0, len(data))

		for len(all) < len(data) {
			b, err := sr.ReadByte()
			c.Assert(err, IsNil, com)
			all = append(all, b)
		}
		c.Assert(data, DeepEquals, all, com)
	}
}

func (s *ReadRecallerImplSuite) TestOffsetWithRead(c *C) {
	for _, impl := range []struct {
		id    string
		newFn implFn
	}{
		{id: "stream", newFn: newStream},
		{id: "seekable", newFn: newSeekable},
	} {
		com := Commentf("implementation %s", impl.id)
		data := []byte{0, 1, 2, 3, 4, 5, 7, 8, 9, 10}
		sr := impl.newFn(data)
		all := make([]byte, 0, len(data))

		for len(all) < len(data) {
			tmp := make([]byte, 3)
			nr, err := sr.Read(tmp)
			c.Assert(err, IsNil, com)
			all = append(all, tmp[:nr]...)

			off, err := sr.Offset()
			c.Assert(err, IsNil, com)
			c.Assert(off, Equals, int64(len(all)), com)
		}
	}
}

func (s *ReadRecallerImplSuite) TestOffsetWithReadByte(c *C) {
	for _, impl := range []struct {
		id    string
		newFn implFn
	}{
		{id: "stream", newFn: newStream},
		{id: "seekable", newFn: newSeekable},
	} {
		com := Commentf("implementation %s", impl.id)
		data := []byte{0, 1, 2, 3, 4, 5, 7, 8, 9, 10}
		sr := impl.newFn(data)
		all := make([]byte, 0, len(data))

		for len(all) < len(data) {
			b, err := sr.ReadByte()
			c.Assert(err, IsNil, com)
			all = append(all, b)

			off, err := sr.Offset()
			c.Assert(err, IsNil, com)
			c.Assert(off, Equals, int64(len(all)), com)
		}
	}
}

func (s *ReadRecallerImplSuite) TestRememberRecall(c *C) {
	packfile := "fixtures/spinnaker-spinnaker.pack"
	f, err := os.Open(packfile)
	c.Assert(err, IsNil)
	defer func() {
		err = f.Close()
		c.Assert(err, IsNil)
	}()

	data, err := ioutil.ReadAll(f)
	c.Assert(err, IsNil)

	for _, impl := range []struct {
		id    string
		newFn implFn
	}{
		{id: "stream", newFn: newStream},
		{id: "seekable", newFn: newSeekable},
	} {
		sr := impl.newFn(data)
		for i, test := range [...]struct {
			off    int64
			obj    core.Object
			err    string // error regexp
			ignore string // ignore this test for this implementation
		}{
			{
				off: 12,
				obj: newObject(core.CommitObject, []byte("tree 44a1cdf21c791867c51caad8f1b77e6baee6f462\nparent 87fe6e7c6b1b89519fe3a03a8961c5aa14d4cc68\nparent 9244ee648182b91a63d8cc4cbe4b9ac2a27c0492\nauthor Matt Duftler <duftler@google.com> 1448290941 -0500\ncommitter Matt Duftler <duftler@google.com> 1448290941 -0500\n\nMerge pull request #615 from ewiseblatt/create_dev\n\nPreserve original credentials of spinnaker-local.yml when transforming it.")),
			}, {
				off: 3037,
				obj: newObject(core.TagObject, []byte("object e0005f50e22140def60260960b21667f1fdfff80\ntype commit\ntag v0.10.0\ntagger cfieber <cfieber@netflix.com> 1447687536 -0800\n\nRelease of 0.10.0\n\n- e0005f50e22140def60260960b21667f1fdfff80: Merge pull request #553 from ewiseblatt/rendezvous\n- e1a2b26b784179e6903a7ae967c037c721899eba: Wait for cassandra before starting spinnaker\n- c756e09461d071e98b8660818cf42d90c90f2854: Merge pull request #552 from duftler/google-c2d-tweaks\n- 0777fadf4ca6f458d7071de414f9bd5417911037: Fix incorrect config prop names:   s/SPINNAKER_GOOGLE_PROJECT_DEFAULT_REGION/SPINNAKER_GOOGLE_DEFAULT_REGION   s/SPINNAKER_GOOGLE_PROJECT_DEFAULT_ZONE/SPINNAKER_GOOGLE_DEFAULT_ZONE Hardcode profile name in generated ~/.aws/credentials to [default]. Restart all of spinnaker after updating cassandra and reconfiguring spinnaker, instead of just restarting clouddriver.\n- d8d031c1ac45801074418c43424a6f2c0dff642c: Merge pull request #551 from kenzanmedia/fixGroup\n- 626d23075f9e92aad19015f2964c95d45f41fa3a: Put in correct block for public image. Delineate cloud provider.\n")),
			}, {
				off: 157625,
				obj: newObject(core.BlobObject, []byte(".gradle\nbuild/\n*.iml\n.idea\n*.pyc\n*~\n#*\nconfig/spinnaker-local.yml\n.DS_Store\npacker/ami_table.md\npacker/ami_table.json\npacker/example_output.txt")),
			}, {
				off: 1234,
				obj: newObject(core.BlobObject, []byte(".gradle\nbuild/\n*.iml\n.idea\n*.pyc\n*~\n#*\nconfig/spinnaker-local.yml\n.DS_Store\npacker/ami_table.md\npacker/ami_table.json\npacker/example_output.txt")),
				err: "duplicated object: with hash .*",
			}, {
				off:    3037,
				obj:    newObject(core.BlobObject, []byte("")),
				err:    "duplicated object: with offset 3037",
				ignore: "seekable",
				// seekable can not check if the offset has already been added
				// for performance reasons.
			},
		} {
			if test.ignore == impl.id {
				continue
			}
			com := Commentf("subtest %d) implementation %s", i, impl.id)

			err := sr.Remember(test.off, test.obj)
			if test.err != "" {
				c.Assert(err, ErrorMatches, test.err, com)
				continue
			}
			c.Assert(err, IsNil, com)

			result, err := sr.RecallByHash(test.obj.Hash())
			c.Assert(err, IsNil, com)
			c.Assert(result.Hash(), Equals, test.obj.Hash())
			c.Assert(result, DeepEquals, test.obj, com)

			result, err = sr.RecallByOffset(test.off)
			c.Assert(err, IsNil, com)
			c.Assert(result.Hash(), Equals, test.obj.Hash())
			c.Assert(result, DeepEquals, test.obj, com)
		}
	}
}

func (s *ReadRecallerImplSuite) TestRecallByHashErrors(c *C) {
	for _, impl := range []struct {
		id    string
		newFn implFn
	}{
		{id: "stream", newFn: newStream},
		{id: "seekable", newFn: newSeekable},
	} {
		com := Commentf("implementation %s", impl.id)
		sr := impl.newFn([]byte{})
		obj := newObject(core.CommitObject, []byte{})

		_, err := sr.RecallByHash(obj.Hash())
		c.Assert(err, ErrorMatches, ErrCannotRecall.Error()+".*", com)

		err = rememberSomeObjects(sr)
		c.Assert(err, IsNil)

		_, err = sr.RecallByHash(obj.Hash())
		c.Assert(err, ErrorMatches, ErrCannotRecall.Error()+".*", com)
	}
}

func (s *ReadRecallerImplSuite) TestRecallByOffsetErrors(c *C) {
	for _, impl := range []struct {
		id    string
		newFn implFn
	}{
		{id: "stream", newFn: newStream},
		// seekalbe allways recall every object in the packfile
	} {
		com := Commentf("implementation %s", impl.id)
		sr := impl.newFn([]byte{})

		_, err := sr.RecallByOffset(15)
		c.Assert(err, ErrorMatches, ErrCannotRecall.Error()+".*", com)

		err = rememberSomeObjects(sr)
		c.Assert(err, IsNil)

		_, err = sr.RecallByOffset(15)
		c.Assert(err, ErrorMatches, ErrCannotRecall.Error()+".*", com)
	}
}

func rememberSomeObjects(sr ReadRecaller) error {
	for i, init := range [...]struct {
		off int64
		obj core.Object
	}{
		{off: 0, obj: newObject(core.CommitObject, []byte{'a'})},  // 93114cce67ec23976d15199514399203f69cc676
		{off: 10, obj: newObject(core.CommitObject, []byte{'b'})}, // 2bb767097e479f668f0ebdabe88df11337bd8f19
		{off: 20, obj: newObject(core.CommitObject, []byte{'c'})}, // 2f8096005677370e6446541a50e074299d43d468
	} {
		err := sr.Remember(init.off, init.obj)
		if err != nil {
			return fmt.Errorf("cannot ask StreamReader to Remember item %d", i)
		}
	}

	return nil
}

func (s *ReadRecallerImplSuite) TestForgetAll(c *C) {
	for _, impl := range []struct {
		id    string
		newFn implFn
	}{
		{id: "stream", newFn: newStream},
		{id: "seekable", newFn: newSeekable},
	} {
		com := Commentf("implementation %s", impl.id)
		sr := impl.newFn([]byte{})

		err := rememberSomeObjects(sr)
		c.Assert(err, IsNil)

		sr.ForgetAll()

		if impl.id != "seekable" { // for efficiency, seekable always finds objects by offset
			_, err = sr.RecallByOffset(0)
			c.Assert(err, ErrorMatches, ErrCannotRecall.Error()+".*", com)
			_, err = sr.RecallByOffset(10)
			c.Assert(err, ErrorMatches, ErrCannotRecall.Error()+".*", com)
			_, err = sr.RecallByOffset(20)
			c.Assert(err, ErrorMatches, ErrCannotRecall.Error()+".*", com)
		}
		_, err = sr.RecallByHash(core.NewHash("93114cce67ec23976d15199514399203f69cc676"))
		c.Assert(err, ErrorMatches, ErrCannotRecall.Error()+".*", com)
		_, err = sr.RecallByHash(core.NewHash("2bb767097e479f668f0ebdabe88df11337bd8f19"))
		c.Assert(err, ErrorMatches, ErrCannotRecall.Error()+".*", com)
		_, err = sr.RecallByHash(core.NewHash("2f8096005677370e6446541a50e074299d43d468"))
		c.Assert(err, ErrorMatches, ErrCannotRecall.Error()+".*", com)
	}
}