Skip to content

Commit

Permalink
Adding test templates
Browse files Browse the repository at this point in the history
  • Loading branch information
jrangelramos committed Dec 17, 2024
1 parent 9597296 commit 94d5c6e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
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 94d5c6e

Please sign in to comment.