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

     1  package topgun_test
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"strings"
     7  	"time"
     8  
     9  	. "github.com/pf-qiu/concourse/v6/topgun/common"
    10  	_ "github.com/lib/pq"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = XDescribe("Fewest Build Containers Found Placement Strategy", func() {
    16  	var firstWorkerName string
    17  	var secondWorkerName string
    18  	BeforeEach(func() {
    19  		Deploy("deployments/concourse.yml", "-o", "operations/worker-instances.yml", "-v", "worker_instances=2", "-o", "operations/add-placement-strategy.yml")
    20  	})
    21  
    22  	Context("when there is a deployment the worker with the fewest containers is assigned the task to execute", func() {
    23  		It("ensures the worker with the least build containers is assigned the task to execute", func() {
    24  			By("stopping one worker instance")
    25  			workers := JobInstances("worker")
    26  
    27  			firstWorkerName = strings.Split(strings.TrimPrefix(workers[0].Name, "worker/"), "-")[0]
    28  			secondWorkerName = strings.Split(strings.TrimPrefix(workers[1].Name, "worker/"), "-")[0]
    29  
    30  			Bosh("stop", fmt.Sprintf("worker/%s", workers[0].ID))
    31  
    32  			By("setting a pipeline with many containers")
    33  			Fly.Run("set-pipeline", "-n", "main", "-c", "pipelines/lots-ata-time.yml", "-p", "many-containers-pipeline")
    34  
    35  			By("unpausing the pipeline")
    36  			Fly.Run("unpause-pipeline", "-p", "many-containers-pipeline")
    37  
    38  			By("waiting a few minutes before re-starting the worker instance")
    39  			time.Sleep(1 * time.Minute)
    40  			Bosh("start", fmt.Sprintf("worker/%s", workers[0].ID))
    41  			time.Sleep(2 * time.Minute)
    42  
    43  			By("setting the second pipeline with many containers")
    44  			Fly.Run("set-pipeline", "-n", "main", "-c", "pipelines/lots-ata-time-2.yml", "-p", "many-containers-pipeline-2")
    45  
    46  			By("unpausing the second pipeline")
    47  			Fly.Run("unpause-pipeline", "-p", "many-containers-pipeline-2")
    48  
    49  			By("getting the container count on the workers")
    50  			time.Sleep(1 * time.Minute)
    51  			containersTable := FlyTable("containers")
    52  			containersOnFirstWorker := 0
    53  			containersOnSecondWorker := 0
    54  			for _, c := range containersTable {
    55  				if c["type"] == "check" {
    56  					continue
    57  				}
    58  
    59  				if strings.HasPrefix(c["worker"], firstWorkerName) {
    60  					containersOnFirstWorker++
    61  				} else if strings.HasPrefix(c["worker"], secondWorkerName) {
    62  					containersOnSecondWorker++
    63  				}
    64  			}
    65  
    66  			fmt.Println("Number of build containers on first worker: ", containersOnFirstWorker)
    67  			fmt.Println("Number of build containers on second worker: ", containersOnSecondWorker)
    68  
    69  			differenceInContainers := math.Abs(float64(containersOnFirstWorker - containersOnSecondWorker))
    70  			totalContainers := float64(containersOnFirstWorker + containersOnSecondWorker)
    71  			Expect(totalContainers).ToNot(BeZero())
    72  			Expect(differenceInContainers).To(BeNumerically("~", 2)) //arbitrary tolerance of 2
    73  		})
    74  
    75  	})
    76  })