2020-04-05 21:54:30 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 ceph-csi authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
|
|
|
)
|
|
|
|
|
2020-07-06 05:31:52 +00:00
|
|
|
func checkError(t *testing.T, msg string, err error) {
|
2021-07-11 06:41:39 +00:00
|
|
|
t.Helper()
|
2020-07-06 05:31:52 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkAndReportError(t *testing.T, msg string, err error) {
|
2021-07-11 06:41:39 +00:00
|
|
|
t.Helper()
|
2020-07-06 05:31:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s (%v)", msg, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// TestFindPoolAndTopology also tests MatchTopologyForPool.
|
2020-04-05 21:54:30 +00:00
|
|
|
func TestFindPoolAndTopology(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2020-04-05 21:54:30 +00:00
|
|
|
var err error
|
2021-07-13 12:21:05 +00:00
|
|
|
label1 := "region"
|
|
|
|
label2 := "zone"
|
|
|
|
l1Value1 := "R1"
|
|
|
|
l1Value2 := "R2"
|
|
|
|
l2Value1 := "Z1"
|
|
|
|
l2Value2 := "Z2"
|
|
|
|
pool1 := "PoolA"
|
|
|
|
pool2 := "PoolB"
|
|
|
|
topologyPrefix := "prefix"
|
|
|
|
emptyTopoPools := []TopologyConstrainedPool{}
|
|
|
|
emptyPoolNameTopoPools := []TopologyConstrainedPool{
|
2020-04-05 21:54:30 +00:00
|
|
|
{
|
|
|
|
DomainSegments: []topologySegment{
|
|
|
|
{
|
|
|
|
DomainLabel: label1,
|
|
|
|
DomainValue: l1Value1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DomainLabel: label2,
|
|
|
|
DomainValue: l2Value1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-07-13 12:21:05 +00:00
|
|
|
emptyDomainsInTopoPools := []TopologyConstrainedPool{
|
2020-04-05 21:54:30 +00:00
|
|
|
{
|
|
|
|
PoolName: pool1,
|
|
|
|
},
|
|
|
|
}
|
2021-07-13 12:21:05 +00:00
|
|
|
partialDomainsInTopoPools := []TopologyConstrainedPool{
|
2020-04-05 21:54:30 +00:00
|
|
|
{
|
|
|
|
PoolName: pool1,
|
|
|
|
DomainSegments: []topologySegment{
|
|
|
|
{
|
|
|
|
DomainLabel: label1,
|
|
|
|
DomainValue: l1Value1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-07-13 12:21:05 +00:00
|
|
|
differentDomainsInTopoPools := []TopologyConstrainedPool{
|
2020-04-05 21:54:30 +00:00
|
|
|
{
|
|
|
|
PoolName: pool1,
|
|
|
|
DomainSegments: []topologySegment{
|
|
|
|
{
|
|
|
|
DomainLabel: label1 + "fuzz1",
|
|
|
|
DomainValue: l1Value1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DomainLabel: label2,
|
|
|
|
DomainValue: l2Value1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PoolName: pool2,
|
|
|
|
DomainSegments: []topologySegment{
|
|
|
|
{
|
|
|
|
DomainLabel: label1,
|
|
|
|
DomainValue: l1Value2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DomainLabel: label2,
|
|
|
|
DomainValue: l2Value2 + "fuzz1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-07-13 12:21:05 +00:00
|
|
|
validSingletonTopoPools := []TopologyConstrainedPool{
|
2020-04-05 21:54:30 +00:00
|
|
|
{
|
|
|
|
PoolName: pool1,
|
|
|
|
DomainSegments: []topologySegment{
|
|
|
|
{
|
|
|
|
DomainLabel: label1,
|
|
|
|
DomainValue: l1Value1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DomainLabel: label2,
|
|
|
|
DomainValue: l2Value1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-07-13 12:21:05 +00:00
|
|
|
validMultipleTopoPools := []TopologyConstrainedPool{
|
2020-04-05 21:54:30 +00:00
|
|
|
{
|
|
|
|
PoolName: pool1,
|
|
|
|
DomainSegments: []topologySegment{
|
|
|
|
{
|
|
|
|
DomainLabel: label1,
|
|
|
|
DomainValue: l1Value1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DomainLabel: label2,
|
|
|
|
DomainValue: l2Value1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PoolName: pool2,
|
|
|
|
DomainSegments: []topologySegment{
|
|
|
|
{
|
|
|
|
DomainLabel: label1,
|
|
|
|
DomainValue: l1Value2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DomainLabel: label2,
|
|
|
|
DomainValue: l2Value2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-07-13 12:21:05 +00:00
|
|
|
emptyAccReq := csi.TopologyRequirement{}
|
|
|
|
emptySegmentAccReq := csi.TopologyRequirement{
|
2020-04-05 21:54:30 +00:00
|
|
|
Requisite: []*csi.Topology{
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
},
|
|
|
|
}
|
2021-07-13 12:21:05 +00:00
|
|
|
partialHigherSegmentAccReq := csi.TopologyRequirement{
|
2020-04-05 21:54:30 +00:00
|
|
|
Preferred: []*csi.Topology{
|
|
|
|
{
|
|
|
|
Segments: map[string]string{
|
|
|
|
topologyPrefix + "/" + label1: l1Value1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-07-13 12:21:05 +00:00
|
|
|
partialLowerSegmentAccReq := csi.TopologyRequirement{
|
2020-04-05 21:54:30 +00:00
|
|
|
Preferred: []*csi.Topology{
|
|
|
|
{
|
|
|
|
Segments: map[string]string{
|
|
|
|
topologyPrefix + "/" + label2: l2Value1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-07-13 12:21:05 +00:00
|
|
|
differentSegmentAccReq := csi.TopologyRequirement{
|
2020-04-05 21:54:30 +00:00
|
|
|
Requisite: []*csi.Topology{
|
|
|
|
{
|
|
|
|
Segments: map[string]string{
|
|
|
|
topologyPrefix + "/" + label1 + "fuzz2": l1Value1,
|
|
|
|
topologyPrefix + "/" + label2: l2Value1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Segments: map[string]string{
|
|
|
|
topologyPrefix + "/" + label1: l1Value2,
|
|
|
|
topologyPrefix + "/" + label2: l2Value2 + "fuzz2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-07-13 12:21:05 +00:00
|
|
|
validAccReq := csi.TopologyRequirement{
|
2020-04-05 21:54:30 +00:00
|
|
|
Requisite: []*csi.Topology{
|
|
|
|
{
|
|
|
|
Segments: map[string]string{
|
|
|
|
topologyPrefix + "/" + label1: l1Value1,
|
|
|
|
topologyPrefix + "/" + label2: l2Value1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Segments: map[string]string{
|
|
|
|
topologyPrefix + "/" + label1: l1Value2,
|
|
|
|
topologyPrefix + "/" + label2: l2Value2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Preferred: []*csi.Topology{
|
|
|
|
{
|
|
|
|
Segments: map[string]string{
|
|
|
|
topologyPrefix + "/" + label1: l1Value1,
|
|
|
|
topologyPrefix + "/" + label2: l2Value1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Segments: map[string]string{
|
|
|
|
topologyPrefix + "/" + label1: l1Value2,
|
|
|
|
topologyPrefix + "/" + label2: l2Value2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
checkOutput := func(err error, poolName string, topoSegment map[string]string) error {
|
|
|
|
if err != nil {
|
2021-07-10 10:45:11 +00:00
|
|
|
return fmt.Errorf("expected success, got err (%w)", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
}
|
|
|
|
if poolName != pool1 || !(len(topoSegment) == 2) &&
|
|
|
|
topoSegment[topologyPrefix+"/"+label1] == l1Value1 &&
|
|
|
|
topoSegment[topologyPrefix+"/"+label2] == l2Value1 {
|
|
|
|
return fmt.Errorf("expected poolName (%s) and topoSegment (%s %s), got (%s) and (%v)", pool1,
|
|
|
|
topologyPrefix+"/"+label1+l1Value1, topologyPrefix+"/"+label2+l2Value1,
|
|
|
|
poolName, topoSegment)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Test nil values
|
2020-04-06 20:19:13 +00:00
|
|
|
_, _, _, err = FindPoolAndTopology(nil, nil)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkAndReportError(t, "expected success due to nil in-args", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
|
2020-04-06 20:19:13 +00:00
|
|
|
poolName, _, _, err := FindPoolAndTopology(&validMultipleTopoPools, nil)
|
2020-04-05 21:54:30 +00:00
|
|
|
if err != nil || poolName != "" {
|
|
|
|
t.Errorf("expected success due to nil accessibility requirements (err - %v) (poolName - %s)", err, poolName)
|
|
|
|
}
|
|
|
|
|
2020-04-06 20:19:13 +00:00
|
|
|
poolName, _, _, err = FindPoolAndTopology(nil, &validAccReq)
|
2020-04-05 21:54:30 +00:00
|
|
|
if err != nil || poolName != "" {
|
|
|
|
t.Errorf("expected success due to nil topology pools (err - %v) (poolName - %s)", err, poolName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test valid accessibility requirement, with invalid topology pools values
|
2020-04-06 20:19:13 +00:00
|
|
|
_, _, _, err = FindPoolAndTopology(&emptyTopoPools, &validAccReq)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkError(t, "expected failure due to empty topology pools", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
|
2020-04-06 20:19:13 +00:00
|
|
|
_, _, _, err = FindPoolAndTopology(&emptyPoolNameTopoPools, &validAccReq)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkError(t, "expected failure due to missing pool name in topology pools", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
|
2020-04-06 20:19:13 +00:00
|
|
|
_, _, _, err = FindPoolAndTopology(&differentDomainsInTopoPools, &validAccReq)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkError(t, "expected failure due to mismatching domains in topology pools", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
|
|
|
|
// Test valid topology pools, with invalid accessibility requirements
|
2020-04-06 20:19:13 +00:00
|
|
|
_, _, _, err = FindPoolAndTopology(&validMultipleTopoPools, &emptyAccReq)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkError(t, "expected failure due to empty accessibility requirements", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
|
2020-04-06 20:19:13 +00:00
|
|
|
_, _, _, err = FindPoolAndTopology(&validSingletonTopoPools, &emptySegmentAccReq)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkError(t, "expected failure due to empty segments in accessibility requirements", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
|
2020-04-06 20:19:13 +00:00
|
|
|
_, _, _, err = FindPoolAndTopology(&validMultipleTopoPools, &partialHigherSegmentAccReq)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkError(t, "expected failure due to partial segments in accessibility requirements", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
|
2020-04-06 20:19:13 +00:00
|
|
|
_, _, _, err = FindPoolAndTopology(&validSingletonTopoPools, &partialLowerSegmentAccReq)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkError(t, "expected failure due to partial segments in accessibility requirements", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
|
2020-04-06 20:19:13 +00:00
|
|
|
_, _, _, err = FindPoolAndTopology(&validMultipleTopoPools, &partialLowerSegmentAccReq)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkError(t, "expected failure due to partial segments in accessibility requirements", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
|
2020-04-06 20:19:13 +00:00
|
|
|
_, _, _, err = FindPoolAndTopology(&validMultipleTopoPools, &differentSegmentAccReq)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkError(t, "expected failure due to mismatching segments in accessibility requirements", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
|
|
|
|
// Test success cases
|
|
|
|
// If a pool is a superset of domains (either empty domain labels or partial), it can be selected
|
2020-04-06 20:19:13 +00:00
|
|
|
poolName, _, topoSegment, err := FindPoolAndTopology(&emptyDomainsInTopoPools, &validAccReq)
|
2020-04-05 21:54:30 +00:00
|
|
|
err = checkOutput(err, poolName, topoSegment)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkAndReportError(t, "expected success got:", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
|
2020-04-06 20:19:13 +00:00
|
|
|
poolName, _, topoSegment, err = FindPoolAndTopology(&partialDomainsInTopoPools, &validAccReq)
|
2020-04-05 21:54:30 +00:00
|
|
|
err = checkOutput(err, poolName, topoSegment)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkAndReportError(t, "expected success got:", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
|
|
|
|
// match in a singleton topology pools
|
2020-04-06 20:19:13 +00:00
|
|
|
poolName, _, topoSegment, err = FindPoolAndTopology(&validSingletonTopoPools, &validAccReq)
|
2020-04-05 21:54:30 +00:00
|
|
|
err = checkOutput(err, poolName, topoSegment)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkAndReportError(t, "expected success got:", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
|
|
|
|
// match first in multiple topology pools
|
2020-04-06 20:19:13 +00:00
|
|
|
poolName, _, topoSegment, err = FindPoolAndTopology(&validMultipleTopoPools, &validAccReq)
|
2020-04-05 21:54:30 +00:00
|
|
|
err = checkOutput(err, poolName, topoSegment)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkAndReportError(t, "expected success got:", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
|
|
|
|
// match non-first in multiple topology pools
|
|
|
|
switchPoolOrder := []TopologyConstrainedPool{}
|
|
|
|
switchPoolOrder = append(switchPoolOrder, validMultipleTopoPools[1], validMultipleTopoPools[0])
|
2020-04-06 20:19:13 +00:00
|
|
|
poolName, _, topoSegment, err = FindPoolAndTopology(&switchPoolOrder, &validAccReq)
|
2020-04-05 21:54:30 +00:00
|
|
|
err = checkOutput(err, poolName, topoSegment)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkAndReportError(t, "expected success got:", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
|
2020-04-06 20:19:13 +00:00
|
|
|
// test valid dataPool return
|
|
|
|
for i := range switchPoolOrder {
|
|
|
|
switchPoolOrder[i].DataPoolName = "ec-" + switchPoolOrder[i].PoolName
|
|
|
|
}
|
|
|
|
poolName, dataPoolName, topoSegment, err := FindPoolAndTopology(&switchPoolOrder, &validAccReq)
|
|
|
|
err = checkOutput(err, poolName, topoSegment)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkAndReportError(t, "expected success got:", err)
|
2020-04-06 20:19:13 +00:00
|
|
|
if dataPoolName != "ec-"+poolName {
|
|
|
|
t.Errorf("expected data pool to be named ec-%s, got %s", poolName, dataPoolName)
|
|
|
|
}
|
|
|
|
|
2020-04-05 21:54:30 +00:00
|
|
|
// TEST: MatchTopologyForPool
|
|
|
|
// check for non-existent pool
|
|
|
|
_, err = MatchTopologyForPool(&validMultipleTopoPools, &validAccReq, pool1+"fuzz")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected failure due to non-existent pool name (%s) got success", pool1+"fuzz")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for existing pool
|
|
|
|
topoSegment, err = MatchTopologyForPool(&validMultipleTopoPools, &validAccReq, pool1)
|
|
|
|
err = checkOutput(err, pool1, topoSegment)
|
2020-07-06 05:31:52 +00:00
|
|
|
checkAndReportError(t, "expected success got:", err)
|
2020-04-05 21:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
// TODO: To test GetTopologyFromDomainLabels we need it to accept a k8s client interface, to mock k8sGetNdeLabels output
|
|
|
|
func TestGetTopologyFromDomainLabels(t *testing.T) {
|
|
|
|
fakeNodes := v1.Node{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: "worker1",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"prefix/region": "R1",
|
|
|
|
"prefix/zone": "Z1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
client := fake.NewSimpleClientset(&fakeNodes)
|
|
|
|
|
|
|
|
_, err := k8sGetNodeLabels(client, "nodeName")
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error due to invalid node name, got success")
|
|
|
|
}
|
|
|
|
|
|
|
|
labels, err := k8sGetNodeLabels(client, "worker1")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected success, got err (%v)", err)
|
|
|
|
}
|
|
|
|
t.Errorf("Read labels (%v)", labels)
|
|
|
|
}*/
|