Skip to content

Commit 89e4fb2

Browse files
committed
Made framework more configurable by removing constants and gce-pd specgcepd specific code into variables
1 parent fe136c9 commit 89e4fb2

File tree

9 files changed

+93
-67
lines changed

9 files changed

+93
-67
lines changed

Makefile

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ all: gce-pd-driver
2323
gce-pd-driver:
2424
mkdir -p bin
2525
go build -ldflags "-X main.vendorVersion=${STAGINGVERSION}" -o bin/gce-pd-csi-driver ./cmd/
26-
#go test -c sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/e2e -o bin/e2e.test
2726

2827
build-container:
2928
docker build --build-arg TAG=$(STAGINGVERSION) -t $(STAGINGIMAGE):$(STAGINGVERSION) .

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (s *nonBlockingGRPCServer) serve(endpoint string, ids csi.IdentityServer, c
114114
glog.Infof("Listening for connections on address: %#v", listener.Addr())
115115

116116
if err := server.Serve(listener); err != nil {
117-
glog.Warningf("Failed to serve: %v", err)
117+
glog.Fatalf("Failed to serve: %v", err)
118118
}
119119

120120
}

test/binremote/binremote/archiver.go renamed to test/binremote/archiver.go

+11-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2016 The Kubernetes Authors.
2+
Copyright 2018 The Kubernetes Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -21,22 +21,21 @@ import (
2121
"io/ioutil"
2222
"os"
2323
"os/exec"
24-
"path"
2524
"path/filepath"
2625

2726
"github.com/golang/glog"
2827
)
2928

