debian-forge-composer/internal/client/source_test.go
Brian C. Lane 55325a8549 weldr: Add API v1 support to source listing
This is the first patch in a series to add APIv1 support to the
/projects/source routes. The change involves using the store.Sources key
in a different way (as an id instead of as a duplicate of the struct's
Name field) but does not actually involve changing the Sources json in
the store.

In the V0 API the name of the source was used as the identifier, and
there was no short id. In V1 the source is identified by the API using
a short id, and the Name is just a field in the struct to describe the
source. This will become more obvious with the /projects/source/info
response.

This commit changes the following:

Changes store.ListSources to ListSourcesByName and explicitly pulls the
name from the source struct instead of the key. v0 will use this
function call.

Adds store.ListSourcesById which returns the source key as the
identifier. This is used by v1.

Adds a new weldr.SourcesListV1 response type, even though it is exactly
the same as the V1 response in this specific case. I thought it would be
better to have one called V1 than to reuse the V0 struct and possibly
confuse people.

The /projects/source/list API now lists the sources by name for v0 and id for v1.

A test has been added. You will notice it still uses v0 to push and
delete the sources. These will be updated when the new version of the
functions are added in subsequent commits.
2020-06-03 21:24:47 +02:00

351 lines
11 KiB
Go

// Package client - source_test contains functions to check the source API
// Copyright (C) 2020 by Red Hat, Inc.
// Tests should be self-contained and not depend on the state of the server
// They should use their own blueprints, not the default blueprints
// They should not assume version numbers for packages will match
// They should run tests that depend on previous results from the same function
// not from other functions.
package client
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// POST a new JSON source
func TestPOSTJSONSourceV0(t *testing.T) {
source := `{
"name": "package-repo-json-v0",
"url": "file://REPO-PATH",
"type": "yum-baseurl",
"proxy": "https://proxy-url/",
"check_ssl": true,
"check_gpg": true,
"gpgkey_urls": ["https://url/path/to/gpg-key"]
}`
source = strings.Replace(source, "REPO-PATH", testState.repoDir, 1)
resp, err := PostJSONSourceV0(testState.socket, source)
require.NoError(t, err, "POST source failed with a client error")
require.True(t, resp.Status, "POST source failed: %#v", resp)
resp, err = DeleteSourceV0(testState.socket, "package-repo-json-v0")
require.NoError(t, err, "DELETE source failed with a client error")
require.True(t, resp.Status, "DELETE source failed: %#v", resp)
}
// POST an empty JSON source
func TestPOSTEmptyJSONSourceV0(t *testing.T) {
resp, err := PostJSONSourceV0(testState.socket, "")
require.NoError(t, err, "POST source failed with a client error")
require.False(t, resp.Status, "did not return an error")
}
// POST an invalid JSON source
func TestPOSTInvalidJSONSourceV0(t *testing.T) {
// Missing quote in url
source := `{
"name": "package-repo-json-v0",
"url": "file://REPO-PATH,
"type": "yum-baseurl",
"proxy": "https://proxy-url/",
"check_ssl": true,
"check_gpg": true,
"gpgkey_urls": ["https://url/path/to/gpg-key"]
}`
resp, err := PostJSONSourceV0(testState.socket, source)
require.NoError(t, err, "POST source failed with a client error")
require.False(t, resp.Status, "did not return an error")
}
// POST a new TOML source
func TestPOSTTOMLSourceV0(t *testing.T) {
source := `
name = "package-repo-toml-v0"
url = "file://REPO-PATH"
type = "yum-baseurl"
proxy = "https://proxy-url/"
check_ssl = true
check_gpg = true
gpgkey_urls = ["https://url/path/to/gpg-key"]
`
source = strings.Replace(source, "REPO-PATH", testState.repoDir, 1)
resp, err := PostTOMLSourceV0(testState.socket, source)
require.NoError(t, err, "POST source failed with a client error")
require.True(t, resp.Status, "POST source failed: %#v", resp)
resp, err = DeleteSourceV0(testState.socket, "package-repo-toml-v0")
require.NoError(t, err, "DELETE source failed with a client error")
require.True(t, resp.Status, "DELETE source failed: %#v", resp)
}
// POST an empty TOML source
func TestPOSTEmptyTOMLSourceV0(t *testing.T) {
resp, err := PostTOMLSourceV0(testState.socket, "")
require.NoError(t, err, "POST source failed with a client error")
require.False(t, resp.Status, "did not return an error")
}
// POST an invalid TOML source
func TestPOSTInvalidTOMLSourceV0(t *testing.T) {
// Missing quote in url
source := `
name = "package-repo-toml-v0"
url = "file://REPO-PATH
type = "yum-baseurl"
proxy = "https://proxy-url/"
check_ssl = true
check_gpg = true
gpgkey_urls = ["https://url/path/to/gpg-key"]
`
resp, err := PostTOMLSourceV0(testState.socket, source)
require.NoError(t, err, "POST source failed with a client error")
require.False(t, resp.Status, "did not return an error")
}
// POST a wrong TOML source
func TestPOSTWrongTOMLSourceV0(t *testing.T) {
// Should not have a [] section
source := `
[package-repo-toml-v0]
name = "package-repo-toml-v0"
url = "file://REPO-PATH
type = "yum-baseurl"
proxy = "https://proxy-url/"
check_ssl = true
check_gpg = true
gpgkey_urls = ["https://url/path/to/gpg-key"]
`
resp, err := PostTOMLSourceV0(testState.socket, source)
require.NoError(t, err, "POST source failed with a client error")
require.False(t, resp.Status, "did not return an error")
}
// list sources using the v0 API
func TestListSourcesV0(t *testing.T) {
sources := []string{`{
"name": "package-repo-1",
"url": "file://REPO-PATH",
"type": "yum-baseurl",
"proxy": "https://proxy-url/",
"check_ssl": true,
"check_gpg": true,
"gpgkey_urls": ["https://url/path/to/gpg-key"]
}`,
`{
"name": "package-repo-2",
"url": "file://REPO-PATH",
"type": "yum-baseurl",
"proxy": "https://proxy-url/",
"check_ssl": true,
"check_gpg": true,
"gpgkey_urls": ["https://url/path/to/gpg-key"]
}`}
for i := range sources {
source := strings.Replace(sources[i], "REPO-PATH", testState.repoDir, 1)
resp, err := PostJSONSourceV0(testState.socket, source)
require.NoError(t, err, "POST source failed with a client error")
require.True(t, resp.Status, "POST source failed: %#v", resp)
}
// Remove the test sources, ignoring any errors
defer func() {
for _, n := range []string{"package-repo-1", "package-repo-2"} {
resp, err := DeleteSourceV0(testState.socket, n)
require.NoError(t, err, "DELETE source failed with a client error")
require.True(t, resp.Status, "DELETE source failed: %#v", resp)
}
}()
// Get the list of sources
list, api, err := ListSourcesV0(testState.socket)
require.NoError(t, err, "GET source failed with a client error")
require.Nil(t, api, "ListSources failed: %#v", api)
require.True(t, len(list) > 1, "Not enough sources returned")
require.Contains(t, list, "package-repo-1")
require.Contains(t, list, "package-repo-2")
}
// list sources using the v1 API
func TestListSourcesV1(t *testing.T) {
sources := []string{`{
"name": "package-repo-1",
"url": "file://REPO-PATH",
"type": "yum-baseurl",
"proxy": "https://proxy-url/",
"check_ssl": true,
"check_gpg": true,
"gpgkey_urls": ["https://url/path/to/gpg-key"]
}`,
`{
"name": "package-repo-2",
"url": "file://REPO-PATH",
"type": "yum-baseurl",
"proxy": "https://proxy-url/",
"check_ssl": true,
"check_gpg": true,
"gpgkey_urls": ["https://url/path/to/gpg-key"]
}`}
// TODO update for PostJSONSourceV1
for i := range sources {
source := strings.Replace(sources[i], "REPO-PATH", testState.repoDir, 1)
resp, err := PostJSONSourceV0(testState.socket, source)
require.NoError(t, err, "POST source failed with a client error")
require.True(t, resp.Status, "POST source failed: %#v", resp)
}
// TODO update for DeleteJSONSourceV1
// Remove the test sources, ignoring any errors
defer func() {
for _, n := range []string{"package-repo-1", "package-repo-2"} {
resp, err := DeleteSourceV0(testState.socket, n)
require.NoError(t, err, "DELETE source failed with a client error")
require.True(t, resp.Status, "DELETE source failed: %#v", resp)
}
}()
// Get the list of sources
list, api, err := ListSourcesV1(testState.socket)
require.NoError(t, err, "GET source failed with a client error")
require.Nil(t, api, "ListSources failed: %#v", api)
require.True(t, len(list) > 1, "Not enough sources returned")
require.Contains(t, list, "package-repo-1")
require.Contains(t, list, "package-repo-2")
}
// Get the source info
func TestGetSourceInfoV0(t *testing.T) {
source := `
name = "package-repo-info-v0"
url = "file://REPO-PATH"
type = "yum-baseurl"
proxy = "https://proxy-url/"
check_ssl = true
check_gpg = true
gpgkey_urls = ["https://url/path/to/gpg-key"]
`
source = strings.Replace(source, "REPO-PATH", testState.repoDir, 1)
resp, err := PostTOMLSourceV0(testState.socket, source)
require.NoError(t, err, "POST source failed with a client error")
require.True(t, resp.Status, "POST source failed: %#v", resp)
info, resp, err := GetSourceInfoV0(testState.socket, "package-repo-info-v0")
require.NoError(t, err, "GET source failed with a client error")
require.Nil(t, resp, "GET source failed: %#v", resp)
require.Contains(t, info, "package-repo-info-v0", "No source info returned")
require.Equal(t, "package-repo-info-v0", info["package-repo-info-v0"].Name)
require.Equal(t, "file://"+testState.repoDir, info["package-repo-info-v0"].URL)
resp, err = DeleteSourceV0(testState.socket, "package-repo-info-v0")
require.NoError(t, err, "DELETE source failed with a client error")
require.True(t, resp.Status, "DELETE source failed: %#v", resp)
}
func UploadUserDefinedSources(t *testing.T, sources []string) {
for i := range sources {
source := strings.Replace(sources[i], "REPO-PATH", testState.repoDir, 1)
resp, err := PostJSONSourceV0(testState.socket, source)
require.NoError(t, err, "POST source failed with a client error")
require.True(t, resp.Status, "POST source failed: %#v", resp)
}
}
// verify user defined sources are not present
func VerifyNoUserDefinedSources(t *testing.T, source_names []string) {
list, api, err := ListSourcesV0(testState.socket)
require.NoError(t, err, "GET source failed with a client error")
require.Nil(t, api, "ListSources failed: %#v", api)
require.GreaterOrEqual(t, len(list), 1, "Not enough sources returned")
for i := range source_names {
require.NotContains(t, list, source_names[i])
}
}
func TestDeleteUserDefinedSourcesV0(t *testing.T) {
source_names := []string{"package-repo-1", "package-repo-2"}
sources := []string{`{
"name": "package-repo-1",
"url": "file://REPO-PATH",
"type": "yum-baseurl",
"proxy": "https://proxy-url/",
"check_ssl": true,
"check_gpg": true,
"gpgkey_urls": ["https://url/path/to/gpg-key"]
}`,
`{
"name": "package-repo-2",
"url": "file://REPO-PATH",
"type": "yum-baseurl",
"proxy": "https://proxy-url/",
"check_ssl": true,
"check_gpg": true,
"gpgkey_urls": ["https://url/path/to/gpg-key"]
}`}
// verify test starts without user defined sources
VerifyNoUserDefinedSources(t, source_names)
// post user defined sources
UploadUserDefinedSources(t, sources)
// note: not verifying user defined sources have been pushed b/c correct
// operation of PostJSONSourceV0 is validated in the test functions above
// Remove the test sources
for _, n := range source_names {
resp, err := DeleteSourceV0(testState.socket, n)
require.NoError(t, err, "DELETE source failed with a client error")
require.True(t, resp.Status, "DELETE source failed: %#v", resp)
}
// verify removed sources are not present after removal
VerifyNoUserDefinedSources(t, source_names)
}
func Index(vs []string, t string) int {
for i, v := range vs {
if v == t {
return i
}
}
return -1
}
func Include(vs []string, t string) bool {
return Index(vs, t) >= 0
}
func TestDeleteSystemSourcesV0(t *testing.T) {
sources_list, api, err := ListSourcesV0(testState.socket)
require.NoError(t, err, "GET source failed with a client error")
require.Nil(t, api, "ListSources failed: %#v", api)
for _, repo_name := range []string{"test-system-repo", "fedora", "baseos"} {
// skip repository names which are not present b/c this test can be
// executed both as a unit test and as an integration test
if !Include(sources_list, repo_name) {
continue
}
// try removing system source
resp, err := DeleteSourceV0(testState.socket, repo_name)
require.NoError(t, err, "DELETE source failed with a client error")
require.True(t, resp.Status, "DELETE source failed: %#v", resp)
// verify that system sources are still there
list, api, err := ListSourcesV0(testState.socket)
require.NoError(t, err, "GET source failed with a client error")
require.Nil(t, api, "ListSources failed: %#v", api)
require.GreaterOrEqual(t, len(list), 1, "Not enough sources returned")
require.Contains(t, list, repo_name)
}
}