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.
This commit is contained in:
parent
856eb59edf
commit
e3934108f7
7 changed files with 88 additions and 11 deletions
|
|
@ -697,6 +697,33 @@ func TestNonBlueprintDepsolveV0(t *testing.T) {
|
|||
|
||||
// freeze a blueprint
|
||||
func TestBlueprintFreezeV0(t *testing.T) {
|
||||
if testState.unitTest {
|
||||
// The unit test mock server uses packages named dep-packageN
|
||||
bp := `{
|
||||
"name": "test",
|
||||
"description": "CheckBlueprintFreezeV0",
|
||||
"version": "0.0.1",
|
||||
"packages": [{"name": "dep-package1", "version": "*"}],
|
||||
"modules": [{"name": "dep-package2", "version": "*"}]
|
||||
}`
|
||||
|
||||
// Push a blueprint
|
||||
resp, err := PostJSONBlueprintV0(testState.socket, bp)
|
||||
require.NoError(t, err, "POST blueprint failed with a client error")
|
||||
require.True(t, resp.Status, "POST blueprint failed: %#v", resp)
|
||||
|
||||
// The unit test server returns hard-coded frozen packages
|
||||
// in the modules section with an empty packages list
|
||||
frozen, api, err := FreezeBlueprintV0(testState.socket, "test")
|
||||
require.NoError(t, err, "Freeze blueprint failed with a client error")
|
||||
require.Nil(t, api, "FreezeBlueprint failed: %#v", api)
|
||||
require.Greater(t, len(frozen.Blueprints), 0, "No frozen blueprints returned")
|
||||
require.Equal(t, 1, len(frozen.Blueprints[0].Blueprint.Packages), "No frozen packages returned")
|
||||
require.Equal(t, 1, len(frozen.Blueprints[0].Blueprint.Modules), "No frozen modules returned")
|
||||
return
|
||||
}
|
||||
|
||||
// Integration test
|
||||
bp := `{
|
||||
"name": "test-freeze-blueprint-v0",
|
||||
"description": "CheckBlueprintFreezeV0",
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ func TestRequest(t *testing.T) {
|
|||
// Test that apiError returns an error when trying to parse non-JSON response
|
||||
_, err = apiError(resp)
|
||||
if err == nil {
|
||||
t.Fatalf("apiError of a 404 response did not return an error")
|
||||
t.Fatalf("apiError of a 404 response did not return an error: %#v", resp)
|
||||
}
|
||||
|
||||
// Make a request with a bad offset to trigger a JSON response with Status set to 400
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ var testState *TestState
|
|||
// Also makes sure there is a running server to test against
|
||||
func TestMain(m *testing.M) {
|
||||
var err error
|
||||
testState, err = setUpTestState("/run/weldr/api.socket", 60*time.Second)
|
||||
testState, err = setUpTestState("/run/weldr/api.socket", 60*time.Second, false)
|
||||
if err != nil {
|
||||
fmt.Printf("ERROR: Test setup failed: %s\n", err)
|
||||
os.Exit(1)
|
||||
|
|
|
|||
|
|
@ -32,7 +32,15 @@ func TestListSomeModulesV0(t *testing.T) {
|
|||
|
||||
// List one module
|
||||
func TestListOneModulesV0(t *testing.T) {
|
||||
modules, api, err := ListModulesV0(testState.socket, "bash")
|
||||
var moduleNames string
|
||||
|
||||
// Unit test uses modules/packages named packageN
|
||||
if testState.unitTest {
|
||||
moduleNames = "package1"
|
||||
} else {
|
||||
moduleNames = "bash"
|
||||
}
|
||||
modules, api, err := ListModulesV0(testState.socket, moduleNames)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, api, "ListModules failed: %#v", api)
|
||||
require.True(t, len(modules) == 1, "Not enough modules returned")
|
||||
|
|
@ -40,7 +48,15 @@ func TestListOneModulesV0(t *testing.T) {
|
|||
|
||||
// List two modules
|
||||
func TestListTwoModulesV0(t *testing.T) {
|
||||
modules, api, err := ListModulesV0(testState.socket, "bash,tmux")
|
||||
var moduleNames string
|
||||
|
||||
// Unit test uses modules/packages named packageN
|
||||
if testState.unitTest {
|
||||
moduleNames = "package1,package2"
|
||||
} else {
|
||||
moduleNames = "bash,tmux"
|
||||
}
|
||||
modules, api, err := ListModulesV0(testState.socket, moduleNames)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, api, "ListModules failed: %#v", api)
|
||||
require.True(t, len(modules) == 2, "Not enough modules returned")
|
||||
|
|
@ -48,7 +64,15 @@ func TestListTwoModulesV0(t *testing.T) {
|
|||
|
||||
// Get info on a specific module
|
||||
func TestOneModuleInfoV0(t *testing.T) {
|
||||
modules, api, err := GetModulesInfoV0(testState.socket, "bash")
|
||||
var moduleNames string
|
||||
|
||||
// Unit test uses modules/packages named packageN
|
||||
if testState.unitTest {
|
||||
moduleNames = "package1"
|
||||
} else {
|
||||
moduleNames = "bash"
|
||||
}
|
||||
modules, api, err := GetModulesInfoV0(testState.socket, moduleNames)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, api, "GetModulesInfo failed: %#v", api)
|
||||
require.True(t, len(modules) == 1, "Not enough modules returned: %#v", modules)
|
||||
|
|
@ -56,7 +80,15 @@ func TestOneModuleInfoV0(t *testing.T) {
|
|||
|
||||
// Get info on two specific modules
|
||||
func TestTwoModuleInfoV0(t *testing.T) {
|
||||
modules, api, err := GetModulesInfoV0(testState.socket, "bash,tmux")
|
||||
var moduleNames string
|
||||
|
||||
// Unit test uses modules/packages named packageN
|
||||
if testState.unitTest {
|
||||
moduleNames = "package1,package2"
|
||||
} else {
|
||||
moduleNames = "bash,tmux"
|
||||
}
|
||||
modules, api, err := GetModulesInfoV0(testState.socket, moduleNames)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, api, "GetModulesInfo failed: %#v", api)
|
||||
require.True(t, len(modules) == 2, "Not enough modules returned: %#v", modules)
|
||||
|
|
|
|||
|
|
@ -33,7 +33,15 @@ func TestListSomeProjectsV0(t *testing.T) {
|
|||
|
||||
// Get info on a specific project
|
||||
func TestOneProjectsInfoV0(t *testing.T) {
|
||||
projs, api, err := GetProjectsInfoV0(testState.socket, "bash")
|
||||
var moduleNames string
|
||||
|
||||
// Unit test uses modules/packages named packageN
|
||||
if testState.unitTest {
|
||||
moduleNames = "package1"
|
||||
} else {
|
||||
moduleNames = "bash"
|
||||
}
|
||||
projs, api, err := GetProjectsInfoV0(testState.socket, moduleNames)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, api, "GetProjectsInfo failed: %#v", api)
|
||||
require.True(t, len(projs) == 1, "Not enough projects returned")
|
||||
|
|
@ -41,7 +49,15 @@ func TestOneProjectsInfoV0(t *testing.T) {
|
|||
|
||||
// Get info on a two specific projects
|
||||
func TestTwoProjectsInfoV0(t *testing.T) {
|
||||
projs, api, err := GetProjectsInfoV0(testState.socket, "bash,tmux")
|
||||
var moduleNames string
|
||||
|
||||
// Unit test uses modules/packages named packageN
|
||||
if testState.unitTest {
|
||||
moduleNames = "package1,package2"
|
||||
} else {
|
||||
moduleNames = "bash,tmux"
|
||||
}
|
||||
projs, api, err := GetProjectsInfoV0(testState.socket, moduleNames)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, api, "GetProjectsInfo failed: %#v", api)
|
||||
require.True(t, len(projs) == 2, "Not enough projects returned")
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ func TestMain(m *testing.M) {
|
|||
}
|
||||
}()
|
||||
|
||||
testState, err = setUpTestState(socketPath, 60*time.Second)
|
||||
testState, err = setUpTestState(socketPath, 60*time.Second, true)
|
||||
if err != nil {
|
||||
log.Fatalf("ERROR: Test setup failed: %s\n", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ type TestState struct {
|
|||
socket *http.Client
|
||||
apiVersion int
|
||||
repoDir string
|
||||
unitTest bool
|
||||
}
|
||||
|
||||
// isStringInSlice returns true if the string is present, false if not
|
||||
|
|
@ -31,8 +32,9 @@ func isStringInSlice(slice []string, s string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func setUpTestState(socketPath string, timeout time.Duration) (*TestState, error) {
|
||||
var state TestState
|
||||
func setUpTestState(socketPath string, timeout time.Duration, unitTest bool) (*TestState, error) {
|
||||
state := TestState{unitTest: unitTest}
|
||||
|
||||
state.socket = &http.Client{
|
||||
// TODO This may be too short/simple for downloading images
|
||||
Timeout: timeout,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue