-
-
Notifications
You must be signed in to change notification settings - Fork 243
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
Create an example for starting mongodb as a replica set #480
Comments
Updated context: when originally posting this, I ran with the options: var out bytes.Buffer
dockertest.ExecOptions{
StdOut: &out,
StdErr: &out,
} I have since tried adding Start exec failed: Unrecognized input header: 47 Again, I feel I am close here, just missing probably one or two (obvious) things here |
I am trying to initialize a replica set for testing, too. Does anyone have a good solution? |
IIRC, |
Hello contributors! I am marking this issue as stale as it has not received any engagement from the community or maintainers for a year. That does not imply that the issue has no merit! If you feel strongly about this issue
Throughout its lifetime, Ory has received over 10.000 issues and PRs. To sustain that growth, we need to prioritize and focus on issues that are important to the community. A good indication of importance, and thus priority, is activity on a topic. Unfortunately, burnout has become a topic of concern amongst open-source projects. It can lead to severe personal and health issues as well as opening catastrophic attack vectors. The motivation for this automation is to help prioritize issues in the backlog and not ignore, reject, or belittle anyone. If this issue was marked as stale erroneously you can exempt it by adding the Thank you for your understanding and to anyone who participated in the conversation! And as written above, please do participate in the conversation if this topic is important to you! Thank you 🙏✌️ |
Was quite the pain to get this to work. Unfortunately I wasn't able to get dyn. ports to work so can only run a single instance at a time but it works. pool, err := dt.NewPool("")
// set up container
resource, err := pool.RunWithOptions(&dt.RunOptions{
Repository: "mongo",
Cmd: []string{"--replSet", "rs0", "--bind_ip_all"},
PortBindings: map[docker.Port][]docker.PortBinding{
"27017/tcp": {{HostIP: "127.0.0.1", HostPort: "27017/tcp"}},
},
}, func(config *docker.HostConfig) {
// set AutoRemove to true so that stopped container goes away by itself
config.AutoRemove = true
config.RestartPolicy = docker.RestartPolicy{
Name: "no",
}
}) // initialize rs
if err := pool.Retry(func() error {
buf := bytes.NewBuffer(nil)
code, err := resource.Exec([]string{`/bin/bash`}, dt.ExecOptions{
StdIn: bytes.NewBufferString(`mongosh --eval "try{rs.initiate({ _id: \"rs0\", members: [{ _id: 0, host: \"localhost:27017\"}]}).ok} catch {rs.status().ok}"`),
StdOut: buf,
StdErr: buf,
})
if err != nil || code != 0 {
return fmt.Errorf("failed command: %w, code: %d", err, code)
}
if result, err := strconv.Atoi(strings.TrimSpace(buf.String())); err != nil || result != 1 {
return fmt.Errorf("failed to initiate %w, result: %s", err, buf.String())
}
mongoDbTestUri = "mongodb://" + resource.GetHostPort("27017/tcp")
ctx := context.Background()
mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoDbTestUri))
if err != nil {
return err
}
defer func() {
_ = mongoClient.Disconnect(ctx)
}()
return mongoClient.Ping(ctx, nil)
}); err != nil {
return "", nil, nil, fmt.Errorf("could not connect to docker: %w", err)
} If anyone figures out how to use dyn. ports let me know |
Preflight checklist
Ory Network Project
No response
Describe your problem
I use
dockertest/v3
extensively in my testing of api servers ingo
.I am getting to the point where I want to do more interesting things the backing
mongodb
database (e.g. change streams, transactions) and still would like to containerize themongo
instance for tests. I feel like I'm close to figuring this out but I'd like to successfully runrs.initiate()
inside themongodb
container after creating the test resource, but have been running into issues successfully using theresource.Exec
function.Describe your ideal solution
Spin up a
mongodb
container, programmatically ingo
, with replica set configuration and execute thers.initiate()
command so that the repl set is connectable in code. Was hoping something like this would work:Workarounds or alternatives
While troubleshooting, I have tried removing the "pipe to mongo shell" at first with no success, so I think I am just going about
resource.Exec
incorrectly here.When calling:
.Exec([]string{
echo "rs.initiate().ok || rs.status().ok"}
, I see output:When adding
"/bin/bash"
in front:Exec([]string{"/bin/bash",
echo "rs.initiate().ok || rs.status().ok"}
:Version
github.com/ory/dockertest/v3 v3.10.0
Additional Context
No response
The text was updated successfully, but these errors were encountered: