Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run tests in individual process #1431

Open
sagar-shah-uipath opened this issue Jun 28, 2024 · 1 comment
Open

Run tests in individual process #1431

sagar-shah-uipath opened this issue Jun 28, 2024 · 1 comment

Comments

@sagar-shah-uipath
Copy link

We have global variable defined as following

var (
	namespaceVal string
	once            sync.Once
	isSet           bool
)

func SetNamespace(namespace string) {
	if namespace == "" {
		panic("SetNamespace must be called with a non-empty string")
	}

	if namespace != "" && namespace != namespaceVal {
		panic("SetNamespace must be called with the same value")
	}

	once.Do(func() {
		namespaceVal = namespace
		isSet = true
	})
}

func IsNamespaceAlreadySet() bool {
	return isSet
}

func GetNamespace() string {
	if !IsNamepaceAlreadySet() {
		panic("SetNamespace must be called before calling GetNamespace")
	}

	return namespaceVal
}

We have various integration tests written for a different golang entrypoints command which calls SetNamespace internally to set value of namespace taken as input from integration test. When we run integration tests using ginkgo since everything is running parallel in single process trying to modify global variable defined above called namespaveVal, some of our tests running into error "SetNamespace must be called with the same value".

Is there anyway to ensure these integration tests don't interact with each other and ensure each run in separate process to avoid this issue.

Sample integration test code

	It("apply with namespace1", Serial, func() {
		ioStreams, _, _, _ := util.NewTestIOStreams()

		rootCmd := root.NewCommand(ioStreams)
		rootCmd.SetArgs([]string{"apply", inputPath, "--namespace", "namespace1"})

		err = rootCmd.Execute()
		Expect(err).To(BeNil())
	})

	It("apply with namespace2", Serial, func() {
		ioStreams, _, _, _ := util.NewTestIOStreams()

		rootCmd := root.NewCommand(ioStreams)
		rootCmd.SetArgs([]string{"apply", inputPath, "--namespace", "namespace2"})

		err = rootCmd.Execute()
		Expect(err).To(BeNil())
	})
@onsi
Copy link
Owner

onsi commented Jun 28, 2024

hey @sagar-shah-uipath - Ginkgo is designed to handle your usecase - I’d suggest taking some time to read the documentation to familiarize yourself with how Ginkgo allows you to set up specs, how it expects your specs to be structured, how it runs them in parallel and to read more about some common Ginkgo patterns.

There are also several example Kubernetes test suites out there.

One thing to note: Ginkgo never runs your specs in parallel in a single process. It always runs specs in a given process serially and parallelization only occurs across multiple processes. The most common pattern I’ve seen is to set up a namespace for each test in a BeforeEach and tear it down in a DeferCleanup. The namespace can be a random guid or something that that uses GinkgoParallelProcess() to get a unique integer per process.

I’d like to encourage you to look through the docs and try organizing your tests around Ginkgo’s model for parallelization - I think you’ll find that the investment will pay off quite quickly :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants