github.com/pachyderm/pachyderm@v1.13.4/src/server/transaction/cmds/cmds_test.go (about) 1 package cmds 2 3 import ( 4 "fmt" 5 "os/exec" 6 "strings" 7 "testing" 8 9 "github.com/pachyderm/pachyderm/src/client/pkg/require" 10 tu "github.com/pachyderm/pachyderm/src/server/pkg/testutil" 11 ) 12 13 // TestTransaction runs a straightforward end-to-end test of starting, adding 14 // to and finishing a transaction. 15 func TestTransaction(t *testing.T) { 16 if testing.Short() { 17 t.Skip("Skipping integration tests in short mode") 18 } 19 repo := tu.UniqueString("TestTransaction-repo") 20 setup := tu.BashCmd(` 21 pachctl start transaction > /dev/null 22 pachctl create repo {{.repo}} 23 pachctl create branch {{.repo}}@master 24 pachctl start commit {{.repo}}@master 25 `, 26 "repo", repo, 27 ) 28 29 output, err := (*exec.Cmd)(setup).Output() 30 require.NoError(t, err) 31 commit := strings.TrimSpace(string(output)) 32 33 // The repo shouldn't exist yet 34 require.YesError(t, tu.BashCmd("pachctl inspect repo {{.repo}}", "repo", repo).Run()) 35 36 // Check that finishing the transaction creates the requested objects 37 require.NoError(t, tu.BashCmd(` 38 pachctl finish transaction 39 pachctl inspect repo {{.repo}} 40 pachctl inspect commit {{.repo}}@master 41 pachctl inspect commit {{.repo}}@{{.commit}} 42 `, 43 "repo", repo, 44 "commit", commit, 45 ).Run()) 46 } 47 48 func startTransaction(t *testing.T) string { 49 output, err := (*exec.Cmd)(tu.BashCmd("pachctl start transaction")).Output() 50 require.NoError(t, err) 51 52 strs := strings.Split(strings.TrimSpace(string(output)), " ") 53 return strs[len(strs)-1] 54 } 55 56 func requireTransactionDoesNotExist(t *testing.T, txn string) { 57 output, err := (*exec.Cmd)(tu.BashCmd("pachctl inspect transaction {{.txn}} 2>&1", "txn", txn)).Output() 58 require.YesError(t, err) 59 expected := fmt.Sprintf("%s not found", txn) 60 require.True(t, strings.Contains(string(output), expected)) 61 } 62 63 func TestDeleteActiveTransaction(t *testing.T) { 64 // Start then delete a transaction 65 txn := startTransaction(t) 66 require.NoError(t, tu.BashCmd("pachctl delete transaction").Run()) 67 68 // Check that the transaction no longer exists 69 requireTransactionDoesNotExist(t, txn) 70 } 71 72 func TestDeleteInactiveTransaction(t *testing.T) { 73 // Start, stop, then delete a transaction 74 txn := startTransaction(t) 75 require.NoError(t, tu.BashCmd("pachctl stop transaction").Run()) 76 require.NoError(t, tu.BashCmd("pachctl delete transaction {{.txn}}", "txn", txn).Run()) 77 78 // Check that the transaction no longer exists 79 requireTransactionDoesNotExist(t, txn) 80 } 81 82 func TestDeleteSpecificTransaction(t *testing.T) { 83 // Start two transactions, delete the first one, make sure the correct one is deleted 84 txn1 := startTransaction(t) 85 txn2 := startTransaction(t) 86 87 // Check that both transactions exist 88 require.NoError(t, tu.BashCmd("pachctl inspect transaction {{.txn}}", "txn", txn1).Run()) 89 require.NoError(t, tu.BashCmd("pachctl inspect transaction {{.txn}}", "txn", txn2).Run()) 90 91 require.NoError(t, tu.BashCmd("pachctl delete transaction {{.txn}}", "txn", txn1).Run()) 92 93 // Check that only the second transaction exists 94 requireTransactionDoesNotExist(t, txn1) 95 require.NoError(t, tu.BashCmd("pachctl inspect transaction {{.txn}}", "txn", txn2).Run()) 96 }