Skip to content
Snippets Groups Projects
server_test.go 1.49 KiB
Newer Older
ale's avatar
ale committed
package acmeserver

import (
	"context"
	"crypto"
	"crypto/x509"
	"errors"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"
	"time"
)

type fakeACME struct {
}

func (f *fakeACME) GetCertificate(_ context.Context, key crypto.Signer, domains []string) (der [][]byte, leaf *x509.Certificate, err error) {
	return nil, nil, errors.New("unimplemented")
}

func TestManager(t *testing.T) {
	dir, err := ioutil.TempDir("", "")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir)
	os.Mkdir(filepath.Join(dir, "config"), 0700)
	ioutil.WriteFile(
		filepath.Join(dir, "config", "test.yml"),
		[]byte("- { cn: example.com }\n"),
		0644,
	)

	m, err := NewManager(&Config{
		Dir:   dir,
		Email: "test@example.com",
	}, &fakeACME{})
	if err != nil {
		t.Fatal(err)
	}
	defer m.Stop()

	if err := m.Start(context.Background()); err != nil {
		t.Fatal("Start:", err)
	}

	// Wait just a little bit to give a chance to m.loop() to run.
	time.Sleep(50 * time.Millisecond)

	// Data race: we read data owned by another goroutine!
	if len(m.certs) < 1 {
		t.Fatal("configuration not loaded?")
	}
	if m.certs[0].domains[0] != "example.com" {
		t.Fatalf("certs[0].domains[0] is %s, expected example.com", m.certs[0].domains[0])
	}

	// Try a reload, catch obvious errors.
	m.Reload()
	time.Sleep(50 * time.Millisecond)

	if len(m.certs) != 1 {
		t.Fatalf("certs count is %d, expected 1", len(m.certs))
	}
	if m.certs[0].domains[0] != "example.com" {
		t.Fatalf("certs[0].domains[0] is %s, expected example.com", m.certs[0].domains[0])
	}
}