Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to run short tests only #52

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ SHELL = /usr/bin/env bash -o pipefail
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.28.x

GO_FLAGS ?= -v

.PHONY: all
all: build

Expand Down Expand Up @@ -46,7 +48,11 @@ vet: ## Run go vet against code.

.PHONY: test
test: fmt vet lint envtest ## Run tests
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test -v ./... -coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $(GO_FLAGS) ./... -coverprofile cover.out

.PHONY: test-short
test-short: fmt vet lint envtest ## Run tests
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test -short $(GO_FLAGS) ./... -coverprofile cover.out

.PHONY: lint
lint: golangci-lint ## Run golangci-lint against code
Expand Down
16 changes: 12 additions & 4 deletions cmd/kubectl-k8ssandra/register/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ import (
)

func TestRegister(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}

deferFunc := startKind()
defer deferFunc()

require := require.New(t)
client1, _ := client.New((*multiEnv)[0].RestConfig(), client.Options{})
client2, _ := client.New((*multiEnv)[1].RestConfig(), client.Options{})
Expand Down Expand Up @@ -113,7 +120,8 @@ func TestRegister(t *testing.T) {
return err == nil
}, time.Second*6, time.Millisecond*100)

destKubeconfig := ClientConfigFromSecret(destSecret)
destKubeconfig, err := ClientConfigFromSecret(destSecret)
require.NoError(err)
require.Equal(
sourceSecret.Data["ca.crt"],
destKubeconfig.Clusters["test-destination"].CertificateAuthorityData)
Expand All @@ -123,10 +131,10 @@ func TestRegister(t *testing.T) {
destKubeconfig.AuthInfos["test-destination"].Token)
}

func ClientConfigFromSecret(s *corev1.Secret) clientcmdapi.Config {
func ClientConfigFromSecret(s *corev1.Secret) (clientcmdapi.Config, error) {
out, err := clientcmd.Load(s.Data["kubeconfig"])
if err != nil {
panic(err)
return clientcmdapi.Config{}, err
}
return *out
return *out, nil
}
5 changes: 1 addition & 4 deletions cmd/kubectl-k8ssandra/register/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ func getDefaultServiceAccount(saName, saNamespace string) *corev1.ServiceAccount
}

