Mohammed Naser | 65cda13 | 2024-05-02 14:34:08 -0400 | [diff] [blame] | 1 | package test |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/require" |
| 9 | "gopkg.in/yaml.v2" |
| 10 | ) |
| 11 | |
| 12 | type replicas struct { |
| 13 | Replicas int `yaml:"replicas"` |
| 14 | } |
| 15 | type loki struct { |
| 16 | Storage struct { |
| 17 | Type string `yaml:"type"` |
| 18 | } `yaml:"storage"` |
| 19 | } |
| 20 | |
| 21 | type values struct { |
| 22 | DeploymentMode string `yaml:"deploymentMode"` |
| 23 | Backend replicas `yaml:"backend"` |
| 24 | Compactor replicas `yaml:"compactor"` |
| 25 | Distributor replicas `yaml:"distributor"` |
| 26 | IndexGateway replicas `yaml:"indexGateway"` |
| 27 | Ingester replicas `yaml:"ingester"` |
| 28 | Querier replicas `yaml:"querier"` |
| 29 | QueryFrontend replicas `yaml:"queryFrontend"` |
| 30 | QueryScheduler replicas `yaml:"queryScheduler"` |
| 31 | Read replicas `yaml:"read"` |
| 32 | Ruler replicas `yaml:"ruler"` |
| 33 | SingleBinary replicas `yaml:"singleBinary"` |
| 34 | Write replicas `yaml:"write"` |
| 35 | |
| 36 | Loki loki `yaml:"loki"` |
| 37 | } |
| 38 | |
| 39 | func templateConfig(t *testing.T, vals values) error { |
| 40 | y, err := yaml.Marshal(&vals) |
| 41 | require.NoError(t, err) |
| 42 | require.Greater(t, len(y), 0) |
| 43 | |
| 44 | f, err := os.CreateTemp("", "values.yaml") |
| 45 | require.NoError(t, err) |
| 46 | |
| 47 | _, err = f.Write(y) |
| 48 | require.NoError(t, err) |
| 49 | |
| 50 | cmd := exec.Command("helm", "dependency", "build") |
| 51 | // Dependency build needs to be run from the parent directory where the chart is located. |
| 52 | cmd.Dir = "../" |
| 53 | var cmdOutput []byte |
| 54 | if cmdOutput, err = cmd.CombinedOutput(); err != nil { |
| 55 | t.Log("dependency build failed", "err", string(cmdOutput)) |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | cmd = exec.Command("helm", "template", "../", "--values", f.Name()) |
| 60 | if cmdOutput, err := cmd.CombinedOutput(); err != nil { |
| 61 | t.Log("template failed", "err", string(cmdOutput)) |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | return nil |
| 66 | } |
| 67 | |
| 68 | // E.Welch these tests fail because the templateConfig function above can't resolve the chart dependencies and I'm not sure how to fix this.... |
| 69 | |
| 70 | //func Test_InvalidConfigs(t *testing.T) { |
| 71 | // t.Run("running both single binary and scalable targets", func(t *testing.T) { |
| 72 | // vals := values{ |
| 73 | // SingleBinary: replicas{Replicas: 1}, |
| 74 | // Write: replicas{Replicas: 1}, |
| 75 | // Loki: loki{ |
| 76 | // Storage: struct { |
| 77 | // Type string `yaml:"type"` |
| 78 | // }{Type: "gcs"}, |
| 79 | // }, |
| 80 | // } |
| 81 | // require.Error(t, templateConfig(t, vals)) |
| 82 | // }) |
| 83 | // |
| 84 | // t.Run("running both single binary and distributed targets", func(t *testing.T) { |
| 85 | // vals := values{ |
| 86 | // SingleBinary: replicas{Replicas: 1}, |
| 87 | // Distributor: replicas{Replicas: 1}, |
| 88 | // Loki: loki{ |
| 89 | // Storage: struct { |
| 90 | // Type string `yaml:"type"` |
| 91 | // }{Type: "gcs"}, |
| 92 | // }, |
| 93 | // } |
| 94 | // require.Error(t, templateConfig(t, vals)) |
| 95 | // }) |
| 96 | // |
| 97 | // t.Run("running both scalable and distributed targets", func(t *testing.T) { |
| 98 | // vals := values{ |
| 99 | // Read: replicas{Replicas: 1}, |
| 100 | // Distributor: replicas{Replicas: 1}, |
| 101 | // Loki: loki{ |
| 102 | // Storage: struct { |
| 103 | // Type string `yaml:"type"` |
| 104 | // }{Type: "gcs"}, |
| 105 | // }, |
| 106 | // } |
| 107 | // require.Error(t, templateConfig(t, vals)) |
| 108 | // }) |
| 109 | // |
| 110 | // t.Run("running scalable with filesystem storage", func(t *testing.T) { |
| 111 | // vals := values{ |
| 112 | // Read: replicas{Replicas: 1}, |
| 113 | // Loki: loki{ |
| 114 | // Storage: struct { |
| 115 | // Type string `yaml:"type"` |
| 116 | // }{Type: "filesystem"}, |
| 117 | // }, |
| 118 | // } |
| 119 | // |
| 120 | // require.Error(t, templateConfig(t, vals)) |
| 121 | // }) |
| 122 | // |
| 123 | // t.Run("running distributed with filesystem storage", func(t *testing.T) { |
| 124 | // vals := values{ |
| 125 | // Distributor: replicas{Replicas: 1}, |
| 126 | // Loki: loki{ |
| 127 | // Storage: struct { |
| 128 | // Type string `yaml:"type"` |
| 129 | // }{Type: "filesystem"}, |
| 130 | // }, |
| 131 | // } |
| 132 | // |
| 133 | // require.Error(t, templateConfig(t, vals)) |
| 134 | // }) |
| 135 | //} |
| 136 | // |
| 137 | //func Test_ValidConfigs(t *testing.T) { |
| 138 | // t.Run("single binary", func(t *testing.T) { |
| 139 | // vals := values{ |
| 140 | // |
| 141 | // DeploymentMode: "SingleBinary", |
| 142 | // |
| 143 | // SingleBinary: replicas{Replicas: 1}, |
| 144 | // |
| 145 | // Backend: replicas{Replicas: 0}, |
| 146 | // Compactor: replicas{Replicas: 0}, |
| 147 | // Distributor: replicas{Replicas: 0}, |
| 148 | // IndexGateway: replicas{Replicas: 0}, |
| 149 | // Ingester: replicas{Replicas: 0}, |
| 150 | // Querier: replicas{Replicas: 0}, |
| 151 | // QueryFrontend: replicas{Replicas: 0}, |
| 152 | // QueryScheduler: replicas{Replicas: 0}, |
| 153 | // Read: replicas{Replicas: 0}, |
| 154 | // Ruler: replicas{Replicas: 0}, |
| 155 | // Write: replicas{Replicas: 0}, |
| 156 | // |
| 157 | // Loki: loki{ |
| 158 | // Storage: struct { |
| 159 | // Type string `yaml:"type"` |
| 160 | // }{Type: "filesystem"}, |
| 161 | // }, |
| 162 | // } |
| 163 | // require.NoError(t, templateConfig(t, vals)) |
| 164 | // }) |
| 165 | // |
| 166 | // t.Run("scalable", func(t *testing.T) { |
| 167 | // vals := values{ |
| 168 | // |
| 169 | // DeploymentMode: "SimpleScalable", |
| 170 | // |
| 171 | // Backend: replicas{Replicas: 1}, |
| 172 | // Read: replicas{Replicas: 1}, |
| 173 | // Write: replicas{Replicas: 1}, |
| 174 | // |
| 175 | // Compactor: replicas{Replicas: 0}, |
| 176 | // Distributor: replicas{Replicas: 0}, |
| 177 | // IndexGateway: replicas{Replicas: 0}, |
| 178 | // Ingester: replicas{Replicas: 0}, |
| 179 | // Querier: replicas{Replicas: 0}, |
| 180 | // QueryFrontend: replicas{Replicas: 0}, |
| 181 | // QueryScheduler: replicas{Replicas: 0}, |
| 182 | // Ruler: replicas{Replicas: 0}, |
| 183 | // SingleBinary: replicas{Replicas: 0}, |
| 184 | // |
| 185 | // Loki: loki{ |
| 186 | // Storage: struct { |
| 187 | // Type string `yaml:"type"` |
| 188 | // }{Type: "gcs"}, |
| 189 | // }, |
| 190 | // } |
| 191 | // require.NoError(t, templateConfig(t, vals)) |
| 192 | // }) |
| 193 | // |
| 194 | // t.Run("distributed", func(t *testing.T) { |
| 195 | // vals := values{ |
| 196 | // DeploymentMode: "Distributed", |
| 197 | // |
| 198 | // Compactor: replicas{Replicas: 1}, |
| 199 | // Distributor: replicas{Replicas: 1}, |
| 200 | // IndexGateway: replicas{Replicas: 1}, |
| 201 | // Ingester: replicas{Replicas: 1}, |
| 202 | // Querier: replicas{Replicas: 1}, |
| 203 | // QueryFrontend: replicas{Replicas: 1}, |
| 204 | // QueryScheduler: replicas{Replicas: 1}, |
| 205 | // Ruler: replicas{Replicas: 1}, |
| 206 | // |
| 207 | // Backend: replicas{Replicas: 0}, |
| 208 | // Read: replicas{Replicas: 0}, |
| 209 | // SingleBinary: replicas{Replicas: 0}, |
| 210 | // Write: replicas{Replicas: 0}, |
| 211 | // |
| 212 | // Loki: loki{ |
| 213 | // Storage: struct { |
| 214 | // Type string `yaml:"type"` |
| 215 | // }{Type: "gcs"}, |
| 216 | // }, |
| 217 | // } |
| 218 | // require.NoError(t, templateConfig(t, vals)) |
| 219 | // }) |
| 220 | //} |