Repository: osbuild/osbuild-composer
Pull request #1194 (2021-02-02 23:05:27)

Test undo and fix error returned when undoing unknown commit
This pull request includes:

- [ ] adequate testing for the new functionality or fixed issue
- [ ] adequate documentation informing people about the change
- [ ] PR contains automated tests for new functionality and
- [ ] Subsequent commits provide bug fixes without modifying the reproducer; CI reports PASS and





Patch:
diff --git a/internal/weldr/api.go b/internal/weldr/api.go
index 166a86661..8b9764723 100644
--- a/internal/weldr/api.go
+++ b/internal/weldr/api.go
@@ -1664,7 +1664,7 @@ func (api *API) blueprintUndoHandler(writer http.ResponseWriter, request *http.R
 	bpChange, err := api.store.GetBlueprintChange(name, commit)
 	if err != nil {
 		errors := responseError{
-			ID:  "BlueprintsError",
+			ID:  "UnknownCommit",
 			Msg: err.Error(),
 		}
 		statusResponseError(writer, http.StatusBadRequest, errors)


Test patch:
diff --git a/internal/weldr/api_test.go b/internal/weldr/api_test.go
index 8af9c1fc8..70e9546df 100644
--- a/internal/weldr/api_test.go
+++ b/internal/weldr/api_test.go
@@ -3,6 +3,7 @@ package weldr
 import (
 	"archive/tar"
 	"bytes"
+	"encoding/json"
 	"io"
 	"io/ioutil"
 	"math/rand"
@@ -533,6 +534,45 @@ func TestBlueprintsDepsolve(t *testing.T) {
 	}
 }
 
+func TestBlueprintsUndo(t *testing.T) {
+	tempdir, err := ioutil.TempDir("", "weldr-tests-")
+	require.NoError(t, err)
+	defer os.RemoveAll(tempdir)
+
+	api, _ := createWeldrAPI(tempdir, rpmmd_mock.BaseFixture)
+	rand.Seed(time.Now().UnixNano())
+	id := strconv.Itoa(rand.Int())
+	ignoreFields := []string{"commit", "timestamp"}
+
+	test.SendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"`+id+`","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}],"version":"0.0.1"}`)
+	test.SendHTTP(api, true, "POST", "/api/v0/blueprints/new", `{"name":"`+id+`","description":"Test","packages":[{"name":"httpd","version":"2.4.*"}, {"name": "tmux", "version":"*"}],"version":"0.1.0"}`)
+
+	test.TestRoute(t, api, true, "GET", "/api/v0/blueprints/changes/"+id, ``, http.StatusOK, `{"blueprints":[{"changes":[{"commit":"","message":"Recipe `+id+`, version 0.1.0 saved.","revision":null,"timestamp":""},{"commit":"","message":"Recipe `+id+`, version 0.0.1 saved.","revision":null,"timestamp":""}],"name":"`+id+`","total":2}],"errors":[],"limit":20,"offset":0}`, ignoreFields...)
+
+	resp := test.SendHTTP(api, true, "GET", "/api/v0/blueprints/changes/"+id, ``)
+	body, err := ioutil.ReadAll(resp.Body)
+	require.Nil(t, err)
+	require.Equal(t, http.StatusOK, resp.StatusCode)
+
+	var changes BlueprintsChangesV0
+	err = json.Unmarshal(body, &changes)
+	require.Nil(t, err)
+	require.Equal(t, 1, len(changes.BlueprintsChanges))
+	require.Equal(t, 2, len(changes.BlueprintsChanges[0].Changes))
+	commit := changes.BlueprintsChanges[0].Changes[1].Commit
+
+	// Undo an unknown commit
+	test.TestRoute(t, api, true, "POST", "/api/v0/blueprints/undo/"+id+"/d7e5fa641aad45300242a0f273827576e32bfc03", ``, http.StatusBadRequest, `{"status":false,"errors":[{"id":"UnknownCommit","msg":"Unknown commit"}]}`)
+
+	// Undo a known commit
+	test.TestRoute(t, api, true, "POST", "/api/v0/blueprints/undo/"+id+"/"+commit, ``, http.StatusOK, `{"status":true}`)
+
+	// Check to make sure the undo is present
+	test.TestRoute(t, api, true, "GET", "/api/v0/blueprints/changes/"+id, ``, http.StatusOK, `{"blueprints":[{"changes":[{"commit":"","message":"`+id+`.toml reverted to commit `+commit+`","revision":null,"timestamp":""},{"commit":"","message":"Recipe `+id+`, version 0.1.0 saved.","revision":null,"timestamp":""},{"commit":"","message":"Recipe `+id+`, version 0.0.1 saved.","revision":null,"timestamp":""}],"name":"`+id+`","total":3}],"errors":[],"limit":20,"offset":0}`, ignoreFields...)
+
+	test.SendHTTP(api, true, "DELETE", "/api/v0/blueprints/delete/"+id, ``)
+}
+
 func TestCompose(t *testing.T) {
 	arch, err := test_distro.New().GetArch("x86_64")
 	require.NoError(t, err)