test: remove dependecy for boson-project git repository on e2e tests (#2537)

This commit is contained in:
Jefferson Ramos 2024-10-10 02:22:03 -03:00 committed by GitHub
parent e97a6925aa
commit 59aa11c4dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 188 additions and 44 deletions

View File

@ -5,7 +5,9 @@ package e2e
import (
"context"
"fmt"
"path"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
@ -102,10 +104,13 @@ func TestConfigEnvs(t *testing.T) {
funcName := "test-config-envs"
funcPath := filepath.Join(t.TempDir(), funcName)
_, thisfile, _, _ := runtime.Caller(0)
testTemplateFolder := path.Join(path.Dir(thisfile), "..", "templates")
knFunc.TestCmd.Exec("create",
"--language", "go",
"--template", "envs",
"--repository", "http://github.com/boson-project/test-templates.git", // TODO Make on config
"--template", "testenvs",
"--repository", "file://"+testTemplateFolder,
funcPath)
knFunc.TestCmd.SourceDir = funcPath

View File

@ -5,7 +5,9 @@ package e2e
import (
"context"
"fmt"
"path"
"path/filepath"
"runtime"
"testing"
"time"
@ -106,10 +108,13 @@ func TestConfigVolumes(t *testing.T) {
funcName := "test-config-volumes"
funcPath := filepath.Join(t.TempDir(), funcName)
_, thisfile, _, _ := runtime.Caller(0)
testTemplateFolder := path.Join(path.Dir(thisfile), "..", "templates")
knFunc.TestCmd.Exec("create",
"--language", "go",
"--template", "volumes",
"--repository", "http://github.com/boson-project/test-templates.git",
"--template", "testvolumes",
"--repository", "file://"+testTemplateFolder,
funcPath)
knFunc.TestCmd.SourceDir = funcPath
@ -247,10 +252,13 @@ func TestConfigVolumesPvcEmptyDir(t *testing.T) {
funcName := "test-config-vol-pvc"
funcPath := filepath.Join(t.TempDir(), funcName)
_, thisfile, _, _ := runtime.Caller(0)
testTemplateFolder := path.Join(path.Dir(thisfile), "..", "templates")
knFunc.TestCmd.Exec("create",
"--language", "go",
"--template", "volumes",
"--repository", "http://github.com/boson-project/test-templates.git",
"--template", "testvolumes",
"--repository", "file://"+testTemplateFolder,
funcPath)
knFunc.TestCmd.SourceDir = funcPath

View File

@ -1,38 +0,0 @@
//go:build e2e
package e2e
import (
"path/filepath"
"strings"
"testing"
"gotest.tools/v3/assert"
"knative.dev/func/test/common"
"knative.dev/func/test/testhttp"
)
// TestRemoteRepository verifies function created using an
// external template from a git repository
func TestRemoteRepository(t *testing.T) {
var funcName = "remote-repo-function"
var funcPath = filepath.Join(t.TempDir(), funcName)
knFunc := common.NewKnFuncShellCli(t)
knFunc.Exec("create",
"--language", "go",
"--template", "e2e",
"--repository", "http://github.com/boson-project/test-templates.git", // TODO Make on config
funcPath)
knFunc.SourceDir = funcPath
knFunc.Exec("deploy", "--registry", common.GetRegistry())
defer knFunc.Exec("delete")
_, functionUrl := common.WaitForFunctionReady(t, funcName)
_, funcResponse := testhttp.TestGet(t, functionUrl)
assert.Assert(t, strings.Contains(funcResponse, "REMOTE_VALID"))
}

View File

@ -0,0 +1,98 @@
//go:build oncluster
package oncluster
import (
"os"
"path/filepath"
"strings"
"testing"
"gotest.tools/v3/assert"
"knative.dev/func/test/common"
"knative.dev/func/test/testhttp"
)
func setupRemoteRepository(t *testing.T) (reposutoryUrl string) {
repositoryPath := filepath.Join(t.TempDir(), "repository")
helloTemplatePath := filepath.Join(repositoryPath, "go", "testhello")
createFolder := func(folderPath string) {
e := os.MkdirAll(folderPath, 0755)
if e != nil {
t.Error(e.Error())
}
}
createFile := func(path string, content string) {
e := os.WriteFile(path, []byte(content), 0644)
if e != nil {
t.Error(e.Error())
}
}
createFolder(helloTemplatePath)
createFolder(filepath.Join(helloTemplatePath, "hello"))
createFile(filepath.Join(helloTemplatePath, "go.mod"), `
module function
go 1.21
`)
createFile(filepath.Join(helloTemplatePath, "handle.go"), `
package function
import (
"fmt"
"function/hello"
"net/http"
)
func Handle(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, hello.Hello("TEST")+"\n") // "HELLO TEST""
}
`)
createFile(filepath.Join(helloTemplatePath, "hello", "hello.go"), `
package hello
func Hello(name string) string {
return "HELLO " + name
}
`)
gitServer := common.GetGitServer(t)
remoteRepo := gitServer.CreateRepository("hello")
t.Cleanup(func() {
gitServer.DeleteRepository("hello")
})
GitInitialCommitAndPush(t, repositoryPath, remoteRepo.ExternalCloneURL)
return remoteRepo.ExternalCloneURL
}
// TestRemoteRepository verifies function created using an
// external template from a git repository
func TestRemoteRepository(t *testing.T) {
var funcName = "remote-repo-function"
var funcPath = filepath.Join(t.TempDir(), funcName)
gitRepoUrl := setupRemoteRepository(t)
knFunc := common.NewKnFuncShellCli(t)
knFunc.Exec("create",
"--language", "go",
"--template", "testhello",
"--repository", gitRepoUrl,
funcPath)
knFunc.SourceDir = funcPath
knFunc.Exec("deploy", "--registry", common.GetRegistry())
defer knFunc.Exec("delete")
_, functionUrl := common.WaitForFunctionReady(t, funcName)
_, funcResponse := testhttp.TestGet(t, functionUrl)
assert.Assert(t, strings.Contains(funcResponse, "HELLO TEST"))
}

View File

@ -0,0 +1,4 @@
module function
go 1.21

View File

@ -0,0 +1,19 @@
package function
import (
"fmt"
"net/http"
"os"
"strings"
)
func Handle(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
testEnvVars := []string{}
for _, e := range os.Environ() {
if strings.HasPrefix(e, "TEST_") {
testEnvVars = append(testEnvVars, e)
}
}
fmt.Fprintf(w, "%v\n", strings.Join(testEnvVars, "\n"))
}

View File

@ -0,0 +1,4 @@
module function
go 1.21

View File

@ -0,0 +1,44 @@
package function
/*
This function template read and (optionally) write the content of a file on the server
The template is meant to be used in by `func config volumes` e2e test
*/
import (
"fmt"
"net/http"
"os"
)
func Handle(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
// v=/test/volume-config/myconfig
// w=hello
path := r.URL.Query().Get("v")
content := r.URL.Query().Get("w")
if path != "" {
if content != "" {
f, err := os.Create(path)
if err != nil {
fmt.Fprintf(os.Stderr, "error creating file: %v\n", err)
} else {
defer f.Close()
err = os.WriteFile(path, []byte(content), 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "error writing file: %v\n", err)
}
}
}
b, err := os.ReadFile(path)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading file: %v", err)
}
_, err = fmt.Fprintf(w, "%v", string(b))
if err != nil {
fmt.Fprintf(os.Stderr, "error on response write: %v", err)
}
}
}