Skip to content
This repository has been archived by the owner on Aug 14, 2020. It is now read-only.

Commit

Permalink
schema/pod: ensure volume names are unique
Browse files Browse the repository at this point in the history
The array "volumes" is indexed via the "name" field on each volume
object. This ensures that names are unique to avoid ambiguity in
mount-volume matching, by rejecting pod manifests containing
duplicate-named volumes.
  • Loading branch information
lucab committed Apr 28, 2017
1 parent d3e9aaa commit 0737776
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
9 changes: 9 additions & 0 deletions schema/pod.go
Expand Up @@ -83,6 +83,15 @@ func (pm *PodManifest) assertValid() error {
if pm.ACKind != PodManifestKind {
return pmKindError
}

// ensure volumes names are unique (unique key)
volNames := make(map[types.ACName]bool, len(pm.Volumes))
for _, vol := range pm.Volumes {
if volNames[vol.Name] {
return fmt.Errorf("duplicate volume name %q", vol.Name)
}
volNames[vol.Name] = true
}
return nil
}

Expand Down
40 changes: 40 additions & 0 deletions schema/pod_test.go
Expand Up @@ -71,3 +71,43 @@ func TestAppList(t *testing.T) {
t.Errorf("want err, got nil")
}
}

func TestPodManifestUniqueVolumeNames(t *testing.T) {
pm := BlankPodManifest()
volA, err := types.VolumeFromString("simple,kind=empty")
if err != nil {
t.Errorf("expected nil error, got %q", err)
}
volB, err := types.VolumeFromString("simple,kind=host,source=/tmp")
if err != nil {
t.Errorf("expected nil error, got %q", err)
}
pm.Volumes = append(pm.Volumes, *volA, *volB)
if _, err := pm.MarshalJSON(); err == nil {
t.Errorf("expected duplicate volume name error, got nil")
}

pmj := `
{
"acVersion": "0.8.10",
"acKind": "PodManifest",
"apps": [
],
"volumes": [
{
"name": "simplename",
"kind": "empty"
},
{
"name": "simplename",
"kind": "host",
"source": "/tmp"
}
]
}
`
err = pm.UnmarshalJSON([]byte(pmj))
if err == nil {
t.Errorf("expected duplicate volume name error, got nil")
}
}

0 comments on commit 0737776

Please sign in to comment.