From 088ca6ec7231e032c8d835f35be54565f7c85fff Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Mon, 7 Nov 2022 14:32:47 -0800 Subject: [PATCH] client: Add GetBlueprintChangeV1 Add a function to recall a specific blueprint change. Also includes tests. --- internal/client/blueprints.go | 16 +++++++++ internal/client/blueprints_test.go | 52 +++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/internal/client/blueprints.go b/internal/client/blueprints.go index 15731462b..2c1003fac 100644 --- a/internal/client/blueprints.go +++ b/internal/client/blueprints.go @@ -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) diff --git a/internal/client/blueprints_test.go b/internal/client/blueprints_test.go index 75e795916..d53d1552c 100644 --- a/internal/client/blueprints_test.go +++ b/internal/client/blueprints_test.go @@ -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") +}