Skip to content

Commit 31e1018

Browse files
committed
Toplogy unit and e2e testing
1 parent 9f33082 commit 31e1018

File tree

5 files changed

+219
-57
lines changed

5 files changed

+219
-57
lines changed

pkg/gce-pd-csi-driver/controller_test.go

+60-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,65 @@ func TestCreateVolumeArguments(t *testing.T) {
136136
Attributes: nil,
137137
},
138138
},
139+
{
140+
name: "success with topology",
141+
req: &csi.CreateVolumeRequest{
142+
Name: "test-vol",
143+
CapacityRange: stdCapRange,
144+
VolumeCapabilities: stdVolCap,
145+
Parameters: map[string]string{"type": "test-type"},
146+
AccessibilityRequirements: &csi.TopologyRequirement{
147+
Requisite: []*csi.Topology{
148+
{
149+
Segments: map[string]string{"zone": "topology-zone"},
150+
},
151+
},
152+
},
153+
},
154+
expVol: &csi.Volume{
155+
CapacityBytes: utils.GbToBytes(20),
156+
Id: "topology-zone/test-vol",
157+
Attributes: nil,
158+
},
159+
},
160+
{
161+
name: "success with topology overwrite zone in params",
162+
req: &csi.CreateVolumeRequest{
163+
Name: "test-vol",
164+
CapacityRange: stdCapRange,
165+
VolumeCapabilities: stdVolCap,
166+
Parameters: stdParams,
167+
AccessibilityRequirements: &csi.TopologyRequirement{
168+
Requisite: []*csi.Topology{
169+
{
170+
Segments: map[string]string{"zone": "topology-zone"},
171+
},
172+
},
173+
},
174+
},
175+
expVol: &csi.Volume{
176+
CapacityBytes: utils.GbToBytes(20),
177+
Id: "topology-zone/test-vol",
178+
Attributes: nil,
179+
},
180+
},
181+
{
182+
name: "fail with malformed topology",
183+
req: &csi.CreateVolumeRequest{
184+
Name: "test-vol",
185+
CapacityRange: stdCapRange,
186+
VolumeCapabilities: stdVolCap,
187+
Parameters: stdParams,
188+
AccessibilityRequirements: &csi.TopologyRequirement{
189+
Requisite: []*csi.Topology{
190+
{
191+
Segments: map[string]string{"ooblezoners": "topology-zone"},
192+
},
193+
},
194+
},
195+
},
196+
expErrCode: codes.InvalidArgument,
197+
},
139198
}
140199

141200
// Run test cases
@@ -181,7 +240,7 @@ func TestCreateVolumeArguments(t *testing.T) {
181240
}
182241

183242
if vol.GetId() != tc.expVol.GetId() {
184-
t.Fatalf("Expected volume id: %v, got: %v", vol.GetId(), tc.expVol.GetId())
243+
t.Fatalf("Expected volume id: %q, got: %q", vol.GetId(), tc.expVol.GetId())
185244
}
186245

