// RunWithMongoInDocker runs the tests with
// a mongodb instance in a docker container.
RunWithMongoInDocker runs the tests with
a mongodb instance in a docker container.
// RunWithMongoInDocker runs the tests with // a mongodb instance in a docker container .
func RunWithMongoInDocker(m *testing.M) int {
	c, err := client.NewEnvClient()
	if err != nil {
		panic(err)
	}
	ctx := context.Background()
	resp, err := c.ContainerCreate(ctx,
		&container.Config{
			Image: image,
			ExposedPorts: nat.PortSet{
				containerPort: {},
			},
		}, &container.HostConfig{
			PortBindings: nat.PortMap{
				containerPort: []nat.PortBinding{
					{
						HostIP:   "127.0.0.1",
						HostPort: "0", 
					},
				},
			},
		}, nil, nil, "")
	if err != nil {
		panic(err)
	}
	containerID := resp.ID
	defer func() {
		err = c.ContainerRemove(ctx, containerID, types.ContainerRemoveOptions{
			Force: true,
		})
		if err != nil {
			panic(err)
		}
	}()
	err = c.ContainerStart(ctx, containerID, types.ContainerStartOptions{})
	if err != nil {
		panic(err)
	}
	inspRes, err := c.ContainerInspect(ctx, containerID)
	if err != nil {
		panic(err)
	}
	hostPort := inspRes.NetworkSettings.Ports[containerPort][0]
	mongoURI = fmt.Sprintf("mongodb://%s:%s", hostPort.HostIP, hostPort.HostPort)
	return m.Run()
}
func RunWithMongoInDocker ( m * testing . M ) int { c , err := client . NewEnvClient ( ) if err != nil { panic ( err ) } ctx := context . Background ( ) resp , err := c . ContainerCreate ( ctx , & container . Config { Image : image , ExposedPorts : nat . PortSet { containerPort : { } , } , } , & container . HostConfig { PortBindings : nat . PortMap { containerPort : [ ] nat . PortBinding { { HostIP : " " , HostPort : " " , } , } , } , } , nil , nil , " " ) if err != nil { panic ( err ) } containerID := resp . ID defer func ( ) { err = c . ContainerRemove ( ctx , containerID , types . ContainerRemoveOptions { Force : true , } ) if err != nil { panic ( err ) } } ( ) err = c . ContainerStart ( ctx , containerID , types . ContainerStartOptions { } ) if err != nil { panic ( err ) } inspRes , err := c . ContainerInspect ( ctx , containerID ) if err != nil { panic ( err ) } hostPort := inspRes . NetworkSettings . Ports [ containerPort ] [ 0 ] mongoURI = fmt . Sprintf ( " " , hostPort . HostIP , hostPort . HostPort ) return m . Run ( ) }
RunWithMongoInDocker runs the tests with
a mongodb instance in a docker container.
RunWithMongoInDocker runs the tests with a mongodb instance in a docker container .
//本机的docker环境 //创建容器 //0自动挑选端口 //移除 //启动 //获取端口