Skip to content

Commit

Permalink
actual resource creation
Browse files Browse the repository at this point in the history
initial test version

checking existence of created Runtime CR

asserting administrators

networking asserted

networking asserted
  • Loading branch information
jaroslaw-pieszka committed Jul 17, 2024
1 parent 605bb97 commit 7b04586
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 9 deletions.
3 changes: 3 additions & 0 deletions cmd/broker/broker_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
imv1 "github.com/kyma-project/infrastructure-manager/api/v1"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -161,6 +162,8 @@ func NewBrokerSuiteTestWithConfig(t *testing.T, cfg *Config, version ...string)
sch := internal.NewSchemeForTests(t)
err := apiextensionsv1.AddToScheme(sch)
require.NoError(t, err)
err = imv1.AddToScheme(sch)
require.NoError(t, err)
additionalKymaVersions := []string{"1.19", "1.20", "main", "2.0"}
additionalKymaVersions = append(additionalKymaVersions, version...)
cli := fake.NewClientBuilder().WithScheme(sch).WithRuntimeObjects(fixK8sResources(defaultKymaVer, additionalKymaVersions)...).Build()
Expand Down
4 changes: 4 additions & 0 deletions cmd/broker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
imv1 "github.com/kyma-project/infrastructure-manager/api/v1"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -204,6 +205,9 @@ func periodicProfile(logger lager.Logger, profiler ProfilerConfig) {
func main() {
err := apiextensionsv1.AddToScheme(scheme.Scheme)
panicOnError(err)
err = imv1.AddToScheme(scheme.Scheme)
panicOnError(err)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down
2 changes: 1 addition & 1 deletion cmd/broker/provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func NewProvisioningProcessingQueue(ctx context.Context, provisionManager *proce
{
condition: provisioning.SkipForOwnClusterPlan,
stage: createRuntimeStageName,
step: provisioning.NewCreateRuntimeResourceStep(db.Operations(), db.RuntimeStates(), db.Instances(), cfg.Broker.KimConfig, cfg.Provisioner),
step: provisioning.NewCreateRuntimeResourceStep(db.Operations(), db.RuntimeStates(), db.Instances(), cli, cfg.Broker.KimConfig),

Check failure on line 90 in cmd/broker/provisioning.go

View workflow job for this annotation

GitHub Actions / run-govulncheck

cannot use cli (variable of type "sigs.k8s.io/controller-runtime/pkg/client".Client) as kim.Config value in argument to provisioning.NewCreateRuntimeResourceStep

Check failure on line 90 in cmd/broker/provisioning.go

View workflow job for this annotation

GitHub Actions / run-govulncheck

cannot use cfg.Broker.KimConfig (variable of type kim.Config) as input.Config value in argument to provisioning.NewCreateRuntimeResourceStep

Check failure on line 90 in cmd/broker/provisioning.go

View workflow job for this annotation

GitHub Actions / run-go-linter

cannot use cli (variable of type "sigs.k8s.io/controller-runtime/pkg/client".Client) as kim.Config value in argument to provisioning.NewCreateRuntimeResourceStep

Check failure on line 90 in cmd/broker/provisioning.go

View workflow job for this annotation

GitHub Actions / run-go-linter

cannot use cfg.Broker.KimConfig (variable of type kim.Config) as input.Config value in argument to provisioning.NewCreateRuntimeResourceStep

Check failure on line 90 in cmd/broker/provisioning.go

View workflow job for this annotation

GitHub Actions / run-go-linter

cannot use cli (variable of type "sigs.k8s.io/controller-runtime/pkg/client".Client) as kim.Config value in argument to provisioning.NewCreateRuntimeResourceStep

Check failure on line 90 in cmd/broker/provisioning.go

View workflow job for this annotation

GitHub Actions / run-go-linter

cannot use cfg.Broker.KimConfig (variable of type kim.Config) as input.Config value in argument to provisioning.NewCreateRuntimeResourceStep

Check failure on line 90 in cmd/broker/provisioning.go

View workflow job for this annotation

GitHub Actions / run-go-linter

cannot use cli (variable of type "sigs.k8s.io/controller-runtime/pkg/client".Client) as kim.Config value in argument to provisioning.NewCreateRuntimeResourceStep

Check failure on line 90 in cmd/broker/provisioning.go

View workflow job for this annotation

GitHub Actions / run-go-linter

cannot use cfg.Broker.KimConfig (variable of type kim.Config) as input.Config value in argument to provisioning.NewCreateRuntimeResourceStep

Check failure on line 90 in cmd/broker/provisioning.go

View workflow job for this annotation

GitHub Actions / run-go-tests / build

cannot use cli (variable of type "sigs.k8s.io/controller-runtime/pkg/client".Client) as kim.Config value in argument to provisioning.NewCreateRuntimeResourceStep

Check failure on line 90 in cmd/broker/provisioning.go

View workflow job for this annotation

GitHub Actions / run-go-tests / build

cannot use cfg.Broker.KimConfig (variable of type kim.Config) as input.Config value in argument to provisioning.NewCreateRuntimeResourceStep
},
{
condition: provisioning.DoForOwnClusterPlanOnly,
Expand Down
15 changes: 7 additions & 8 deletions internal/process/provisioning/create_runtime_resource_step.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package provisioning

import (
"context"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/client"
"time"

gardener "github.com/gardener/gardener/pkg/apis/core/v1beta1"
Expand All @@ -28,6 +30,7 @@ type CreateRuntimeResourceStep struct {
operationManager *process.OperationManager
instanceStorage storage.Instances
runtimeStateStorage storage.RuntimeStates
k8sClient client.Client
kimConfig kim.Config

config input.Config
Expand Down Expand Up @@ -73,11 +76,12 @@ func (s *CreateRuntimeResourceStep) Run(operation internal.Operation, log logrus
fmt.Println(yaml)
}
} else {
err := s.CreateResource(runtimeCR)
err := s.k8sClient.Create(context.Background(), runtimeCR)
if err != nil {
return s.operationManager.OperationFailed(operation, fmt.Sprintf("while creating Runtime CR resource: %s", err), err, log)
log.Error("unable to create Runtime resource: %s", err)
return s.operationManager.OperationFailed(operation, fmt.Sprintf("unable to Runtime resource: %s", err), err, log)
}
log.Info("Runtime CR creation process finished successfully")
log.Infof("Runtime CR %s creation process finished successfully", operation.RuntimeID)
}
return operation, 0, nil
}
Expand All @@ -91,11 +95,6 @@ func getKymaNames(operation internal.Operation) (string, string) {
return template.GetName(), template.GetNamespace()
}

func (s *CreateRuntimeResourceStep) CreateResource(cr *imv1.Runtime) error {
logrus.Info("Creating Runtime CR - TO BE IMPLEMENTED")
return nil
}

func (s *CreateRuntimeResourceStep) createRuntimeResourceObject(operation internal.Operation, kymaName, kymaNamespace string) (*imv1.Runtime, error) {

runtime := imv1.Runtime{}
Expand Down
124 changes: 124 additions & 0 deletions internal/process/provisioning/create_runtime_resource_step_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
package provisioning

import (
"context"
"fmt"
imv1 "github.com/kyma-project/infrastructure-manager/api/v1"
"github.com/kyma-project/kyma-environment-broker/internal/broker"
"os"
"reflect"
"strings"
"testing"

"github.com/kyma-project/kyma-environment-broker/internal/process/input"

"github.com/kyma-project/kyma-environment-broker/internal"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/kyma-project/kyma-environment-broker/internal/fixture"
"github.com/kyma-project/kyma-environment-broker/internal/kim"

"github.com/kyma-project/kyma-environment-broker/internal/storage"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"

"k8s.io/client-go/kubernetes/scheme"
)

var expectedAdministrators = []string{"[email protected]", "[email protected]"}

func TestCreateRuntimeResourceStep_HappyPath_YamlOnly(t *testing.T) {
// given
log := logrus.New()
Expand Down Expand Up @@ -47,3 +63,111 @@ func TestCreateRuntimeResourceStep_HappyPath_YamlOnly(t *testing.T) {
assert.NoError(t, err)

}

func TestCreateRuntimeResourceStep_HappyPath_ActualCreation(t *testing.T) {
// given
log := logrus.New()
memoryStorage := storage.NewMemoryStorage()

err := imv1.AddToScheme(scheme.Scheme)

instance := fixInstance()
preOperation := fixOperationForCreateRuntimeResource(instance.InstanceID)

err = memoryStorage.Operations().InsertOperation(preOperation)
assert.NoError(t, err)

err = memoryStorage.Instances().Insert(instance)
assert.NoError(t, err)

kimConfig := kim.Config{
Enabled: true,
Plans: []string{"azure"},
ViewOnly: false,
DryRun: false,
}

cli := getClientForTests(t)
step := NewCreateRuntimeResourceStep(memoryStorage.Operations(), memoryStorage.RuntimeStates(), memoryStorage.Instances(), cli, kimConfig)

Check failure on line 91 in internal/process/provisioning/create_runtime_resource_step_test.go

View workflow job for this annotation

GitHub Actions / run-go-linter

cannot use cli (variable of type "sigs.k8s.io/controller-runtime/pkg/client".Client) as kim.Config value in argument to NewCreateRuntimeResourceStep

Check failure on line 91 in internal/process/provisioning/create_runtime_resource_step_test.go

View workflow job for this annotation

GitHub Actions / run-go-linter

cannot use kimConfig (variable of type kim.Config) as input.Config value in argument to NewCreateRuntimeResourceStep

Check failure on line 91 in internal/process/provisioning/create_runtime_resource_step_test.go

View workflow job for this annotation

GitHub Actions / run-go-linter

cannot use cli (variable of type "sigs.k8s.io/controller-runtime/pkg/client".Client) as kim.Config value in argument to NewCreateRuntimeResourceStep

Check failure on line 91 in internal/process/provisioning/create_runtime_resource_step_test.go

View workflow job for this annotation

GitHub Actions / run-go-linter

cannot use kimConfig (variable of type kim.Config) as input.Config value in argument to NewCreateRuntimeResourceStep

Check failure on line 91 in internal/process/provisioning/create_runtime_resource_step_test.go

View workflow job for this annotation

GitHub Actions / run-go-tests / build

cannot use cli (variable of type "sigs.k8s.io/controller-runtime/pkg/client".Client) as kim.Config value in argument to NewCreateRuntimeResourceStep

Check failure on line 91 in internal/process/provisioning/create_runtime_resource_step_test.go

View workflow job for this annotation

GitHub Actions / run-go-tests / build

cannot use kimConfig (variable of type kim.Config) as input.Config value in argument to NewCreateRuntimeResourceStep

// when
entry := log.WithFields(logrus.Fields{"step": "TEST"})
_, repeat, err := step.Run(preOperation, entry)

// then
assert.NoError(t, err)
assert.Zero(t, repeat)

runtime := imv1.Runtime{}
err = cli.Get(context.Background(), client.ObjectKey{
Namespace: "kyma-system",
Name: preOperation.RuntimeID,
}, &runtime)
assert.NoError(t, err)
assert.Equal(t, preOperation.RuntimeID, runtime.Name)
assert.Equal(t, "my-kyma", runtime.Labels["operator.kyma-project.io/kyma-name"])

assertLabels(t, preOperation, runtime)
assertSecurity(t, runtime)

_, err = memoryStorage.Instances().GetByID(preOperation.InstanceID)
assert.NoError(t, err)

}

func assertSecurity(t *testing.T, runtime imv1.Runtime) {
assert.True(t, reflect.DeepEqual(runtime.Spec.Security.Administrators, expectedAdministrators))
assert.Equal(t, runtime.Spec.Security.Networking.Filter, imv1.Filter{
Ingress: (*imv1.Ingress)(nil), //TODO change when previous PR is merged
Egress: imv1.Egress{Enabled: false}})
}

func assertLabels(t *testing.T, preOperation internal.Operation, runtime imv1.Runtime) {
assert.Equal(t, preOperation.InstanceID, runtime.Labels["kyma-project.io/instance-id"])
assert.Equal(t, preOperation.RuntimeID, runtime.Labels["kyma-project.io/runtime-id"])
assert.Equal(t, preOperation.ProvisioningParameters.PlanID, runtime.Labels["kyma-project.io/broker-plan-id"])
assert.Equal(t, broker.PlanNamesMapping[preOperation.ProvisioningParameters.PlanID], runtime.Labels["kyma-project.io/broker-plan-name"])
assert.Equal(t, preOperation.ProvisioningParameters.ErsContext.GlobalAccountID, runtime.Labels["kyma-project.io/global-account-id"])
assert.Equal(t, preOperation.ProvisioningParameters.ErsContext.SubAccountID, runtime.Labels["kyma-project.io/subaccount-id"])
assert.Equal(t, preOperation.ShootName, runtime.Labels["kyma-project.io/shoot-name"])
assert.Equal(t, *preOperation.ProvisioningParameters.Parameters.Region, runtime.Labels["kyma-project.io/region"])
}

func fixOperationForCreateRuntimeResource(instanceID string) internal.Operation {
operation := fixture.FixOperation("op-id", instanceID, internal.OperationTypeProvision)

operation.ProvisioningParameters.Parameters.RuntimeAdministrators = expectedAdministrators
operation.KymaTemplate = `
apiVersion: operator.kyma-project.io/v1beta2
kind: Kyma
metadata:
name: my-kyma
namespace: kyma-system
spec:
sync:
strategy: secret
channel: stable
modules: []
`
return operation
}

func getClientForTests(t *testing.T) client.Client {
var cli client.Client
if len(os.Getenv("KUBECONFIG")) > 0 && strings.ToLower(os.Getenv("USE_KUBECONFIG")) == "true" {
config, err := clientcmd.BuildConfigFromFlags("", os.Getenv("KUBECONFIG"))
if err != nil {
t.Fatal(err.Error())
}

cli, err = client.New(config, client.Options{})
if err != nil {
t.Fatal(err.Error())
}
fmt.Println("using kubeconfig")
} else {
fmt.Println("using fake client")
cli = fake.NewClientBuilder().Build()
}
return cli
}

0 comments on commit 7b04586

Please sign in to comment.