Skip to content

Commit

Permalink
feat: add tests for autoscaler integration
Browse files Browse the repository at this point in the history
  • Loading branch information
rriski committed Oct 30, 2024
1 parent 291b2f8 commit de975aa
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apiVersion: aiven.io/v1alpha1
kind: ServiceIntegration
metadata:
name: my-service-integration
spec:
authSecretRef:
name: aiven-token
key: token

project: aiven-project-name
integrationType: autoscaler
sourceServiceName: my-pg
# Look up autoscaler integration endpoint ID via Console
destinationEndpointId: my-destination-endpoint-id

---

apiVersion: aiven.io/v1alpha1
kind: PostgreSQL
metadata:
name: my-pg
spec:
authSecretRef:
name: aiven-token
key: token

project: aiven-project-name
cloudName: google-europe-west1
plan: startup-4
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: aiven.io/v1alpha1
kind: ServiceIntegrationEndpoint
metadata:
name: my-service-integration-endpoint
spec:
authSecretRef:
name: aiven-token
key: token

project: aiven-project-name
endpointName: my-autoscaler
endpointType: autoscaler

autoscaler:
autoscaling:
- type: autoscale_disk
cap_gb: 100
57 changes: 55 additions & 2 deletions docs/docs/api-reference/serviceintegration.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,59 @@ title: "ServiceIntegration"

## Usage examples

??? example "autoscaler"
```yaml
apiVersion: aiven.io/v1alpha1
kind: ServiceIntegration
metadata:
name: my-service-integration
spec:
authSecretRef:
name: aiven-token
key: token

project: aiven-project-name
integrationType: autoscaler
sourceServiceName: my-pg
# Specify autoscaler integration endpoint ID
destinationEndpointId: e8417f8c-513f-487a-9213-5a903a8e62d9

---

apiVersion: aiven.io/v1alpha1
kind: ServiceIntegrationEndpoint
metadata:
name: my-service-integration-endpoint
spec:
authSecretRef:
name: aiven-token
key: token

project: aiven-project-name
endpointName: my-autoscaler
endpointType: autoscaler

autoscaler:
autoscaling:
- type: autoscale_disk
cap_gb: 100

---

apiVersion: aiven.io/v1alpha1
kind: PostgreSQL
metadata:
name: my-pg
spec:
authSecretRef:
name: aiven-token
key: token

project: aiven-project-name
cloudName: google-europe-west1
plan: startup-4
```

??? example "clickhouse_postgresql"
```yaml
apiVersion: aiven.io/v1alpha1
Expand Down Expand Up @@ -225,8 +278,8 @@ kubectl get serviceintegrations my-service-integration

The output is similar to the following:
```shell
Name Project Type Source Service Name Destination Service Name
my-service-integration aiven-project-name clickhouse_postgresql my-pg my-clickhouse
Name Project Type Source Service Name Destination Endpoint ID
my-service-integration aiven-project-name autoscaler my-pg e8417f8c-513f-487a-9213-5a903a8e62d9
```

## ServiceIntegration {: #ServiceIntegration }
Expand Down
25 changes: 23 additions & 2 deletions docs/docs/api-reference/serviceintegrationendpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ title: "ServiceIntegrationEndpoint"

## Usage examples

??? example "autoscaler"
```yaml
apiVersion: aiven.io/v1alpha1
kind: ServiceIntegrationEndpoint
metadata:
name: my-service-integration-endpoint
spec:
authSecretRef:
name: aiven-token
key: token

project: aiven-project-name
endpointName: my-autoscaler
endpointType: autoscaler

autoscaler:
autoscaling:
- type: autoscale_disk
cap_gb: 100
```

??? example "external_postgresql"
```yaml
apiVersion: aiven.io/v1alpha1
Expand Down Expand Up @@ -66,8 +87,8 @@ kubectl get serviceintegrationendpoints my-service-integration-endpoint

The output is similar to the following:
```shell
Name Project Endpoint Name Endpoint Type ID
my-service-integration-endpoint aiven-project-name my-external-postgresql external_postgresql <id>
Name Project Endpoint Name Endpoint Type ID
my-service-integration-endpoint aiven-project-name my-autoscaler autoscaler <id>
```

## ServiceIntegrationEndpoint {: #ServiceIntegrationEndpoint }
Expand Down
58 changes: 58 additions & 0 deletions tests/serviceintegration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,64 @@ func TestServiceIntegrationKafkaConnect(t *testing.T) {
assert.Equal(t, "__connect_offsets", *si.Spec.KafkaConnectUserConfig.KafkaConnect.OffsetStorageTopic)
}

