|
1 | 1 | package common
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "context" |
| 4 | + "fmt" |
5 | 5 |
|
6 | 6 | "strings"
|
7 | 7 | "testing"
|
8 | 8 |
|
9 | 9 | "knative.dev/func/pkg/k8s"
|
10 |
| - |
11 |
| - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
12 |
| - "k8s.io/client-go/kubernetes" |
13 | 10 | )
|
14 | 11 |
|
15 | 12 | var DefaultGitServer GitProvider
|
@@ -39,71 +36,44 @@ type GitProvider interface {
|
39 | 36 | // ------------------------------------------------------
|
40 | 37 |
|
41 | 38 | 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 |
46 | 42 | }
|
47 | 43 |
|
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" |
78 | 45 |
|
| 46 | +func (g *GitTestServerKnativeProvider) Init(t *testing.T) { |
| 47 | + g.t = t |
79 | 48 | if g.Kubectl == nil {
|
80 | 49 | g.Kubectl = &TestExecCmd{
|
81 | 50 | Binary: "kubectl",
|
82 | 51 | ShouldDumpCmdLine: true,
|
83 | 52 | ShouldDumpOnSuccess: true,
|
84 |
| - T: T, |
| 53 | + T: t, |
85 | 54 | }
|
86 | 55 | }
|
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) |
88 | 60 | }
|
89 | 61 |
|
90 | 62 | 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) |
93 | 64 | if !strings.Contains(cmdResult.Out, "created") {
|
94 | 65 | g.t.Fatal("unable to create git bare repository " + repoName)
|
95 | 66 | }
|
96 |
| - namespace, _, _ := k8s.GetClientConfig().Namespace() |
97 | 67 | gitRepo := &GitRemoteRepo{
|
98 | 68 | 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), |
101 | 71 | }
|
102 | 72 | return gitRepo
|
103 | 73 | }
|
104 | 74 |
|
105 | 75 | 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) |
107 | 77 | if !strings.Contains(cmdResult.Out, "deleted") {
|
108 | 78 | g.t.Fatal("unable to delete git bare repository " + repoName)
|
109 | 79 | }
|
|
0 commit comments