187246
for akey, aval := range tc.expVol.GetAttributes() {

pkg/gce-pd-csi-driver/identity_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func TestGetPluginCapabilities(t *testing.T) {
5959
for _, capability := range resp.GetCapabilities() {
6060
switch capability.GetService().GetType() {
6161
case csi.PluginCapability_Service_CONTROLLER_SERVICE:
62+
case csi.PluginCapability_Service_ACCESSIBILITY_CONSTRAINTS:
6263
default:
6364
t.Fatalf("Unknown capability: %v", capability.GetService().GetType())
6465
}

test/e2e/tests/multi_zone_e2e_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
15+
package tests
16+
17+
import (
18+
. "github.com/onsi/ginkgo"
19+
. "github.com/onsi/gomega"
20+
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/e2e/utils"
21+
)
22+
23+
var _ = Describe("GCE PD CSI Driver Multi-Zone", func() {
24+
BeforeEach(func() {
25+
Expect(len(testInstances)).To(BeNumerically(">", 1))
26+
// TODO: Check whether the instances are in different zones???
27+
// I Think there should be a better way of guaranteeing this. Like a map from zone to instance for testInstances (?)
28+
})
29+
30+
It("Should get reasonable toplogy from nodes with NodeGetInfo", func() {
31+
for _, instance := range testInstances {
32+
testContext, err := utils.SetupNewDriverAndClient(instance)
33+
Expect(err).To(BeNil(), "Set up new Driver and Client failed with error")
34+
35+
resp, err := testContext.Client.NodeGetInfo()
36+
Expect(err).To(BeNil())
37+
38+
// Get Cloud Instance
39+
p, z, n := testContext.Instance.GetIdentity()
40+
cloudInstance, err := computeService.Instances.Get(p, z, n).Do()
41+
Expect(err).To(BeNil())
42+
Expect(cloudInstance).ToNot(BeNil())
43+
44+
// Check topology matches
45+
segments := resp.GetAccessibleTopology().GetSegments()
46+
Expect(segments).ToNot(BeNil())
47+
48+
Expect(segments["zone"]).To(Equal(z))
49+
Expect(len(segments)).To(Equal(1))
50+
}
51+
52+
})
53+
54+
})

test/e2e/tests/setup_e2e_test.go

+55-3
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,72 @@ package tests
1616

1717
import (
1818
"flag"
19+
"fmt"
20+
"math/rand"
1921
"testing"
22+
"time"
2023

2124
. "github.com/onsi/ginkgo"
2225
. "github.com/onsi/gomega"
26+
compute "google.golang.org/api/compute/v1"
27+
remote "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/binremote"
28+
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/e2e/utils"
2329
)
2430

2531
var (
26-
project = flag.String("project", "", "Project to run tests in")
27-
serviceAccount = flag.String("service-account", "", "Service account to bring up instance with")
28-
runInProw = flag.Bool("run-in-prow", false, "If true, use a Boskos loaned project and special CI service accounts and ssh keys")
32+
project = flag.String("project", "", "Project to run tests in")
33+
serviceAccount = flag.String("service-account", "", "Service account to bring up instance with")
34+
runInProw = flag.Bool("run-in-prow", false, "If true, use a Boskos loaned project and special CI service accounts and ssh keys")
35+
deleteInstances = flag.Bool("delete-instances", false, "Delete the instances after tests run")
36+
37+
testInstances = []*remote.InstanceInfo{}
38+
computeService *compute.Service
2939
)
3040

3141
func TestE2E(t *testing.T) {
3242
flag.Parse()
3343
RegisterFailHandler(Fail)
3444
RunSpecs(t, "Google Compute Engine Persistent Disk Container Storage Interface Driver Tests")
3545
}
46+
47+
var _ = BeforeSuite(func() {
48+
var err error
49+
zones := []string{"us-central1-c", "us-central1-b"}
50+
51+
rand.Seed(time.Now().UnixNano())
52+
53+
computeService, err = remote.GetComputeClient()
54+
Expect(err).To(BeNil())
55+
56+
for _, zone := range zones {
57+
nodeID := fmt.Sprintf("gce-pd-csi-e2e-%s", zone)
58+
59+
if *runInProw {
60+
*project, *serviceAccount = utils.SetupProwConfig()
61+
}
62+
63+
Expect(*project).ToNot(BeEmpty(), "Project should not be empty")
64+
Expect(*serviceAccount).ToNot(BeEmpty(), "Service account should not be empty")
65+
66+
i, err := utils.SetupInstance(*project, zone, nodeID, *serviceAccount, computeService)
67+
Expect(err).To(BeNil())
68+
69+
testInstances = append(testInstances, i)
70+
}
71+
72+
})
73+
74+
var _ = AfterSuite(func() {
75+
/*
76+
err := node.client.CloseConn()
77+
if err != nil {
78+
Logf("Failed to close the client")
79+
} else {
80+
Logf("Closed the client")
81+
*/
82+
for _, i := range testInstances {
83+
if *deleteInstances {
84+
i.DeleteInstance()
85+
}
86+
}
87+
})

test/e2e/tests/single_zone_e2e_test.go

+49-53
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ import (
2020
"strings"
2121

2222
"k8s.io/apimachinery/pkg/util/uuid"
23-
remote "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/binremote"
23+
gce "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider"
2424
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/e2e/utils"
2525

26+
csi "github.com/container-storage-interface/spec/lib/go/csi/v0"
2627
. "github.com/onsi/ginkgo"
2728
. "github.com/onsi/gomega"
2829
)
2930

3031
const (
31-
network = "unix"
3232
testNamePrefix = "gcepd-csi-e2e-"
3333

3434
defaultSizeGb int64 = 5
@@ -37,83 +37,48 @@ const (
3737
ssdDiskType = "pd-ssd"
3838
)
3939

40-
var (
41-
client *utils.CsiClient
42-
instance *remote.InstanceInfo
43-
//gceCloud *gce.CloudProvider
44-
nodeID string
45-
)
46-
47-
var _ = BeforeSuite(func() {
48-
var err error
49-
// TODO(dyzz): better defaults
50-
nodeID = "gce-pd-csi-e2e-us-central1-c"
51-
port := "2000"
52-
if *runInProw {
53-
*project, *serviceAccount = utils.SetupProwConfig()
54-
}
55-
56-
Expect(*project).ToNot(BeEmpty(), "Project should not be empty")
57-
Expect(*serviceAccount).ToNot(BeEmpty(), "Service account should not be empty")
58-
59-
instance, err = utils.SetupInstanceAndDriver(*project, "us-central1-c", nodeID, port, *serviceAccount)
60-
Expect(err).To(BeNil())
61-
62-
client = utils.CreateCSIClient(fmt.Sprintf("localhost:%s", port))
63-
})
64-
65-
var _ = AfterSuite(func() {
66-
// Close the client
67-
err := client.CloseConn()
68-
if err != nil {
69-
Logf("Failed to close the client")
70-
} else {
71-
Logf("Closed the client")
72-
}
73-
74-
// instance.DeleteInstance()
75-
})
76-
7740
var _ = Describe("GCE PD CSI Driver", func() {
7841

79-
BeforeEach(func() {
80-
err := client.AssertCSIConnection()
81-
Expect(err).To(BeNil(), "Failed to assert csi client connection: %v", err)
82-
})
83-
8442
It("Should create->attach->stage->mount volume and check if it is writable, then unmount->unstage->detach->delete and check disk is deleted", func() {
43+
// Create new driver and client
44+
// TODO: Should probably actual have some object that includes both client and instance so we can relate the two??
45+
Expect(testInstances).NotTo(BeEmpty())
46+
testContext, err := utils.SetupNewDriverAndClient(testInstances[0])
47+
Expect(err).To(BeNil(), "Set up new Driver and Client failed with error")
48+
p, z, _ := testContext.Instance.GetIdentity()
49+
client := testContext.Client
50+
instance := testContext.Instance
51+
8552
// Create Disk
8653
volName := testNamePrefix + string(uuid.NewUUID())
87-
volId, err := client.CreateVolume(volName)
54+
volId, err := client.CreateVolume(volName, nil)
8855
Expect(err).To(BeNil(), "CreateVolume failed with error: %v", err)
8956

9057
// TODO: Validate Disk Created
91-
/*cloudDisk, err := gceCloud.GetDiskOrError(context.Background(), gceCloud.GetZone(), volName)
58+
cloudDisk, err := computeService.Disks.Get(p, z, volName).Do()
9259
Expect(err).To(BeNil(), "Could not get disk from cloud directly")
9360
Expect(cloudDisk.Type).To(ContainSubstring(standardDiskType))
9461
Expect(cloudDisk.Status).To(Equal(readyState))
9562
Expect(cloudDisk.SizeGb).To(Equal(defaultSizeGb))
96-
Expect(cloudDisk.Name).To(Equal(volName))*/
63+
Expect(cloudDisk.Name).To(Equal(volName))
9764

9865
defer func() {
9966
// Delete Disk
10067
client.DeleteVolume(volId)
10168
Expect(err).To(BeNil(), "DeleteVolume failed")
10269

10370
// TODO: Validate Disk Deleted
104-
/*_, err = gceCloud.GetDiskOrError(context.Background(), gceCloud.GetZone(), volName)
105-
serverError, ok := status.FromError(err)
106-
Expect(ok).To(BeTrue())
107-
Expect(serverError.Code()).To(Equal(codes.NotFound))*/
71+
_, err = computeService.Disks.Get(p, z, volName).Do()
72+
Expect(gce.IsGCEError(err, "notFound")).To(BeTrue(), "Expected disk to not be found")
10873
}()
10974

11075
// Attach Disk
111-
err = client.ControllerPublishVolume(volId, nodeID)
76+
err = client.ControllerPublishVolume(volId, instance.GetName())
11277
Expect(err).To(BeNil(), "ControllerPublishVolume failed with error")
11378

11479
defer func() {
11580
// Detach Disk
116-
err := client.ControllerUnpublishVolume(volId, nodeID)
81+
err := client.ControllerUnpublishVolume(volId, instance.GetName())
11782
Expect(err).To(BeNil(), "ControllerUnpublishVolume failed with error")
11883
}()
11984

@@ -166,6 +131,37 @@ var _ = Describe("GCE PD CSI Driver", func() {
166131

167132
})
168133

134+
It("Should create disks in correct zones when toplogy is specified", func() {
135+
///
136+
Expect(testInstances).NotTo(BeEmpty())
137+
testContext, err := utils.SetupNewDriverAndClient(testInstances[0])
138+
Expect(err).To(BeNil(), "Failed to set up new driver and client")
139+
p, _, _ := testContext.Instance.GetIdentity()
140+
141+
zones := []string{"us-central1-c", "us-central1-b", "us-central1-a"}
142+
143+
for _, zone := range zones {
144+
volName := testNamePrefix + string(uuid.NewUUID())
145+
topReq := &csi.TopologyRequirement{
146+
Requisite: []*csi.Topology{
147+
{
148+
Segments: map[string]string{"zone": zone},
149+
},
150+
},
151+
}
152+
volID, err := testContext.Client.CreateVolume(volName, topReq)
153+
Expect(err).To(BeNil(), "Failed to create volume")
154+
defer func() {
155+
err = testContext.Client.DeleteVolume(volID)
156+
Expect(err).To(BeNil(), "Failed to delete volume")
157+
}()
158+
159+
_, err = computeService.Disks.Get(p, zone, volName).Do()
160+
Expect(err).To(BeNil(), "Could not find disk in correct zone")
161+
}
162+
163+
})
164+
169165
// Test volume already exists
170166

171167
// Test volume with op pending

0 commit comments

Comments
 (0)