github.com/benchkram/bob@v0.0.0-20240314204020-b7a57f2f9be9/test/e2e/artifacts/artifacts_extraction_test.go (about)

     1  package artifactstest
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  
     7  	"github.com/benchkram/bob/bob"
     8  	"github.com/benchkram/bob/bob/playbook"
     9  	"github.com/benchkram/bob/bobtask"
    10  	"github.com/benchkram/bob/pkg/dockermobyutil"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Test artifact creation and extraction", func() {
    16  	Context("in a fresh playground", func() {
    17  
    18  		It("should initialize bob playground", func() {
    19  			Expect(bob.CreatePlayground(bob.PlaygroundOptions{Dir: dir})).NotTo(HaveOccurred())
    20  		})
    21  
    22  		It("assure to be in the test root dir", func() {
    23  			wd, err := os.Getwd()
    24  			Expect(err).NotTo(HaveOccurred())
    25  			Expect(wd).To(Equal(dir))
    26  		})
    27  
    28  		It("should build", func() {
    29  			ctx := context.Background()
    30  			err := b.Build(ctx, bob.BuildTargetwithdirsTargetName)
    31  			Expect(err).NotTo(HaveOccurred())
    32  		})
    33  
    34  		var artifactID string
    35  		It("should check that exactly one artifact was created", func() {
    36  			fs, err := os.ReadDir(artifactDir)
    37  			Expect(err).NotTo(HaveOccurred())
    38  			Expect(len(fs)).To(Equal(1))
    39  			artifactID = fs[0].Name()
    40  			println(artifactID)
    41  		})
    42  
    43  		It("inspect artifact", func() {
    44  			artifact, _, err := artifactStore.GetArtifact(context.Background(), artifactID)
    45  			Expect(err).NotTo(HaveOccurred())
    46  			description, err := bobtask.ArtifactInspectFromReader(artifact)
    47  			Expect(err).NotTo(HaveOccurred())
    48  
    49  			println(description)
    50  		})
    51  
    52  		It("cleanup build/target dir", func() {
    53  			err := os.RemoveAll(".bbuild")
    54  			Expect(err).NotTo(HaveOccurred())
    55  		})
    56  
    57  		It("extract artifact from store on rebuild", func() {
    58  			state, err := buildTask(b, bob.BuildTargetwithdirsTargetName)
    59  			Expect(err).NotTo(HaveOccurred())
    60  			Expect(state.State()).To(Equal(playbook.StateNoRebuildRequired))
    61  		})
    62  
    63  		It("cleanup", func() {
    64  			err := b.CleanBuildInfoStore()
    65  			Expect(err).NotTo(HaveOccurred())
    66  
    67  			err = b.CleanLocalStore()
    68  			Expect(err).NotTo(HaveOccurred())
    69  
    70  			err = reset()
    71  			Expect(err).NotTo(HaveOccurred())
    72  		})
    73  
    74  	})
    75  })
    76  
    77  var _ = Describe("Test artifact creation and extraction from docker targets", func() {
    78  	Context("in a fresh playground", func() {
    79  
    80  		mobyClient, err := dockermobyutil.NewRegistryClient()
    81  		Expect(err).NotTo(HaveOccurred())
    82  
    83  		It("should initialize bob playground", func() {
    84  			Expect(bob.CreatePlayground(bob.PlaygroundOptions{Dir: dir})).NotTo(HaveOccurred())
    85  		})
    86  
    87  		It("should assure test image is not in docker registry", func() {
    88  			exists, err := mobyClient.ImageExists(bob.BuildTargetBobTestImage)
    89  			Expect(err).NotTo(HaveOccurred())
    90  
    91  			if exists {
    92  				err = mobyClient.ImageRemove(bob.BuildTargetBobTestImage)
    93  				Expect(err).NotTo(HaveOccurred())
    94  			}
    95  		})
    96  
    97  		It("assure to be in the test root dir", func() {
    98  			wd, err := os.Getwd()
    99  			Expect(err).NotTo(HaveOccurred())
   100  			Expect(wd).To(Equal(dir))
   101  		})
   102  
   103  		It("should build", func() {
   104  			ctx := context.Background()
   105  			err := b.Build(ctx, bob.BuildTargetDockerImageName)
   106  			Expect(err).NotTo(HaveOccurred())
   107  		})
   108  
   109  		It("should check that the docker image was created correctly", func() {
   110  			exists, err := mobyClient.ImageExists(bob.BuildTargetBobTestImage)
   111  			Expect(err).NotTo(HaveOccurred())
   112  			Expect(exists).To(BeTrue())
   113  		})
   114  
   115  		var artifactID string
   116  		It("should check that exactly one artifact was created", func() {
   117  			fs, err := os.ReadDir(artifactDir)
   118  			Expect(err).NotTo(HaveOccurred())
   119  			Expect(len(fs)).To(Equal(1))
   120  			artifactID = fs[0].Name()
   121  			println(artifactID)
   122  		})
   123  
   124  		It("inspect artifact", func() {
   125  			artifact, _, err := artifactStore.GetArtifact(context.Background(), artifactID)
   126  			Expect(err).NotTo(HaveOccurred())
   127  			_, err = bobtask.ArtifactInspectFromReader(artifact)
   128  			Expect(err).NotTo(HaveOccurred())
   129  		})
   130  
   131  		It("should remove test image from docker registry", func() {
   132  			exists, err := mobyClient.ImageExists(bob.BuildTargetBobTestImage)
   133  			Expect(err).NotTo(HaveOccurred())
   134  
   135  			if exists {
   136  				err = mobyClient.ImageRemove(bob.BuildTargetBobTestImage)
   137  				Expect(err).NotTo(HaveOccurred())
   138  			}
   139  		})
   140  
   141  		It("should extract artifact from store on rebuild", func() {
   142  			state, err := buildTask(b, bob.BuildTargetDockerImageName)
   143  			Expect(err).NotTo(HaveOccurred())
   144  			Expect(state.State()).To(Equal(playbook.StateNoRebuildRequired))
   145  		})
   146  
   147  		It("should check that the docker image was created correctly", func() {
   148  			// TODO: check tasks for cached !@!
   149  			exists, err := mobyClient.ImageExists(bob.BuildTargetBobTestImage)
   150  			Expect(err).NotTo(HaveOccurred())
   151  			Expect(exists).To(BeTrue())
   152  		})
   153  
   154  		It("cleanup", func() {
   155  			err := b.CleanBuildInfoStore()
   156  			Expect(err).NotTo(HaveOccurred())
   157  
   158  			err = b.CleanLocalStore()
   159  			Expect(err).NotTo(HaveOccurred())
   160  
   161  			err = reset()
   162  			Expect(err).NotTo(HaveOccurred())
   163  
   164  			exists, err := mobyClient.ImageExists(bob.BuildTargetBobTestImage)
   165  			Expect(err).NotTo(HaveOccurred())
   166  
   167  			if exists {
   168  				err = mobyClient.ImageRemove(bob.BuildTargetBobTestImage)
   169  				Expect(err).NotTo(HaveOccurred())
   170  			}
   171  		})
   172  
   173  	})
   174  })