Home  >  Article  >  Backend Development  >  Golang testcontainers, can't get network to work

Golang testcontainers, can't get network to work

WBOY
WBOYforward
2024-02-14 13:30:091062browse

Golang testcontainers,无法使网络工作

php editor strawberry encountered a problem when using Golang testcontainers, that is, it could not make the network work properly. Golang testcontainers is a tool for running containers in tests, which helps developers quickly start and destroy containers in a test environment. However, in actual use, PHP editor Strawberry found that the network function could not be used normally in the container, which caused certain problems in the test. Next, we will explore the solution to this problem together.

Question content

I'm trying to create some tests in my microservice, I want to create a network to attach my database test container (postgres) and my microservice test container to this network. No matter what I try, I can't get my microservice to connect to the database. My microservice is golang using Fiber and Gorm. I try to connect to the database in the db.go configuration file like this:

func SetupDB(port string, host string) *gorm.DB {

    dsn := "host=" + host + " user=postgres password=password dbname=prescription port=" + port + " sslmode=disable"

    db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

    if err != nil {
        panic("error connecting to database")
    }

    db.AutoMigrate(&model.Prescription{})

    return db
}

This is what my test container looks like:

prescriptionDBContainer, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{
        ContainerRequest: testcontainers.ContainerRequest{
            Image:        "postgres",
            ExposedPorts: []string{postgresPort.Port()},
            Env: map[string]string{
                "POSTGRES_USER":     "postgres",
                "POSTGRES_PASSWORD": "password",
                "POSTGRES_DB":       "prescription",
            },
            Networks: []string{network.Name},
            NetworkAliases: map[string][]string{
                network.Name: {"db-network"},
            },
            WaitingFor: wait.ForAll(
                wait.ForLog("database system is ready to accept connections"),
                wait.ForListeningPort(postgresPort),
            ),
        },
        Started: true,
    })
prescriptionContainer, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{
        ContainerRequest: testcontainers.ContainerRequest{
            FromDockerfile: testcontainers.FromDockerfile{Context: "../../../../prescription"},
            Networks:       []string{network.Name},
            NetworkAliases: map[string][]string{
                network.Name: {"db-network"},
            },
            Env: map[string]string{
                "POSTGRES_USER":     "postgres",
                "POSTGRES_PASSWORD": "password",
                "POSTGRES_DB":       "prescription",
                "HOST":              prescriptionDBHost,
                "DB_PORT":           prescriptionDBPort.Port(),
            },
            ExposedPorts: []string{pMicroPort.Port()},
            WaitingFor:   wait.ForListeningPort("8080"),
        },
        Started: true,
    })

Maybe it's because I simply don't understand what's going on during the network connection process in docker, but I'm really lost, when I set up the environment for HOST and DB_PORT (I've tried every combination under the sun), It refuses to connect to the database microservice

In the test container of the microservice I tried:

"HOST":              prescriptionDBHost,
"DB_PORT":           prescriptionDBPort.Port(),

The extraction method of prescriptionDBHost is:

prescriptionDBHost, err := prescriptionDBContainer.Name(context.Background())

Results in error message:

failed to initialize database, got error failed to connect to `host=/stoic_heyrovsky user=postgres database=prescription`: dial error (dial unix /stoic_heyrovsky/.s.PGSQL.53802: connect: no such file or directory)
panic: error connecting to database

Then I tried removing the "/" from the hostname, for example:

"HOST":              strings.Trim(prescriptionDBHost,"/"),
"DB_PORT":           prescriptionDBPort.Port(),

I also tried:

"HOST":              "localhost",
"DB_PORT":           prescriptionDBPort.Port(),
"HOST":              "127.0.0.1",
"DB_PORT":           prescriptionDBPort.Port(),
prescriptionDBHost, err := prescriptionDBContainer.ContainerIP(context.Background())

"HOST":              prescriptionDBHost,
"DB_PORT":           prescriptionDBPort.Port(),

The last 4 examples here will all result in some kind of dialup TCP error, for example:

failed to initialize database, got error failed to connect to `host=localhost user=postgres database=prescription`: dial error (dial tcp [::1]:53921: connect: cannot assign requested address)

I also debugged and stopped after creating the database container in testcontainer, then went to my microservice and hardcoded a connection to the container using DB_HOST=localhost and port= and it worked, so I'm really lost on what's going on What happened was wrong. The only thing I can think of is that the microservice container is not connected to the network before trying to connect to the database? I did a docker network check and I can see that the database container is attached, but the microservice never attaches (but maybe that's just for other reasons?).

Solution

You can do this:

prescriptionDBContainer, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{
    ContainerRequest: testcontainers.ContainerRequest{
        Image:        "postgres",
        ExposedPorts: []string{"5432/tcp"},
        Env: map[string]string{
            "POSTGRES_USER":     "postgres",
            "POSTGRES_PASSWORD": "password",
            "POSTGRES_DB":       "prescription",
        },
        Networks:       []string{networkName},
        NetworkAliases: map[string][]string{networkName: []string{"postgres"}},
        WaitingFor: wait.ForAll(
            wait.ForLog("database system is ready to accept connections"),
            wait.ForListeningPort("5432/tcp"),
        ),
    },
    Started: true,
})
if err != nil {
    t.Fatal(err)
}

prescriptionContainer, err := testcontainers.GenericContainer(context.Background(), testcontainers.GenericContainerRequest{
    ContainerRequest: testcontainers.ContainerRequest{
        FromDockerfile: testcontainers.FromDockerfile{Context: "./testapp"},
        ExposedPorts:   []string{"8080/tcp"},
        Networks:       []string{networkName},
        NetworkAliases: map[string][]string{networkName: []string{"blah"}},
        Env: map[string]string{
            "DATABASE_URL": "postgres://postgres:password@postgres:5432/prescription",
        },
        WaitingFor: wait.ForListeningPort("8080/tcp"),
    },
    Started: true,
})

Note how NetworkAliases are configured; in your code you set both to db-network However, I guess, this is due to a misunderstanding. This setting configures an alias that can refer to the container (in this example, I'm using postgres as the postgres container; this means that when connecting to HOST, according to the URL used in the example above , postgres will be postgres).

As an alternative, you can use port, err := prescription DBContainer.MappedPort(context.Background(), "5432/tcp") Get the port exposed on the host and then connect to the port host.docker.internal port.Port(). This method is often used when the application under test is running on the host rather than in a container (but in this case In this case, you will connect to localhost and use the report returned from MappedPort()).

The above is the detailed content of Golang testcontainers, can't get network to work. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete