Skip to content

Commit

Permalink
refactor:return nil if parse failed;modify build test case;umount tmp…
Browse files Browse the repository at this point in the history
…dir if build failed (sealerio#326)
  • Loading branch information
kakaZhou719 authored Jun 17, 2021
1 parent 6fefd3b commit c576e36
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
9 changes: 6 additions & 3 deletions build/local_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,16 @@ func (l *LocalBuilder) mountAndExecLayer(layer *v1.Layer, tempTarget, tempUpper
if err != nil {
return fmt.Errorf("failed to mount target %s:%v", tempTarget, err)
}
defer func() {
if err = driver.Unmount(tempTarget); err != nil {
logger.Warn(fmt.Errorf("failed to umount %s:%v", tempTarget, err))
}
}()

err = l.execLayer(layer, tempTarget)
if err != nil {
return fmt.Errorf("failed to exec layer %v:%v", layer, err)
}
if err = driver.Unmount(tempTarget); err != nil {
return fmt.Errorf("failed to umount %s:%v", tempTarget, err)
}
return nil
}

Expand Down
1 change: 1 addition & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (p *Parser) Parse(kubeFile []byte, name string) *v1.Image {
layerType, layerValue, err := decodeLine(text)
if err != nil {
logger.Warn("decode kubeFile line failed, err: %v", err)
return nil
}
if layerType == "" {
continue
Expand Down
12 changes: 6 additions & 6 deletions test/sealer_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var _ = Describe("sealer build", func() {
It("specific the base rootfs that do not exist", func() {
//base rootfs not exist
imageName := build.GetImageNameTemplate("no_rootfs")
err := testhelper.WriteFile(tempFile, []byte("from abc\ncopy . ."))
err := testhelper.WriteFile(tempFile, []byte("FROM abc\nCOPY . ."))
Expect(err).NotTo(HaveOccurred())
cmd := build.NewArgsOfBuild().
SetKubeFile(tempFile).
Expand All @@ -57,14 +57,14 @@ var _ = Describe("sealer build", func() {
Build()
sess, err := testhelper.Start(cmd)
Expect(err).NotTo(HaveOccurred())
Eventually(sess).Should(Exit(2))
Eventually(sess).Should(Exit(1))
Expect(build.CheckIsImageExist(imageName)).ShouldNot(BeTrue())
})

It("copy src that do not exist", func() {
//copy: copy src not exist;
imageName := build.GetImageNameTemplate("no_src_copy")
err := testhelper.WriteFile(tempFile, []byte("from sealer-io/kubernetes:v1.19.9\ncopy abc123 ."))
err := testhelper.WriteFile(tempFile, []byte("FROM sealer-io/kubernetes:v1.19.9\nCOPY abc123 ."))
Expect(err).NotTo(HaveOccurred())
cmd := build.NewArgsOfBuild().
SetKubeFile(tempFile).
Expand All @@ -74,14 +74,14 @@ var _ = Describe("sealer build", func() {
Build()
sess, err := testhelper.Start(cmd)
Expect(err).NotTo(HaveOccurred())
Eventually(sess).Should(Exit(2))
Eventually(sess).Should(Exit(1))
Expect(build.CheckIsImageExist(imageName)).ShouldNot(BeTrue())
})

It("exec cmd that do not exist in system", func() {
//run&cmd: exec cmd not exist
imageName := build.GetImageNameTemplate("no_cmd_run")
err := testhelper.WriteFile(tempFile, []byte("from sealer-io/kubernetes:v1.19.9\ncmd abc ."))
err := testhelper.WriteFile(tempFile, []byte("FROM sealer-io/kubernetes:v1.19.9\nCMD abc ."))
Expect(err).NotTo(HaveOccurred())
cmd := build.NewArgsOfBuild().
SetKubeFile(tempFile).
Expand All @@ -91,7 +91,7 @@ var _ = Describe("sealer build", func() {
Build()
sess, err := testhelper.Start(cmd)
Expect(err).NotTo(HaveOccurred())
Eventually(sess).Should(Exit(2))
Eventually(sess).Should(Exit(1))
Expect(build.CheckIsImageExist(imageName)).ShouldNot(BeTrue())
})
})
Expand Down

0 comments on commit c576e36

Please sign in to comment.