orlangure/gnomock

Request: need option to reuse container created before, and Preset need Clear/Reset method

kkHAIKE opened this issue ยท 5 comments

Create Container every test run, maybe waste a lot of time ๐Ÿ˜ฎโ€๐Ÿ’จ(mariadb inited cost 1m+-)

Hi! Thanks for bringing this up.

To avoid creating a container multiple times I suggest using subtests (t.Run) or TestMain, and structuring the tests in a different way.

I'll look into a way to reset presets in a week when I'm back from vacation, but I think there isn't a reliable way to reset every preset to its original state without recreating it. You would need to guess every possible change that the other code makes to revert it.

some large business app want to use db from multi package.... ๐Ÿ˜ญ

i make PR for that, WIP now, add test and db preset support tomorrow
#294

i think should use client check exist myself..

@kkHAIKE, I see you closed the PR and this issue, so I assume you figured out how to solve your issue locally. Thanks.

use this function check first, than gnomock.Start

func checkExist(name string, namedPorts gnomock.NamedPorts) (_ gnomock.NamedPorts, err error) {
	cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
	if err != nil {
		return
	}

	cont, err := cli.ContainerInspect(context.Background(), name)
	if err != nil {
		if client.IsErrNotFound(err) {
			err = nil
		}
		return
	}

	return boundNamedPorts(cont, namedPorts)
}

name is WithContainerName set