debian-forge-composer/internal/client/unit_test.go
Brian C. Lane e3934108f7 client: Handle the differences between unit tests and integration tests
The mock server used by unit tests is slightly different than the
running server, mostly related to package names that are hard-coded.

This adds a bool to testState that can be used in the tests to alter the
expected behavior. It should be used as little as possible.
2020-03-27 19:07:33 +01:00

68 lines
1.6 KiB
Go

// Package client contains functions for communicating with the API server
// Copyright (C) 2020 by Red Hat, Inc.
// +build !integration
package client
import (
"io/ioutil"
"log"
"net"
"net/http"
"os"
"testing"
"time"
"github.com/osbuild/osbuild-composer/internal/distro/fedoratest"
rpmmd_mock "github.com/osbuild/osbuild-composer/internal/mocks/rpmmd"
"github.com/osbuild/osbuild-composer/internal/rpmmd"
"github.com/osbuild/osbuild-composer/internal/weldr"
)
// Hold test state to share between tests
var testState *TestState
func TestMain(m *testing.M) {
// Setup the mocked server running on a temporary domain socket
tmpdir, err := ioutil.TempDir("", "client_test-")
if err != nil {
panic(err)
}
defer os.RemoveAll(tmpdir)
socketPath := tmpdir + "/client_test.socket"
ln, err := net.Listen("unix", socketPath)
if err != nil {
panic(err)
}
// Create a mock API server listening on the temporary socket
fixture := rpmmd_mock.BaseFixture()
rpm := rpmmd_mock.NewRPMMDMock(fixture)
distro := fedoratest.New()
arch, err := distro.GetArch("x86_64")
if err != nil {
panic(err)
}
repos := []rpmmd.RepoConfig{{Id: "test-id", BaseURL: "http://example.com/test/os/test_arch"}}
logger := log.New(os.Stdout, "", 0)
api := weldr.New(rpm, arch, distro, repos, logger, fixture.Store)
server := http.Server{Handler: api}
defer server.Close()
go func() {
err := server.Serve(ln)
if err != nil {
panic(err)
}
}()
testState, err = setUpTestState(socketPath, 60*time.Second, true)
if err != nil {
log.Fatalf("ERROR: Test setup failed: %s\n", err)
}
// Run the tests
os.Exit(m.Run())
}