Skip to content

Commit

Permalink
add e2e tests skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
Trojan295 committed Aug 28, 2024
1 parent 85d2fb8 commit 1513b24
Show file tree
Hide file tree
Showing 18 changed files with 439 additions and 60 deletions.
18 changes: 18 additions & 0 deletions charts/cast-cloud-proxy/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,25 @@ spec:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
env:
- name: GRPC_ENDPOINT
value: {{ .Values.config.grpc.endpoint | quote }}
- name: GRPC_KEY
value: {{ .Values.config.grpc.key | quote }}
{{- if .Values.config.gcpCredentials }}
- name: GOOGLE_APPLICATION_CREDENTIALS2
value: dasd
{{- end }}
livenessProbe:
{{- toYaml .Values.livenessProbe | nindent 12 }}
readinessProbe:
{{- toYaml .Values.readinessProbe | nindent 12 }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumeMounts:
- name: gcp-creds
mountPath: "/root/.config/gcloud"
readOnly: true
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
Expand All @@ -51,3 +64,8 @@ spec:
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
volumes:
- name: gcp-creds
secret:
secretName: {{ include "cast-cloud-proxy.fullname" . }}-gcp-creds
optional: true
11 changes: 11 additions & 0 deletions charts/cast-cloud-proxy/templates/gcp-credentials-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{- if .Values.config.gcpCredentials }}
apiVersion: v1
kind: Secret
metadata:
name: {{ include "cast-cloud-proxy.fullname" . }}-gcp-creds
labels:
{{- include "cast-cloud-proxy.labels" . | nindent 4 }}
stringData:
application_default_credentials.json: | {{ .Values.config.gcpCredentials | nindent 4 }}
{{- end }}

6 changes: 6 additions & 0 deletions charts/cast-cloud-proxy/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

config:
gcpCredentials: ""
grpc:
endpoint: ""
key: ""

replicaCount: 1

image:
Expand Down
92 changes: 38 additions & 54 deletions cmd/proxy/main.go
Original file line number Diff line number Diff line change
@@ -1,76 +1,60 @@
package main

import (
"flag"
"log"
"fmt"
"net/http"
"os"
"time"
"path"
"runtime"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/castai/cloud-proxy/internal/castai/dummy"
"github.com/castai/cloud-proxy/internal/config"
"github.com/castai/cloud-proxy/internal/gcpauth"
"github.com/castai/cloud-proxy/internal/localtest"
"github.com/castai/cloud-proxy/internal/proxy"
)

const (
// TODO: Change accordingly for local testing

projectID = "engineering-test-353509"
location = "europe-north1-a"
testCluster = "lachezar-2708"
"github.com/sirupsen/logrus"
)

var (
runSanityTests = flag.Bool("sanity-checks", false, "run sanity checks that validate auth loading and basic executor function")
runMockCastTest = flag.Bool("mockcast", true, "run a test using a mock Cast.AI server")
GitCommit = "undefined"
GitRef = "no-ref"
Version = "local"
)

func main() {
flag.Parse()

if runSanityTests != nil && *runSanityTests {
log.Println("run sanity tests is true, starting")
go func() {
localtest.RunBasicTests(projectID, location, testCluster)
localtest.RunProxyTest(projectID, location, testCluster)
}()
cfg := config.Get()

logger := logrus.New()
logger.SetLevel(logrus.Level(cfg.Log.Level))
logger.SetReportCaller(true)
logger.Formatter = &logrus.TextFormatter{
CallerPrettyfier: func(f *runtime.Frame) (function string, file string) {
filename := path.Base(f.File)
return fmt.Sprintf("%s()", f.Function), fmt.Sprintf("%s:%d", filename, f.Line)
},
}

if runMockCastTest != nil && *runMockCastTest {
log.Println("run mockcast tests is true, starting")
go func() {
log.Println("Starting mock cast instance")
mockCast := &dummy.MockCast{}
if err := mockCast.Run(); err != nil {
log.Panicln("Error running mock Cast:", err)
}
}()
logger.WithFields(logrus.Fields{
"GitCommit": GitCommit,
"GitRef": GitRef,
"Version": Version,
}).Println("Starting cloud-proxy")

go func() {
loggerClientProxy := log.New(os.Stderr, "[CLUSTER PROXY] ", log.LstdFlags)
loggerClientProxy.Println("Starting proxy client")
conn, err := grpc.NewClient("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
loggerClientProxy.Panicf("Failed to connect to server: %v", err)
}
defer func(conn *grpc.ClientConn) {
err := conn.Close()
if err != nil {
loggerClientProxy.Panicf("Failed to close gRPC connection: %v", err)
}
}(conn)

src := gcpauth.GCPCredentialsSource{}
executor := proxy.NewExecutor(src, http.DefaultClient)
client := proxy.NewClient(executor, loggerClientProxy)
client.Run(conn)
}()
conn, err := grpc.NewClient(cfg.GRPC.Endpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
logger.Panicf("Failed to connect to server: %v", err)
}

log.Println("Sleeping for 1h, feel free to kill me")
time.Sleep(1 * time.Hour)
defer func(conn *grpc.ClientConn) {
err := conn.Close()
if err != nil {
logger.Panicf("Failed to close gRPC connection: %v", err)
}
}(conn)

src := gcpauth.GCPCredentialsSource{}

executor := proxy.NewExecutor(src, http.DefaultClient)
client := proxy.NewClient(executor, logger)
client.Run(conn)
}
4 changes: 4 additions & 0 deletions e2e/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM gcr.io/distroless/static-debian11
COPY bin/cloud-proxy-e2e /usr/local/bin/cloud-proxy-e2e
CMD ["cloud-proxy-e2e"]

23 changes: 23 additions & 0 deletions e2e/chart/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
24 changes: 24 additions & 0 deletions e2e/chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: cloud-proxy-e2e
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"
62 changes: 62 additions & 0 deletions e2e/chart/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "e2e.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "e2e.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "e2e.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "e2e.labels" -}}
helm.sh/chart: {{ include "e2e.chart" . }}
{{ include "e2e.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "e2e.selectorLabels" -}}
app.kubernetes.io/name: {{ include "e2e.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "e2e.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "e2e.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
21 changes: 21 additions & 0 deletions e2e/chart/templates/job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "e2e.fullname" . }}
labels:
{{- include "e2e.labels" . | nindent 4}}
spec:
template:
metadata:
labels:
{{- include "e2e.selectorLabels" . | nindent 8 }}
spec:
restartPolicy: Never
containers:
- image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
imagePullPolicy: Never
name: {{ .Chart.Name }}
ports:
- containerPort: 50051
name: grpc

15 changes: 15 additions & 0 deletions e2e/chart/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "e2e.fullname" . }}
labels:
{{- include "e2e.labels" . | nindent 4 }}
spec:
type: ClusterIP
ports:
- port: 50051
targetPort: grpc
protocol: TCP
name: grpc
selector:
{{- include "e2e.selectorLabels" . | nindent 4 }}
5 changes: 5 additions & 0 deletions e2e/chart/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
image:
repository: cloud-proxy-e2e
pullPolicy: IfNotPresent
tag: ""

15 changes: 15 additions & 0 deletions e2e/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"log"

"github.com/castai/cloud-proxy/internal/castai/dummy"
)

func main() {
log.Println("Starting mock cast instance")
mockCast := &dummy.MockCast{}
if err := mockCast.Run(); err != nil {
log.Panicln("Error running mock Cast:", err)
}
}
Loading

0 comments on commit 1513b24

Please sign in to comment.