Skip to content

Commit

Permalink
Force payer service to use 1 replica (#19)
Browse files Browse the repository at this point in the history
Force the helm charts to only ever run 1 replica.

Some drive-by test cleanup.
  • Loading branch information
mkysel authored Dec 19, 2024
1 parent bab6b91 commit 9a8b93e
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 85 deletions.
2 changes: 1 addition & 1 deletion helm/xmtp-payer/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ type: application
maintainers:
- name: XMTPD
version: 0.1.0
appVersion: "v0.1.1"
appVersion: "v0.1.3"
7 changes: 6 additions & 1 deletion helm/xmtp-payer/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
labels:
{{- include "xmtp-payer.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
replicas: 1
selector:
matchLabels:
{{- include "xmtp-payer.selectorLabels" . | nindent 6 }}
Expand Down Expand Up @@ -66,3 +66,8 @@ spec:
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 0
maxUnavailable: 1
7 changes: 5 additions & 2 deletions helm/xmtp-payer/values.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Default values for xmtpd.
# Default values for Payer.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1

# we do not support more than 1 process with the same key
# https://github.com/xmtp/xmtpd/issues/334
# replicaCount: 1

# This sets the container image more information can be found here: https://kubernetes.io/docs/concepts/containers/images/
image:
Expand Down
2 changes: 1 addition & 1 deletion helm/xmtpd/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ type: application
maintainers:
- name: XMTPD
version: 0.1.0
appVersion: "v0.1.1"
appVersion: "v0.1.3"
99 changes: 99 additions & 0 deletions test/testlib/objects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package testlib

import (
"fmt"
"github.com/gruntwork-io/terratest/modules/helm"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
"strings"
"testing"
)

func ExtractIngressE(t *testing.T, output string) *netv1.Ingress {
parts := strings.Split(output, "---")
for _, part := range parts {
if len(part) == 0 {
continue
}

if !strings.Contains(part, "kind: Ingress") {
continue
}

var object netv1.Ingress
helm.UnmarshalK8SYaml(t, part, &object)

return &object
}

return nil
}

func ExtractIngress(t *testing.T, output string) *netv1.Ingress {
ingress := ExtractIngressE(t, output)

if ingress == nil {
t.Fatalf("Could not extract ingress from template")
}

return ingress
}

func ExtractNamedSecretE(t *testing.T, output string, secretName string) *corev1.Secret {
parts := strings.Split(output, "---")
for _, part := range parts {
if len(part) == 0 {
continue
}

if !strings.Contains(part, "kind: Secret") {
continue
}

if !strings.Contains(part, fmt.Sprintf("name: %s", secretName)) {
continue
}

var object corev1.Secret
helm.UnmarshalK8SYaml(t, part, &object)

return &object
}

return nil
}

func ExtractDeploymentE(t *testing.T, output string, deploymentName string) *appsv1.Deployment {
parts := strings.Split(output, "---")
for _, part := range parts {
if len(part) == 0 {
continue
}

if !strings.Contains(part, "kind: Deployment") {
continue
}

if !strings.Contains(part, fmt.Sprintf("name: %s", deploymentName)) {
continue
}

var object appsv1.Deployment
helm.UnmarshalK8SYaml(t, part, &object)

return &object
}

return nil
}

func ExtractDeployment(t *testing.T, output string, deploymentName string) *appsv1.Deployment {
deployment := ExtractDeploymentE(t, output, deploymentName)

if deployment == nil {
t.Fatalf("Could not extract deployment from template")
}

return deployment
}
1 change: 0 additions & 1 deletion test/testlib/xmtp_xmtpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ func getLastSection(envKey string) string {
// 3. Default values.
func GetDefaultSecrets(t *testing.T) map[string]string {
defaultSecrets := map[string]string{
"env.secret.XMTPD_DB_WRITER_CONNECTION_STRING": "<replace-me>",
"env.secret.XMTPD_SIGNER_PRIVATE_KEY": "<replace-me>",
"env.secret.XMTPD_PAYER_PRIVATE_KEY": "<replace-me>",
"env.secret.XMTPD_CONTRACTS_RPC_URL": "https://rpc-testnet-staging-88dqtxdinc.t.conduit.xyz/",
Expand Down
7 changes: 0 additions & 7 deletions test/xmtp-helm/base_payer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@ func TestKubernetesBasicPayerInstall(t *testing.T) {
SetValues: map[string]string{},
}

// technically the payer does NOT require a DB
// but XMTPD 0.1.1 has some incorrect defaults
// which will prevent the start of the service without DB connectivity
defer testlib.Teardown(testlib.TEARDOWN_DATABASE)
_, _, db := testlib.StartDB(t, &options, namespace)

secrets := testlib.GetDefaultSecrets(t)
secrets["env.secret.XMTPD_DB_WRITER_CONNECTION_STRING"] = db.ConnString

options = helm.Options{
SetValues: secrets,
Expand Down
32 changes: 24 additions & 8 deletions test/xmtp-helm/template_payer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/gruntwork-io/terratest/modules/helm"
"github.com/stretchr/testify/assert"
"github.com/xmtp/xmtpd-infrastructure/v1/test/testlib"
v1 "k8s.io/api/networking/v1"
netv1 "k8s.io/api/networking/v1"
"testing"
)

Expand All @@ -14,9 +14,8 @@ func TestPayerEmpty(t *testing.T) {
}
output := helm.RenderTemplate(t, options, testlib.XMTP_PAYER_HELM_CHART_PATH, "release-name", []string{})

ingress := extractIngressE(t, output)
ingress := testlib.ExtractIngressE(t, output)
assert.Nil(t, ingress)

}

func TestPayerEnableIngress(t *testing.T) {
Expand All @@ -29,7 +28,7 @@ func TestPayerEnableIngress(t *testing.T) {

output := helm.RenderTemplate(t, options, testlib.XMTP_PAYER_HELM_CHART_PATH, "release-name", []string{})

ingress := extractIngress(t, output)
ingress := testlib.ExtractIngress(t, output)
assert.Contains(t, ingress.Annotations, "kubernetes.io/ingress.class")
assert.Equal(t, "nginx", *ingress.Spec.IngressClassName)
}
Expand All @@ -45,7 +44,7 @@ func TestPayerIngressTLSNoSecret(t *testing.T) {

output := helm.RenderTemplate(t, options, testlib.XMTP_PAYER_HELM_CHART_PATH, "release-name", []string{})

ingress := extractIngress(t, output)
ingress := testlib.ExtractIngress(t, output)
assert.Contains(t, ingress.Annotations, "cert-manager.io/cluster-issuer")
assert.Equal(t, "cert-manager", ingress.Annotations["cert-manager.io/cluster-issuer"])
assert.Empty(t, ingress.Spec.TLS)
Expand All @@ -64,16 +63,33 @@ func TestPayerIngressTLSSecretNoCreate(t *testing.T) {

output := helm.RenderTemplate(t, options, testlib.XMTP_PAYER_HELM_CHART_PATH, "release-name", []string{})

ingress := extractIngress(t, output)
ingress := testlib.ExtractIngress(t, output)
assert.Contains(t, ingress.Annotations, "cert-manager.io/cluster-issuer")
assert.Equal(t, "cert-manager", ingress.Annotations["cert-manager.io/cluster-issuer"])

expectedTLS := v1.IngressTLS{
expectedTLS := netv1.IngressTLS{
Hosts: []string{"my-host"},
SecretName: "my-secret",
}
assert.Contains(t, ingress.Spec.TLS, expectedTLS)

secret := extractNamedSecretE(t, output, "my-secret")
secret := testlib.ExtractNamedSecretE(t, output, "my-secret")
assert.Nil(t, secret)
}

func TestPayerMax1Replica(t *testing.T) {
// once we have fixed https://github.com/xmtp/xmtpd/issues/334, this should be adjusted

options := &helm.Options{
SetValues: map[string]string{
"replicas": "7",
},
}
output := helm.RenderTemplate(t, options, testlib.XMTP_PAYER_HELM_CHART_PATH, "release-name", []string{})

deployment := testlib.ExtractDeployment(t, output, "release-name-xmtp-payer")

assert.EqualValues(t, 1, *deployment.Spec.Replicas)
assert.NotNil(t, deployment.Spec.Strategy.RollingUpdate)

}
71 changes: 7 additions & 64 deletions test/xmtp-helm/template_xmtpd_test.go
Original file line number Diff line number Diff line change
@@ -1,77 +1,20 @@
package xmtp_helm

import (
"fmt"
"github.com/gruntwork-io/terratest/modules/helm"
"github.com/stretchr/testify/assert"
"github.com/xmtp/xmtpd-infrastructure/v1/test/testlib"
v2 "k8s.io/api/core/v1"
v1 "k8s.io/api/networking/v1"
"strings"
netv1 "k8s.io/api/networking/v1"
"testing"
)

func extractIngressE(t *testing.T, output string) *v1.Ingress {
parts := strings.Split(output, "---")
for _, part := range parts {
if len(part) == 0 {
continue
}

if !strings.Contains(part, "kind: Ingress") {
continue
}

var object v1.Ingress
helm.UnmarshalK8SYaml(t, part, &object)

return &object
}

return nil
}

func extractIngress(t *testing.T, output string) *v1.Ingress {
ingress := extractIngressE(t, output)

if ingress == nil {
t.Fatalf("Could not extract ingress from template")
}

return ingress
}

func extractNamedSecretE(t *testing.T, output string, secretName string) *v2.Secret {
parts := strings.Split(output, "---")
for _, part := range parts {
if len(part) == 0 {
continue
}

if !strings.Contains(part, "kind: Secret") {
continue
}

if !strings.Contains(part, fmt.Sprintf("name: %s", secretName)) {
continue
}

var object v2.Secret
helm.UnmarshalK8SYaml(t, part, &object)

return &object
}

return nil
}

func TestXmtpdEmpty(t *testing.T) {
options := &helm.Options{
SetValues: map[string]string{},
}
output := helm.RenderTemplate(t, options, testlib.XMTPD_HELM_CHART_PATH, "release-name", []string{})

ingress := extractIngressE(t, output)
ingress := testlib.ExtractIngressE(t, output)
assert.Nil(t, ingress)

}
Expand All @@ -86,7 +29,7 @@ func TestXmtpdEnableIngress(t *testing.T) {

output := helm.RenderTemplate(t, options, testlib.XMTPD_HELM_CHART_PATH, "release-name", []string{})

ingress := extractIngress(t, output)
ingress := testlib.ExtractIngress(t, output)
assert.Contains(t, ingress.Annotations, "kubernetes.io/ingress.class")
assert.Equal(t, "nginx", *ingress.Spec.IngressClassName)
}
Expand All @@ -102,7 +45,7 @@ func TestXmtpdIngressTLSNoSecret(t *testing.T) {

output := helm.RenderTemplate(t, options, testlib.XMTPD_HELM_CHART_PATH, "release-name", []string{})

ingress := extractIngress(t, output)
ingress := testlib.ExtractIngress(t, output)
assert.Contains(t, ingress.Annotations, "cert-manager.io/cluster-issuer")
assert.Equal(t, "cert-manager", ingress.Annotations["cert-manager.io/cluster-issuer"])
assert.Empty(t, ingress.Spec.TLS)
Expand All @@ -121,16 +64,16 @@ func TestXmtpdIngressTLSSecretNoCreate(t *testing.T) {

output := helm.RenderTemplate(t, options, testlib.XMTPD_HELM_CHART_PATH, "release-name", []string{})

ingress := extractIngress(t, output)
ingress := testlib.ExtractIngress(t, output)
assert.Contains(t, ingress.Annotations, "cert-manager.io/cluster-issuer")
assert.Equal(t, "cert-manager", ingress.Annotations["cert-manager.io/cluster-issuer"])

expectedTLS := v1.IngressTLS{
expectedTLS := netv1.IngressTLS{
Hosts: []string{"my-host"},
SecretName: "my-secret",
}
assert.Contains(t, ingress.Spec.TLS, expectedTLS)

secret := extractNamedSecretE(t, output, "my-secret")
secret := testlib.ExtractNamedSecretE(t, output, "my-secret")
assert.Nil(t, secret)
}

0 comments on commit 9a8b93e

Please sign in to comment.