Skip to content

Commit

Permalink
Merge pull request #11 from evryn/dev
Browse files Browse the repository at this point in the history
Fix workflow
  • Loading branch information
AmirrezaNasiri authored Aug 11, 2023
2 parents 6c89237 + c6d3575 commit 792a4e1
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 30 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/build-and-push-docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ name: Build and publish a Docker image
on:
push:
branches:
- '*'
- 'main'
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
- 'v*'
pull_request:
types: [opened, synchronize, reopened]
branches:
- '*'
release:
types: [created]
- 'main'

jobs:
build:
Expand Down
22 changes: 19 additions & 3 deletions .github/workflows/ci-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:

env:
# Golang version to use across CI steps
GOLANG_VERSION: '1.20'
GOLANG_VERSION: '1.20.5'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down Expand Up @@ -104,10 +104,26 @@ jobs:
with:
path: ~/.cache/go-build
key: ${{ runner.os }}-go-build-v1-${{ github.run_id }}
- name: Download all Go modules
-
name: Download all Go modules
run: make mod-download-local
- name: Compile all packages
-
name: Compile all packages
run: make test-e2e
id: e2e-testing-step
-
name: Generate E2E log artifacts
if: always()
uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
with:
name: e2e-test-results
path: e2e-test-results
# -
# name: Fail of tests are failed
# if: ${{ steps.e2e-testing-step.outcome == 'failure' }}
# run: |
# echo "E2E tests failed. See the step details and inspect logs artifact."
# exit 1

analyze:
name: Process & analyze test artifacts
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.dev
docker-compose.yml
coverage.out
e2e-test-results
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ test-with-coverage:

.PHONY: test-e2e
test-e2e:
go test -v ./tests/e2e/...
rm -rf e2e-test-results
go build -v -o /tmp/kermoo .
KERMOO_BINARY="/tmp/kermoo" go test -v ./tests/e2e/...
2 changes: 1 addition & 1 deletion modules/user_config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type UserConfigType struct {
}

