debian-forge-composer/internal/client/utils.go
Brian C. Lane 856eb59edf client: Move the weldrcheck integration tests to client
With this change the integration tests can now also be run as unit tests
against the mocked server. The way it works is this:

internal/client/unit_test.go sets up the mock server and is built
when the `integration` build tag is *not* included.

internal/client/integration_test.go sets up the connection to an
existing server and is built when the `integration` build tag *is*
included.

The test code is built and run for both cases.

Currently they all pass for the integration test run. The unit test
cases need some work because the mocked server isn't a real server with
real depsolving and package lists. A future commit will fix this.
2020-03-27 19:07:33 +01:00

63 lines
1.6 KiB
Go

// Package weldrcheck contains functions used to run integration tests on a running API server
// Copyright (C) 2020 by Red Hat, Inc.
// nolint: deadcode,unused // These functions are used by the *_test.go code
package client
import (
"context"
"fmt"
"net"
"net/http"
"sort"
"strconv"
"time"
)
type TestState struct {
socket *http.Client
apiVersion int
repoDir string
}
// isStringInSlice returns true if the string is present, false if not
// slice must be sorted
// TODO decide if this belongs in a more widely useful package location
func isStringInSlice(slice []string, s string) bool {
i := sort.SearchStrings(slice, s)
if i < len(slice) && slice[i] == s {
return true
}
return false
}
func setUpTestState(socketPath string, timeout time.Duration) (*TestState, error) {
var state TestState
state.socket = &http.Client{
// TODO This may be too short/simple for downloading images
Timeout: timeout,
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", socketPath)
},
},
}
// Make sure the server is running
status, resp, err := GetStatusV0(state.socket)
if err != nil {
return nil, fmt.Errorf("status request failed with client error: %s", err)
}
if resp != nil {
return nil, fmt.Errorf("status request failed: %v\n", resp)
}
apiVersion, e := strconv.Atoi(status.API)
if e != nil {
state.apiVersion = 0
} else {
state.apiVersion = apiVersion
}
fmt.Printf("Running tests against %s %s server using V%d API\n\n", status.Backend, status.Build, state.apiVersion)
return &state, nil
}