diff --git a/test/e2e/scenario_config-envs_test.go b/test/e2e/scenario_config-envs_test.go index 27a0d80e11..05c72e6e4c 100644 --- a/test/e2e/scenario_config-envs_test.go +++ b/test/e2e/scenario_config-envs_test.go @@ -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 diff --git a/test/e2e/scenario_config-volumes_test.go b/test/e2e/scenario_config-volumes_test.go index b520c519fe..46d073f5c6 100644 --- a/test/e2e/scenario_config-volumes_test.go +++ b/test/e2e/scenario_config-volumes_test.go @@ -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 diff --git a/test/e2e/scenario_remote-repository_test.go b/test/e2e/scenario_remote-repository_test.go deleted file mode 100644 index 20d1d0d68c..0000000000 --- a/test/e2e/scenario_remote-repository_test.go +++ /dev/null @@ -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")) - -} diff --git a/test/oncluster/scenario_remote-repository_test.go b/test/oncluster/scenario_remote-repository_test.go new file mode 100644 index 0000000000..cc727cec2a --- /dev/null +++ b/test/oncluster/scenario_remote-repository_test.go @@ -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")) + +} diff --git a/test/templates/go/testenvs/go.mod b/test/templates/go/testenvs/go.mod new file mode 100644 index 0000000000..0de336c81c --- /dev/null +++ b/test/templates/go/testenvs/go.mod @@ -0,0 +1,4 @@ +module function + +go 1.21 + diff --git a/test/templates/go/testenvs/handle.go b/test/templates/go/testenvs/handle.go new file mode 100644 index 0000000000..579db3c6e5 --- /dev/null +++ b/test/templates/go/testenvs/handle.go @@ -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")) +} diff --git a/test/templates/go/testvolumes/go.mod b/test/templates/go/testvolumes/go.mod new file mode 100644 index 0000000000..0de336c81c --- /dev/null +++ b/test/templates/go/testvolumes/go.mod @@ -0,0 +1,4 @@ +module function + +go 1.21 + diff --git a/test/templates/go/testvolumes/handle.go b/test/templates/go/testvolumes/handle.go new file mode 100644 index 0000000000..e0e73f7460 --- /dev/null +++ b/test/templates/go/testvolumes/handle.go @@ -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) + } + } + +}