func (u *UserConfigType) Validate() error {
if u.SchemaVersion != "0.1-beta" {
if u.SchemaVersion != "" && u.SchemaVersion != "0.1-beta" {
return fmt.Errorf("schema version is not supported")
}

Expand Down
8 changes: 6 additions & 2 deletions modules/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,18 @@ func convertMapKeysToString(m map[interface{}]interface{}) map[string]interface{

func GetDuplicates(items []string) []string {
itemCount := make(map[string]int)
var order []string
duplicates := []string{}

for _, item := range items {
if itemCount[item] == 0 {
order = append(order, item) // Save the order of first appearance
}
itemCount[item]++
}

for item, count := range itemCount {
if count > 1 {
for _, item := range order {
if itemCount[item] > 1 {
duplicates = append(duplicates, item)
}
}
Expand Down
4 changes: 0 additions & 4 deletions tests/e2e/process_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ func TestProcessManagerEndToEnd(t *testing.T) {
e2e := NewE2E(t)

e2e.Start(`
schemaVersion: "0.1-beta"
process:
exit:
after:
Expand All @@ -29,7 +28,6 @@ func TestProcessManagerEndToEnd(t *testing.T) {
e2e := NewE2E(t)

e2e.Start(`
schemaVersion: "0.1-beta"
process:
exit:
after:
Expand All @@ -50,7 +48,6 @@ func TestProcessManagerEndToEnd(t *testing.T) {
e2e := NewE2E(t)

e2e.Start(`
schemaVersion: "0.1-beta"
process:
exit:
after:
Expand All @@ -69,7 +66,6 @@ func TestProcessManagerEndToEnd(t *testing.T) {
e2e := NewE2E(t)

e2e.Start(`
schemaVersion: "0.1-beta"
process:
exit:
after:
Expand Down
93 changes: 91 additions & 2 deletions tests/e2e/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"syscall"
"testing"
Expand Down Expand Up @@ -52,7 +54,67 @@ func (e *E2E) WithEnv(env string) {
e.envs = append(e.envs, env)
}

// Get exit code from command process if binary is used
// or parse the last output line to determine the exit code.
func (e *E2E) GetExitCode() (int, error) {
if e.GetKermooBinaryPath() != "" {
return e.cmd.ProcessState.ExitCode(), nil
}

re, err := regexp.Compile(`\d+`)
if err != nil {
return 0, fmt.Errorf("error compiling regex: %v", err)
}

// Find the first match in the string
lastLine := e.GetLastOutputLine()
match := re.FindString(lastLine)
if match == "" {
return 0, fmt.Errorf("no match found: %s", lastLine)
}

// Convert the matched string to an integer
i, err := strconv.Atoi(match)
if err != nil {
return 0, fmt.Errorf("error converting exit code string to int: %v", err)
}

return i, nil
}

func (e *E2E) GetFileLogWriter() *os.File {
logDir := RootPath("e2e-test-results")

if err := os.MkdirAll(logDir, 0755); err != nil {
panic(err)
}

name := strings.ReplaceAll(e.t.Name(), "/", "_")

file, err := os.OpenFile(logDir+"/"+name+".log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
panic(err)
}

file.Write([]byte("prepared.\n"))

// io.Writer to append content to the file
return file
}

func (e *E2E) GetKermooBinaryPath() string {
return os.Getenv("KERMOO_BINARY")
}

func (e *E2E) Start(config string, timeout time.Duration) {
if e.GetKermooBinaryPath() != "" {
e.StartBinary(config, timeout)
} else {
e.StartBuild(config, timeout)
}
}

func (e *E2E) StartBuild(config string, timeout time.Duration) {
e.context, e.cancel = context.WithTimeout(context.Background(), timeout)

e.cmd = exec.CommandContext(e.context, "go", "run", RootPath("main.go"), "start", "-v", "debug", "-f", "-")
Expand Down Expand Up @@ -80,6 +142,29 @@ func (e *E2E) Start(config string, timeout time.Duration) {
assert.NoError(e.t, e.cmd.Start())
}

func (e *E2E) StartBinary(config string, timeout time.Duration) {
e.context, e.cancel = context.WithTimeout(context.Background(), timeout)

var mw io.Writer

e.cmd = exec.CommandContext(e.context, e.GetKermooBinaryPath(), "start", "-v", "debug", "-f", "-")

//mw = io.MultiWriter(os.Stdout, e.GetFileLogWriter(), &e.out)
mw = io.MultiWriter(e.GetFileLogWriter(), &e.out)

e.cmd.Env = os.Environ()
e.cmd.Env = append(e.cmd.Env, e.envs...)

e.cmd.Stdin = strings.NewReader(strings.Trim(config, "\t"))

e.cmd.Stdout = mw
e.cmd.Stderr = mw

e.startedAt = time.Now()

require.NoError(e.t, e.cmd.Start())
}

func (e *E2E) Wait() {
defer e.cancel()

Expand Down Expand Up @@ -112,8 +197,12 @@ func (e *E2E) RequireTimedOut() {
require.True(e.t, e.timedout, "command is NOT timedout")
}

func (e *E2E) AssertExitCode(code int) {
assert.Equal(e.t, fmt.Sprintf("exit status %d", code), e.GetLastOutputLine())
func (e *E2E) AssertExitCode(expectedCode int) {
exitCode, err := e.GetExitCode()

require.Nil(e.t, err)

assert.Equal(e.t, expectedCode, exitCode)
}

func (e *E2E) GetOutput() string {
Expand Down
16 changes: 4 additions & 12 deletions tests/e2e/webserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"kermoo/modules/web_server"
"net/http"
"testing"
Expand All @@ -19,7 +19,6 @@ func TestWebserverEndToEnd(t *testing.T) {
e2e := NewE2E(t)

e2e.Start(`
schemaVersion: "0.1-beta"
webServers:
- port: 8080
routes:
Expand All @@ -40,7 +39,6 @@ func TestWebserverEndToEnd(t *testing.T) {
e2e := NewE2E(t)

e2e.Start(`
schemaVersion: "0.1-beta"
webServers:
- port: 8000
interface: 127.0.0.1
Expand All @@ -67,7 +65,6 @@ func TestWebserverEndToEnd(t *testing.T) {

e2e.WithEnv("HOSTNAME=container-123")
e2e.Start(`
schemaVersion: "0.1-beta"
webServers:
- port: 8000
interface: 127.0.0.1
Expand Down Expand Up @@ -98,7 +95,6 @@ func TestWebserverEndToEnd(t *testing.T) {
e2e := NewE2E(t)

e2e.Start(`
schemaVersion: "0.1-beta"
webServers:
- port: 8080
routes:
Expand Down Expand Up @@ -129,7 +125,6 @@ func TestWebserverEndToEnd(t *testing.T) {
e2e := NewE2E(t)

e2e.Start(`
schemaVersion: "0.1-beta"
plans:
- name: disaster
interval: 100ms
Expand Down Expand Up @@ -163,7 +158,6 @@ func TestWebserverEndToEnd(t *testing.T) {
e2e := NewE2E(t)

e2e.Start(`
schemaVersion: "0.1-beta"
plans:
- name: disaster
subPlans:
Expand Down Expand Up @@ -219,7 +213,6 @@ func TestWebserverEndToEnd(t *testing.T) {
e2e := NewE2E(t)

e2e.Start(`
schemaVersion: "0.1-beta"
webServers:
- port: 8080
routes:
Expand Down Expand Up @@ -253,7 +246,6 @@ func TestWebserverEndToEnd(t *testing.T) {
e2e := NewE2E(t)

e2e.Start(`
schemaVersion: "0.1-beta"
plans:
- name: readiness
interval: 100ms
Expand All @@ -270,12 +262,12 @@ func TestWebserverEndToEnd(t *testing.T) {
- readiness
clientErrors: true
serverErrors: true
`, 2*time.Second)
`, 5*time.Second)

// Wait a few while for webserver to become available
time.Sleep(500 * time.Millisecond)

inspect := InspectRoute(t, "GET", "http://0.0.0.0:8080/my-probe", 50*time.Millisecond)
inspect := InspectRoute(t, "GET", "http://0.0.0.0:8080/my-probe", 30*time.Millisecond)

e2e.Wait()

Expand Down Expand Up @@ -379,7 +371,7 @@ func sendRequest(method, url string, body []byte) (string, *http.Response, error
defer resp.Body.Close()

// Read the response
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 792a4e1

Please sign in to comment.