mirror of
https://github.com/hanzoai/deploy.git
synced 2026-10-07 08:44:50 +00:00
go.mod still declared `module github.com/hanzoai/deploy` after the repo was renamed. A Go module path is not cosmetic — it is the resolvable location. So `go get github.com/hanzoai/cd` could not work, and anything importing this module had to spell a name that no longer exists at that address. The build log of v3.6.0 is where it surfaced: the compiler was still emitting github.com/hanzoai/deploy/cmd/cd/commands. 3541 import paths across 690 .go, 19 .proto, 4 .yaml, 3 .sh, 2 .mod, Procfile and Makefile. Plus the 62 ghcr.io/hanzoai/deploy image references, moved to ghcr.io/hanzoai/cd to match hanzo.yml's build target — a distinct string from the module path, replaced separately so neither could clobber the other. go mod tidy clean, CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./... clean. Co-authored-by: Hanzo Dev <[email protected]>
163 lines
3.5 KiB
Go
163 lines
3.5 KiB
Go
package test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
"k8s.io/client-go/tools/cache"
|
|
"sigs.k8s.io/yaml"
|
|
|
|
"github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
|
|
)
|
|
|
|
// StartInformer is a helper to start an informer, wait for its cache to sync and return a cancel func
|
|
func StartInformer(informer cache.SharedIndexInformer) context.CancelFunc {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
go informer.Run(ctx.Done())
|
|
if !cache.WaitForCacheSync(ctx.Done(), informer.HasSynced) {
|
|
log.Fatal("Timed out waiting for informer cache to sync")
|
|
}
|
|
return cancel
|
|
}
|
|
|
|
// GetFreePort finds an available free port on the OS
|
|
func GetFreePort() (int, error) {
|
|
lc := &net.ListenConfig{}
|
|
ln, err := lc.Listen(context.Background(), "tcp", "[::]:0")
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return ln.Addr().(*net.TCPAddr).Port, ln.Close()
|
|
}
|
|
|
|
// WaitForPortListen waits until the given address is listening on the port
|
|
func WaitForPortListen(addr string, timeout time.Duration) error {
|
|
ticker := time.NewTicker(100 * time.Millisecond)
|
|
defer ticker.Stop()
|
|
timer := time.NewTimer(timeout)
|
|
if timeout == 0 {
|
|
timer.Stop()
|
|
} else {
|
|
defer timer.Stop()
|
|
}
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
if portIsOpen(addr) {
|
|
return nil
|
|
}
|
|
case <-timer.C:
|
|
return fmt.Errorf("timeout after %s", timeout.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func portIsOpen(addr string) bool {
|
|
d := &net.Dialer{}
|
|
conn, err := d.DialContext(context.Background(), "tcp", addr)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
_ = conn.Close()
|
|
return true
|
|
}
|
|
|
|
// Read the contents of a file and returns it as string. Panics on error.
|
|
func MustLoadFileToString(path string) string {
|
|
o, err := os.ReadFile(path)
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
return string(o)
|
|
}
|
|
|
|
func YamlToUnstructured(yamlStr string) *unstructured.Unstructured {
|
|
obj := make(map[string]any)
|
|
err := yaml.Unmarshal([]byte(yamlStr), &obj)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return &unstructured.Unstructured{Object: obj}
|
|
}
|
|
|
|
func YamlToApplication(yamlStr string) *v1alpha1.Application {
|
|
app := v1alpha1.Application{}
|
|
err := yaml.Unmarshal([]byte(yamlStr), &app)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return &app
|
|
}
|
|
|
|
// ToMap converts any object to a map[string]any
|
|
func ToMap(obj any) map[string]any {
|
|
data, err := json.Marshal(obj)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
var res map[string]any
|
|
err = json.Unmarshal(data, &res)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return res
|
|
}
|
|
|
|
// GetTestDir will return the full directory path of the
|
|
// calling test file.
|
|
func GetTestDir(t *testing.T) string {
|
|
t.Helper()
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return cwd
|
|
}
|
|
|
|
var logCaptureMutex sync.Mutex
|
|
|
|
type threadSafeBuffer struct {
|
|
buf bytes.Buffer
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
func (tsb *threadSafeBuffer) Write(p []byte) (n int, err error) {
|
|
tsb.mu.Lock()
|
|
defer tsb.mu.Unlock()
|
|
return tsb.buf.Write(p)
|
|
}
|
|
|
|
func (tsb *threadSafeBuffer) String() string {
|
|
tsb.mu.RLock()
|
|
defer tsb.mu.RUnlock()
|
|
return tsb.buf.String()
|
|
}
|
|
|
|
// CaptureLogEntries captures log entries generated by the logger and return it as string
|
|
func CaptureLogEntries(run func()) string {
|
|
logCaptureMutex.Lock()
|
|
defer logCaptureMutex.Unlock()
|
|
|
|
f := log.StandardLogger().Formatter
|
|
log.SetFormatter(&log.TextFormatter{DisableColors: true})
|
|
defer log.SetFormatter(f)
|
|
output := &threadSafeBuffer{}
|
|
original := log.StandardLogger().Out
|
|
log.SetOutput(output)
|
|
log.SetLevel(log.DebugLevel)
|
|
defer log.SetOutput(original)
|
|
|
|
run()
|
|
|
|
return output.String()
|
|
}
|