Skip to content

Commit

Permalink
Create basic testing framework for gke-networking-recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixiansong committed Jul 18, 2023
1 parent 4ff221a commit f007953
Show file tree
Hide file tree
Showing 6 changed files with 408 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bin/
asm/
.vscode/
.crt
Expand Down
40 changes: 40 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2023 The Kubernetes 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.

BOSKOS_RESOURCE_TYPE ?= gke-internal-project
RUN_IN_PROW ?= false
LOCATION ?= us-central1-c
NUM_NODES ?= 3
TEST_TO_RUN ?= .*

all: bin/recipes-test

bin/recipes-test:
mkdir bin/
go test -c -o $@ ./test

.PHONY: test
test: bin/recipes-test
bin/recipes-test \
--run-in-prow=$(RUN_IN_PROW) \
--boskos-resource-type=$(BOSKOS_RESOURCE_TYPE) \
--test-project-id=$(PROJECT_ID) \
--cluster-name=$(CLUSTER_NAME) \
--location=$(LOCATION) \
--num-nodes=$(NUM_NODES) \
-test.run=$(TEST_TO_RUN) \

.PHONY: clean
clean:
rm -rf bin/
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ GKE is a managed Kubernetes platform that provides a more opinionated and seamle
- [Single Cluster Global LoadBalancer HTTPS between the GCLB and the Backend app](./gateway/single-cluster/global-l7-xlb-https-backend) - Deploy an app behind a Global LoadBalancer with the GatewayClass gke-l7-xlb and encrypt traffic between the LB and the backend app using HAProxy.
- [Single Cluster Regional Internal LoadBalancer](./gateway/single-cluster/regional-l7-ilb) - Deploy an application and expose it with the Gateway API using the GatewayClass gke-l7-rilb.

### Testing the recipes

See [test/README.md](test/README.md) for instructions on how to run the examples recipes against your project.

### Contributions

Do you have a GKE networking recipe that would be useful for others? [Contribute it](CONTRIBUTING.md) and help build the shared knowledge of the GKE community!
45 changes: 45 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!--
Copyright 2023 Google LLC
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
https://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.
-->

# Test

The tests here are intended to test the recipes in this repo to make sure it workable and up-to-date.

## Running the test locally

Make sure you have valid credentials for accessing GCP:

```
gcloud auth login
```

You will need to set the project to run the test locally:

```
export PROJECT_ID=<your-project>
```

To specify the location to create the test cluster, you will need to set the environment varible, it will be `us-central1-c` by default.

```
export LOCATION=<your-location>
```

Then run to start the test or select a set of tests to run:

```
make test TEST_TO_RUN=<regex to filter test>
```
140 changes: 140 additions & 0 deletions test/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
Copyright 2023 The Kubernetes 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 test

import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
backendconfigclient "k8s.io/ingress-gce/pkg/backendconfig/client/clientset/versioned"
frontendconfigclient "k8s.io/ingress-gce/pkg/frontendconfig/client/clientset/versioned"
"k8s.io/klog/v2"
)

var (
flags struct {
kubeconfig string
testProjectID string
testClusterName string
location string
numOfNodes int
// Test infrastructure flags.
boskosResourceType string
inProw bool
}
Framework struct {
Clientset *kubernetes.Clientset
BackendConfigClient *backendconfigclient.Clientset
FrontendConfigClient *frontendconfigclient.Clientset
Cloud cloud.Cloud
}
)

func init() {
flag.StringVar(&flags.kubeconfig, "kubeconfig", "", "path to the .kube config file. This will default to $HOME/.kube/config if unset.")
flag.StringVar(&flags.boskosResourceType, "boskos-resource-type", "gke-internal-project", "name of the boskos resource type to reserve")
flag.BoolVar(&flags.inProw, "run-in-prow", false, "is the test running in PROW")
flag.StringVar(&flags.testProjectID, "test-project-id", "", "Project ID of the test cluster")
flag.StringVar(&flags.testClusterName, "cluster-name", "", "Name of the test cluster")
flag.StringVar(&flags.location, "location", "", "Location of the test cluster")
flag.IntVar(&flags.numOfNodes, "num-nodes", 3, "The number of nodes to be created in each of the cluster's zones")
}

func TestMain(m *testing.M) {
flag.Parse()
klog.Infof("Flags: %+v", flags)

if flags.kubeconfig == "" {
if home := os.Getenv("HOME"); home != "" {
flags.kubeconfig = filepath.Join(home, ".kube", "config")
} else {
klog.Fatalf("kubeconfig path required but not provided")
}
}

if flags.testClusterName == "" {
randSuffix := randSeq(6)
flags.testClusterName = "gke-networking-recipes-" + randSuffix
}

if flags.location == "" {
fmt.Fprintln(os.Stderr, "--location must be set to run the test")
os.Exit(1)
}

project := flags.testProjectID

// If running in Prow, then acquire and set up a project through Boskos.
if flags.inProw {
project = setupProwConfig(flags.boskosResourceType)
if _, ok := os.LookupEnv("USER"); !ok {
if err := os.Setenv("USER", "prow"); err != nil {
klog.Fatalf("failed to set user in prow to prow: %v", err)
}
}
}

output, _ := exec.Command("gcloud", "config", "get-value", "project").CombinedOutput()
oldProject := strings.Split(string(output), "\n")[1]

klog.Infof("Using project %s for testing", project)

if err := setEnvProject(project); err != nil {
klog.Fatalf("failed to set project environment to %q: %v", project, err)
}

// After the test, reset the project
defer func() {
if err := setEnvProject(oldProject); err != nil {
klog.Errorf("failed to set project environment to %s: %v", oldProject, err)
}
}()

klog.Infof("createCluster(%q, %q, %d)", flags.location, flags.testClusterName, flags.numOfNodes)
if err := createCluster(flags.location, flags.testClusterName, flags.numOfNodes); err != nil {
klog.Fatalf("createCluster(%q, %q, %d) = %v", flags.location, flags.testClusterName, flags.numOfNodes, err)
}
defer func() {
klog.Infof("deleteCluster(%q, %q)", flags.location, flags.testClusterName)
if err := deleteCluster(flags.location, flags.testClusterName); err != nil {
klog.Errorf("deleteCluster(%q, %q) = %v", flags.location, flags.testClusterName, err)
}
}()

klog.Infof("Using kubeconfig %q", flags.kubeconfig)
kubeconfig, err := clientcmd.BuildConfigFromFlags("", flags.kubeconfig)
if err != nil {
klog.Fatalf("Error creating kubernetes clients from %q: %v", flags.kubeconfig, err)
}
Framework.Clientset = kubernetes.NewForConfigOrDie(kubeconfig)
Framework.BackendConfigClient = backendconfigclient.NewForConfigOrDie(kubeconfig)
Framework.FrontendConfigClient = frontendconfigclient.NewForConfigOrDie(kubeconfig)

Framework.Cloud, err = newCloud(project)
if err != nil {
klog.Fatalf("Error creating compute client for project %q: %v", project, err)
}
m.Run()
}
Loading

0 comments on commit f007953

Please sign in to comment.