weldr: Add support for the v1 API to /projects/source/new

This commit changes the store.PushSource function to take the key as
well as the SourceConfig so that it can be used for v0 or v1.

It adds helper functions for decoding the toml/json into a new
SourceConfig interface type which lets the core source/new code be
shared between the versions.

It also adds tests for the new API behavior.
This commit is contained in:
Brian C. Lane 2020-04-29 15:29:17 -07:00 committed by Tom Gundersen
parent 55325a8549
commit ddd2010815
6 changed files with 304 additions and 32 deletions

View file

@ -37,14 +37,21 @@ func TestPOSTJSONSourceV0(t *testing.T) {
require.True(t, resp.Status, "DELETE source failed: %#v", resp)
}
// POST an empty JSON source
// POST an empty JSON source using V0 API
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
// POST an empty JSON source using V1 API
func TestPOSTEmptyJSONSourceV1(t *testing.T) {
resp, err := PostJSONSourceV1(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 using V0 API
func TestPOSTInvalidJSONSourceV0(t *testing.T) {
// Missing quote in url
source := `{
@ -62,7 +69,26 @@ func TestPOSTInvalidJSONSourceV0(t *testing.T) {
require.False(t, resp.Status, "did not return an error")
}
// POST a new TOML source
// POST an invalid JSON source using V1 API
func TestPOSTInvalidJSONSourceV1(t *testing.T) {
// Missing quote in url
source := `{
"id": "package-repo-json-v1",
"name": "json package repo",
"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 := PostJSONSourceV1(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 using V0 API
func TestPOSTTOMLSourceV0(t *testing.T) {
source := `
name = "package-repo-toml-v0"
@ -84,14 +110,45 @@ func TestPOSTTOMLSourceV0(t *testing.T) {
require.True(t, resp.Status, "DELETE source failed: %#v", resp)
}
// POST an empty TOML source
// POST a new TOML source using V1 API
func TestPOSTTOMLSourceV1(t *testing.T) {
source := `
id = "package-repo-toml-v1"
name = "toml package repo"
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 := PostTOMLSourceV1(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
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 using V0 API
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
// POST an empty TOML source using V1 API
func TestPOSTEmptyTOMLSourceV1(t *testing.T) {
resp, err := PostTOMLSourceV1(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 using V0 API
func TestPOSTInvalidTOMLSourceV0(t *testing.T) {
// Missing quote in url
source := `
@ -109,7 +166,26 @@ func TestPOSTInvalidTOMLSourceV0(t *testing.T) {
require.False(t, resp.Status, "did not return an error")
}
// POST a wrong TOML source
// POST an invalid TOML source using V1 API
func TestPOSTInvalidTOMLSourceV1(t *testing.T) {
// Missing quote in url
source := `
id = "package-repo-toml-v1"
name = "toml package repo"
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 := PostTOMLSourceV1(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 using V0 API
func TestPOSTWrongTOMLSourceV0(t *testing.T) {
// Should not have a [] section
source := `
@ -128,6 +204,26 @@ func TestPOSTWrongTOMLSourceV0(t *testing.T) {
require.False(t, resp.Status, "did not return an error")
}
// POST a wrong TOML source using V1 API
func TestPOSTWrongTOMLSourceV1(t *testing.T) {
// Should not have a [] section
source := `
[package-repo-toml-v1]
id = "package-repo-toml-v1"
name = "toml package repo"
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 := PostTOMLSourceV1(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{`{
@ -177,7 +273,8 @@ func TestListSourcesV0(t *testing.T) {
// list sources using the v1 API
func TestListSourcesV1(t *testing.T) {
sources := []string{`{
"name": "package-repo-1",
"id": "package-repo-1",
"name": "First test package repo",
"url": "file://REPO-PATH",
"type": "yum-baseurl",
"proxy": "https://proxy-url/",
@ -186,7 +283,8 @@ func TestListSourcesV1(t *testing.T) {
"gpgkey_urls": ["https://url/path/to/gpg-key"]
}`,
`{
"name": "package-repo-2",
"id": "package-repo-2",
"name": "Second test package repo",
"url": "file://REPO-PATH",
"type": "yum-baseurl",
"proxy": "https://proxy-url/",
@ -195,10 +293,9 @@ func TestListSourcesV1(t *testing.T) {
"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)
resp, err := PostJSONSourceV1(testState.socket, source)
require.NoError(t, err, "POST source failed with a client error")
require.True(t, resp.Status, "POST source failed: %#v", resp)
}
@ -251,7 +348,7 @@ func TestGetSourceInfoV0(t *testing.T) {
require.True(t, resp.Status, "DELETE source failed: %#v", resp)
}
func UploadUserDefinedSources(t *testing.T, sources []string) {
func UploadUserDefinedSourcesV0(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)
@ -260,8 +357,17 @@ func UploadUserDefinedSources(t *testing.T, sources []string) {
}
}
func UploadUserDefinedSourcesV1(t *testing.T, sources []string) {
for i := range sources {
source := strings.Replace(sources[i], "REPO-PATH", testState.repoDir, 1)
resp, err := PostJSONSourceV1(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) {
func VerifyNoUserDefinedSourcesV0(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)
@ -271,6 +377,17 @@ func VerifyNoUserDefinedSources(t *testing.T, source_names []string) {
}
}
// verify user defined sources are not present
func VerifyNoUserDefinedSourcesV1(t *testing.T, source_names []string) {
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.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{`{
@ -293,10 +410,10 @@ func TestDeleteUserDefinedSourcesV0(t *testing.T) {
}`}
// verify test starts without user defined sources
VerifyNoUserDefinedSources(t, source_names)
VerifyNoUserDefinedSourcesV0(t, source_names)
// post user defined sources
UploadUserDefinedSources(t, sources)
UploadUserDefinedSourcesV0(t, sources)
// note: not verifying user defined sources have been pushed b/c correct
// operation of PostJSONSourceV0 is validated in the test functions above
@ -308,7 +425,50 @@ func TestDeleteUserDefinedSourcesV0(t *testing.T) {
}
// verify removed sources are not present after removal
VerifyNoUserDefinedSources(t, source_names)
VerifyNoUserDefinedSourcesV0(t, source_names)
}
func TestDeleteUserDefinedSourcesV1(t *testing.T) {
source_names := []string{"package-repo-1", "package-repo-2"}
sources := []string{`{
"id": "package-repo-1",
"name": "First test package repo",
"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"]
}`,
`{
"id": "package-repo-2",
"name": "Second test package repo",
"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
VerifyNoUserDefinedSourcesV1(t, source_names)
// post user defined sources
UploadUserDefinedSourcesV1(t, sources)
// note: not verifying user defined sources have been pushed b/c correct
// operation of PostJSONSourceV0 is validated in the test functions above
// TODO update for DeleteJSONSourceV1
// 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
VerifyNoUserDefinedSourcesV0(t, source_names)
}
func Index(vs []string, t string) int {