30-
func CreateDriverArchive(archiveName string) (string, error) {
29+
func CreateDriverArchive(archiveName, pkgPath, binPath string) (string, error) {
3130
glog.V(2).Infof("Building archive...")
32-
tarDir, err := ioutil.TempDir("", "gce-pd-archive")
31+
tarDir, err := ioutil.TempDir("", "driver-temp-archive")
3332
if err != nil {
3433
return "", fmt.Errorf("failed to create temporary directory %v", err)
3534
}
3635
defer os.RemoveAll(tarDir)
3736

3837
// Call the suite function to setup the test package.
39-
err = setupBinaries(tarDir)
38+
err = setupBinaries(tarDir, pkgPath, binPath)
4039
if err != nil {
4140
return "", fmt.Errorf("failed to setup test package %q: %v", tarDir, err)
4241
}
@@ -54,28 +53,20 @@ func CreateDriverArchive(archiveName string) (string, error) {
5453
return filepath.Join(dir, archiveName), nil
5554
}
5655

57-
func setupBinaries(tarDir string) error {
58-
// TODO(dyzz): build the gce driver tests instead
59-
goPath, ok := os.LookupEnv("GOPATH")
60-
if !ok {
61-
return fmt.Errorf("Could not find environment variable GOPATH")
62-
}
63-
binPath := path.Join(goPath, "src/sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/bin/gce-pd-csi-driver")
64-
65-
// TODO make the driver but from the base dir
66-
out, err := exec.Command("go", "build", "-ldflags", "-X main.vendorVersion=latest", "-o", binPath, "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/cmd").CombinedOutput()
56+
func setupBinaries(tarDir, pkgPath, binPath string) error {
57+
glog.V(4).Infof("Making binaries and copying to temp dir...")
58+
out, err := exec.Command("make", "-C", pkgPath).CombinedOutput()
6759
if err != nil {
68-
return fmt.Errorf("Failed to make gce-pd-driver: %v: %v", string(out), err)
60+
return fmt.Errorf("Failed to make at %s: %v: %v", pkgPath, string(out), err)
6961
}
7062

7163
// Copy binaries
72-
7364
if _, err := os.Stat(binPath); err != nil {
74-
return fmt.Errorf("failed to locate test binary %s: %v", "gce-pd-csi-driver", err)
65+
return fmt.Errorf("failed to locate test binary %s: %v", binPath, err)
7566
}
76-
out, err = exec.Command("cp", binPath, filepath.Join(tarDir, "gce-pd-csi-driver")).CombinedOutput()
67+
out, err = exec.Command("cp", binPath, tarDir).CombinedOutput()
7768
if err != nil {
78-
return fmt.Errorf("failed to copy %q: %v Output: %q", "gce-pd-csi-driver", err, out)
69+
return fmt.Errorf("failed to copy %q: %v Output: %q", binPath, err, out)
7970
}
8071

8172
return nil

test/binremote/binremote/instance.go renamed to test/binremote/instance.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2016 The Kubernetes Authors.
2+
Copyright 2018 The Kubernetes Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -36,6 +36,9 @@ import (
3636
const (
3737
defaultMachine = "n1-standard-1"
3838
defaultFirewallRule = "default-allow-ssh"
39+
40+
// timestampFormat is the timestamp format used in the e2e directory name.
41+
timestampFormat = "20060102T150405"
3942
)
4043

4144
type InstanceInfo struct {
@@ -66,16 +69,15 @@ func CreateInstanceInfo(project, zone, name string) (*InstanceInfo, error) {
6669
func (i *InstanceInfo) CreateInstance(serviceAccount string) error {
6770
var err error
6871
var instance *compute.Instance
72+
glog.V(4).Infof("Creating instance: %v", i.name)
6973

7074
myuuid := string(uuid.NewUUID())
7175

72-
err = i.CreateDefaultFirewallRule()
76+
err = i.createDefaultFirewallRule()
7377
if err != nil {
7478
return fmt.Errorf("Failed to create firewall rule: %v", err)
7579
}
7680

77-
glog.V(4).Infof("Creating instance: %v", i.name)
78-
7981
// TODO: Pick a better boot disk image
8082
imageURL := "projects/ml-images/global/images/family/tf-1-9"
8183
inst := &compute.Instance{
@@ -197,13 +199,6 @@ func getexternalIP(instance *compute.Instance) string {
197199
return ""
198200
}
199201

200-
const (
201-
// workspaceDirPrefix is the string prefix used in the workspace directory name.
202-
workspaceDirPrefix = "gce-pd-e2e-"
203-
// timestampFormat is the timestamp format used in the e2e directory name.
204-
timestampFormat = "20060102T150405"
205-
)
206-
207202
func getTimestamp() string {
208203
return fmt.Sprintf(time.Now().Format(timestampFormat))
209204
}
@@ -216,8 +211,10 @@ func machineType(zone, machine string) string {
216211
}
217212

218213
// Create default SSH filewall rule if it does not exist
219-
func (i *InstanceInfo) CreateDefaultFirewallRule() error {
214+
func (i *InstanceInfo) createDefaultFirewallRule() error {
220215
var err error
216+
glog.V(4).Infof("Creating default firewall rule %s...", defaultFirewallRule)
217+
221218
if _, err = i.computeService.Firewalls.Get(i.project, defaultFirewallRule).Do(); err != nil {
222219
glog.Infof("Default firewall rule %v does not exist, creating", defaultFirewallRule)
223220
f := &compute.Firewall{
@@ -243,6 +240,8 @@ func getComputeClient() (*compute.Service, error) {
243240
const retries = 10
244241
const backoff = time.Second * 6
245242

243+
glog.V(4).Infof("Getting compute client...")
244+
246245
// Setup the gce client for provisioning instances
247246
// Getting credentials on gce jenkins is flaky, so try a couple times
248247
var err error

test/binremote/binremote/runner.go renamed to test/binremote/runner.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2016 The Kubernetes Authors.
2+
Copyright 2018 The Kubernetes Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -18,46 +18,46 @@ package binremote
1818

1919
import (
2020
"fmt"
21+
"path"
2122
"path/filepath"
2223

2324
"github.com/golang/glog"
2425
)
2526

26-
func (i *InstanceInfo) UploadAndRun(archivePath, archiveName, instanceName, port string) error {
27-
endpoint := fmt.Sprintf("tcp://localhost:%s", port)
28-
workspace := newWorkspaceDir()
27+
func (i *InstanceInfo) UploadAndRun(archivePath, remoteWorkspace, driverRunCmd string) error {
2928

3029
// Create the temp staging directory
31-
glog.V(2).Infof("Staging test binaries on %q", i.name)
30+
glog.V(4).Infof("Staging test binaries on %q", i.name)
3231

3332
// Do not sudo here, so that we can use scp to copy test archive to the directdory.
34-
if output, err := i.SSHNoSudo("mkdir", workspace); err != nil {
33+
if output, err := i.SSHNoSudo("mkdir", remoteWorkspace); err != nil {
3534
// Exit failure with the error
36-
return fmt.Errorf("failed to create workspace directory %q on i.name %q: %v output: %q", workspace, i.name, err, output)
35+
return fmt.Errorf("failed to create remoteWorkspace directory %q on i.name %q: %v output: %q", remoteWorkspace, i.name, err, output)
3736
}
3837

3938
// Copy the archive to the staging directory
40-
if output, err := runSSHCommand("scp", archivePath, fmt.Sprintf("%s:%s/", i.GetSSHTarget(), workspace)); err != nil {
39+
if output, err := runSSHCommand("scp", archivePath, fmt.Sprintf("%s:%s/", i.GetSSHTarget(), remoteWorkspace)); err != nil {
4140
// Exit failure with the error
4241
return fmt.Errorf("failed to copy test archive: %v, output: %q", err, output)
4342
}
4443

4544
// Extract the archive
45+
archiveName := path.Base(archivePath)
4646
cmd := getSSHCommand(" && ",
47-
fmt.Sprintf("cd %s", workspace),
47+
fmt.Sprintf("cd %s", remoteWorkspace),
4848
fmt.Sprintf("tar -xzvf ./%s", archiveName),
4949
)
50-
glog.V(2).Infof("Extracting tar on %q", i.name)
50+
glog.V(4).Infof("Extracting tar on %q", i.name)
5151
// Do not use sudo here, because `sudo tar -x` will recover the file ownership inside the tar ball, but
5252
// we want the extracted files to be owned by the current user.
5353
if output, err := i.SSHNoSudo("sh", "-c", cmd); err != nil {
5454
// Exit failure with the error
5555
return fmt.Errorf("failed to extract test archive: %v, output: %q", err, output)
5656
}
5757

58-
glog.V(2).Infof("Starting driver on %q", i.name)
58+
glog.V(4).Infof("Starting driver on %q", i.name)
5959
// When the process is killed the driver should close the TCP endpoint, then we want to download the logs
60-
output, err := i.SSH(fmt.Sprintf("sh -c '/usr/bin/nohup %s/gce-pd-csi-driver --endpoint=%s --nodeid=%s > %s/prog.out 2> %s/prog.err < /dev/null &'", workspace, endpoint, instanceName, workspace, workspace))
60+
output, err := i.SSH(driverRunCmd)
6161

6262
if err != nil {
6363
// Exit failure with the error
@@ -70,6 +70,6 @@ func (i *InstanceInfo) UploadAndRun(archivePath, archiveName, instanceName, port
7070
return nil
7171
}
7272

73-
func newWorkspaceDir() string {
73+
func NewWorkspaceDir(workspaceDirPrefix string) string {
7474
return filepath.Join("/tmp", workspaceDirPrefix+getTimestamp())
7575
}

test/binremote/binremote/ssh.go renamed to test/binremote/ssh.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2016 The Kubernetes Authors.
2+
Copyright 2018 The Kubernetes Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -102,7 +102,7 @@ func runSSHCommand(cmd string, args ...string) (string, error) {
102102
}
103103
args = append(strings.Split(sshOption, " "), args...)
104104

105-
glog.V(4).Infof("Executing command: %v %v", cmd, args)
105+
glog.V(4).Infof("Executing SSH command: %v %v", cmd, args)
106106

107107
output, err := exec.Command(cmd, args...).CombinedOutput()
108108
if err != nil {

test/e2e/setup_e2e_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 e2e
16+
17+
import (
18+
"flag"
19+
"testing"
20+
21+
. "github.com/onsi/ginkgo"
22+
. "github.com/onsi/gomega"
23+
)
24+
25+
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")
29+
)
30+
31+
func TestE2E(t *testing.T) {
32+
flag.Parse()
33+
RegisterFailHandler(Fail)
34+
RunSpecs(t, "Google Compute Engine Persistent Disk Container Storage Interface Driver Tests")
35+
}

test/e2e/single_zone_e2e_test.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ limitations under the License.
1515
package e2e
1616

1717
import (
18-
"flag"
1918
"fmt"
2019
"path/filepath"
2120
"strings"
22-
"testing"
2321

2422
"k8s.io/apimachinery/pkg/util/uuid"
25-
remote "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/binremote/binremote"
23+
remote "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/binremote"
2624

2725
. "github.com/onsi/ginkgo"
2826
. "github.com/onsi/gomega"
@@ -43,19 +41,8 @@ var (
4341
instance *remote.InstanceInfo
4442
//gceCloud *gce.CloudProvider
4543
nodeID string
46-
47-
project = flag.String("project", "", "Project to run tests in")
48-
serviceAccount = flag.String("service-account", "", "Service account to bring up instance with")
49-
runInProw = flag.Bool("run-in-prow", false, "If true, use a Boskos loaned project and special CI service accounts and ssh keys")
5044
)
5145

52-
func TestE2E(t *testing.T) {
53-
flag.Parse()
54-
RegisterFailHandler(Fail)
55-
RunSpecs(t, "Google Compute Engine Persistent Disk Container Storage Interface Driver Tests")
56-
57-
}
58-
5946
var _ = BeforeSuite(func() {
6047
var err error
6148
// TODO(dyzz): better defaults

test/e2e/utils.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,24 @@ import (
1818
"context"
1919
"fmt"
2020
"os"
21+
"path"
2122
"time"
2223

2324
"github.com/golang/glog"
2425
"golang.org/x/oauth2/google"
2526
cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v1"
2627
boskosclient "k8s.io/test-infra/boskos/client"
27-
remote "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/binremote/binremote"
28+
remote "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/binremote"
2829
)
2930

3031
var (
3132
boskos = boskosclient.NewClient(os.Getenv("JOB_NAME"), "http://boskos")
3233
)
3334

35+
const (
36+
archiveName = "e2e_gce_pd_test.tar.gz"
37+
)
38+
3439
func setupInstanceAndDriver(instanceProject, instanceZone, instanceName, port, instanceServiceAccount string) (*remote.InstanceInfo, error) {
3540
// Create the instance in the requisite zone
3641
instance, err := remote.CreateInstanceInfo(instanceProject, instanceZone, instanceName)
@@ -45,8 +50,14 @@ func setupInstanceAndDriver(instanceProject, instanceZone, instanceName, port, i
4550
}
4651

4752
// Create Driver Archive
48-
archiveName := "e2e_gce_pd_test.tar.gz"
49-
archivePath, err := remote.CreateDriverArchive(archiveName)
53+
54+
goPath, ok := os.LookupEnv("GOPATH")
55+
if !ok {
56+
return nil, fmt.Errorf("Could not find environment variable GOPATH")
57+
}
58+
pkgPath := path.Join(goPath, "src/sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/")
59+
binPath := path.Join(pkgPath, "bin/gce-pd-csi-driver")
60+
archivePath, err := remote.CreateDriverArchive(archiveName, pkgPath, binPath)
5061
if err != nil {
5162
return nil, err
5263
}
@@ -58,7 +69,11 @@ func setupInstanceAndDriver(instanceProject, instanceZone, instanceName, port, i
5869
}()
5970

6071
// Upload archive to instance and run binaries
61-
err = instance.UploadAndRun(archivePath, archiveName, instanceName, port)
72+
endpoint := fmt.Sprintf("tcp://localhost:%s", port)
73+
workspace := remote.NewWorkspaceDir("gce-pd-e2e-")
74+
driverRunCmd := fmt.Sprintf("sh -c '/usr/bin/nohup %s/gce-pd-csi-driver --endpoint=%s --nodeid=%s > %s/prog.out 2> %s/prog.err < /dev/null &'",
75+
workspace, endpoint, instanceName, workspace, workspace)
76+
err = instance.UploadAndRun(archivePath, workspace, driverRunCmd)
6277
if err != nil {
6378
return nil, err
6479
}

0 commit comments

Comments
 (0)