From 98b2dab12890e14e1a129299dc8a966165d28956 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Tue, 2 Feb 2021 13:52:09 -0800 Subject: [PATCH] weldr: Add tests for blueprints/undo Test undo, and undo of an unknown commit. --- internal/weldr/api_test.go | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) 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)