Skip to content

Commit

Permalink
Change the integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
spetrovic77 committed Aug 2, 2023
1 parent 1c43a18 commit 43c0170
Show file tree
Hide file tree
Showing 9 changed files with 469 additions and 165 deletions.
137 changes: 74 additions & 63 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

name: Integration Tests
name: Integration Test

env:
CONFIG_FILE: deploy.toml
IMAGE_NAME: $(echo "ghcr.io/${{ github.repository }}/echo" | tr '[:upper:]' '[:lower:]')
WAIT_TIMEOUT: "15m"

on:
push:
Expand All @@ -21,71 +26,77 @@ on:
pull_request:

jobs:
collatz:
name: Build collatz binary
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
name: Checkout collatz example
with:
repository: ServiceWeaver/weaver
ref: 'v0.15.0'
- uses: actions/setup-go@v4
with:
go-version: '1.20'
cache: true
- name: Build collatz example
working-directory: ./examples/collatz
run: |
# Disable CGO to ensure the binary is statically linked
CGO_ENABLED=0 \
go build -o collatz
- uses: actions/upload-artifact@v3
with:
name: collatz-binary
path: ./examples/collatz/collatz
deploy:
name: Deploy and run tests
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
needs: collatz
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
cache: true
- uses: azure/setup-kubectl@v3
- uses: medyagh/setup-minikube@master
- uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Install weaver-kube
run: |
go install ./cmd/weaver-kube
- uses: actions/download-artifact@v3
with:
name: collatz-binary
path: ./examples/collatz
- name: Run integration script
working-directory: ./examples/collatz
run: ../../dev/integration.sh
shell: bash
env:
IMAGE_NAME: ghcr.io/${{ github.repository }}/example-collatz
- name: Display deployment logs
if: failure()
working-directory: ./examples/collatz
run: |
- name: Srdjan
run: echo ${{env.IMAGE_NAME}}
- name: Check out the repository
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
cache: true

- name: Setup kubectl.
uses: azure/setup-kubectl@v3

- name: Setup minikube
uses: medyagh/setup-minikube@master

- name: Login into Github Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Install weaver-kube
run: cd cmd/weaver-kube; go build .

- name: Build echo example.
run: cd examples/echo; go build .

- name: Generate app config file
run: |
CONFIG=$(cat << EOF
[serviceweaver]
binary = "./examples/echo/echo"
[kube]
image = "${{ env.IMAGE_NAME }}"
listeners.echo = {public = true}
EOF
)
echo "$CONFIG" > ${{ env.CONFIG_FILE }}
- name: Build the docker image and push
run: ./cmd/weaver-kube/weaver-kube deploy ${{ env.CONFIG_FILE }}

- name: Deploy the application
run: kubectl apply -f ./kube_*.yaml

- name: Get the name of the echo listener service
run: |
NAME=$(timeout ${{ env.WAIT_TIMEOUT }} /bin/sh -c 'while true; do NAME=$(kubectl get service -l lisName=echo -o jsonpath={.items[].metadata.name}) && echo $NAME && break; done')
echo "SERVICE_NAME=$NAME" >> $GITHUB_ENV
- name: Call the echo endpoint until it succeeds
run: |
timeout ${{ env.WAIT_TIMEOUT }} /bin/sh -c 'while true; do kubectl run -i --rm --restart=Never --image=busybox:latest test-api --command wget -- -q -O - http://${{ env.SERVICE_NAME }}?s=testme && break; done'
- name: Display deployment logs
if: failure()
run: |
kubectl get all && \
kubectl describe pod -l app_name=collatz && \
kubectl logs -l app_name=collatz
- uses: actions/upload-artifact@v3
if: always()
with:
name: collatz-kube-manifests
path: ./examples/collatz/kube_*.yaml
- name: Upload manifest files
if: failure()
uses: actions/upload-artifact@v3
with:
name: echo-kube-manifests
path: ./kube_*.yaml
52 changes: 0 additions & 52 deletions dev/integration.sh

This file was deleted.

24 changes: 24 additions & 0 deletions examples/echo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Echo

This directory contains a Service Weaver web application that echos the request
string back to the caller.

This application exists primarily to test the correctness of the Kube deployer.

## How to run?

To run this application inside a Kubernetes cluster, run `weaver kube deploy`.

```console
$ weaver kube deploy weaver.toml # Generate YAML file
$ kubectl apply -f <filename> # Deploy YAML file
```

## How to interact with the application?

Get the IP address of the listener service on your kubernetes cluster. Then run:

```console
$ curl http://<IPAddress>?s=foo
foo
```
45 changes: 45 additions & 0 deletions examples/echo/echo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"

"github.com/ServiceWeaver/weaver"
"github.com/ServiceWeaver/weaver/metrics"
)

// stringLength is a histogram that tracks the length of strings passed to the
// Echo method.
var stringLength = metrics.NewHistogram(
"echo_string_length",
"The length of strings passed to the Echo method",
metrics.NonNegativeBuckets,
)

// Echoer component.
type Echoer interface {
Echo(context.Context, string) (string, error)
}

// Implementation of the Echoer component.
type echoer struct {
weaver.Implements[Echoer]
}

func (e echoer) Echo(ctx context.Context, s string) (string, error) {
stringLength.Put(float64(len(s))) // Update the stringLength metric.
return s, nil
}
59 changes: 59 additions & 0 deletions examples/echo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main

import (
"context"
"flag"
"fmt"
"log"
"net/http"

"github.com/ServiceWeaver/weaver"
)

//go:generate weaver generate

func main() {
// Initialize the Service Weaver application.
flag.Parse()
if err := weaver.Run(context.Background(), serve); err != nil {
log.Fatal(err)
}
}

type server struct {
weaver.Implements[weaver.Main]
echoer weaver.Ref[Echoer]
lis weaver.Listener `weaver:"echo"`
}

func serve(ctx context.Context, s *server) error {
// Setup the HTTP handler.
var mux http.ServeMux
mux.Handle("/", weaver.InstrumentHandlerFunc("echo",
func(w http.ResponseWriter, r *http.Request) {
input := r.URL.Query().Get("s")
output, err := s.echoer.Get().Echo(r.Context(), input)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte(output))
}))
mux.HandleFunc(weaver.HealthzURL, weaver.HealthzHandler)

fmt.Printf("echo listener available on %v\n", s.lis)
return http.Serve(s.lis, &mux)
}
Loading

0 comments on commit 43c0170

Please sign in to comment.