Skip to content
Open
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
58 changes: 58 additions & 0 deletions pkg/app/piped/executor/kubernetes/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import (
"context"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -44,6 +47,49 @@ import (
"github.com/pipe-cd/pipecd/pkg/model"
)

var requiredEnvtestBinaries = []string{
"etcd",
"kube-apiserver",
}

func envtestAssetsDir() string {
if dir := os.Getenv("KUBEBUILDER_ASSETS"); dir != "" {
return dir
}
return "/usr/local/kubebuilder/bin"
}

func missingEnvtestBinaries(dir string) []string {
missing := make([]string, 0, len(requiredEnvtestBinaries))
for _, binary := range requiredEnvtestBinaries {
if _, err := os.Stat(filepath.Join(dir, binary)); err != nil {
missing = append(missing, binary)
}
}
return missing
}

func requireEnvtestAssets(t *testing.T) {
t.Helper()

dir := envtestAssetsDir()
missing := missingEnvtestBinaries(dir)
if len(missing) == 0 {
return
}

if os.Getenv("KUBEBUILDER_ASSETS") == "" {
for _, binary := range requiredEnvtestBinaries {
if _, err := exec.LookPath(binary); err != nil {
t.Skipf("skipping envtest-dependent test: missing envtest binaries in %q (%s); run `make test/go` or set KUBEBUILDER_ASSETS", dir, strings.Join(missing, ", "))
}
}
return
}

t.Skipf("skipping envtest-dependent test: KUBEBUILDER_ASSETS=%q is missing %s; run `make test/go` or reinstall envtest assets", dir, strings.Join(missing, ", "))
}

func TestEnsureSync(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -184,6 +230,8 @@ func TestEnsureSync(t *testing.T) {
}

func TestExecutor_ensureSync(t *testing.T) {
requireEnvtestAssets(t)

ctrl := gomock.NewController(t)

// initialize tool registry
Expand Down Expand Up @@ -282,6 +330,16 @@ func TestExecutor_ensureSync(t *testing.T) {
assert.Equal(t, "0123456789", deployment.GetAnnotations()["pipecd.dev/commit-hash"])
}

func TestMissingEnvtestBinaries(t *testing.T) {
t.Parallel()

dir := t.TempDir()
err := os.WriteFile(filepath.Join(dir, "etcd"), []byte("#!/bin/sh\n"), 0755)
require.NoError(t, err)

assert.Equal(t, []string{"kube-apiserver"}, missingEnvtestBinaries(dir))
}

func kubeconfigFromRestConfig(restConfig *rest.Config) (string, error) {
clusters := make(map[string]*clientcmdapi.Cluster)
clusters["default-cluster"] = &clientcmdapi.Cluster{
Expand Down
Loading