Files
deploy/util/util_test.go
zeekayandHanzo Dev 1ab6e799c1 cd: the module is github.com/hanzoai/cd, where it is actually hosted
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]>
2026-07-28 21:16:24 -07:00

133 lines
3.5 KiB
Go

package util_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/hanzoai/cd/util"
"github.com/hanzoai/cd/util/webhook"
)
func TestMakeSignature(t *testing.T) {
t.Parallel()
for size := 1; size <= 64; size++ {
s, err := util.MakeSignature(size)
require.NoError(t, err, "Could not generate signature of size %d: %v", size, err)
t.Logf("Generated token: %v", s)
}
}
func TestParseRevision(t *testing.T) {
t.Parallel()
type args struct {
ref string
}
tests := []struct {
name string
args args
want string
}{
{name: "gitflow", args: args{ref: "refs/heads/fix/appset-webhook"}, want: "fix/appset-webhook"},
{name: "tag", args: args{ref: "refs/tags/v3.14.1"}, want: "v3.14.1"},
{name: "env-branch", args: args{ref: "refs/heads/env/dev"}, want: "env/dev"},
{name: "main", args: args{ref: "refs/heads/main"}, want: "main"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equalf(t, tt.want, webhook.ParseRevision(tt.args.ref), "parseRevision(%v)", tt.args.ref)
})
}
}
func TestSliceCopy(t *testing.T) {
t.Parallel()
type args struct {
secrets []*corev1.Secret
}
tests := []struct {
name string
args args
want []*corev1.Secret
}{
{name: "nil", args: args{secrets: nil}, want: []*corev1.Secret{}},
{
name: "Three", args: args{secrets: []*corev1.Secret{
{ObjectMeta: metav1.ObjectMeta{Name: "one"}},
{ObjectMeta: metav1.ObjectMeta{Name: "two"}},
{ObjectMeta: metav1.ObjectMeta{Name: "three"}},
}},
want: []*corev1.Secret{
{ObjectMeta: metav1.ObjectMeta{Name: "one"}},
{ObjectMeta: metav1.ObjectMeta{Name: "two"}},
{ObjectMeta: metav1.ObjectMeta{Name: "three"}},
},
},
{
name: "One", args: args{secrets: []*corev1.Secret{{ObjectMeta: metav1.ObjectMeta{Name: "one"}}}},
want: []*corev1.Secret{{ObjectMeta: metav1.ObjectMeta{Name: "one"}}},
},
{name: "Zero", args: args{secrets: []*corev1.Secret{}}, want: []*corev1.Secret{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
secretsCopy := util.SliceCopy(tt.args.secrets)
assert.Equalf(t, tt.want, secretsCopy, "SliceCopy(%v)", tt.args.secrets)
for i := range tt.args.secrets {
assert.NotSame(t, secretsCopy[i], tt.args.secrets[i])
}
})
}
}
// TestGenerateCacheKey tests the GenerateCacheKey function
func TestGenerateCacheKey(t *testing.T) {
t.Parallel()
// Define test cases
testCases := []struct {
format string
args []any
expected string
shouldErr bool
}{
{
format: "Hello %s",
args: []any{"World"},
expected: "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e",
shouldErr: false,
},
{
format: "",
args: []any{},
expected: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
shouldErr: false,
},
{
format: "Number: %d",
args: []any{123},
expected: "665fb090bf37fa3cbba9b92b9f82e6fa4bb851bad4c0a0a6edfb0aefa542150b",
shouldErr: false,
},
// Add more test cases as needed
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("format=%s args=%v", tc.format, tc.args), func(t *testing.T) {
t.Parallel()
key, err := util.GenerateCacheKey(tc.format, tc.args...)
if tc.shouldErr {
require.Errorf(t, err, "expected error but got none")
return
}
require.NoErrorf(t, err, "unexpected error: %v", err)
require.Equalf(t, tc.expected, key, "expected %s but got %s", tc.expected, key)
})
}
}