|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + mariadbv1alpha1 "github.com/mariadb-operator/mariadb-operator/api/v1alpha1" |
| 7 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | +) |
| 9 | + |
| 10 | +func TestConfigMarshal(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + mariadb *mariadbv1alpha1.MariaDB |
| 14 | + env map[string]string |
| 15 | + wantConfig string |
| 16 | + wantErr bool |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "no env", |
| 20 | + mariadb: &mariadbv1alpha1.MariaDB{ |
| 21 | + ObjectMeta: v1.ObjectMeta{ |
| 22 | + Name: "mariadb-galera", |
| 23 | + Namespace: "default", |
| 24 | + }, |
| 25 | + Spec: mariadbv1alpha1.MariaDBSpec{ |
| 26 | + Galera: &mariadbv1alpha1.Galera{ |
| 27 | + SST: mariadbv1alpha1.SSTRsync, |
| 28 | + ReplicaThreads: 1, |
| 29 | + }, |
| 30 | + Replicas: 3, |
| 31 | + }, |
| 32 | + }, |
| 33 | + env: map[string]string{}, |
| 34 | + wantConfig: "", |
| 35 | + wantErr: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "rsync", |
| 39 | + mariadb: &mariadbv1alpha1.MariaDB{ |
| 40 | + ObjectMeta: v1.ObjectMeta{ |
| 41 | + Name: "mariadb-galera", |
| 42 | + Namespace: "default", |
| 43 | + }, |
| 44 | + Spec: mariadbv1alpha1.MariaDBSpec{ |
| 45 | + Galera: &mariadbv1alpha1.Galera{ |
| 46 | + SST: mariadbv1alpha1.SSTRsync, |
| 47 | + ReplicaThreads: 1, |
| 48 | + }, |
| 49 | + Replicas: 3, |
| 50 | + }, |
| 51 | + }, |
| 52 | + env: map[string]string{ |
| 53 | + "HOSTNAME": "mariadb-galera-0", |
| 54 | + "MARIADB_ROOT_PASSWORD": "foo", |
| 55 | + }, |
| 56 | + //nolint:lll |
| 57 | + wantConfig: `[mysqld] |
| 58 | +bind-address=0.0.0.0 |
| 59 | +default_storage_engine=InnoDB |
| 60 | +binlog_format=row |
| 61 | +innodb_autoinc_lock_mode=2 |
| 62 | +
|
| 63 | +# Cluster configuration |
| 64 | +wsrep_on=ON |
| 65 | +wsrep_provider=/usr/lib/galera/libgalera_smm.so |
| 66 | +wsrep_cluster_address="gcomm://mariadb-galera-0.mariadb-galera-internal.default.svc.cluster.local,mariadb-galera-1.mariadb-galera-internal.default.svc.cluster.local,mariadb-galera-2.mariadb-galera-internal.default.svc.cluster.local" |
| 67 | +wsrep_cluster_name=mariadb-operator |
| 68 | +wsrep_slave_threads=1 |
| 69 | +
|
| 70 | +# Node configuration |
| 71 | +wsrep_node_address="mariadb-galera-0.mariadb-galera-internal.default.svc.cluster.local" |
| 72 | +wsrep_node_name="mariadb-galera-0" |
| 73 | +wsrep_sst_method="rsync" |
| 74 | +`, |
| 75 | + wantErr: false, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "mariabackup", |
| 79 | + mariadb: &mariadbv1alpha1.MariaDB{ |
| 80 | + ObjectMeta: v1.ObjectMeta{ |
| 81 | + Name: "mariadb-galera", |
| 82 | + Namespace: "default", |
| 83 | + }, |
| 84 | + Spec: mariadbv1alpha1.MariaDBSpec{ |
| 85 | + Galera: &mariadbv1alpha1.Galera{ |
| 86 | + SST: mariadbv1alpha1.SSTMariaBackup, |
| 87 | + ReplicaThreads: 2, |
| 88 | + }, |
| 89 | + Replicas: 3, |
| 90 | + }, |
| 91 | + }, |
| 92 | + env: map[string]string{ |
| 93 | + "HOSTNAME": "mariadb-galera-1", |
| 94 | + "MARIADB_ROOT_PASSWORD": "foo", |
| 95 | + }, |
| 96 | + //nolint:lll |
| 97 | + wantConfig: `[mysqld] |
| 98 | +bind-address=0.0.0.0 |
| 99 | +default_storage_engine=InnoDB |
| 100 | +binlog_format=row |
| 101 | +innodb_autoinc_lock_mode=2 |
| 102 | +
|
| 103 | +# Cluster configuration |
| 104 | +wsrep_on=ON |
| 105 | +wsrep_provider=/usr/lib/galera/libgalera_smm.so |
| 106 | +wsrep_cluster_address="gcomm://mariadb-galera-0.mariadb-galera-internal.default.svc.cluster.local,mariadb-galera-1.mariadb-galera-internal.default.svc.cluster.local,mariadb-galera-2.mariadb-galera-internal.default.svc.cluster.local" |
| 107 | +wsrep_cluster_name=mariadb-operator |
| 108 | +wsrep_slave_threads=2 |
| 109 | +
|
| 110 | +# Node configuration |
| 111 | +wsrep_node_address="mariadb-galera-1.mariadb-galera-internal.default.svc.cluster.local" |
| 112 | +wsrep_node_name="mariadb-galera-1" |
| 113 | +wsrep_sst_method="mariabackup" |
| 114 | +wsrep_sst_auth="root:foo" |
| 115 | +`, |
| 116 | + wantErr: false, |
| 117 | + }, |
| 118 | + } |
| 119 | + for _, tt := range tests { |
| 120 | + t.Run(tt.name, func(t *testing.T) { |
| 121 | + config := NewConfigFile(tt.mariadb) |
| 122 | + for k, v := range tt.env { |
| 123 | + t.Setenv(k, v) |
| 124 | + } |
| 125 | + |
| 126 | + bytes, err := config.Marshal() |
| 127 | + if tt.wantErr && err == nil { |
| 128 | + t.Fatal("error expected, got nil") |
| 129 | + } |
| 130 | + if !tt.wantErr && err != nil { |
| 131 | + t.Fatalf("error unexpected, got %v", err) |
| 132 | + } |
| 133 | + if tt.wantConfig != string(bytes) { |
| 134 | + t.Fatalf("unexpected result:\nexpected:\n%s\ngot:\n%s\n", tt.wantConfig, string(bytes)) |
| 135 | + } |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
0 commit comments