|
15 | 15 | package cmd
|
16 | 16 |
|
17 | 17 | import (
|
| 18 | + "bytes" |
18 | 19 | "errors"
|
| 20 | + "fmt" |
19 | 21 | "reflect"
|
20 | 22 | "testing"
|
| 23 | + |
| 24 | + "github.com/sorintlab/stolon/internal/cluster" |
| 25 | + "github.com/sorintlab/stolon/internal/common" |
21 | 26 | )
|
22 | 27 |
|
23 | 28 | var curUID int
|
@@ -73,3 +78,158 @@ func TestParseSynchronousStandbyNames(t *testing.T) {
|
73 | 78 | }
|
74 | 79 | }
|
75 | 80 | }
|
| 81 | + |
| 82 | +func TestGenerateHBA(t *testing.T) { |
| 83 | + // minimal clusterdata with only the fields used by generateHBA |
| 84 | + cd := &cluster.ClusterData{ |
| 85 | + Cluster: &cluster.Cluster{ |
| 86 | + Spec: &cluster.ClusterSpec{}, |
| 87 | + Status: cluster.ClusterStatus{}, |
| 88 | + }, |
| 89 | + Keepers: cluster.Keepers{}, |
| 90 | + DBs: cluster.DBs{ |
| 91 | + "db1": &cluster.DB{ |
| 92 | + UID: "db1", |
| 93 | + Spec: &cluster.DBSpec{ |
| 94 | + Role: common.RoleMaster, |
| 95 | + }, |
| 96 | + Status: cluster.DBStatus{ |
| 97 | + ListenAddress: "192.168.0.1", |
| 98 | + }, |
| 99 | + }, |
| 100 | + "db2": &cluster.DB{ |
| 101 | + UID: "db2", |
| 102 | + Spec: &cluster.DBSpec{ |
| 103 | + Role: common.RoleStandby, |
| 104 | + FollowConfig: &cluster.FollowConfig{ |
| 105 | + Type: cluster.FollowTypeInternal, |
| 106 | + DBUID: "db1", |
| 107 | + }, |
| 108 | + }, |
| 109 | + Status: cluster.DBStatus{ |
| 110 | + ListenAddress: "192.168.0.2", |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + Proxy: &cluster.Proxy{}, |
| 115 | + } |
| 116 | + |
| 117 | + tests := []struct { |
| 118 | + DefaultSUReplAccessMode cluster.SUReplAccessMode |
| 119 | + dbUID string |
| 120 | + pgHBA []string |
| 121 | + out []string |
| 122 | + }{ |
| 123 | + { |
| 124 | + DefaultSUReplAccessMode: cluster.SUReplAccessAll, |
| 125 | + dbUID: "db1", |
| 126 | + out: []string{ |
| 127 | + "local postgres superuser md5", |
| 128 | + "local replication repluser md5", |
| 129 | + "host all superuser 0.0.0.0/0 md5", |
| 130 | + "host all superuser ::0/0 md5", |
| 131 | + "host replication repluser 0.0.0.0/0 md5", |
| 132 | + "host replication repluser ::0/0 md5", |
| 133 | + "host all all 0.0.0.0/0 md5", |
| 134 | + "host all all ::0/0 md5", |
| 135 | + }, |
| 136 | + }, |
| 137 | + { |
| 138 | + DefaultSUReplAccessMode: cluster.SUReplAccessAll, |
| 139 | + dbUID: "db2", |
| 140 | + out: []string{ |
| 141 | + "local postgres superuser md5", |
| 142 | + "local replication repluser md5", |
| 143 | + "host all superuser 0.0.0.0/0 md5", |
| 144 | + "host all superuser ::0/0 md5", |
| 145 | + "host replication repluser 0.0.0.0/0 md5", |
| 146 | + "host replication repluser ::0/0 md5", |
| 147 | + "host all all 0.0.0.0/0 md5", |
| 148 | + "host all all ::0/0 md5", |
| 149 | + }, |
| 150 | + }, |
| 151 | + { |
| 152 | + DefaultSUReplAccessMode: cluster.SUReplAccessAll, |
| 153 | + dbUID: "db1", |
| 154 | + pgHBA: []string{ |
| 155 | + "host all all 192.168.0.0/24 md5", |
| 156 | + }, |
| 157 | + out: []string{ |
| 158 | + "local postgres superuser md5", |
| 159 | + "local replication repluser md5", |
| 160 | + "host all superuser 0.0.0.0/0 md5", |
| 161 | + "host all superuser ::0/0 md5", |
| 162 | + "host replication repluser 0.0.0.0/0 md5", |
| 163 | + "host replication repluser ::0/0 md5", |
| 164 | + "host all all 192.168.0.0/24 md5", |
| 165 | + }, |
| 166 | + }, |
| 167 | + { |
| 168 | + DefaultSUReplAccessMode: cluster.SUReplAccessAll, |
| 169 | + dbUID: "db2", |
| 170 | + pgHBA: []string{ |
| 171 | + "host all all 192.168.0.0/24 md5", |
| 172 | + }, |
| 173 | + out: []string{ |
| 174 | + "local postgres superuser md5", |
| 175 | + "local replication repluser md5", |
| 176 | + "host all superuser 0.0.0.0/0 md5", |
| 177 | + "host all superuser ::0/0 md5", |
| 178 | + "host replication repluser 0.0.0.0/0 md5", |
| 179 | + "host replication repluser ::0/0 md5", |
| 180 | + "host all all 192.168.0.0/24 md5", |
| 181 | + }, |
| 182 | + }, |
| 183 | + { |
| 184 | + DefaultSUReplAccessMode: cluster.SUReplAccessStrict, |
| 185 | + dbUID: "db1", |
| 186 | + out: []string{ |
| 187 | + "local postgres superuser md5", |
| 188 | + "local replication repluser md5", |
| 189 | + "host all superuser 192.168.0.2/32 md5", |
| 190 | + "host replication repluser 192.168.0.2/32 md5", |
| 191 | + "host all all 0.0.0.0/0 md5", |
| 192 | + "host all all ::0/0 md5", |
| 193 | + }, |
| 194 | + }, |
| 195 | + { |
| 196 | + DefaultSUReplAccessMode: cluster.SUReplAccessStrict, |
| 197 | + dbUID: "db2", |
| 198 | + out: []string{ |
| 199 | + "local postgres superuser md5", |
| 200 | + "local replication repluser md5", |
| 201 | + "host all all 0.0.0.0/0 md5", |
| 202 | + "host all all ::0/0 md5", |
| 203 | + }, |
| 204 | + }, |
| 205 | + } |
| 206 | + |
| 207 | + for i, tt := range tests { |
| 208 | + p := &PostgresKeeper{ |
| 209 | + pgSUAuthMethod: "md5", |
| 210 | + pgSUUsername: "superuser", |
| 211 | + pgReplAuthMethod: "md5", |
| 212 | + pgReplUsername: "repluser", |
| 213 | + } |
| 214 | + |
| 215 | + cd.Cluster.Spec.DefaultSUReplAccessMode = &tt.DefaultSUReplAccessMode |
| 216 | + |
| 217 | + db := cd.DBs[tt.dbUID] |
| 218 | + db.Spec.PGHBA = tt.pgHBA |
| 219 | + |
| 220 | + out := p.generateHBA(cd, db) |
| 221 | + |
| 222 | + if !reflect.DeepEqual(out, tt.out) { |
| 223 | + var b bytes.Buffer |
| 224 | + b.WriteString(fmt.Sprintf("#%d: wrong output: got:\n", i)) |
| 225 | + for _, o := range out { |
| 226 | + b.WriteString(fmt.Sprintf("%s\n", o)) |
| 227 | + } |
| 228 | + b.WriteString(fmt.Sprintf("\nwant:\n")) |
| 229 | + for _, o := range tt.out { |
| 230 | + b.WriteString(fmt.Sprintf("%s\n", o)) |
| 231 | + } |
| 232 | + t.Errorf(b.String()) |
| 233 | + } |
| 234 | + } |
| 235 | +} |
0 commit comments