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

     1  package artifactstest
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/benchkram/bob/bob"
     9  	"github.com/benchkram/bob/bob/playbook"
    10  	"github.com/benchkram/bob/pkg/dockermobyutil"
    11  	"github.com/benchkram/bob/pkg/file"
    12  	"github.com/benchkram/errz"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  // file targets
    18  var _ = Describe("Test artifact and target invalidation", func() {
    19  	Context("in a fresh playground", func() {
    20  
    21  		It("should initialize bob playground", func() {
    22  			Expect(bob.CreatePlayground(bob.PlaygroundOptions{Dir: dir})).NotTo(HaveOccurred())
    23  		})
    24  
    25  		It("should build", func() {
    26  			ctx := context.Background()
    27  			err := b.Build(ctx, "build")
    28  			errz.Log(err)
    29  			Expect(err).NotTo(HaveOccurred())
    30  		})
    31  
    32  		var artifactID string
    33  		It("should check that exactly one artifact was created", func() {
    34  			fs, err := os.ReadDir(artifactDir)
    35  			Expect(err).NotTo(HaveOccurred())
    36  			Expect(len(fs)).To(Equal(1))
    37  			artifactID = fs[0].Name()
    38  		})
    39  
    40  		// 5)
    41  		It("should not rebuild but update the target", func() {
    42  			err := artifactRemove(artifactID)
    43  			Expect(err).NotTo(HaveOccurred())
    44  
    45  			//time.Sleep(1 * time.Minute)
    46  
    47  			state, err := buildTask(b, "build")
    48  			Expect(err).NotTo(HaveOccurred())
    49  			Expect(state.State()).To(Equal(playbook.StateCompleted))
    50  
    51  			exists, err := artifactExists(artifactID)
    52  			Expect(err).NotTo(HaveOccurred())
    53  			Expect(exists).To(BeTrue())
    54  		})
    55  
    56  		// 6)
    57  		It("should not rebuild but unpack from local artifact", func() {
    58  			state, err := buildTask(b, "build")
    59  			Expect(err).NotTo(HaveOccurred())
    60  			Expect(state.State()).To(Equal(playbook.StateNoRebuildRequired))
    61  		})
    62  
    63  		// 7)
    64  		It("should rebuild and update local artifact", func() {
    65  			err := artifactRemove(artifactID)
    66  			Expect(err).NotTo(HaveOccurred())
    67  			err = targetChange(filepath.Join(dir, "run"))
    68  			Expect(err).NotTo(HaveOccurred())
    69  
    70  			state, err := buildTask(b, "build")
    71  			Expect(err).NotTo(HaveOccurred())
    72  			Expect(state.State()).To(Equal(playbook.StateCompleted))
    73  
    74  			exists, err := artifactExists(artifactID)
    75  			Expect(err).NotTo(HaveOccurred())
    76  			Expect(exists).To(BeTrue())
    77  		})
    78  
    79  		// 8)
    80  		It("should update target from local artifact", func() {
    81  			err := targetChange(filepath.Join(dir, "run"))
    82  			Expect(err).NotTo(HaveOccurred())
    83  
    84  			state, err := buildTask(b, "build")
    85  			Expect(err).NotTo(HaveOccurred())
    86  			Expect(state.State()).To(Equal(playbook.StateNoRebuildRequired))
    87  
    88  			file.Exists(filepath.Join(dir, "run"))
    89  		})
    90  
    91  		It("cleanup", func() {
    92  			err := b.CleanBuildInfoStore()
    93  			Expect(err).NotTo(HaveOccurred())
    94  
    95  			err = b.CleanLocalStore()
    96  			Expect(err).NotTo(HaveOccurred())
    97  
    98  			err = reset()
    99  			Expect(err).NotTo(HaveOccurred())
   100  		})
   101  
   102  	})
   103  })
   104  
   105  // docker targets
   106  var _ = Describe("Test artifact and docker-target invalidation", func() {
   107  	Context("in a fresh playground", func() {
   108  
   109  		mobyClient, err := dockermobyutil.NewRegistryClient()
   110  		Expect(err).NotTo(HaveOccurred())
   111  
   112  		It("should initialize bob playground", func() {
   113  			Expect(bob.CreatePlayground(bob.PlaygroundOptions{Dir: dir})).NotTo(HaveOccurred())
   114  		})
   115  
   116  		It("should build", func() {
   117  			ctx := context.Background()
   118  			err := b.Build(ctx, bob.BuildTargetDockerImageName)
   119  			Expect(err).NotTo(HaveOccurred())
   120  		})
   121  
   122  		var artifactID string
   123  		It("should check that exactly one artifact was created", func() {
   124  			fs, err := os.ReadDir(artifactDir)
   125  			Expect(err).NotTo(HaveOccurred())
   126  			Expect(len(fs)).To(Equal(1))
   127  			artifactID = fs[0].Name()
   128  		})
   129  
   130  		// 5)
   131  		It("should not rebuild but update the target", func() {
   132  			err := artifactRemove(artifactID)
   133  			Expect(err).NotTo(HaveOccurred())
   134  
   135  			state, err := buildTask(b, bob.BuildTargetDockerImageName)
   136  			Expect(err).NotTo(HaveOccurred())
   137  			Expect(state.State()).To(Equal(playbook.StateCompleted))
   138  
   139  			exists, err := artifactExists(artifactID)
   140  			Expect(err).NotTo(HaveOccurred())
   141  			Expect(exists).To(BeTrue())
   142  		})
   143  
   144  		// 6)
   145  		It("should not rebuild but extract from local artifact", func() {
   146  			state, err := buildTask(b, bob.BuildTargetDockerImageName)
   147  			Expect(err).NotTo(HaveOccurred())
   148  			Expect(state.State()).To(Equal(playbook.StateNoRebuildRequired))
   149  		})
   150  
   151  		// 7)
   152  		It("should rebuild and update local artifact", func() {
   153  			err := artifactRemove(artifactID)
   154  			Expect(err).NotTo(HaveOccurred())
   155  
   156  			// alter the target by tagging it with a different image
   157  			_, err = buildTask(b, bob.BuildTargetDockerImagePlusName)
   158  			Expect(err).NotTo(HaveOccurred())
   159  			err = mobyClient.ImageTag(
   160  				bob.BuildTargetBobTestImagePlus,
   161  				bob.BuildTargetBobTestImage,
   162  			)
   163  			Expect(err).NotTo(HaveOccurred())
   164  
   165  			state, err := buildTask(b, bob.BuildTargetDockerImageName)
   166  			Expect(err).NotTo(HaveOccurred())
   167  			Expect(state.State()).To(Equal(playbook.StateCompleted))
   168  
   169  			exists, err := artifactExists(artifactID)
   170  			Expect(err).NotTo(HaveOccurred())
   171  			Expect(exists).To(BeTrue())
   172  		})
   173  
   174  		// 8)
   175  		It("should update target from local artifact", func() {
   176  			// alter the target by tagging it with a different image
   177  			_, err := buildTask(b, bob.BuildTargetDockerImagePlusName)
   178  			Expect(err).NotTo(HaveOccurred())
   179  			err = mobyClient.ImageTag(
   180  				bob.BuildTargetBobTestImagePlus,
   181  				bob.BuildTargetBobTestImage,
   182  			)
   183  			Expect(err).NotTo(HaveOccurred())
   184  
   185  			state, err := buildTask(b, bob.BuildTargetDockerImageName)
   186  			Expect(err).NotTo(HaveOccurred())
   187  			Expect(state.State()).To(Equal(playbook.StateNoRebuildRequired))
   188  
   189  			exists, err := mobyClient.ImageExists(bob.BuildTargetBobTestImage)
   190  			Expect(err).NotTo(HaveOccurred())
   191  			Expect(exists).To(BeTrue())
   192  		})
   193  
   194  		It("cleanup", func() {
   195  			err := b.CleanBuildInfoStore()
   196  			Expect(err).NotTo(HaveOccurred())
   197  
   198  			err = b.CleanLocalStore()
   199  			Expect(err).NotTo(HaveOccurred())
   200  
   201  			err = reset()
   202  			Expect(err).NotTo(HaveOccurred())
   203  
   204  			exists, err := mobyClient.ImageExists(bob.BuildTargetBobTestImage)
   205  			Expect(err).NotTo(HaveOccurred())
   206  			if exists {
   207  				err = mobyClient.ImageRemove(bob.BuildTargetBobTestImage)
   208  				Expect(err).NotTo(HaveOccurred())
   209  			}
   210  
   211  			exists, err = mobyClient.ImageExists(bob.BuildTargetBobTestImagePlus)
   212  			Expect(err).NotTo(HaveOccurred())
   213  			if exists {
   214  				err = mobyClient.ImageRemove(bob.BuildTargetBobTestImagePlus)
   215  				Expect(err).NotTo(HaveOccurred())
   216  			}
   217  		})
   218  
   219  	})
   220  })