Integration testing for modules in Go

Suppose I develop a new module Foo with a simple config block in Caddyfile like foo{} :eyes:

I wanna run an integration test for my module with caddy, instead of building it with xcaddy build –with <module-path>. Here’s my code:

func TestFoo_basic(t *testing.T) {
	caddyfile := `
	{
		auto_https off
	}
	
	:8080 {
		foo {
			respond "Hello World"
		}
	}`
	ta := caddytest.NewTester(t)
	ta.InitServer(caddyfile, "caddyfile")
   // stuff
}

If I run this, I’ll be getting the following:

adapting config using caddyfile adapter: Caddyfile:7: unrecognized directive: foo"

Question: How do I compile my module to run this integration test :roll_eyes:

Thank you!

1 Like

You need to make sure your module’s init() is called during test execution to register it. Importing your module’s package in the test should probably be enough, I think.

4 Likes

@francislavoie Thanks for your response. Below is the code that worked well for me:

func TestModule_Basic(t *testing.T) {
	tester := caddytest.NewTester(t)
	tester.InitServer(`
	{
		skip_install_trust
		http_port     2020
		https_port    9443
		grace_period 1ns
	}
	:2020 {
		route {
			foo {
			  anomaly_threshold 10
                          // module config here 
			}
		}
		respond "Hello, World!"
	}
	`, "caddyfile")

	time.Sleep(100 * time.Millisecond)
	tester.AssertGetResponse("http://localhost:2020/", 200, "Hello, World!")
}
1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.