Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adapt simple UI testing tool #973

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions tools/simpleui/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"fmt"
"html/template"
"log"
Expand All @@ -10,9 +11,18 @@ import (
"github.com/gorilla/mux"
pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)

var (
// Command line flags
// Cert file can be generated by: kubectl get secrets tekton-results-tls -n tekton-pipelines --template='{{index .data "tls.crt"}}' | base64 -d > /tmp/results.crt
certFile = flag.String("cert", "/tmp/results.crt", "Path to the TLS certificate file.")
target = flag.String("target", "tekton-results-api-service.tekton-pipelines.svc.cluster.local", "Target server name")
port = flag.String("port", "8081", "Port to listen on")
grpcAddr = flag.String("grpc-addr", "localhost:8080", "gRPC server address")

// Templates
resultTmpl = template.Must(template.New("results.html").ParseFiles("templates/results.html"))
recordTmpl = template.Must(template.New("records.html").
Funcs(template.FuncMap{
Expand Down Expand Up @@ -68,21 +78,35 @@ type ui struct {
}

func main() {
conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure(), grpc.WithBlock())
// Parse command line flags
flag.Parse()

// Load the TLS certificate
creds, err := credentials.NewClientTLSFromFile(*certFile, *target)
if err != nil {
log.Fatalf("failed to load credentials: %v", err)
}

// Connect to the gRPC server
conn, err := grpc.NewClient(*grpcAddr,
grpc.WithTransportCredentials(creds),
grpc.WithAuthority(*target))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
u := &ui{
client: pb.NewResultsClient(conn),
}
log.Printf("Connected to gRPC server at %s", *grpcAddr)

r := mux.NewRouter()
r.HandleFunc("/", u.home)
r.HandleFunc("/{namespace}", u.results)
r.HandleFunc("/{namespace}/results/{name}", u.records)
http.Handle("/", r)

log.Println("Running on http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
addr := ":" + *port
log.Printf("Running on http://localhost%s", addr)
log.Fatal(http.ListenAndServe(addr, nil))
}
14 changes: 3 additions & 11 deletions tools/simpleui/template.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
package main

import (
"fmt"
"strings"

"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/types/known/anypb"

_ "github.com/tektoncd/results/proto/pipeline/v1/pipeline_go_proto"
pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
)

func textproto(a *anypb.Any) (string, error) {
m, err := a.UnmarshalNew()
if err != nil {
fmt.Println(err)
return "", err
}
return prototext.Format(m), nil
func textproto(a *pb.Any) string {
return a.String()
}

func parent(in string) string {
Expand Down
4 changes: 2 additions & 2 deletions tools/simpleui/templates/records.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ <h1>Records</h1>
{{range .Response.Records}}
<tr>
<td>{{.Name}}</td>
<td>{{trimprefix .Data.TypeUrl "type.googleapis.com/"}}</td>
<td>{{trimprefix .Data.Type "type.googleapis.com/"}}</td>
<td><a href="/{{parent .Name}}">{{parent .Name}}</a></td>
<td>{{.CreatedTime.AsTime}}</td>
<td><span style="white-space: pre-wrap"><code>{{textproto .Data}}</code></span></td>
</tr>
{{end}}
</table>
</body>
</html>
</html>