worker: fixed duplicated ns and ns not inherited bug

This commit is contained in:
Sida Chen 2017-05-30 13:45:14 -04:00 committed by Sida Chen
parent 75d5d40d79
commit f0e21df783
2 changed files with 19 additions and 11 deletions

View File

@ -145,26 +145,29 @@ func detectContent(imageFormat, name, path string, headers map[string]string, pa
return return
} }
// detectNamespaces returns a list of unique namespaces detected in a layer and its ancestry.
func detectNamespaces(name string, files tarutil.FilesMap, parent *database.Layer) (namespaces []database.Namespace, err error) { func detectNamespaces(name string, files tarutil.FilesMap, parent *database.Layer) (namespaces []database.Namespace, err error) {
namespaces, err = featurens.Detect(files) nsSet := map[string]*database.Namespace{}
nsCurrent, err := featurens.Detect(files)
if err != nil { if err != nil {
return return
} }
if len(namespaces) > 0 {
for _, ns := range namespaces { for _, ns := range nsCurrent {
nsSet[ns.Name] = &ns
log.WithFields(log.Fields{logLayerName: name, "detected namespace": ns.Name}).Debug("detected namespace") log.WithFields(log.Fields{logLayerName: name, "detected namespace": ns.Name}).Debug("detected namespace")
} }
return
}
// Fallback to the parent's namespace.
if parent != nil { if parent != nil {
for _, ns := range parent.Namespaces { for _, ns := range parent.Namespaces {
nsSet[ns.Name] = &ns
log.WithFields(log.Fields{logLayerName: name, "detected namespace": ns.Name}).Debug("detected namespace (from parent)") log.WithFields(log.Fields{logLayerName: name, "detected namespace": ns.Name}).Debug("detected namespace (from parent)")
} }
return
} }
for _, ns := range nsSet {
namespaces = append(namespaces, *ns)
}
return return
} }

View File

@ -101,11 +101,16 @@ func TestProcessWithDistUpgrade(t *testing.T) {
// Ensure that the 'wheezy' layer has the expected namespace and non-upgraded features. // Ensure that the 'wheezy' layer has the expected namespace and non-upgraded features.
jessie, ok := datastore.layers["jessie"] jessie, ok := datastore.layers["jessie"]
if assert.True(t, ok, "layer 'jessie' not processed") { if assert.True(t, ok, "layer 'jessie' not processed") {
if !assert.Len(t, jessie.Namespaces, 1) { if !assert.Len(t, jessie.Namespaces, 2) {
return return
} }
assert.Equal(t, "debian:8", jessie.Namespaces[0].Name) nsNames := []string{jessie.Namespaces[0].Name, jessie.Namespaces[1].Name}
assert.Len(t, jessie.Features, 74) // the old features and namespace should remain here with only the features in new namespaces detected
assert.Contains(t, nsNames, "debian:8")
assert.Contains(t, nsNames, "debian:7")
// because the featurefmt detects the features under "debian:8" and "debian:7", therefore the number of features is duplicated
assert.Len(t, jessie.Features, 148)
for _, nufv := range nonUpgradedFeatureVersions { for _, nufv := range nonUpgradedFeatureVersions {
nufv.Feature.Namespace.Name = "debian:7" nufv.Feature.Namespace.Name = "debian:7"