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

Add DeleteIfExist to prior creating a container make sure it is deleted. #205

Closed
DrLeh opened this issue Sep 13, 2021 · 5 comments · Fixed by #206
Closed

Add DeleteIfExist to prior creating a container make sure it is deleted. #205

DrLeh opened this issue Sep 13, 2021 · 5 comments · Fixed by #206
Assignees
Milestone

Comments

@DrLeh
Copy link

DrLeh commented Sep 13, 2021

I'm working on putting in FluentDocker into some unit tests for elasticsearch. Under some circumstances, the container sometimes isn't getting disposed. Like if I'm debugging through the test and stop debugging, sometimes it seems Dispose() is not getting called.

I see we have the ReuseIfExists() method, which is nice, but is there any way to do DeleteIfExists() / ReCreateIfExists()? For a unit test I always want the container to be fresh, so I don't want .Build() or Start() to fail.

I could presumably do something like this but it feels really janky:

var b = new Builder().UseContainer()
    .ResuseIfExists() //so it doesn't error if exists
   //...
    ;

var container = b.Build();
container.Dispose();
container = b.Build();
container.Start();

@mariotoffia mariotoffia changed the title Force recreate a container Add DeleteIfExist to prior creating a container make sure it is deleted. Sep 14, 2021
@mariotoffia
Copy link
Owner

That's true, if you have it in a using statement, you have to let it run or throw an exception otherwise it will not call Dispose.

That is a really good idea!!

Hmm... I would vote for DeleteIfExist. I'll do that tonight/tomorrow. I'll add this as an enhancement.

But it has to be on named containers only!

Cheers,
Mario :)

@mariotoffia
Copy link
Owner

mariotoffia commented Sep 14, 2021

Hi @DrLeh, I've implemented the DeleteIfExists. You probably need to wait one hour or so in order to tests and it to get published onto NuGet.

Cheers,
Mario :)

🐅 Sample usage

      var name = Guid.NewGuid().ToString();

      var container = Fd.UseContainer()
        .UseImage("postgres:9.6-alpine")
        .WithName($"name-{name}")
        .Build();

      var id = container.Id;

      using (var c = Fd
        .UseContainer()
        .DeleteIfExists() // <-- It will delete the named container if it exists *before* it creates the new one.
        .UseImage("postgres:9.6-alpine")
        .WithName($"name-{name}")
        .Build())
      {
        // Ids should not be equal - since deleted and then created.
        AreNotEqual(id, c.Id);
      }

@DrLeh
Copy link
Author

DrLeh commented Sep 14, 2021

@mariotoffia looks like i'm still getting an error when the container is still running. Is there an option to stop it if it's running to delete it?

image

My code has IDisposable but it seems when i stop debugging it doesn't get called:

public class ElasticContainerFixture : IDisposable
{
    public string ClusterName = "es-docker-cluster-elastic-ci-tests";

    public IContainerService Container;

    public void Dispose()
    {
        Container.Dispose();
    }

    public ElasticContainerFixture()
    {
        var b = new Builder().UseContainer()
             .UseImage("elasticsearch:7.14.1")
             .WithEnvironment(@"node.name=elastic-ci-tests",
                 @$"cluster.name={ClusterName}",
                 @"discovery.type=single-node",
                 @"""ES_JAVA_OPTS=-Xms512m -Xmx512m""")
             .ExposePort(9800, 9200)
             .ExposePort(9900, 9300)
             .WithName($"elastic-ci-tests_{Environment.MachineName.Replace(" ", "_")}")
             .DeleteIfExists()
             .WaitForHttp("http://localhost:9800", continuation: (req, i) =>
             {
                 //this is ultimately the same code they have in WaitForHttp with
                 //no continuation: specified. We might want to check a different url
                 if (req.Code == System.Net.HttpStatusCode.OK)
                     return 0L;
                 return 2000;
             });
        Container = b.Build();
        Container.Start();
    }
}

@mariotoffia
Copy link
Owner

mariotoffia commented Sep 14, 2021

Hi @DrLeh try to use option force: true when doing DeleteIfExists if that Will solve your problem.

Yes, when you break, it Will not call dispose - if you just continue - it will call it :) I always let the test run or throw so it Will cleanup properly

but curious if force: true will solve the problem - could you please respond if it worked or not - if not, I’ll fix it now that I’ve got a unit test.

@DrLeh
Copy link
Author

DrLeh commented Sep 14, 2021

Thanks I didn't see that force option! Thanks for the incredibly quick turnaround, loving this library!

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

Successfully merging a pull request may close this issue.

2 participants