github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/topgun/both/image_resource_gc_test.go (about)

     1  package topgun_test
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	. "github.com/pf-qiu/concourse/v6/topgun/common"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/onsi/gomega/gbytes"
    11  )
    12  
    13  var _ = Describe("A build using an image_resource", func() {
    14  	BeforeEach(func() {
    15  		Deploy("deployments/concourse.yml", "-o", "operations/fast-gc.yml")
    16  	})
    17  
    18  	Describe("one-off builds", func() {
    19  		It("does not garbage-collect the image immediately", func() {
    20  			By("running a task with an image_resource")
    21  			Fly.Run("execute", "-c", "tasks/tiny.yml")
    22  
    23  			By("verifying that the image cache sticks around")
    24  			Consistently(func() []string {
    25  				volumes := FlyTable("volumes")
    26  				resourceVolumeHandles := []string{}
    27  				for _, volume := range volumes {
    28  					// there is going to be one for image resource
    29  					if volume["type"] == "resource" && volume["identifier"] == "version:task-image" {
    30  						resourceVolumeHandles = append(resourceVolumeHandles, volume["handle"])
    31  					}
    32  				}
    33  
    34  				return resourceVolumeHandles
    35  			}, time.Minute).Should(HaveLen(1))
    36  		})
    37  	})
    38  
    39  	Describe("pipeline builds", func() {
    40  		It("keeps images for the latest build", func() {
    41  			By("setting a pipeline that uses image A")
    42  			Fly.Run("set-pipeline", "-n", "-c", "pipelines/build-image-gc-part-1.yml", "-p", "test")
    43  
    44  			By("unpausing the pipeline")
    45  			Fly.Run("unpause-pipeline", "-p", "test")
    46  
    47  			By("triggering a build that waits")
    48  			watchSession := Fly.Start("trigger-job", "-w", "-j", "test/some-job")
    49  			//For the initializing block
    50  			Eventually(watchSession).Should(gbytes.Say("echo 'waiting for /tmp/stop-waiting to exist'"))
    51  			//For the output from the running step
    52  			Eventually(watchSession).Should(gbytes.Say("waiting for /tmp/stop-waiting to exist"))
    53  
    54  			By("getting the resource cache volumes")
    55  			volumes := FlyTable("volumes")
    56  			originalResourceVolumeHandles := []string{}
    57  			for _, volume := range volumes {
    58  				if volume["type"] == "resource" && volume["identifier"] == "version:pipeline-image-1" {
    59  					originalResourceVolumeHandles = append(originalResourceVolumeHandles, volume["handle"])
    60  				}
    61  			}
    62  			Expect(originalResourceVolumeHandles).To(HaveLen(1))
    63  
    64  			By("setting a pipeline that uses image B")
    65  			Fly.Run("set-pipeline", "-n", "-c", "pipelines/build-image-gc-part-2.yml", "-p", "test")
    66  
    67  			By("triggering a build that succeeds")
    68  			Fly.Run("trigger-job", "-w", "-j", "test/some-job")
    69  
    70  			By("verifying that both image caches stick around")
    71  			Consistently(func() []string {
    72  				volumes := FlyTable("volumes")
    73  				resourceVolumeHandles := []string{}
    74  				for _, volume := range volumes {
    75  					if volume["type"] == "resource" && strings.HasPrefix(volume["identifier"], "version:pipeline-image-") {
    76  						resourceVolumeHandles = append(resourceVolumeHandles, volume["handle"])
    77  					}
    78  				}
    79  
    80  				return resourceVolumeHandles
    81  			}, time.Minute).Should(HaveLen(2))
    82  
    83  			By("hijacking the previous build to tell it to finish")
    84  			hijackSession := Fly.Start(
    85  				"hijack",
    86  				"-j", "test/some-job",
    87  				"-b", "1",
    88  				"-s", "wait",
    89  				"touch", "/tmp/stop-waiting",
    90  			)
    91  			<-hijackSession.Exited
    92  			Expect(hijackSession.ExitCode()).To(Equal(0))
    93  
    94  			By("waiting for the build to exit")
    95  			Eventually(watchSession, 1*time.Minute).Should(gbytes.Say("done"))
    96  			<-watchSession.Exited
    97  			Expect(watchSession.ExitCode()).To(Equal(0))
    98  
    99  			By("eventually expiring the previous build's resource cache volume")
   100  			var remainingHandles []string
   101  			Eventually(func() []string {
   102  				volumes := FlyTable("volumes")
   103  				resourceVolumeHandles := []string{}
   104  				for _, volume := range volumes {
   105  					if volume["type"] == "resource" && strings.HasPrefix(volume["identifier"], "version:pipeline-image-") {
   106  						resourceVolumeHandles = append(resourceVolumeHandles, volume["handle"])
   107  					}
   108  				}
   109  
   110  				remainingHandles = resourceVolumeHandles
   111  
   112  				return resourceVolumeHandles
   113  			}, 10*time.Minute, 10*time.Second).Should(HaveLen(1))
   114  
   115  			By("keeping the new image")
   116  			Expect(remainingHandles).ToNot(Equal(originalResourceVolumeHandles))
   117  		})
   118  	})
   119  })