github.com/benchkram/bob@v0.0.0-20240314204020-b7a57f2f9be9/test/e2e/artifacts/nobuildinfo_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/pkg/dockermobyutil" 10 "github.com/benchkram/errz" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 ) 14 15 // file targets 16 var _ = Describe("Test artifact and target lifecycle without existing buildinfo", func() { 17 Context("in a fresh playground", func() { 18 19 It("should initialize bob playground", func() { 20 Expect(bob.CreatePlayground(bob.PlaygroundOptions{Dir: dir})).NotTo(HaveOccurred()) 21 }) 22 23 It("should build", func() { 24 ctx := context.Background() 25 err := b.Build(ctx, "build") 26 errz.Log(err) 27 Expect(err).NotTo(HaveOccurred()) 28 }) 29 30 var artifactID string 31 It("should check that exactly one artifact was created", func() { 32 fs, err := os.ReadDir(artifactDir) 33 Expect(err).NotTo(HaveOccurred()) 34 Expect(len(fs)).To(Equal(1)) 35 artifactID = fs[0].Name() 36 }) 37 38 It("clean artifacts & buildinfo", func() { 39 err := b.Clean() 40 Expect(err).NotTo(HaveOccurred()) 41 }) 42 43 // 1) 44 It("should rebuild, update the target and write the artifact", func() { 45 state, err := buildTask(b, "build") 46 Expect(err).NotTo(HaveOccurred()) 47 Expect(state.State()).To(Equal(playbook.StateCompleted)) 48 49 exists, err := artifactExists(artifactID) 50 Expect(err).NotTo(HaveOccurred()) 51 Expect(exists).To(BeTrue()) 52 }) 53 54 It("clean artifacts & buildinfo", func() { 55 err := b.Clean() 56 Expect(err).NotTo(HaveOccurred()) 57 }) 58 59 // 2) 4) 60 It("should not rebuild as a artifact exists", func() { 61 // create artifact 62 state, err := buildTask(b, "build") 63 Expect(err).NotTo(HaveOccurred()) 64 Expect(state.State()).To(Equal(playbook.StateCompleted)) 65 exists, err := artifactExists(artifactID) 66 Expect(err).NotTo(HaveOccurred()) 67 Expect(exists).To(BeTrue()) 68 69 // clean buildinfo 70 err = b.CleanBuildInfoStore() 71 Expect(err).NotTo(HaveOccurred()) 72 73 state, err = buildTask(b, "build") 74 Expect(err).NotTo(HaveOccurred()) 75 Expect(state.State()).To(Equal(playbook.StateNoRebuildRequired)) 76 }) 77 78 It("clean artifacts & buildinfo", func() { 79 err := b.Clean() 80 Expect(err).NotTo(HaveOccurred()) 81 }) 82 83 // 3) 84 It("should rebuild as no artifact exists", func() { 85 state, err := buildTask(b, "build") 86 Expect(err).NotTo(HaveOccurred()) 87 Expect(state.State()).To(Equal(playbook.StateCompleted)) 88 89 exists, err := artifactExists(artifactID) 90 Expect(err).NotTo(HaveOccurred()) 91 Expect(exists).To(BeTrue()) 92 }) 93 94 It("cleanup", func() { 95 err := b.Clean() 96 Expect(err).NotTo(HaveOccurred()) 97 err = reset() 98 Expect(err).NotTo(HaveOccurred()) 99 }) 100 }) 101 }) 102 103 // docker targets 104 var _ = Describe("Test artifact and target lifecycle for docker images without existing buildinfo", func() { 105 Context("in a fresh playground", func() { 106 107 mobyClient, err := dockermobyutil.NewRegistryClient() 108 Expect(err).NotTo(HaveOccurred()) 109 110 It("should initialize bob playground", func() { 111 Expect(bob.CreatePlayground(bob.PlaygroundOptions{Dir: dir})).NotTo(HaveOccurred()) 112 }) 113 114 It("should build", func() { 115 ctx := context.Background() 116 err := b.Build(ctx, bob.BuildTargetDockerImageName) 117 Expect(err).NotTo(HaveOccurred()) 118 }) 119 120 var artifactID string 121 It("should check that exactly one artifact was created", func() { 122 fs, err := os.ReadDir(artifactDir) 123 Expect(err).NotTo(HaveOccurred()) 124 Expect(len(fs)).To(Equal(1)) 125 artifactID = fs[0].Name() 126 }) 127 128 It("clean artifacts & buildinfo", func() { 129 err := b.Clean() 130 Expect(err).NotTo(HaveOccurred()) 131 }) 132 133 // 1) 134 It("should rebuild, update the target and write the artifact", func() { 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 exists, err = mobyClient.ImageExists(bob.BuildTargetBobTestImage) 144 Expect(err).NotTo(HaveOccurred()) 145 Expect(exists).To(BeTrue()) 146 }) 147 148 It("clean artifacts & buildinfo", func() { 149 err := b.Clean() 150 Expect(err).NotTo(HaveOccurred()) 151 }) 152 153 // 2) 4) 154 It("should not rebuild as a artifact exists", func() { 155 // create artifact 156 state, err := buildTask(b, bob.BuildTargetDockerImageName) 157 Expect(err).NotTo(HaveOccurred()) 158 Expect(state.State()).To(Equal(playbook.StateCompleted)) 159 exists, err := artifactExists(artifactID) 160 Expect(err).NotTo(HaveOccurred()) 161 Expect(exists).To(BeTrue()) 162 163 // clean buildinfo 164 err = b.CleanBuildInfoStore() 165 Expect(err).NotTo(HaveOccurred()) 166 167 state, err = buildTask(b, bob.BuildTargetDockerImageName) 168 Expect(err).NotTo(HaveOccurred()) 169 Expect(state.State()).To(Equal(playbook.StateNoRebuildRequired)) 170 }) 171 172 It("clean artifacts & buildinfo", func() { 173 err := b.Clean() 174 Expect(err).NotTo(HaveOccurred()) 175 }) 176 177 // 3) 178 It("should rebuild as no artifact exists", func() { 179 state, err := buildTask(b, bob.BuildTargetDockerImageName) 180 Expect(err).NotTo(HaveOccurred()) 181 Expect(state.State()).To(Equal(playbook.StateCompleted)) 182 183 exists, err := artifactExists(artifactID) 184 Expect(err).NotTo(HaveOccurred()) 185 Expect(exists).To(BeTrue()) 186 }) 187 188 It("cleanup", func() { 189 err := b.Clean() 190 Expect(err).NotTo(HaveOccurred()) 191 err = reset() 192 Expect(err).NotTo(HaveOccurred()) 193 194 exists, err := mobyClient.ImageExists(bob.BuildTargetBobTestImage) 195 Expect(err).NotTo(HaveOccurred()) 196 197 if exists { 198 err = mobyClient.ImageRemove(bob.BuildTargetBobTestImage) 199 Expect(err).NotTo(HaveOccurred()) 200 } 201 }) 202 }) 203 })