-1

I have a Go project which contains a testcase file main_test.go for testing the commands after the project is built and an executable is created.

package cmd

import (
    "testing"
    "os"
    "os/exec"
)

func TestCommand(t *testing.T) {
    rootCmd, _ := exec.LookPath("executable")

    storeCmd := &exec.Cmd {
        Path: rootCmd,
        Args: []string{ rootCmd, "store", "--mongo", "<path_to_mongo_config_file>"},
        Stdout: os.Stdout,
        Stderr: os.Stdout,
    }

    if err := storeCmd.Run(); err != nil {
        t.Error("Error", err)
    }
}

The function takes different configuration data such as hostname, username, password, etc, from the mongo config file. Everything works fine if I run the file on my local machine. But when running in docker, it can't connect to MongoDB. I tried many things like using a ubuntu base image and then installing golang and mongodb packages to accomplish the work.

All packages get installed, but MongoDB server can't start, so I cannot create a database in MongoDB inside docker container for which the configuration file is to be utilised.

How can I write a dockerfile which would automatically create a mongoDB database, insert some data into it and then carry on testing.

This is the dockerfile

FROM ubuntu

RUN apt-get update
RUN apt-get upgrade
RUN apt-get install -y golang mongodb git

WORKDIR /go/src/path
COPY . .

RUN go build

ENTRYPOINT [ "mongo" ]
CMD [ "use testdb \
db.testcol.insert({name: 'test1', address: 'testadd'})"]

RUN go test -v

This dockerfile is required for CI/CD pipeline on Google Cloud Platform, so all the commands (from creating database to testing) need to be specified in dockerfile only. No human interaction can be allowed. We cannot run commands like docker run

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Shady98
  • 21
  • 1
  • 5
  • Seems like docker-compose would be a better fit for this job, try to read about it – Omer Tuchfeld Jul 31 '20 at 10:33
  • I already used docker-compose. I made 2 dockerfiles, one for mongo and one for golang. But it would get stuck when mongo server starts. What I want is a solution using which mongo server can be started, in the next step a database should be created and then the testing should happen on its own. No user interaction should be required other than just triggering the build on Google cloud build – Shady98 Jul 31 '20 at 11:22
  • "It would get stuck" then why not fix it? Why ditch docker-compose altogether? Just have the go container continuously poll the mongo container until it's ready. Once it's ready, have your tests (running on the go container) create the database and whatever data is needed for the test, and then perform the test – Omer Tuchfeld Jul 31 '20 at 11:51
  • Your dockerfile tries to execute javascript code as shell commands. I suggest you read up separately on how docker works, how javascript/mongo shell works, follow mongodb docs for installing server & client, go through some tutorials etc. THEN try to assemble the pieces together. – D. SM Jul 31 '20 at 21:12
  • You can use Gnomock, it supports MongoDB container setup with database/collections/documents setup for integration tests: https://github.com/orlangure/gnomock (mongo example - https://github.com/orlangure/gnomock/tree/master/preset/mongo). You won't need a dockerfile at all. – Yury Fedorov Aug 02 '20 at 08:30

1 Answers1

0

Have a look at Dockertest, it's not doing exactly what you are, but it should work for you. It allows you to run a docker container (all within TestMain()) that you can now reliably test against.

From there you can run stuff like db.Exec() to insert what you want.

chabad360
  • 640
  • 1
  • 8
  • 16