-
Notifications
You must be signed in to change notification settings - Fork 85
add unit test for ValidateLayout function #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
image/manifest_test.go
Outdated
| } | ||
| } | ||
|
|
||
| func createTarFile(name string, list []tarContent) (descriptor, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use tar files for image-layout as well. This helper is about creating layers, so it should probably be createLayer or similar to distinguish from image-layout tooling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And we have a public Descriptor type in github.com/opencontainers/image-spec/specs-go. Can we return that instead of the private, local descriptor (one step closer to being able to drop the local descriptor ;)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And we have a public Descriptor type in github.com/opencontainers/image-spec/specs-go. Can we return that instead of the private, local descriptor (one step closer to being able to drop the local descriptor ;)?
if it doesn't need to call any of the method attached to descriptor - then yes I would use the public Descriptor as well - otherwise let's keep descriptor for now
image/manifest_test.go
Outdated
| if _, err = io.Copy(tw, bytes.NewReader(content.b)); err != nil { | ||
| tw.Close() | ||
| gw.Close() | ||
| f.Close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a job for defer ;).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps you avoid it because you want to ensure the file is completed and closed before you start hashing it. You could jump through some hoops to get the defered functions to fire, but it's probably easier and more efficient to use something like:
var file *os.File
file, err = os.Create(name) // + err checking
defer file.Close()
hash := sha256.New()
counter := NewCountingWriter() // TODO: write this up. It counts bytes written to it and discards the content
hashingWriter := io.MultiWriter(file, hash, counter)
gzipWriter := gzip.NewWriter(hashingWriter)
defer gzipWriter.Close()
// create tarWriter and writer content
err = tarWriter.Close() // + err checking
err = gzipWriter.Close() // + err checking
return &specs.Descriptor{
MediaType: v1.MediaTypeImageLayer,
Digest: fmt.Sprintf("sha256:%x", h.Sum(nil)),
Size: counter.Size,
}, nilOn success, the deferred closes might error out (because we've already successfully closed the files). But who cares? We successfully closed the files. On error, we're ignoring possible Close errors. But who cares? We're already returning an error.
| err = os.MkdirAll(filepath.Join(root, "refs"), 0700) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This image-layout directory initialization also duplicates existing logic in manifest_test.go. Can we pull it out into createDirectoryImageLayout or some such?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it looks like manifest_test.go is only stubbing in the blobs directory, but not setting up the rest of image-layout. It would certainly help if we had a createDirectoryImageLayout with the more complete logic you have here so we could have a full image-layout there instead of its current partial format.
image/image_test.go
Outdated
| return descriptor{}, err | ||
| } | ||
|
|
||
| err = os.Rename(layerPath, filepath.Join(root, "blobs", "sha256", desc.Digest)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
manifest_test.go is also doing similar post-creation renaming. Can we use createLayerFile there as well?
image/image_test.go
Outdated
| t.Fatal(err) | ||
| } | ||
|
|
||
| err = ValidateLayout(root, []string{"latest"}, nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit odd to have latest explicitly here, but to have it implicit in the createRefFile call earlier. Can we have createRefFile(root string, ref string, descriptor *specs.Descriptor) err?
|
On Fri, Sep 16, 2016 at 01:01:27AM -0700, Antonio Murdaca wrote:
I think having public and private descriptor types is more |
|
I improve the patch according to reviewer's comments.
PTAL. |
image/manifest_test.go
Outdated
| // generate sha256 hash | ||
| h := sha256.New() | ||
| file, err := os.Open(tarfile) | ||
| err = os.MkdirAll(filepath.Join(root, "blobs", "sha256"), 0700) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use createImageLayoutBundle here to get oci-layout, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems some inappropriate to use createImageLayoutBundle here.
the TestUnpackLayer prerequisite is just layer file with right path and its descriptor. It even needn't real manifest file. So it is enough to make layer dir and create layer file.
If TestUnpackLayer function is missing some test case( I read testManifest.unpack() function, and think TestUnpackLayer is fine), it is another story.
if it is correct?
|
|
||
| // generate sha256 hash | ||
| hash := sha256.New() | ||
| size, err := io.Copy(hash, file) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a second pass through a file that you just streamed to disk. It would be more efficient to hash and size while you were streaming to disk, but as long as this is just for the test suite a second pass is probably fine.
image/image_test.go
Outdated
| } | ||
|
|
||
| func createRefFile(root string, mft descriptor) error { | ||
| refpath := filepath.Join(root, "refs", "latest") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"latest" → refTag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
|
On Sat, Sep 17, 2016 at 11:16:24PM -0700, xiekeyang wrote:
…/blobs/sha256/… is part of an image-layout, but not a complete |
I'm all for you. But I honestly dislike to fix other bugs on this patch. |
|
On Sun, Sep 18, 2016 at 01:09:05AM -0700, xiekeyang wrote:
It's all going to end up in the same stomach ;). My preference would |
|
@wking |
|
cc @runcom @stevvooe @jonboulle @s-urbaniak @wking |
|
On Sun, Sep 18, 2016 at 02:28:09AM -0700, xiekeyang wrote:
It still needs to pull layers from the image-layout directory, so I |
Signed-off-by: xiekeyang <[email protected]>
|
On Thu, Sep 22, 2016 at 01:19:54AM -0700, xiekeyang wrote:
f3064f0 → e85687e just dropped all of your changes from |
|
@wking Right. hopefully this image test will stand. And I'll submit manifest test. |
|
PTAL. |
|
LGTM |
Outsource this stuff to avoid duplication of effort. I have no idea why newDescriptor was returning just the hex and createHashedBlob (its only consumer) was fixing that (by the "Normalize the hashed digest" comment). But that is how the image tests have worked since they landed in e85687e (unit test for layout validation, 2016-09-22, opencontainers#4). This commit drops the "Normalization" hack and fixes newDescriptor to actually return a valid digest. Signed-off-by: W. Trevor King <[email protected]>
migrate patch from opencontainers/image-spec#310
cc @runcom @vbatts @wking @stevvooe