-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from spacelift-io/feat/add_run_controller_tests
feat: add run controller tests
- Loading branch information
Showing
5 changed files
with
191 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package controller_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/suite" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
"github.com/spacelift-io/spacelift-operator/api/v1beta1" | ||
"github.com/spacelift-io/spacelift-operator/tests/integration" | ||
) | ||
|
||
type RunControllerSuite struct { | ||
integration.IntegrationTestSuite | ||
} | ||
|
||
func (s *RunControllerSuite) TestRunCreation_InvalidSpec() { | ||
cases := []struct { | ||
Name string | ||
Spec v1beta1.RunSpec | ||
ExpectedErr string | ||
}{ | ||
{ | ||
Spec: v1beta1.RunSpec{}, | ||
Name: "missing stackName", | ||
ExpectedErr: `Run.app.spacelift.io "invalid-run" is invalid: spec.stackName: Invalid value: "": spec.stackName in body should be at least 1 chars long`, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
s.T().Run(c.Name, func(t *testing.T) { | ||
newRun := &v1beta1.Run{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Run", | ||
APIVersion: v1beta1.GroupVersion.String(), | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "invalid-run", | ||
Namespace: "default", | ||
}, | ||
Spec: c.Spec, | ||
} | ||
err := s.Client().Create(s.Context(), newRun) | ||
s.Assert().EqualError(err, c.ExpectedErr) | ||
}) | ||
} | ||
} | ||
|
||
func (s *RunControllerSuite) TestRunCreation_OK() { | ||
run := &v1beta1.Run{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Run", | ||
APIVersion: v1beta1.GroupVersion.String(), | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-run", | ||
Namespace: "default", | ||
}, | ||
Spec: v1beta1.RunSpec{ | ||
StackName: "foobar", | ||
}, | ||
} | ||
err := s.Client().Create(s.Context(), run) | ||
s.Require().NoError(err) | ||
|
||
// Assert that the Queued state has been applied | ||
s.Eventually(func() bool { | ||
run, err = s.RunRepo().Get(s.Context(), types.NamespacedName{ | ||
Namespace: run.Namespace, | ||
Name: run.Name, | ||
}) | ||
s.Require().NoError(err) | ||
return run.Status.State == v1beta1.RunStateQueued | ||
}, 10*time.Second, 1*time.Second) | ||
} | ||
|
||
func TestRunController(t *testing.T) { | ||
suite.Run(t, new(RunControllerSuite)) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package integration | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"github.com/stretchr/testify/suite" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"go.uber.org/zap/zaptest/observer" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
kubezap "sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
|
||
"github.com/spacelift-io/spacelift-operator/api/v1beta1" | ||
"github.com/spacelift-io/spacelift-operator/internal/controller" | ||
"github.com/spacelift-io/spacelift-operator/internal/k8s/repository" | ||
) | ||
|
||
type IntegrationTestSuite struct { | ||
suite.Suite | ||
|
||
ctx context.Context | ||
cancel context.CancelFunc | ||
k8sClient client.Client | ||
testEnv *envtest.Environment | ||
logs *observer.ObservedLogs | ||
|
||
runRepo *repository.RunRepository | ||
} | ||
|
||
func (s *IntegrationTestSuite) SetupSuite() { | ||
logf.SetLogger(kubezap.New( | ||
kubezap.WriteTo(os.Stdout), | ||
kubezap.UseDevMode(true), | ||
kubezap.RawZapOpts( | ||
zap.WrapCore(func(core zapcore.Core) zapcore.Core { | ||
zapCoreObserver, logs := observer.New(zapcore.InfoLevel) | ||
s.logs = logs | ||
return zapcore.NewTee(core, zapCoreObserver) | ||
})), | ||
)) | ||
s.ctx, s.cancel = context.WithCancel(context.Background()) | ||
|
||
s.testEnv = &envtest.Environment{ | ||
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, | ||
ErrorIfCRDPathMissing: true, | ||
|
||
// The BinaryAssetsDirectory is only required if you want to run the tests directly | ||
// without call the makefile target test. If not informed it will look for the | ||
// default path defined in controller-runtime which is /usr/local/kubebuilder/. | ||
// Note that you must have the required binaries setup under the bin directory to perform | ||
// the tests directly. When we run make test it will be setup and used automatically. | ||
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s", | ||
fmt.Sprintf("1.28.0-%s-%s", runtime.GOOS, runtime.GOARCH)), | ||
} | ||
|
||
cfg, err := s.testEnv.Start() | ||
s.Require().NoError(err) | ||
s.Require().NotNil(cfg) | ||
|
||
err = v1beta1.AddToScheme(scheme.Scheme) | ||
s.Require().NoError(err) | ||
|
||
//+kubebuilder:scaffold:scheme | ||
|
||
s.k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) | ||
s.Require().NoError(err) | ||
s.Require().NotNil(s.k8sClient) | ||
|
||
mgr, err := ctrl.NewManager(cfg, ctrl.Options{ | ||
Scheme: scheme.Scheme, | ||
}) | ||
s.Require().NoError(err) | ||
|
||
s.runRepo = repository.NewRunRepository(mgr.GetClient()) | ||
err = (&controller.RunReconciler{RunRepository: s.runRepo}).SetupWithManager(mgr) | ||
s.Require().NoError(err) | ||
|
||
go func() { | ||
err := mgr.Start(s.ctx) | ||
s.Require().NoError(err) | ||
}() | ||
} | ||
|
||
func (s *IntegrationTestSuite) TearDownSuite() { | ||
s.cancel() | ||
err := s.testEnv.Stop() | ||
s.Require().NoError(err) | ||
} | ||
|
||
func (s *IntegrationTestSuite) Client() client.Client { | ||
return s.k8sClient | ||
} | ||
|
||
func (s *IntegrationTestSuite) RunRepo() *repository.RunRepository { | ||
return s.runRepo | ||
} | ||
|
||
func (s *IntegrationTestSuite) Context() context.Context { | ||
return s.ctx | ||
} |