tryvium-travels/memongo

issue running inside of docker

guiaramos opened this issue · 4 comments

I am trying to use it inside of golang on docker, any idea how to achieve that?

Config:

	opts := &memongo.Options{
		MongoVersion: "5.0.0",
	}

	if runtime.GOARCH == "arm64" && runtime.GOOS == "darwin" {
		// Only set the custom url as workaround for arm64 macs
		opts.DownloadURL = "https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-5.0.0.tgz"
	}

	mongoServer, err := memongo.StartWithOptions(opts)
	assert.NoError(t, err)

Dockerfile:

FROM golang:latest as builder

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download && go mod verify

COPY . .

ENV DOCKER_RUNNING=true

RUN CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go test -failfast ./it

Getting the following error:

> [builder 8/9] RUN CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go test -failfast ./it:
#16 28.82 GOARCH:  amd64
#16 28.82 GOOS:  linux
#16 28.82 [memongo] [INFO]  Starting MongoDB with options &memongo.Options{ShouldUseReplica:false, Port:41831, CachePath:"/root/.cache/memongo", MongoVersion:"5.0.0", DownloadURL:"https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-debian10-5.0.0.tgz", MongodBin:"", Logger:(*log.Logger)(nil), LogLevel:0, StartupTimeout:10000000000, Auth:false}
#16 28.82 [memongo] [INFO]  mongod from https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-debian10-5.0.0.tgz does not exist in cache, downloading to /root/.cache/memongo/mongodb-linux-x86_64-debian10-5_0_0_tgz_fe6e20c131/mongod
#16 28.82 [memongo] [INFO]  finished downloading mongod to /root/.cache/memongo/mongodb-linux-x86_64-debian10-5_0_0_tgz_fe6e20c131/mongod in 11.270371588s
#16 28.82 --- FAIL: TestE2ETestSuite (21.39s)
#16 28.82     testing.go:77: 
#16 28.82               Error Trace:    testing.go:77
#16 28.82                                                       e2e_test.go:66
#16 28.82                                                       suite.go:118
#16 28.82                                                       e2e_test.go:44
#16 28.82               Error:          Received unexpected error:
#16 28.82                               timed out waiting for mongod to start

Please give some details of your machine and the TestE2E functions.

Also note that in your tests you should always pass to the mongo client a context that has a timeout. If you do so, the tests will pass.

My machine is MacOS M1.
I am passing the context to the mongo client.
When running outside docker it work well, the problem is that i need to run the test inside of docker with golang:latest image...

This is the main e2e:

func TestE2ETestSuite(t *testing.T) {
	suite.Run(t, &e2eTestSuite{})
}

func (s *e2eTestSuite) SetupSuite() {
	// change current dir to root dir
	os.Chdir("..")

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	logger := utils.GetLogger()

	// Load configs
	os.Setenv("GO_E2E", "true")
	cfg := config.FromEnv("dev")

	// mock redis
	s.mr = miniredis.RunT(s.T())
	cfg.Redis.Addr = s.mr.Addr()

	// mock mongodb
	s.mockMongoDB = it.NewMockMongoDB()
	s.mockMongoDB.Register(s.T(), cfg)

	s.dummy = it.NewDummy()
	s.serverinfo = it.NewServer(cfg)
	s.serverinfo.SetRawUrl()

	s.server = sling.New().Base(s.serverinfo.RawURL)

	s.container = services.NewServiceContainer()
	err := s.container.RegisterAll(ctx, cfg)
	if err != nil {
		logger.Panic("failed to create service container: %w", err)
	}

	var router = Routes.SetupRouter(s.container)

	// clears the database before running test
	s.container.Mongo.DB().Drop(ctx)

	appReady := make(chan bool)
	app := application.New(logger, appReady)

	app.SetServer(httputils.NewServer(
		&http.Server{
			Addr:    cfg.HTTP.Addr,
			Handler: router,
		},
		logger,
	))

	go app.Run(ctx)
	<-appReady
}

func (s *e2eTestSuite) TearDownSuite() {
	s.mr.Close()
	s.mockMongoDB.Close()
	s.container.Close()
}

func (s *e2eTestSuite) Test_EndToEnd_000_Ping() {
	path := "ping"
	request := s.server.New().Get(path)

	response := it.Request(s.T(), request)
	s.Equal(http.StatusOK, response.StatusCode)

	byteBody, err := ioutil.ReadAll(response.Body)
	s.NoError(err)

	s.Equal("\"pong\"", strings.Trim(string(byteBody), "\n"))
	response.Body.Close()
}

@saniales I have one workaround:
It does work inside of an official mongo image by specifying MEMONGO_MONGOD_BIN, I think its because this image uses Ubuntu whereas golang uses Debian? or perhaps we have to run additional commands to start mongod? I will try more later and update if I have any progress.

   
FROM mongo:latest as test

# Required envs
ENV GO111MODULE=on \
    CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64 \
    MEMONGO_MONGOD_BIN=/usr/bin/mongod

# Update and upgrade repo
RUN apt-get update -y -q && apt-get upgrade -y -q 
# Install tools we might need
RUN DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y -q curl build-essential ca-certificates git 
# Download Go 1.17 and install it to /usr/local/go
RUN curl -OL https://golang.org/dl/go1.17.linux-arm64.tar.gz; mkdir /etc/golang; tar -xvzf go1.17.linux-arm64.tar.gz -C /etc/golang; ln -s /etc/golang/go/bin/go /usr/bin/go; rm -f go1.17.linux-arm64.tar.gz

# Make dir for app
WORKDIR /app
# Copy mod files
COPY go.mod go.sum ./
# Download and verify module
RUN go mod download && go mod verify
# Copy app
COPY . .

# Run tests
RUN go test -failfast ./it

FROM golang:latest as builder

COPY --from=test /app /app

WORKDIR /app

# Download and verify module
RUN go mod download && go mod verify

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main ./api/

Closed because we have a workaround. Thanks @guiaramos