Add TestAddTarget

This test adds two targets, and after each one, inspects the changelist
files to make sure they meet expectations.
This commit is contained in:
Aaron Lehmann 2015-07-08 18:31:14 -07:00 committed by Diogo Monica
parent 6bff14a679
commit abe320307d
1 changed files with 107 additions and 0 deletions

View File

@ -130,3 +130,110 @@ func TestInitRepo(t *testing.T) {
}
}
}
type tufChange struct {
// Abbreviated because Go doesn't permit a field and method of the same name
Actn int `json:"action"`
Role string `json:"role"`
ChangeType string `json:"type"`
ChangePath string `json:"path"`
Data []byte `json:"data"`
}
// TestAddTarget adds a target to the repo and confirms that the changelist
// is updated correctly.
func TestAddTarget(t *testing.T) {
// Temporary directory where test files will be created
tempBaseDir, err := ioutil.TempDir("", "notary-test-")
assert.NoError(t, err, "failed to create a temporary directory: %s", err)
client, err := NewClient(tempBaseDir)
assert.NoError(t, err, "error creating client: %s", err)
rootKeyID, err := client.GenRootKey("passphrase")
assert.NoError(t, err, "error generating root key: %s", err)
rootKey, err := client.GetRootKey(rootKeyID, "passphrase")
assert.NoError(t, err, "error retreiving root key: %s", err)
gun := "docker.com/notary"
repo, err := client.InitRepository(gun, "", nil, rootKey)
assert.NoError(t, err, "error creating repository: %s", err)
// Add fixtures/ca.cert as a target. There's no particular reason
// for using this file except that it happens to be available as
// a fixture.
target, err := NewTarget("latest", "../fixtures/ca.cert")
assert.NoError(t, err, "error creating target")
err = repo.AddTarget(target)
assert.NoError(t, err, "error adding target")
// Look for the changelist file
changelistDirPath := filepath.Join(tempBaseDir, "tuf", gun, "changelist")
changelistDir, err := os.Open(changelistDirPath)
assert.NoError(t, err, "could not open changelist directory")
fileInfos, err := changelistDir.Readdir(0)
assert.NoError(t, err, "could not read changelist directory")
// Should only be one file in the directory
assert.Len(t, fileInfos, 1, "wrong number of changelist files found")
clName := fileInfos[0].Name()
raw, err := ioutil.ReadFile(filepath.Join(changelistDirPath, clName))
assert.NoError(t, err, "could not read changelist file %s", clName)
c := &tufChange{}
err = json.Unmarshal(raw, c)
assert.NoError(t, err, "could not unmarshal changelist file %s", clName)
assert.EqualValues(t, 0, c.Actn)
assert.Equal(t, "targets", c.Role)
assert.Equal(t, "target", c.ChangeType)
assert.Equal(t, "latest", c.ChangePath)
assert.NotEmpty(t, c.Data)
changelistDir.Close()
// Create a second target
target, err = NewTarget("current", "../fixtures/ca.cert")
assert.NoError(t, err, "error creating target")
err = repo.AddTarget(target)
assert.NoError(t, err, "error adding target")
changelistDir, err = os.Open(changelistDirPath)
assert.NoError(t, err, "could not open changelist directory")
// There should now be a second file in the directory
fileInfos, err = changelistDir.Readdir(0)
assert.NoError(t, err, "could not read changelist directory")
assert.Len(t, fileInfos, 2, "wrong number of changelist files found")
newFileFound := false
for _, fileInfo := range fileInfos {
if fileInfo.Name() != clName {
clName2 := fileInfo.Name()
raw, err := ioutil.ReadFile(filepath.Join(changelistDirPath, clName2))
assert.NoError(t, err, "could not read changelist file %s", clName2)
c := &tufChange{}
err = json.Unmarshal(raw, c)
assert.NoError(t, err, "could not unmarshal changelist file %s", clName2)
assert.EqualValues(t, 0, c.Actn)
assert.Equal(t, "targets", c.Role)
assert.Equal(t, "target", c.ChangeType)
assert.Equal(t, "current", c.ChangePath)
assert.NotEmpty(t, c.Data)
newFileFound = true
break
}
}
assert.True(t, newFileFound, "second changelist file not found")
changelistDir.Close()
}