Skip to content

Commit 6934153

Browse files
committed
Refacted e2e utils out to own package
1 parent 6b630c4 commit 6934153

File tree

6 files changed

+40
-35
lines changed

6 files changed

+40
-35
lines changed

test/e2e/setup_e2e_test.go renamed to test/e2e/tests/setup_e2e_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ See the License for the specific language governing permissions and
1212
limitations under the License.
1313
*/
1414

15-
package e2e
15+
package tests
1616

1717
import (
1818
"flag"

test/e2e/single_zone_e2e_test.go renamed to test/e2e/tests/single_zone_e2e_test.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ See the License for the specific language governing permissions and
1212
limitations under the License.
1313
*/
1414

15-
package e2e
15+
package tests
1616

1717
import (
1818
"fmt"
@@ -21,6 +21,7 @@ import (
2121

2222
"k8s.io/apimachinery/pkg/util/uuid"
2323
remote "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/binremote"
24+
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/e2e/utils"
2425

2526
. "github.com/onsi/ginkgo"
2627
. "github.com/onsi/gomega"
@@ -37,7 +38,7 @@ const (
3738
)
3839

3940
var (
40-
client *csiClient
41+
client *utils.CsiClient
4142
instance *remote.InstanceInfo
4243
//gceCloud *gce.CloudProvider
4344
nodeID string
@@ -49,21 +50,21 @@ var _ = BeforeSuite(func() {
4950
nodeID = "gce-pd-csi-e2e-us-central1-c"
5051
port := "2000"
5152
if *runInProw {
52-
*project, *serviceAccount = setupProwConfig()
53+
*project, *serviceAccount = utils.SetupProwConfig()
5354
}
5455

5556
Expect(*project).ToNot(BeEmpty(), "Project should not be empty")
5657
Expect(*serviceAccount).ToNot(BeEmpty(), "Service account should not be empty")
5758

58-
instance, err = setupInstanceAndDriver(*project, "us-central1-c", nodeID, port, *serviceAccount)
59+
instance, err = utils.SetupInstanceAndDriver(*project, "us-central1-c", nodeID, port, *serviceAccount)
5960
Expect(err).To(BeNil())
6061

61-
client = createCSIClient(fmt.Sprintf("localhost:%s", port))
62+
client = utils.CreateCSIClient(fmt.Sprintf("localhost:%s", port))
6263
})
6364

6465
var _ = AfterSuite(func() {
6566
// Close the client
66-
err := client.conn.Close()
67+
err := client.CloseConn()
6768
if err != nil {
6869
Logf("Failed to close the client")
6970
} else {
@@ -76,7 +77,7 @@ var _ = AfterSuite(func() {
7677
var _ = Describe("GCE PD CSI Driver", func() {
7778

7879
BeforeEach(func() {
79-
err := client.assertCSIConnection()
80+
err := client.AssertCSIConnection()
8081
Expect(err).To(BeNil(), "Failed to assert csi client connection: %v", err)
8182
})
8283

@@ -125,21 +126,21 @@ var _ = Describe("GCE PD CSI Driver", func() {
125126
// Unstage Disk
126127
err := client.NodeUnstageVolume(volId, stageDir)
127128
Expect(err).To(BeNil(), "NodeUnstageVolume failed with error")
128-
err = rmAll(instance, filepath.Join("/tmp/", volName))
129+
err = utils.RmAll(instance, filepath.Join("/tmp/", volName))
129130
Expect(err).To(BeNil(), "Failed to remove temp directory")
130131
}()
131132

132133
// Mount Disk
133134
publishDir := filepath.Join("/tmp/", volName, "mount")
134135
err = client.NodePublishVolume(volId, stageDir, publishDir)
135136
Expect(err).To(BeNil(), "NodePublishVolume failed with error")
136-
err = forceChmod(instance, filepath.Join("/tmp/", volName), "777")
137+
err = utils.ForceChmod(instance, filepath.Join("/tmp/", volName), "777")
137138
Expect(err).To(BeNil(), "Chmod failed with error")
138139

139140
// Write a file
140141
testFileContents := "test"
141142
testFile := filepath.Join(publishDir, "testfile")
142-
err = writeFile(instance, testFile, testFileContents)
143+
err = utils.WriteFile(instance, testFile, testFileContents)
143144
Expect(err).To(BeNil(), "Failed to write file")
144145

145146
// Unmount Disk
@@ -150,12 +151,12 @@ var _ = Describe("GCE PD CSI Driver", func() {
150151
secondPublishDir := filepath.Join("/tmp/", volName, "secondmount")
151152
err = client.NodePublishVolume(volId, stageDir, secondPublishDir)
152153
Expect(err).To(BeNil(), "NodePublishVolume failed with error")
153-
err = forceChmod(instance, filepath.Join("/tmp/", volName), "777")
154+
err = utils.ForceChmod(instance, filepath.Join("/tmp/", volName), "777")
154155
Expect(err).To(BeNil(), "Chmod failed with error")
155156

156157
// Read File
157158
secondTestFile := filepath.Join(secondPublishDir, "testfile")
158-
readContents, err := readFile(instance, secondTestFile)
159+
readContents, err := utils.ReadFile(instance, secondTestFile)
159160
Expect(err).To(BeNil(), "ReadFile failed with error")
160161
Expect(strings.TrimSpace(string(readContents))).To(Equal(testFileContents))
161162

test/e2e/client_wrappers.go renamed to test/e2e/utils/client_wrappers.go

+17-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ See the License for the specific language governing permissions and
1212
limitations under the License.
1313
*/
1414

15-
package e2e
15+
package utils
1616

1717
import (
1818
"context"
@@ -40,7 +40,7 @@ var (
4040
}
4141
)
4242

43-
type csiClient struct {
43+
type CsiClient struct {
4444
conn *grpc.ClientConn
4545
idClient csipb.IdentityClient
4646
nodeClient csipb.NodeClient
@@ -49,11 +49,11 @@ type csiClient struct {
4949
endpoint string
5050
}
5151

52-
func createCSIClient(endpoint string) *csiClient {
53-
return &csiClient{endpoint: endpoint}
52+
func CreateCSIClient(endpoint string) *CsiClient {
53+
return &CsiClient{endpoint: endpoint}
5454
}
5555

56-
func (c *csiClient) assertCSIConnection() error {
56+
func (c *CsiClient) AssertCSIConnection() error {
5757
var err error
5858

5959
if err != nil {
@@ -83,7 +83,11 @@ func (c *csiClient) assertCSIConnection() error {
8383
return nil
8484
}
8585

86-
func (c *csiClient) CreateVolume(volName string) (string, error) {
86+
func (c *CsiClient) CloseConn() error {
87+
return c.conn.Close()
88+
}
89+
90+
func (c *CsiClient) CreateVolume(volName string) (string, error) {
8791
cvr := &csipb.CreateVolumeRequest{
8892
Name: volName,
8993
VolumeCapabilities: stdVolCaps,
@@ -95,15 +99,15 @@ func (c *csiClient) CreateVolume(volName string) (string, error) {
9599
return cresp.GetVolume().GetId(), nil
96100
}
97101

98-
func (c *csiClient) DeleteVolume(volId string) error {
102+
func (c *CsiClient) DeleteVolume(volId string) error {
99103
dvr := &csipb.DeleteVolumeRequest{
100104
VolumeId: volId,
101105
}
102106
_, err := c.ctrlClient.DeleteVolume(context.Background(), dvr)
103107
return err
104108
}
105109

106-
func (c *csiClient) ControllerPublishVolume(volId, nodeId string) error {
110+
func (c *CsiClient) ControllerPublishVolume(volId, nodeId string) error {
107111
cpreq := &csipb.ControllerPublishVolumeRequest{
108112
VolumeId: volId,
109113
NodeId: nodeId,
@@ -114,7 +118,7 @@ func (c *csiClient) ControllerPublishVolume(volId, nodeId string) error {
114118
return err
115119
}
116120

117-
func (c *csiClient) ControllerUnpublishVolume(volId, nodeId string) error {
121+
func (c *CsiClient) ControllerUnpublishVolume(volId, nodeId string) error {
118122
cupreq := &csipb.ControllerUnpublishVolumeRequest{
119123
VolumeId: volId,
120124
NodeId: nodeId,
@@ -123,7 +127,7 @@ func (c *csiClient) ControllerUnpublishVolume(volId, nodeId string) error {
123127
return err
124128
}
125129

126-
func (c *csiClient) NodeStageVolume(volId, stageDir string) error {
130+
func (c *CsiClient) NodeStageVolume(volId, stageDir string) error {
127131
nodeStageReq := &csipb.NodeStageVolumeRequest{
128132
VolumeId: volId,
129133
StagingTargetPath: stageDir,
@@ -133,7 +137,7 @@ func (c *csiClient) NodeStageVolume(volId, stageDir string) error {
133137
return err
134138
}
135139

136-
func (c *csiClient) NodeUnstageVolume(volId, stageDir string) error {
140+
func (c *CsiClient) NodeUnstageVolume(volId, stageDir string) error {
137141
nodeUnstageReq := &csipb.NodeUnstageVolumeRequest{
138142
VolumeId: volId,
139143
StagingTargetPath: stageDir,
@@ -142,7 +146,7 @@ func (c *csiClient) NodeUnstageVolume(volId, stageDir string) error {
142146
return err
143147
}
144148

145-
func (c *csiClient) NodeUnpublishVolume(volumeId, publishDir string) error {
149+
func (c *CsiClient) NodeUnpublishVolume(volumeId, publishDir string) error {
146150
nodeUnpublishReq := &csipb.NodeUnpublishVolumeRequest{
147151
VolumeId: volumeId,
148152
TargetPath: publishDir,
@@ -151,7 +155,7 @@ func (c *csiClient) NodeUnpublishVolume(volumeId, publishDir string) error {
151155
return err
152156
}
153157

154-
func (c *csiClient) NodePublishVolume(volumeId, stageDir, publishDir string) error {
158+
func (c *CsiClient) NodePublishVolume(volumeId, stageDir, publishDir string) error {
155159
nodePublishReq := &csipb.NodePublishVolumeRequest{
156160
VolumeId: volumeId,
157161
StagingTargetPath: stageDir,

test/e2e/utils.go renamed to test/e2e/utils/utils.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ See the License for the specific language governing permissions and
1212
limitations under the License.
1313
*/
1414

15-
package e2e
15+
package utils
1616

1717
import (
1818
"context"
@@ -36,7 +36,7 @@ const (
3636
archiveName = "e2e_gce_pd_test.tar.gz"
3737
)
3838

39-
func setupInstanceAndDriver(instanceProject, instanceZone, instanceName, port, instanceServiceAccount string) (*remote.InstanceInfo, error) {
39+
func SetupInstanceAndDriver(instanceProject, instanceZone, instanceName, port, instanceServiceAccount string) (*remote.InstanceInfo, error) {
4040
// Create the instance in the requisite zone
4141
instance, err := remote.CreateInstanceInfo(instanceProject, instanceZone, instanceName)
4242
if err != nil {
@@ -87,7 +87,7 @@ func setupInstanceAndDriver(instanceProject, instanceZone, instanceName, port, i
8787
return instance, nil
8888
}
8989

90-
func setupProwConfig() (project, serviceAccount string) {
90+
func SetupProwConfig() (project, serviceAccount string) {
9191
// Try to get a Boskos project
9292
glog.V(4).Infof("Running in PROW")
9393
glog.V(4).Infof("Fetching a Boskos loaned project")
@@ -135,7 +135,7 @@ func setupProwConfig() (project, serviceAccount string) {
135135
return project, serviceAccount
136136
}
137137

138-
func forceChmod(instance *remote.InstanceInfo, filePath string, perms string) error {
138+
func ForceChmod(instance *remote.InstanceInfo, filePath string, perms string) error {
139139
originalumask, err := instance.SSHNoSudo("umask")
140140
if err != nil {
141141
return fmt.Errorf("failed to umask. Output: %v, errror: %v", originalumask, err)
@@ -155,23 +155,23 @@ func forceChmod(instance *remote.InstanceInfo, filePath string, perms string) er
155155
return nil
156156
}
157157

158-
func writeFile(instance *remote.InstanceInfo, filePath, fileContents string) error {
158+
func WriteFile(instance *remote.InstanceInfo, filePath, fileContents string) error {
159159
output, err := instance.SSHNoSudo("echo", fileContents, ">", filePath)
160160
if err != nil {
161161
return fmt.Errorf("failed to write test file %s. Output: %v, errror: %v", filePath, output, err)
162162
}
163163
return nil
164164
}
165165

166-
func readFile(instance *remote.InstanceInfo, filePath string) (string, error) {
166+
func ReadFile(instance *remote.InstanceInfo, filePath string) (string, error) {
167167
output, err := instance.SSHNoSudo("cat", filePath)
168168
if err != nil {
169169
return "", fmt.Errorf("failed to read test file %s. Output: %v, errror: %v", filePath, output, err)
170170
}
171171
return output, nil
172172
}
173173

174-
func rmAll(instance *remote.InstanceInfo, filePath string) error {
174+
func RmAll(instance *remote.InstanceInfo, filePath string) error {
175175
output, err := instance.SSH("rm", "-rf", filePath)
176176
if err != nil {
177177
return fmt.Errorf("failed to delete all %s. Output: %v, errror: %v", filePath, output, err)

test/run-e2e-local.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ set -o errexit
55

66
readonly PKGDIR=sigs.k8s.io/gcp-compute-persistent-disk-csi-driver
77

8-
go test --v=true "${PKGDIR}/test/e2e" --logtostderr --project ${PROJECT} --service-account ${IAM_NAME}
8+
go test --v=true "${PKGDIR}/test/e2e/tests" --logtostderr --project ${PROJECT} --service-account ${IAM_NAME}

test/run-e2e.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ set -x
55

66
readonly PKGDIR=sigs.k8s.io/gcp-compute-persistent-disk-csi-driver
77

8-
go test --v=true "${PKGDIR}/test/e2e" --logtostderr --run-in-prow=true
8+
go test --v=true "${PKGDIR}/test/e2e/tests" --logtostderr --run-in-prow=true

0 commit comments

Comments
 (0)