Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated cherry pick of #88987: make filteredZones order predictable #89238

Merged
merged 2 commits into from Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -119,7 +119,7 @@ func addTopology(pv *v1.PersistentVolume, topologyKey string, zones []string) er
}
}

zones = filteredZones.UnsortedList()
zones = filteredZones.List()
if len(zones) < 1 {
return errors.New("there are no valid zones to add to pv")
}
Expand Down
Expand Up @@ -129,3 +129,86 @@ func TestTranslateAllowedTopologies(t *testing.T) {
}
}
}

func TestAddTopology(t *testing.T) {
testCases := []struct {
name string
topologyKey string
zones []string
expErr bool
expectedAffinity *v1.VolumeNodeAffinity
}{
{
name: "empty zones",
topologyKey: GCEPDTopologyKey,
zones: nil,
expErr: true,
},
{
name: "only whitespace-named zones",
topologyKey: GCEPDTopologyKey,
zones: []string{" ", "\n", "\t", " "},
expErr: true,
},
{
name: "including whitespace-named zones",
topologyKey: GCEPDTopologyKey,
zones: []string{" ", "us-central1-a"},
expErr: false,
expectedAffinity: &v1.VolumeNodeAffinity{
Required: &v1.NodeSelector{
NodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchExpressions: []v1.NodeSelectorRequirement{
{
Key: GCEPDTopologyKey,
Operator: v1.NodeSelectorOpIn,
Values: []string{"us-central1-a"},
},
},
},
},
},
},
},
{
name: "unsorted zones",
topologyKey: GCEPDTopologyKey,
zones: []string{"us-central1-f", "us-central1-a", "us-central1-c", "us-central1-b"},
expErr: false,
expectedAffinity: &v1.VolumeNodeAffinity{
Required: &v1.NodeSelector{
NodeSelectorTerms: []v1.NodeSelectorTerm{
{
MatchExpressions: []v1.NodeSelectorRequirement{
{
Key: GCEPDTopologyKey,
Operator: v1.NodeSelectorOpIn,
// Values are expected to be ordered
Values: []string{"us-central1-a", "us-central1-b", "us-central1-c", "us-central1-f"},
},
},
},
},
},
},
},
}

for _, tc := range testCases {
t.Logf("Running test: %v", tc.name)
pv := &v1.PersistentVolume{
Spec: v1.PersistentVolumeSpec{},
}
err := addTopology(pv, tc.topologyKey, tc.zones)
if err != nil && !tc.expErr {
t.Errorf("Did not expect an error, got: %v", err)
}
if err == nil && tc.expErr {
t.Errorf("Expected an error but did not get one")
}
if err == nil && !reflect.DeepEqual(pv.Spec.NodeAffinity, tc.expectedAffinity) {
t.Errorf("Expected affinity: %v, but got: %v", tc.expectedAffinity, pv.Spec.NodeAffinity)
}
}
}