cmd/osbuild-worker-executor: import verbatim from mvo5/oaas
This commit imports the repository https://github.com/mvo5/oaas without keeping the history. The history is largely unimportant and can be looked up in https://github.com/mvo5/oaas/commits/main if needed.
This commit is contained in:
parent
fa416e4545
commit
372d9f07dd
12 changed files with 1024 additions and 0 deletions
74
cmd/osbuild-worker-executor/main_test.go
Normal file
74
cmd/osbuild-worker-executor/main_test.go
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
package main_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
logrusTest "github.com/sirupsen/logrus/hooks/test"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
main "github.com/osbuild/osbuild-composer/cmd/oaas"
|
||||
)
|
||||
|
||||
const defaultTimeout = 5 * time.Second
|
||||
|
||||
func waitReady(ctx context.Context, timeout time.Duration, endpoint string) error {
|
||||
client := http.Client{}
|
||||
startTime := time.Now()
|
||||
for {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
resp.Body.Close()
|
||||
return nil
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
if time.Since(startTime) >= timeout {
|
||||
return fmt.Errorf("timeout reached while waiting for endpoint")
|
||||
}
|
||||
// wait a little while between checks
|
||||
time.Sleep(250 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func runTestServer(t *testing.T) (baseURL, buildBaseDir string, loggerHook *logrusTest.Hook) {
|
||||
host := "localhost"
|
||||
port := "18002"
|
||||
buildBaseDir = t.TempDir()
|
||||
baseURL = fmt.Sprintf("http://%s:%s/", host, port)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
|
||||
loggerHook, restore := main.MockLogger()
|
||||
defer restore()
|
||||
|
||||
args := []string{
|
||||
"-host", host,
|
||||
"-port", port,
|
||||
"-build-path", buildBaseDir,
|
||||
}
|
||||
go main.Run(ctx, args, os.Getenv)
|
||||
|
||||
err := waitReady(ctx, defaultTimeout, baseURL)
|
||||
assert.NoError(t, err)
|
||||
|
||||
return baseURL, buildBaseDir, loggerHook
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue