Fix lots of unrelated data races

This is primarily to stop goroutine spawned by ensureTemplateStopped from reading s.ContainerFactory after the test is done, as the test setup code for the next test will then write to s.ContainerFactory.