Skip to content

Commit

Permalink
tests and PR comments for status cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: David Lawrence <[email protected]> (github: endophage)
  • Loading branch information
David Lawrence committed Aug 1, 2016
1 parent b14b5e1 commit 57379e8
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 10 deletions.
23 changes: 23 additions & 0 deletions client/changelist/changelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,26 @@ func TestMemChangeIterator(t *testing.T) {
var iterError IteratorBoundsError
require.IsType(t, iterError, err, "IteratorBoundsError type")
}

func TestMemChangelistRemove(t *testing.T) {
cl := memChangelist{}
it, err := cl.NewIterator()
require.Nil(t, err, "Non-nil error from NewIterator")
require.False(t, it.HasNext(), "HasNext returns false for empty ChangeList")

c1 := NewTUFChange(ActionCreate, "t1", "target1", "test/targ1", []byte{1})
cl.Add(c1)

c2 := NewTUFChange(ActionUpdate, "t2", "target2", "test/targ2", []byte{2})
cl.Add(c2)

c3 := NewTUFChange(ActionUpdate, "t3", "target3", "test/targ3", []byte{3})
cl.Add(c3)

err = cl.Remove([]int{0, 1})
require.NoError(t, err)

chs := cl.List()
require.Len(t, chs, 1)
require.Equal(t, "t3", chs[0].Scope())
}
5 changes: 2 additions & 3 deletions client/changelist/file_changelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func getFileNames(dirName string) ([]os.FileInfo, error) {
}
fileInfos = append(fileInfos, f)
}
sort.Sort(fileChanges(fileInfos))
return fileInfos, nil
}

Expand All @@ -70,7 +71,6 @@ func (cl FileChangelist) List() []Change {
if err != nil {
return changes
}
sort.Sort(fileChanges(fileInfos))
for _, f := range fileInfos {
c, err := unmarshalFile(cl.dir, f)
if err != nil {
Expand Down Expand Up @@ -98,7 +98,6 @@ func (cl FileChangelist) Remove(idxs []int) error {
if err != nil {
return err
}
sort.Sort(fileChanges(fileInfos))
remove := make(map[int]struct{})
for _, i := range idxs {
remove[i] = struct{}{}
Expand All @@ -115,6 +114,7 @@ func (cl FileChangelist) Remove(idxs []int) error {
}

// Clear clears the change list
// N.B. archiving not currently implemented
func (cl FileChangelist) Clear(archive string) error {
dir, err := os.Open(cl.dir)
if err != nil {
Expand Down Expand Up @@ -143,7 +143,6 @@ func (cl FileChangelist) NewIterator() (ChangeIterator, error) {
if err != nil {
return &FileChangeListIterator{}, err
}
sort.Sort(fileChanges(fileInfos))
return &FileChangeListIterator{dirname: cl.dir, collection: fileInfos}, nil
}

Expand Down
14 changes: 7 additions & 7 deletions cmd/notary/tuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ type tufCommander struct {
output string
quiet bool

changes []int
resetStatus bool
deleteIdx []int
reset bool
archiveChangelist string
}

Expand All @@ -109,8 +109,8 @@ func (t *tufCommander) AddToCommand(cmd *cobra.Command) {
cmd.AddCommand(cmdTUFInit)

cmdStatus := cmdTUFStatusTemplate.ToCommand(t.tufStatus)
cmdStatus.Flags().IntSliceVarP(&t.changes, "unstage", "u", nil, "Numbers of changes to delete, as show in status list")
cmdStatus.Flags().BoolVar(&t.resetStatus, "reset", false, "Reset the changelist for the GUN by deleting all pending changes")
cmdStatus.Flags().IntSliceVarP(&t.deleteIdx, "unstage", "u", nil, "Numbers of changes to delete, as shown in status list")
cmdStatus.Flags().BoolVar(&t.reset, "reset", false, "Reset the changelist for the GUN by deleting all pending changes")
cmd.AddCommand(cmdStatus)

cmd.AddCommand(cmdTUFPublishTemplate.ToCommand(t.tufPublish))
Expand Down Expand Up @@ -448,12 +448,12 @@ func (t *tufCommander) tufStatus(cmd *cobra.Command, args []string) error {
return err
}

if t.resetStatus {
if t.reset {
return cl.Clear(t.archiveChangelist)
}

if len(t.changes) > 0 {
return cl.Remove(t.changes)
if len(t.deleteIdx) > 0 {
return cl.Remove(t.deleteIdx)
}

if len(cl.List()) == 0 {
Expand Down
64 changes: 64 additions & 0 deletions cmd/notary/tuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package main
import (
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -118,3 +121,64 @@ func TestAdminTokenAuthNon200Non401Status(t *testing.T) {
require.NoError(t, err)
require.Nil(t, auth)
}

func TestStatusUnstageAndReset(t *testing.T) {
setUp(t)
tempBaseDir := tempDirWithConfig(t, "{}")
defer os.RemoveAll(tempBaseDir)

tc := &tufCommander{
configGetter: func() (*viper.Viper, error) {
v := viper.New()
v.SetDefault("trust_dir", tempBaseDir)
return v, nil
},
}

tc.reset = true

// run a reset with an empty changelist and make sure it succeeds
err := tc.tufStatus(&cobra.Command{}, []string{"gun"})
require.NoError(t, err)

// add some targets
tc.sha256 = "88b76b34ab83a9e4d5abe3697950fb73f940aab1aa5b534f80cf9de9708942be"
err = tc.tufAddByHash(&cobra.Command{}, []string{"gun", "test1", "100"})
require.NoError(t, err)
tc.sha256 = "4a7c203ce63b036a1999ea74eebd307c338368eb2b32218b722de6c5fdc7f016"
err = tc.tufAddByHash(&cobra.Command{}, []string{"gun", "test2", "100"})
require.NoError(t, err)
tc.sha256 = "64bd0565907a6a55fc66fd828a71dbadd976fa875d0a3869f53d02eb8710ecb4"
err = tc.tufAddByHash(&cobra.Command{}, []string{"gun", "test3", "100"})
require.NoError(t, err)
tc.sha256 = "9d9e890af64dd0f44b8a1538ff5fa0511cc31bf1ab89f3a3522a9a581a70fad8"
err = tc.tufAddByHash(&cobra.Command{}, []string{"gun", "test4", "100"})
require.NoError(t, err)

out, err := runCommand(t, tempBaseDir, "status", "gun")
require.NoError(t, err)
require.Contains(t, out, "test1")
require.Contains(t, out, "test2")
require.Contains(t, out, "test3")
require.Contains(t, out, "test4")

_, err = runCommand(t, tempBaseDir, "status", "gun", "--unstage", "-1,1,3,10")

out, err = runCommand(t, tempBaseDir, "status", "gun")
require.NoError(t, err)
require.Contains(t, out, "test1")
require.NotContains(t, out, "test2")
require.Contains(t, out, "test3")
require.NotContains(t, out, "test4")

_, err = runCommand(t, tempBaseDir, "status", "gun", "--reset")
require.NoError(t, err)

out, err = runCommand(t, tempBaseDir, "status", "gun")
require.NoError(t, err)
require.NotContains(t, out, "test1")
require.NotContains(t, out, "test2")
require.NotContains(t, out, "test3")
require.NotContains(t, out, "test4")

}

0 comments on commit 57379e8

Please sign in to comment.