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