client: Add GetBlueprintChangeV1

Add a function to recall a specific blueprint change. Also includes
tests.
This commit is contained in:
Brian C. Lane 2022-11-07 14:32:47 -08:00 committed by Ondřej Budai
parent e2011652e2
commit 088ca6ec72
2 changed files with 67 additions and 1 deletions

View file

@ -8,6 +8,7 @@ import (
"net/http"
"strings"
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/weldr"
)
@ -121,6 +122,21 @@ func GetBlueprintsChangesV0(socket *http.Client, bpNames []string) (weldr.Bluepr
return changes, nil, nil
}
// GetBlueprintChangeV1 returns a specific blueprint change
func GetBlueprintChangeV1(socket *http.Client, name, commit string) (blueprint.Blueprint, *APIResponse, error) {
route := fmt.Sprintf("/api/v1/blueprints/change/%s/%s", name, commit)
body, resp, err := GetRaw(socket, "GET", route)
if resp != nil || err != nil {
return blueprint.Blueprint{}, resp, err
}
var bp blueprint.Blueprint
err = json.Unmarshal(body, &bp)
if err != nil {
return blueprint.Blueprint{}, nil, err
}
return bp, nil, nil
}
// UndoBlueprintChangeV0 reverts a blueprint to a previous commit
func UndoBlueprintChangeV0(socket *http.Client, blueprint, commit string) (*APIResponse, error) {
request := fmt.Sprintf("/api/v0/blueprints/undo/%s/%s", blueprint, commit)

View file

@ -1082,4 +1082,54 @@ func TestFreezeInvalidBlueprintV0(t *testing.T) {
require.Contains(t, api.Errors[0].Msg, "Invalid characters in API path")
}
// TODO diff of blueprint changes
// Make several changes to a blueprint and get the oldest change by commit hash
func TestBlueprintChangeV1(t *testing.T) {
bps := []string{`{
"name": "test-blueprint-change-v1",
"description": "CheckBlueprintChangeV1",
"version": "0.0.1",
"packages": [{"name": "bash", "version": "*"}],
"modules": [{"name": "util-linux", "version": "*"}]
}`,
`{
"name": "test-blueprint-change-v1",
"description": "CheckBlueprintChangeV1",
"version": "0.1.0",
"packages": [{"name": "bash", "version": "*"}, {"name": "tmux", "version": "*"}],
"modules": [{"name": "util-linux", "version": "*"}],
"customizations": {"user": [{"name": "root", "password": "qweqweqwe"}]}
}`,
`{
"name": "test-blueprint-change-v1",
"description": "CheckBlueprintChangeV1",
"version": "0.1.1",
"packages": [{"name": "bash", "version": "*"}, {"name": "tmux", "version": "*"}],
"modules": [],
"customizations": {"user": [{"name": "root", "password": "asdasdasd"}]}
}`}
// Push 3 changes to the blueprint
for i := range bps {
resp, err := PostJSONBlueprintV0(testState.socket, bps[i])
require.NoError(t, err, "POST blueprint #%d failed with a client error")
require.True(t, resp.Status, "POST blueprint #%d failed: %#v", i, resp)
}
// List the changes
changes, api, err := GetBlueprintsChangesV0(testState.socket, []string{"test-blueprint-change-v1"})
require.NoError(t, err, "GET blueprint failed with a client error")
require.Nil(t, api, "GetBlueprintsChanges failed: %#v", api)
require.Equal(t, 1, len(changes.BlueprintsChanges), "No changes returned")
require.Equal(t, "test-blueprint-change-v1", changes.BlueprintsChanges[0].Name, "Wrong blueprint changes returned")
require.Greater(t, len(changes.BlueprintsChanges[0].Changes), 2, "Wrong number of changes returned")
// Get the oldest commit
commit := changes.BlueprintsChanges[0].Changes[2].Commit
t.Logf("commit = %s", commit)
t.Logf("changes = %v", changes)
bp, api, err := GetBlueprintChangeV1(testState.socket, "test-blueprint-change-v1", commit)
require.NoError(t, err, "GET blueprint change failed with a client error")
require.Nil(t, api, "GET blueprint change failed: %#v", api)
require.NotNil(t, bp, "GET blueprint change failed: missing blueprint")
assert.Equal(t, bp.Version, "0.0.1")
}