Kong / kong-pongo

Tooling to run plugin tests with Kong and Kong Enterprise
Apache License 2.0
154 stars 57 forks source link

Integration tests fails on the first pongo run #626

Closed Aditya-Chowdhry closed 1 month ago

Aditya-Chowdhry commented 1 month ago

Hi,

I have been developing a plugin with Pongo setup. Here is the basic integration test

lazy_setup(function()
        assert(helpers.start_kong({
            plugins = "bundled,plugin_name", -- Enable the custom plugin
        }))
        mock = http_mock.new(8000, [[
                ngx.status = 200
                ngx.print("hello world")
            ]], {
            prefix = "mockserver",
            log_opts = {
                resp = true,
                resp_body = true,
            },
            tls = false,
        })

        assert(mock:start())
        -- Start Kong with the plugin enabled
        local bp = helpers.get_db_utils(strategy, nil, { "plugin_name" })

        -- Set up a mock upstream service using Nginx's echo server
        local service = bp.services:insert({
            name = "mock-service",
            url = "http://127.0.0.1:8000",
        })

        local route = bp.routes:insert({
            hosts = { "example.com" },
            paths = { "/login" }, -- Specify the route path
            service = service,
        })
        bp.plugins:insert({
            name = "plugin_name",
            route = { id = route.id },
            config = {}
        })
        client = helpers.proxy_client()
    end)

  it("should respond with the mocked API response on /login", function()
        local res = assert(client:request({
            method = "GET",
            path = "/login",
            headers = {
                host = "example.com",
            }
        }))
        -- Assert the response status code
        local body = assert.res_status(200, res)
        -- Assert the response body
        assert.is_not_nil(body)
        assert.equal("hello world", body)
    end)

When running the first time or after pongo clean. Usually, I follow with command pongo shell "kong migrations bootstrap && kong migrations up && exit"

Then running pongo run, tests always fail with error no Route matched with those values

Subsequent pongo runs are successful. I tried debugging and played around by making the setup wait for 5-10 seconds in order for kong to register routes but with no success. I failed to find any relevant docs or resources.

Let me know if there is any issue with my setup. Thanks

Aditya-Chowdhry commented 1 month ago

I made an error in ordering

 assert(helpers.start_kong({
            plugins = "bundled,plugin_name", -- Enable the custom plugin
        }))

Kong should be started after the initialization of services and routes.

Tieske commented 1 month ago

Thx for responding with your fix. Future us will be grateful 😉