<commit_before>package networking_test

import (
	"fmt"
	"os"
	"os/exec"
	"path"
	"syscall"

	"github.com/cloudfoundry-incubator/garden"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Networking recovery", func() {
	Context("with two containers in the same subnet", func() {
		var (
			ctr1           garden.Container
			ctr2Path       string
			bridgeEvidence string
		)
		BeforeEach(func() {
			client = startGarden()

			containerNetwork := fmt.Sprintf("10.%d.0.0/24", GinkgoParallelNode())
			var err error
			ctr1, err = client.Create(garden.ContainerSpec{Network: containerNetwork})
			Ω(err).ShouldNot(HaveOccurred())
			ctr2, err := client.Create(garden.ContainerSpec{Network: containerNetwork})
			Ω(err).ShouldNot(HaveOccurred())
			info2, err := ctr2.Info()
			Ω(err).ShouldNot(HaveOccurred())
			ctr2Path = info2.ContainerPath

			bridgeEvidence = fmt.Sprintf("inet 10.%d.0.254/24 scope global w%db-", GinkgoParallelNode(), GinkgoParallelNode())
			cmd := exec.Command("ip", "a")
			Ω(cmd.CombinedOutput()).Should(ContainSubstring(bridgeEvidence))
		})

		Context("when garden is killed and restarted", func() {
			BeforeEach(func() {
				gardenProcess.Signal(syscall.SIGKILL)
				Eventually(gardenProcess.Wait(), "10s").Should(Receive())

				client = startGarden()
				Ω(client.Ping()).ShouldNot(HaveOccurred())
			})

			It("the subnet's bridge no longer exists", func() {
				cmd := exec.Command("ip", "a")
				Ω(cmd.CombinedOutput()).ShouldNot(ContainSubstring(bridgeEvidence))
			})
		})

		Context("when garden is killed, a persisted container network is deleted, and garden is restarted", func() {
			BeforeEach(func() {
				gardenProcess.Signal(syscall.SIGKILL)
				Eventually(gardenProcess.Wait(), "10s").Should(Receive())

				err := os.Remove(path.Join(ctr2Path, "cnetConfig.json"))
				Ω(err).ShouldNot(HaveOccurred())

				client = startGarden()
				Ω(client.Ping()).ShouldNot(HaveOccurred())
			})

			It("the subnet's bridge no longer exists", func() {
				cmd := exec.Command("ip", "a")
				Ω(cmd.CombinedOutput()).ShouldNot(ContainSubstring(bridgeEvidence))
			})<commit_msg>Add test to exercise network recovery and bridge deletion better.<commit_after>package networking_test

import (
	"fmt"
	"os"
	"os/exec"
	"path"
	"syscall"

	"github.com/cloudfoundry-incubator/garden"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Networking recovery", func() {
	Context("with two containers in the same subnet", func() {
		var (
			ctr1           garden.Container
            ctr2           garden.Container
			ctr2Path       string
			bridgeEvidence string
		)
		BeforeEach(func() {
			client = startGarden()

			containerNetwork := fmt.Sprintf("10.%d.0.0/24", GinkgoParallelNode())
			var err error
			ctr1, err = client.Create(garden.ContainerSpec{Network: containerNetwork})
			Ω(err).ShouldNot(HaveOccurred())
			ctr2, err = client.Create(garden.ContainerSpec{Network: containerNetwork})
			Ω(err).ShouldNot(HaveOccurred())
			info2, err := ctr2.Info()
			Ω(err).ShouldNot(HaveOccurred())
			ctr2Path = info2.ContainerPath

			bridgeEvidence = fmt.Sprintf("inet 10.%d.0.254/24 scope global w%db-", GinkgoParallelNode(), GinkgoParallelNode())
			cmd := exec.Command("ip", "a")
			Ω(cmd.CombinedOutput()).Should(ContainSubstring(bridgeEvidence))
		})

		Context("when garden is killed and restarted using SIGKILL", func() {
			BeforeEach(func() {
				gardenProcess.Signal(syscall.SIGKILL)
				Eventually(gardenProcess.Wait(), "10s").Should(Receive())

				client = startGarden()
				Ω(client.Ping()).ShouldNot(HaveOccurred())
			})

			It("the subnet's bridge no longer exists", func() {
				cmd := exec.Command("ip", "a")
				Ω(cmd.CombinedOutput()).ShouldNot(ContainSubstring(bridgeEvidence))
			})
		})

		Context("when garden is shut down cleanly and restarted, and the containers are deleted", func() {
			BeforeEach(func() {
				gardenProcess.Signal(syscall.SIGTERM)
				Eventually(gardenProcess.Wait(), "10s").Should(Receive())

				client = startGarden()
				Ω(client.Ping()).ShouldNot(HaveOccurred())

                cmd := exec.Command("ip", "a")
                Ω(cmd.CombinedOutput()).Should(ContainSubstring(bridgeEvidence))

                Ω(client.Destroy(ctr1.Handle())).Should(Succeed())
                Ω(client.Destroy(ctr2.Handle())).Should(Succeed())
            })

			It("the subnet's bridge no longer exists", func() {
                cmd := exec.Command("ip", "a")
				Ω(cmd.CombinedOutput()).ShouldNot(ContainSubstring(bridgeEvidence))
			})
		})

		Context("when garden is killed, a persisted container network is deleted, and garden is restarted", func() {
			BeforeEach(func() {
				gardenProcess.Signal(syscall.SIGKILL)
				Eventually(gardenProcess.Wait(), "10s").Should(Receive())

				err := os.Remove(path.Join(ctr2Path, "cnetConfig.json"))
				Ω(err).ShouldNot(HaveOccurred())

				client = startGarden()
				Ω(client.Ping()).ShouldNot(HaveOccurred())
			})

			It("the subnet's bridge no longer exists", func() {
				cmd := exec.Command("ip", "a")
				Ω(cmd.CombinedOutput()).ShouldNot(ContainSubstring(bridgeEvidence))
			})
{'fraction_non_alphanumeric': 0.15287490287490288, 'fraction_numerical': 0.01146076146076146, 'mean_word_length': 3.965284474445516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2258216', 'n_tokens_mistral': 1957, 'n_tokens_neox': 1680, 'n_words': 309}