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

     1  package k8s_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	. "github.com/pf-qiu/concourse/v6/topgun"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  )
    12  
    13  var _ = Describe("Garden Config", func() {
    14  
    15  	var (
    16  		garden              Endpoint
    17  		helmDeployTestFlags []string
    18  	)
    19  
    20  	BeforeEach(func() {
    21  		setReleaseNameAndNamespace("gc")
    22  	})
    23  
    24  	JustBeforeEach(func() {
    25  		deployConcourseChart(releaseName, helmDeployTestFlags...)
    26  
    27  		waitAllPodsInNamespaceToBeReady(namespace)
    28  
    29  		By("Getting the worker pod")
    30  		pods := getPods(namespace, metav1.ListOptions{LabelSelector: "app=" + releaseName + "-worker"})
    31  		Expect(pods).To(HaveLen(1))
    32  
    33  		By("port-forwarding the garden API")
    34  		garden = endpointFactory.NewPodEndpoint(
    35  			namespace,
    36  			pods[0].ObjectMeta.Name,
    37  			"7777",
    38  		)
    39  	})
    40  
    41  	AfterEach(func() {
    42  		garden.Close()
    43  		cleanupReleases()
    44  	})
    45  
    46  	Context("passing a config map location to the worker to be used by gdn", func() {
    47  		BeforeEach(func() {
    48  			helmDeployTestFlags = []string{
    49  				`--set=worker.replicas=1`,
    50  				`--set=worker.additionalVolumes[0].name=garden-config`,
    51  				`--set=worker.additionalVolumes[0].configMap.name=garden-config`,
    52  				`--set=worker.additionalVolumeMounts[0].name=garden-config`,
    53  				`--set=worker.additionalVolumeMounts[0].mountPath=/foo`,
    54  				`--set=concourse.worker.garden.config=/foo/garden-config.ini`,
    55  			}
    56  
    57  			configMapCreationArgs := []string{
    58  				"create",
    59  				"configmap",
    60  				"garden-config",
    61  				"--namespace=" + namespace,
    62  				`--from-literal=garden-config.ini=
    63  [server]
    64    max-containers = 100`,
    65  			}
    66  
    67  			Run(nil, "kubectl", "create", "namespace", namespace)
    68  			Run(nil, "kubectl", configMapCreationArgs...)
    69  		})
    70  
    71  		It("returns the configured number of max containers", func() {
    72  			Expect(getMaxContainers(garden.Address())).To(Equal(100))
    73  		})
    74  	})
    75  
    76  	Context("passing the CONCOURSE_GARDEN_ env vars to the gdn server", func() {
    77  		BeforeEach(func() {
    78  			helmDeployTestFlags = []string{
    79  				`--set=worker.replicas=1`,
    80  				`--set=worker.env[0].name=CONCOURSE_GARDEN_MAX_CONTAINERS`,
    81  				`--set=worker.env[0].value="100"`,
    82  			}
    83  		})
    84  
    85  		It("returns the configured number of max containers", func() {
    86  			Expect(getMaxContainers(garden.Address())).To(Equal(100))
    87  		})
    88  	})
    89  
    90  	Context("passing the CONCOURSE_GARDEN_DENY_NETWORK env var to the gdn server", func() {
    91  		BeforeEach(func() {
    92  			helmDeployTestFlags = []string{
    93  				`--set=worker.replicas=1`,
    94  				`--set=worker.env[0].name=CONCOURSE_GARDEN_DENY_NETWORK`,
    95  				`--set=worker.env[0].value="8.8.8.8/24"`,
    96  			}
    97  		})
    98  
    99  		It("causes requests to the specified IP range to fail", func() {
   100  			atc := waitAndLogin(namespace, releaseName+"-web")
   101  			defer atc.Close()
   102  			buildSession := fly.Start("execute", "-c", "tasks/garden-deny-network.yml")
   103  			<-buildSession.Exited
   104  
   105  			Expect(buildSession.ExitCode()).NotTo(Equal(0))
   106  		})
   107  	})
   108  })
   109  
   110  type gardenCap struct {
   111  	MaxContainers int `json:"max_containers"`
   112  }
   113  
   114  func getMaxContainers(addr string) int {
   115  	req, err := http.NewRequest("GET", "http://"+addr+"/capacity", nil)
   116  	Expect(err).ToNot(HaveOccurred())
   117  
   118  	resp, err := http.DefaultClient.Do(req)
   119  	Expect(err).ToNot(HaveOccurred())
   120  
   121  	defer resp.Body.Close()
   122  
   123  	gardenCapObject := gardenCap{}
   124  
   125  	err = json.NewDecoder(resp.Body).Decode(&gardenCapObject)
   126  	Expect(err).ToNot(HaveOccurred())
   127  
   128  	return gardenCapObject.MaxContainers
   129  }