func (e *RegistrationExecutor) RegisterCluster() error {
log.Printf("Registering cluster from %s Context: %s to %s Context: %s",
registration.GetKubeconfigFileLocation(e.SourceKubeconfig), e.SourceContext,
registration.GetKubeconfigFileLocation(e.DestKubeconfig), e.DestContext,
)
log.Printf("Registering cluster from context: %s to context: %s", e.SourceContext, e.DestContext)

if e.SourceContext == e.DestContext && e.SourceKubeconfig == e.DestKubeconfig {
return NonRecoverable("source and destination context and kubeconfig are the same, you should not register the same cluster to itself. Reference it by leaving the k8sContext field blank instead")
Expand Down
7 changes: 3 additions & 4 deletions cmd/kubectl-k8ssandra/register/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package register

import (
"os"
"testing"

"github.com/k8ssandra/k8ssandra-client/internal/envtest"
)
Expand All @@ -13,12 +12,12 @@ var (
err error
)

func TestMain(m *testing.M) {
func startKind() (deferFunc func()) {
testDir, err = os.MkdirTemp("", "k8ssandra-client-test")
if err != nil {
panic(err.Error())
}
os.Exit(envtest.RunMultiKind(m, func(e *envtest.MultiK8sEnvironment) {
return envtest.RunMultiKind(func(e *envtest.MultiK8sEnvironment) {
multiEnv = e
}, []int{1, 1}, testDir))
}, []int{1, 1}, testDir)
}
6 changes: 0 additions & 6 deletions internal/envtest/envtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,7 @@ func (e *Environment) Start() {
panic(err)
}

//+kubebuilder:scaffold:scheme

e.client = k8sClient
// e.Kubeconfig, err = CreateKubeconfigFileForRestConfig(e.env.Config)
// if err != nil {
// panic(err)
// }
}

func (e *Environment) Stop() {
Expand Down
13 changes: 6 additions & 7 deletions internal/envtest/multi_envtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func RunMulti(m *testing.M, setupFunc func(e *MultiK8sEnvironment), numClusters
return exitCode
}

func RunMultiKind(m *testing.M, setupFunc func(e *MultiK8sEnvironment), topology []int, testDir string) (code int) {
func RunMultiKind(setupFunc func(e *MultiK8sEnvironment), topology []int, testDir string) (deferFunc func()) {
e := make(MultiK8sEnvironment, len(topology))
ctx := ctrl.SetupSignalHandler()
var wg sync.WaitGroup
Expand All @@ -46,7 +46,10 @@ func RunMultiKind(m *testing.M, setupFunc func(e *MultiK8sEnvironment), topology
}(i)
}
wg.Wait()
defer func() {

setupFunc(&e)
return func() {
var wg sync.WaitGroup
for i := 0; i < len(topology); i++ {
wg.Add(1)
go func(i int) {
Expand All @@ -58,9 +61,5 @@ func RunMultiKind(m *testing.M, setupFunc func(e *MultiK8sEnvironment), topology
}(i)
}
wg.Wait()
}()

setupFunc(&e)
exitCode := m.Run()
return exitCode
}
}
4 changes: 4 additions & 0 deletions pkg/helmutil/crds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
)

func TestUpgradingCRDs(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}

require := require.New(t)
chartNames := []string{"cass-operator"}
for _, chartName := range chartNames {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,26 @@ import (
)

func GetClient(configFileLocation string, contextName string) (client.Client, error) {
clientConfig, err := clientcmd.LoadFromFile(GetKubeconfigFileLocation(configFileLocation))
path, err := GetKubeconfigFileLocation(configFileLocation)
if err != nil {
return nil, err
}
clientConfig, err := clientcmd.LoadFromFile(path)
if err != nil {
return nil, err
}
var restConfig *rest.Config
if contextName == "" {
restConfig, err := clientcmd.NewDefaultClientConfig(*clientConfig, &clientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
panic(err)
return nil, err
}
return client.New(restConfig, client.Options{})
}

context, found := clientConfig.Contexts[contextName]
if !found {
panic(errors.New(fmt.Sprint("context not found in supplied kubeconfig ", "contextName: ", contextName, " configFileLocation: ", GetKubeconfigFileLocation(configFileLocation))))
return nil, errors.New(fmt.Sprint("context not found in supplied kubeconfig ", "contextName: ", contextName, " configFileLocation: ", path))
}
overrides := &clientcmd.ConfigOverrides{
Context: *context,
Expand All @@ -39,27 +43,32 @@ func GetClient(configFileLocation string, contextName string) (client.Client, er
restConfig, err = cConfig.ClientConfig()

if err != nil {
panic(err)
return nil, err
}

return client.New(restConfig, client.Options{})
}

func GetKubeconfigFileLocation(location string) string {
func GetKubeconfigFileLocation(location string) (string, error) {
if location != "" {
return location
return location, nil
} else if kubeconfigEnvVar, found := os.LookupEnv("KUBECONFIG"); found {
return kubeconfigEnvVar
return kubeconfigEnvVar, nil
} else {
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
return "", err
}
return filepath.Join(homeDir, ".kube", "config")
return filepath.Join(homeDir, ".kube", "config"), nil
}
}

func KubeconfigToHost(configFileLocation string, contextName string) (string, error) {
clientConfig, err := clientcmd.LoadFromFile(GetKubeconfigFileLocation(configFileLocation))
path, err := GetKubeconfigFileLocation(configFileLocation)
if err != nil {
return "", err
}
clientConfig, err := clientcmd.LoadFromFile(path)
if err != nil {
return "", err
}
Expand All @@ -73,7 +82,7 @@ func KubeconfigToHost(configFileLocation string, contextName string) (string, er

context, found := clientConfig.Contexts[contextName]
if !found {
panic(errors.New(fmt.Sprint("context not found in supplied kubeconfig ", "contextName: ", contextName, " configFileLocation: ", GetKubeconfigFileLocation(configFileLocation))))
return "", errors.New(fmt.Sprint("context not found in supplied kubeconfig ", "contextName: ", contextName, " configFileLocation: ", path))
}
return clientConfig.Clusters[context.Cluster].Server, nil
}
Loading