@@ -2,14 +2,14 @@ package common
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
6
7
"strings"
7
8
"testing"
8
9
9
- "knative.dev/func/pkg/k8s"
10
-
11
10
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
- "k8s.io/client-go/kubernetes"
11
+
12
+ "knative.dev/func/pkg/k8s"
13
13
)
14
14
15
15
var DefaultGitServer GitProvider
@@ -39,71 +39,61 @@ type GitProvider interface {
39
39
// ------------------------------------------------------
40
40
41
41
type GitTestServerKnativeProvider struct {
42
- PodName string
43
- ServiceUrl string
44
- Kubectl * TestExecCmd
45
- t * testing.T
42
+ Kubectl * TestExecCmd
43
+ namespace string
44
+ externalHost string
45
+ t * testing.T
46
46
}
47
47
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
- }
48
+ // name of the pod,svc and ingress
49
+ const podName = "func-git"
50
+ const svcName = podName
51
+ const ingressName = podName
78
52
53
+ func (g * GitTestServerKnativeProvider ) Init (t * testing.T ) {
54
+ g .t = t
79
55
if g .Kubectl == nil {
80
56
g .Kubectl = & TestExecCmd {
81
57
Binary : "kubectl" ,
82
58
ShouldDumpCmdLine : true ,
83
59
ShouldDumpOnSuccess : true ,
84
- T : T ,
60
+ T : t ,
61
+ }
62
+ }
63
+ if g .namespace == "" {
64
+ g .namespace , _ , _ = k8s .GetClientConfig ().Namespace ()
65
+ }
66
+
67
+ if g .externalHost == "" {
68
+ cli , err := k8s .NewKubernetesClientset ()
69
+ if err != nil {
70
+ t .Fatal (err )
85
71
}
72
+ i , err := cli .NetworkingV1 ().Ingresses (g .namespace ).Get (context .Background (), ingressName , metav1.GetOptions {})
73
+ if err != nil {
74
+ t .Fatal (err )
75
+ }
76
+ g .externalHost = i .Spec .Rules [0 ].Host
86
77
}
87
- T .Logf ("Initialized HTTP Func Git Server: Server URL = %v Pod Name = %v\n " , g .ServiceUrl , g .PodName )
78
+
79
+ t .Logf ("Initialized HTTP Func Git Server: Server URL = %s Pod Name = %s\n " , g .externalHost , podName )
88
80
}
89
81
90
82
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 )
83
+ cmdResult := g .Kubectl .Exec ("exec" , podName , "--" , "git-repo" , "create" , repoName )
93
84
if ! strings .Contains (cmdResult .Out , "created" ) {
94
85
g .t .Fatal ("unable to create git bare repository " + repoName )
95
86
}
96
- namespace , _ , _ := k8s .GetClientConfig ().Namespace ()
97
87
gitRepo := & GitRemoteRepo {
98
88
RepoName : repoName ,
99
- ExternalCloneURL : g . ServiceUrl + "/" + repoName + ".git" ,
100
- ClusterCloneURL : "http://func-git." + namespace + " .svc.cluster.local/" + repoName + ".git" ,
89
+ ExternalCloneURL : fmt . Sprintf ( "http://%s/%s.git" , g . externalHost , repoName ) ,
90
+ ClusterCloneURL : fmt . Sprintf ( "http://%s.%s .svc.cluster.local/%s.git" , svcName , g . namespace , repoName ) ,
101
91
}
102
92
return gitRepo
103
93
}
104
94
105
95
func (g * GitTestServerKnativeProvider ) DeleteRepository (repoName string ) {
106
- cmdResult := g .Kubectl .Exec ("exec" , g . PodName , "-c" , "user-container" , "--" , "git-repo" , "delete" , repoName )
96
+ cmdResult := g .Kubectl .Exec ("exec" , podName , "--" , "git-repo" , "delete" , repoName )
107
97
if ! strings .Contains (cmdResult .Out , "deleted" ) {
108
98
g .t .Fatal ("unable to delete git bare repository " + repoName )
109
99
}
0 commit comments