Skip to content

Commit

Permalink
test: remove dependecy for boson-project git repository on e2e tests (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jrangelramos authored Oct 10, 2024
1 parent e97a692 commit 59aa11c
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 44 deletions.
9 changes: 7 additions & 2 deletions test/e2e/scenario_config-envs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package e2e
import (
"context"
"fmt"
"path"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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

Expand Down
16 changes: 12 additions & 4 deletions test/e2e/scenario_config-volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package e2e
import (
"context"
"fmt"
"path"
"path/filepath"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
38 changes: 0 additions & 38 deletions test/e2e/scenario_remote-repository_test.go

This file was deleted.

98 changes: 98 additions & 0 deletions test/oncluster/scenario_remote-repository_test.go
Original file line number Diff line number Diff line change
@@ -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"))

}
4 changes: 4 additions & 0 deletions test/templates/go/testenvs/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module function

go 1.21

19 changes: 19 additions & 0 deletions test/templates/go/testenvs/handle.go
Original file line number Diff line number Diff line change
@@ -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"))
}
4 changes: 4 additions & 0 deletions test/templates/go/testvolumes/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module function

go 1.21

44 changes: 44 additions & 0 deletions test/templates/go/testvolumes/handle.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

}

0 comments on commit 59aa11c

Please sign in to comment.