Skip to content

Commit 934d96a

Browse files
committed
Test framework changes to run properly on PROW
1 parent e035ac6 commit 934d96a

File tree

4 files changed

+91
-19
lines changed

4 files changed

+91
-19
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ all: gce-pd-driver
2222
gce-pd-driver:
2323
mkdir -p bin
2424
go build -o bin/gce-pd-csi-driver ./cmd/
25+
go test -c sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/e2e -o bin/e2e.test
2526

2627
build-container: gce-pd-driver
2728
docker build -t $(STAGINGIMAGE):$(STAGINGVERSION) .

test/remote/remote/e2e.go

+10-13
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,30 @@ func InitE2ERemote() TestSuite {
3636

3737
// SetupTestPackage sets up the test package with binaries k8s required for node e2e tests
3838
func (n *E2ERemote) SetupTestPackage(tardir string) error {
39-
// Make sure we can find the newly built binaries
40-
gopath, ok := os.LookupEnv("GOPATH")
41-
if !ok {
42-
return fmt.Errorf("Could not find gopath")
43-
}
44-
4539
// TODO(dyzz): build the gce driver tests instead.
4640

47-
cmd := exec.Command("ginkgo", "build", "test/e2e")
41+
cmd := exec.Command("go", "test", "-c", "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/e2e", "-o", "test/e2e/e2e.test")
4842
err := cmd.Run()
4943

5044
if err != nil {
51-
return fmt.Errorf("Failed to ginkgo build test: %v", err)
45+
return fmt.Errorf("Failed to build test: %v", err)
5246
}
5347

54-
cmd = exec.Command("cp", "test/e2e/e2e.test", "bin")
48+
cmd = exec.Command("mkdir", "-p", "bin")
5549
err = cmd.Run()
5650
if err != nil {
57-
return fmt.Errorf("Failed to copy: %v", err)
51+
return fmt.Errorf("Failed to mkdir bin/: %v", err)
5852
}
5953

60-
buildOutputDir := filepath.Join(gopath, "src/sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/bin")
61-
54+
cmd = exec.Command("cp", "test/e2e/e2e.test", "bin/e2e.test")
55+
err = cmd.Run()
56+
if err != nil {
57+
return fmt.Errorf("Failed to copy: %v", err)
58+
}
6259
// Copy binaries
6360
requiredBins := []string{"e2e.test"}
6461
for _, bin := range requiredBins {
65-
source := filepath.Join(buildOutputDir, bin)
62+
source := filepath.Join("bin", bin)
6663
if _, err := os.Stat(source); err != nil {
6764
return fmt.Errorf("failed to locate test binary %s: %v", bin, err)
6865
}

test/remote/remote/ssh.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package remote
1919
import (
2020
"flag"
2121
"fmt"
22+
"os"
2223
"os/exec"
2324
"os/user"
2425
"strings"
@@ -69,6 +70,8 @@ func GetHostnameOrIP(hostname string) string {
6970
}
7071
if *sshUser != "" {
7172
host = fmt.Sprintf("%s@%s", *sshUser, host)
73+
} else if _, ok := os.LookupEnv("JENKINS_GCE_SSH_PRIVATE_KEY_FILE"); ok {
74+
host = fmt.Sprintf("prow@%s", host)
7275
}
7376
return host
7477
}
@@ -92,7 +95,10 @@ func SSHNoSudo(host string, cmd ...string) (string, error) {
9295

9396
// runSSHCommand executes the ssh or scp command, adding the flag provided --ssh-options
9497
func runSSHCommand(cmd string, args ...string) (string, error) {
95-
if *sshKey != "" {
98+
if pk, ok := os.LookupEnv("JENKINS_GCE_SSH_PRIVATE_KEY_FILE"); ok {
99+
glog.V(4).Infof("Running on Jenkins, using special private key file at %v", pk)
100+
args = append([]string{"-i", pk}, args...)
101+
} else if *sshKey != "" {
96102
args = append([]string{"-i", *sshKey}, args...)
97103
} else if key, found := sshDefaultKeyMap[*sshEnv]; found {
98104
args = append([]string{"-i", key}, args...)

test/remote/run_remote/run_remote.go

+73-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"flag"
2222
"fmt"
23+
"io/ioutil"
2324
"math/rand"
2425
"net/http"
2526
"os"
@@ -29,6 +30,7 @@ import (
2930

3031
"k8s.io/apimachinery/pkg/util/uuid"
3132
"k8s.io/apimachinery/pkg/util/wait"
33+
gce "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider"
3234
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/test/remote/remote"
3335

3436
"github.com/golang/glog"
@@ -268,7 +270,7 @@ func createInstance(serviceAccount string) (string, error) {
268270
myuuid := string(uuid.NewUUID())
269271
glog.V(2).Infof("Creating instance: %v", name)
270272

271-
imageURL := "https://www.googleapis.com/compute/v1/projects/eip-images/global/images/debian-9-drawfork-v20180423"
273+
imageURL := "https://www.googleapis.com/compute/v1/projects/ml-images/global/images/debian-9-tf-1-9-v20180626"
272274
i := &compute.Instance{
273275
Name: name,
274276
MachineType: machineType(""),
@@ -303,8 +305,9 @@ func createInstance(serviceAccount string) (string, error) {
303305
}
304306

305307
var err error
306-
if _, err = computeService.Instances.Get(*project, *zone, i.Name).Do(); err != nil {
308+
if gotInstance, err := computeService.Instances.Get(*project, *zone, i.Name).Do(); err != nil {
307309
op, err := computeService.Instances.Insert(*project, *zone, i).Do()
310+
glog.V(4).Infof("Inserted instance in project %v, zone %v: %#v", *project, *zone, i)
308311
if err != nil {
309312
ret := fmt.Sprintf("could not create instance %s: API error: %v", name, err)
310313
if op != nil {
@@ -314,10 +317,24 @@ func createInstance(serviceAccount string) (string, error) {
314317
} else if op.Error != nil {
315318
return "", fmt.Errorf("could not create instance %s: %+v", name, op.Error)
316319
}
320+
} else {
321+
glog.V(4).Infof("Compute service GOT instance %v, skipping instance creation: %#v", i.Name, gotInstance)
322+
}
323+
324+
pubkey, ok := os.LookupEnv("JENKINS_GCE_SSH_PUBLIC_KEY_FILE")
325+
if ok {
326+
glog.Infof("Running on Jenkins and JENKINS_GCE_SSH_PUBLIC_KEY_FILE set")
327+
// If we're on CI add public SSH keys to the instance
328+
err = addPubKeyToInstance(*project, *zone, i.Name, pubkey)
329+
if err != nil {
330+
return "", fmt.Errorf("could not add Jenkins public key %v to instance %v: %v", pubkey, i.Name, err)
331+
}
332+
} else {
333+
glog.V(4).Infof("JENKINS_GCE_SSH_PUBLIC_KEY_FILE not set, not adding SSH public key to instance")
317334
}
318335

319336
then := time.Now()
320-
err = wait.Poll(15*time.Second, 10*time.Minute, func() (bool, error) {
337+
err = wait.Poll(10*time.Second, 5*time.Minute, func() (bool, error) {
321338
glog.V(2).Infof("Waiting for instance %v to come up. %v elapsed", name, time.Since(then))
322339
var instance *compute.Instance
323340
instance, err = computeService.Instances.Get(*project, *zone, name).Do()
@@ -337,9 +354,9 @@ func createInstance(serviceAccount string) (string, error) {
337354
remote.AddHostnameIP(name, externalIP)
338355
}
339356

340-
if _, err = remote.SSHNoSudo(name, "echo"); err != nil {
357+
if sshOut, err := remote.SSHNoSudo(name, "echo"); err != nil {
341358
err = fmt.Errorf("Instance %v in state RUNNING but not available by SSH: %v", name, err)
342-
glog.Error(err)
359+
glog.Errorf("SSH encountered an error: %v, output: %v", err, sshOut)
343360
return false, nil
344361
}
345362

@@ -355,6 +372,54 @@ func createInstance(serviceAccount string) (string, error) {
355372
return name, nil
356373
}
357374

375+
func addPubKeyToInstance(project, zone, name, pubKeyFile string) error {
376+
newKeys := ""
377+
i, err := computeService.Instances.Get(project, zone, name).Do()
378+
if err != nil {
379+
return err
380+
}
381+
fingerprint := i.Metadata.Fingerprint
382+
items := i.Metadata.Items
383+
for _, item := range items {
384+
if item.Key == "ssh-keys" {
385+
glog.V(2).Infof("Found existing ssh-keys, prepending to new key string")
386+
newKeys += *item.Value
387+
break
388+
}
389+
}
390+
publicKeyByte, err := ioutil.ReadFile(pubKeyFile)
391+
if err != nil {
392+
return err
393+
}
394+
395+
publicKey := string(publicKeyByte)
396+
397+
// Take username and prepend it to the public key
398+
tokens := strings.Split(publicKey, " ")
399+
if len(tokens) != 3 {
400+
return fmt.Errorf("Public key not comprised of 3 parts, instead was: %v", publicKey)
401+
}
402+
publicKey = strings.TrimSpace(tokens[2]) + ":" + publicKey
403+
404+
newKeys = newKeys + publicKey
405+
glog.V(4).Infof("New ssh-keys for instance %v: %v", name, newKeys)
406+
newMeta := &compute.Metadata{
407+
Fingerprint: fingerprint,
408+
Items: []*compute.MetadataItems{
409+
&compute.MetadataItems{
410+
Key: "ssh-keys",
411+
Value: &newKeys,
412+
},
413+
},
414+
}
415+
_, err = computeService.Instances.SetMetadata(project, zone, name, newMeta).Do()
416+
if err != nil {
417+
return err
418+
}
419+
return nil
420+
421+
}
422+
358423
func getexternalIP(instance *compute.Instance) string {
359424
for i := range instance.NetworkInterfaces {
360425
ni := instance.NetworkInterfaces[i]
@@ -400,6 +465,9 @@ func deleteInstance(host string) {
400465
glog.Infof("Deleting instance %q", host)
401466
_, err := computeService.Instances.Delete(*project, *zone, host).Do()
402467
if err != nil {
468+
if gce.IsGCEError(err, "notFound") {
469+
return
470+
}
403471
glog.Errorf("Error deleting instance %q: %v", host, err)
404472
}
405473
}

0 commit comments

Comments
 (0)