1package k8s_test
2
3import (
4	"github.com/onsi/gomega/gbytes"
5
6	. "github.com/onsi/ginkgo"
7	. "github.com/onsi/gomega"
8)
9
10var _ = Describe("Container Limits", func() {
11	const (
12		TaskCPULimit    = "--set=concourse.web.defaultTaskCpuLimit=512"
13		TaskMemoryLimit = "--set=concourse.web.defaultTaskMemoryLimit=1GB"
14		COS             = "--set=worker.nodeSelector.nodeImage=cos"
15		UBUNTU          = "--set=worker.nodeSelector.nodeImage=ubuntu"
16	)
17
18	BeforeEach(func() {
19		setReleaseNameAndNamespace("cl")
20	})
21
22	onPks(func() {
23		containerLimitsWork(TaskCPULimit, TaskMemoryLimit)
24	})
25
26	onGke(func() {
27		containerLimitsWork(COS, TaskCPULimit, TaskMemoryLimit)
28		containerLimitsFail(UBUNTU, TaskCPULimit, TaskMemoryLimit)
29	})
30
31	AfterEach(func() {
32		cleanupReleases()
33	})
34
35})
36
37func deployWithSelectors(selectorFlags ...string) {
38	helmDeployTestFlags := []string{
39		"--set=concourse.web.kubernetes.enabled=false",
40		"--set=worker.replicas=1",
41	}
42
43	deployConcourseChart(releaseName, append(helmDeployTestFlags, selectorFlags...)...)
44}
45
46func containerLimitsWork(selectorFlags ...string) {
47	Context("container limits work", func() {
48		It("returns the configure default container limit", func() {
49			deployWithSelectors(selectorFlags...)
50
51			atc := waitAndLogin(namespace, releaseName+"-web")
52			defer atc.Close()
53
54			buildSession := fly.Start("execute", "-c", "tasks/tiny.yml")
55			<-buildSession.Exited
56
57			Expect(buildSession.ExitCode()).To(Equal(0))
58
59			hijackSession := fly.Start(
60				"hijack",
61				"-b", "1",
62				"-s", "one-off",
63				"--", "sh", "-c",
64				"cat /sys/fs/cgroup/memory/memory.memsw.limit_in_bytes; cat /sys/fs/cgroup/cpu/cpu.shares",
65			)
66			<-hijackSession.Exited
67
68			Expect(hijackSession.ExitCode()).To(Equal(0))
69			Expect(hijackSession).To(gbytes.Say("1073741824\n512"))
70		})
71	})
72}
73
74func containerLimitsFail(selectorFlags ...string) {
75	Context("container limits fail", func() {
76		It("fails to set the memory limit", func() {
77			deployWithSelectors(selectorFlags...)
78
79			atc := waitAndLogin(namespace, releaseName+"-web")
80			defer atc.Close()
81
82			buildSession := fly.Start("execute", "-c", "tasks/tiny.yml")
83			<-buildSession.Exited
84			Expect(buildSession.ExitCode()).To(Equal(2))
85			Expect(buildSession).To(gbytes.Say(
86				"memory.memsw.limit_in_bytes: permission denied",
87			))
88		})
89	})
90}
91