func TestServiceIntegrationAutoscaler(t *testing.T) {
t.Parallel()
defer recoverPanic(t)

endpointID := os.Getenv("AUTOSCALER_ENDPOINT_ID")
if endpointID == "" {
t.Skip("Provide AUTOSCALER_ENDPOINT_ID for this test")
}

// GIVEN
ctx, cancel := testCtx()
defer cancel()

pgName := randName("postgresql")
siName := randName("autoscaler")

yml, err := loadExampleYaml("serviceintegration.autoscaler.yaml", map[string]string{
"aiven-project-name": cfg.Project,
"google-europe-west1": cfg.PrimaryCloudName,
"my-pg": pgName,
"my-service-integration": siName,
"my-destination-endpoint-id": endpointID,
})
require.NoError(t, err)
s := NewSession(ctx, k8sClient, cfg.Project)

// Cleans test afterward
defer s.Destroy(t)

// WHEN
// Applies given manifest
require.NoError(t, s.Apply(yml))

// Waits kube objects
pg := new(v1alpha1.PostgreSQL)
require.NoError(t, s.GetRunning(pg, pgName))

si := new(v1alpha1.ServiceIntegration)
require.NoError(t, s.GetRunning(si, siName))

// THEN
// Validates PostgreSQL
pgAvn, err := avnGen.ServiceGet(ctx, cfg.Project, pgName)
require.NoError(t, err)
assert.Equal(t, pgAvn.ServiceName, pg.GetName())
assert.Contains(t, serviceRunningStatesAiven, pgAvn.State)

// Validates ServiceIntegration
siAvn, err := avnGen.ServiceIntegrationGet(ctx, cfg.Project, si.Status.ID)
require.NoError(t, err)
assert.EqualValues(t, "autoscaler", siAvn.IntegrationType)
assert.EqualValues(t, siAvn.IntegrationType, si.Spec.IntegrationType)
assert.Equal(t, pgName, siAvn.SourceService)
assert.Equal(t, endpointID, *siAvn.DestEndpointId)
assert.True(t, siAvn.Active)
assert.True(t, siAvn.Enabled)
}

// todo: refactor when ServiceIntegrationEndpoint released
func TestServiceIntegrationDatadog(t *testing.T) {
t.Parallel()
Expand Down
42 changes: 42 additions & 0 deletions tests/serviceintegrationendpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,45 @@ func TestServiceIntegrationEndpoint(t *testing.T) {
assert.EqualValues(t, endpointRegistryAvn.UserConfig["basic_auth_username"], *endpointRegistry.Spec.ExternalSchemaRegistry.BasicAuthUsername)
assert.EqualValues(t, endpointRegistryAvn.UserConfig["basic_auth_password"], *endpointRegistry.Spec.ExternalSchemaRegistry.BasicAuthPassword)
}

func TestServiceIntegrationEndpointAutoscaler(t *testing.T) {
t.Parallel()
defer recoverPanic(t)

// GIVEN
ctx, cancel := testCtx()
defer cancel()

endpointName := randName("autoscaler")

yml, err := loadExampleYaml("serviceintegrationendpoint.autoscaler.yaml", map[string]string{
"aiven-project-name": cfg.Project,
"my-service-integration-endpoint": endpointName,
})
require.NoError(t, err)
s := NewSession(ctx, k8sClient, cfg.Project)

// Cleans test afterward
defer s.Destroy(t)

// WHEN
// Applies given manifest
require.NoError(t, s.Apply(yml))

// THEN

// Validates autoscaler ServiceIntegrationEndpoint
endpointAutoscaler := new(v1alpha1.ServiceIntegrationEndpoint)
require.NoError(t, s.GetRunning(endpointAutoscaler, endpointName))

endpointAvn, err := avnGen.ServiceIntegrationEndpointGet(ctx, cfg.Project, endpointAutoscaler.Status.ID, service.ServiceIntegrationEndpointGetIncludeSecrets(true))
require.NoError(t, err)

assert.EqualValues(t, "autoscaler", endpointAvn.EndpointType)
assert.EqualValues(t, endpointAvn.EndpointType, endpointAutoscaler.Spec.EndpointType)
// TODO: remove type assertions once generated client has full user config typing
assert.EqualValues(t, "autoscale_disk", endpointAutoscaler.Spec.Autoscaler.Autoscaling[0].Type)
assert.EqualValues(t, "autoscale_disk", endpointAvn.UserConfig["autoscaling"].([]interface{})[0].(map[string]interface{})["type"])
assert.EqualValues(t, 100, endpointAutoscaler.Spec.Autoscaler.Autoscaling[0].CapGb)
assert.EqualValues(t, 100, endpointAvn.UserConfig["autoscaling"].([]interface{})[0].(map[string]interface{})["cap_gb"])
}

0 comments on commit de975aa

Please sign in to comment.