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

Change the integration tests. #41

Merged
merged 1 commit into from
Aug 2, 2023
Merged
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
136 changes: 70 additions & 66 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: "5m"

on:
push:
Expand All @@ -21,71 +26,70 @@ 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: |
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: 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 --runInDevMode ${{ 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; sleep 2; 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; sleep 2; done'

- name: Display deployment logs
if: failure()
run: |
kubectl get all
kubectl describe pod
kubectl logs -l appName=echo


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