Skip to content

Commit 5e23d8b

Browse files
committed
test: simplify func-git service
Use k8s primitives pod/svc/ingress instead of knative service. Signed-off-by: Matej Vašek <[email protected]>
1 parent cdc06ee commit 5e23d8b

File tree

2 files changed

+74
-65
lines changed

2 files changed

+74
-65
lines changed

hack/install-git-server.sh

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,70 @@ set -o nounset
1717
set -o pipefail
1818

1919
git_server() {
20-
echo "Creating Git Server Knative service..."
21-
cat << EOF | kubectl apply -f -
22-
apiVersion: serving.knative.dev/v1
23-
kind: Service
20+
echo "Creating Git Server"
21+
22+
local name="func-git"
23+
24+
local namespace
25+
namespace="$(kubectl config view --minify --output 'jsonpath={..namespace}')"
26+
namespace="${namespace:-"default"}"
27+
28+
local -r func_git_host="${name}.${namespace}.localtest.me"
29+
30+
kubectl apply -f - <<EOF
31+
apiVersion: v1
32+
kind: Pod
2433
metadata:
25-
name: func-git
34+
name: "${name}"
35+
namespace: "${namespace}"
2636
labels:
27-
app: git
37+
app.kubernetes.io/name: "${name}"
2838
spec:
29-
template:
30-
metadata:
31-
annotations:
32-
autoscaling.knative.dev/max-scale: "1"
33-
autoscaling.knative.dev/min-scale: "1"
34-
client.knative.dev/user-image: quay.io/mvasek/gitserver
35-
spec:
36-
containers:
37-
- image: quay.io/mvasek/gitserver
38-
ports:
39+
containers:
40+
- name: "${name}"
41+
image: quay.io/mvasek/gitserver
42+
ports:
3943
- containerPort: 80
40-
resources: {}
41-
status: {}
44+
name: http
45+
---
46+
apiVersion: v1
47+
kind: Service
48+
metadata:
49+
name: "${name}"
50+
namespace: "${namespace}"
51+
spec:
52+
selector:
53+
app.kubernetes.io/name: "${name}"
54+
ports:
55+
- name: http
56+
protocol: TCP
57+
port: 80
58+
targetPort: http
59+
type: ClusterIP
60+
---
61+
apiVersion: networking.k8s.io/v1
62+
kind: Ingress
63+
metadata:
64+
name: "${name}"
65+
namespace: "${namespace}"
66+
spec:
67+
ingressClassName: contour-external
68+
rules:
69+
- host: "${func_git_host}"
70+
http:
71+
paths:
72+
- backend:
73+
service:
74+
name: "${name}"
75+
port:
76+
number: 80
77+
pathType: Prefix
78+
path: /
4279
EOF
4380

44-
kubectl wait ksvc --for=condition=RoutesReady --timeout=30s -l "app=git"
81+
echo "starting func-git service at: ${func_git_host}"
82+
83+
kubectl wait --for=condition=Ready "pod/${name}" --timeout=30s
4584
}
4685

4786
git_server

test/common/gitserver.go

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package common
22

33
import (
4-
"context"
4+
"fmt"
55

66
"strings"
77
"testing"
88

99
"knative.dev/func/pkg/k8s"
10-
11-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12-
"k8s.io/client-go/kubernetes"
1310
)
1411

1512
var DefaultGitServer GitProvider
@@ -39,71 +36,44 @@ type GitProvider interface {
3936
// ------------------------------------------------------
4037

4138
type GitTestServerKnativeProvider struct {
42-
PodName string
43-
ServiceUrl string
44-
Kubectl *TestExecCmd
45-
t *testing.T
39+
Kubectl *TestExecCmd
40+
ns string
41+
t *testing.T
4642
}
4743

48-
func (g *GitTestServerKnativeProvider) Init(T *testing.T) {
49-
50-
g.t = T
51-
if g.PodName == "" {
52-
config, err := k8s.GetClientConfig().ClientConfig()
53-
if err != nil {
54-
T.Fatal(err.Error())
55-
}
56-
clientSet, err := kubernetes.NewForConfig(config)
57-
if err != nil {
58-
T.Fatal(err.Error())
59-
}
60-
ctx := context.Background()
61-
62-
namespace, _, _ := k8s.GetClientConfig().Namespace()
63-
podList, err := clientSet.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{
64-
LabelSelector: "serving.knative.dev/service=func-git",
65-
})
66-
if err != nil {
67-
T.Fatal(err.Error())
68-
}
69-
for _, pod := range podList.Items {
70-
g.PodName = pod.Name
71-
}
72-
}
73-
74-
if g.ServiceUrl == "" {
75-
// Get Route Name
76-
_, g.ServiceUrl = GetKnativeServiceRevisionAndUrl(T, "func-git")
77-
}
44+
const podName = "func-git"
7845

46+
func (g *GitTestServerKnativeProvider) Init(t *testing.T) {
47+
g.t = t
7948
if g.Kubectl == nil {
8049
g.Kubectl = &TestExecCmd{
8150
Binary: "kubectl",
8251
ShouldDumpCmdLine: true,
8352
ShouldDumpOnSuccess: true,
84-
T: T,
53+
T: t,
8554
}
8655
}
87-
T.Logf("Initialized HTTP Func Git Server: Server URL = %v Pod Name = %v\n", g.ServiceUrl, g.PodName)
56+
if g.ns == "" {
57+
g.ns, _, _ = k8s.GetClientConfig().Namespace()
58+
}
59+
t.Logf("Initialized HTTP Func Git Server: Server URL = func-git.%s.localtest.me Pod Name = %s\n", g.ns, podName)
8860
}
8961

9062
func (g *GitTestServerKnativeProvider) CreateRepository(repoName string) *GitRemoteRepo {
91-
// kubectl exec $podname -c user-container -- git-repo create $reponame
92-
cmdResult := g.Kubectl.Exec("exec", g.PodName, "-c", "user-container", "--", "git-repo", "create", repoName)
63+
cmdResult := g.Kubectl.Exec("exec", podName, "--", "git-repo", "create", repoName)
9364
if !strings.Contains(cmdResult.Out, "created") {
9465
g.t.Fatal("unable to create git bare repository " + repoName)
9566
}
96-
namespace, _, _ := k8s.GetClientConfig().Namespace()
9767
gitRepo := &GitRemoteRepo{
9868
RepoName: repoName,
99-
ExternalCloneURL: g.ServiceUrl + "/" + repoName + ".git",
100-
ClusterCloneURL: "http://func-git." + namespace + ".svc.cluster.local/" + repoName + ".git",
69+
ExternalCloneURL: fmt.Sprintf("http://func-git.%s.localtest.me/%s.git", g.ns, repoName),
70+
ClusterCloneURL: fmt.Sprintf("http://func-git.%s.svc.cluster.local/%s.git", g.ns, repoName),
10171
}
10272
return gitRepo
10373
}
10474

10575
func (g *GitTestServerKnativeProvider) DeleteRepository(repoName string) {
106-
cmdResult := g.Kubectl.Exec("exec", g.PodName, "-c", "user-container", "--", "git-repo", "delete", repoName)
76+
cmdResult := g.Kubectl.Exec("exec", podName, "--", "git-repo", "delete", repoName)
10777
if !strings.Contains(cmdResult.Out, "deleted") {
10878
g.t.Fatal("unable to delete git bare repository " + repoName)
10979
}

0 commit comments

Comments
 (0)