|
| 1 | +package strvals |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "github.com/pkg/errors" |
| 7 | + "gotest.tools/assert" |
| 8 | + "log" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestSetStringFlag(t *testing.T) { |
| 13 | + rawConfig := getActualRawConfig() |
| 14 | + s := "deployments.dev.helm.values.containers[0]=" |
| 15 | + err := ParseIntoString(s, rawConfig) |
| 16 | + if err != nil { |
| 17 | + fmt.Println(errors.Wrap(err, "parsing --set-string flag")) |
| 18 | + log.Fatal(err) |
| 19 | + } |
| 20 | + b, err := json.Marshal(rawConfig) |
| 21 | + if err != nil { |
| 22 | + fmt.Println(err) |
| 23 | + } |
| 24 | + fmt.Println("output : " + string(b)) |
| 25 | + assert.DeepEqual(t, rawConfig, getExpectedRawConfigForSetString()) |
| 26 | +} |
| 27 | + |
| 28 | +func TestSetFlag(t *testing.T) { |
| 29 | + s := "deployments.dev.helm.values.containers[1].image=" |
| 30 | + rawConfig := getActualRawConfig() |
| 31 | + err := ParseInto(s, rawConfig) |
| 32 | + if err != nil { |
| 33 | + fmt.Println(errors.Wrap(err, "parsing --set flag")) |
| 34 | + log.Fatal(err) |
| 35 | + } |
| 36 | + b, err := json.Marshal(rawConfig) |
| 37 | + if err != nil { |
| 38 | + fmt.Println(err) |
| 39 | + } |
| 40 | + fmt.Println("output : " + string(b)) |
| 41 | + assert.DeepEqual(t, rawConfig, getExpectedRawConfigForSet()) |
| 42 | +} |
| 43 | + |
| 44 | +func getExpectedRawConfigForSet() map[string]interface{} { |
| 45 | + jsonStr := "{\"deployments\":{\"dev\":{\"helm\":{\"values\":{\"containers\":[{\"image\":\"alpine\"},{\"image\":null}]}}}},\"name\":\"run-pipelines-demo\",\"pipelines\":{\"deploy\":\"create_deployments --all\",\"dev\":\"run_pipelines deploy --set deployments.dev.helm.values.containers[0].image=nginx --set-string deployments.dev.helm.values.containers[0].name=mynginx\"},\"version\":\"v2beta1\"}" |
| 46 | + rawConfig := map[string]interface{}{} |
| 47 | + err := json.Unmarshal([]byte(jsonStr), &rawConfig) |
| 48 | + if err != nil { |
| 49 | + fmt.Println(err) |
| 50 | + } |
| 51 | + return rawConfig |
| 52 | +} |
| 53 | + |
| 54 | +func getExpectedRawConfigForSetString() map[string]interface{} { |
| 55 | + jsonStr := "{\"deployments\":{\"dev\":{\"helm\":{\"values\":{\"containers\":[null,{\"image\":\"ns\"}]}}}},\"name\":\"run-pipelines-demo\",\"pipelines\":{\"deploy\":\"create_deployments --all\",\"dev\":\"run_pipelines deploy --set deployments.dev.helm.values.containers[0].image=nginx --set-string deployments.dev.helm.values.containers[0].name=mynginx\"},\"version\":\"v2beta1\"}" |
| 56 | + rawConfig := map[string]interface{}{} |
| 57 | + err := json.Unmarshal([]byte(jsonStr), &rawConfig) |
| 58 | + if err != nil { |
| 59 | + fmt.Println(err) |
| 60 | + } |
| 61 | + return rawConfig |
| 62 | +} |
| 63 | + |
| 64 | +func getActualRawConfig() map[string]interface{} { |
| 65 | + jsonStr := "{\n \"deployments\": {\n \"dev\": { \"helm\": { \"values\": { \"containers\": [{ \"image\": \"alpine\" },{ \"image\": \"ns\" }] } } }\n },\n \"name\": \"run-pipelines-demo\",\n \"pipelines\": {\n \"deploy\": \"create_deployments --all\",\n \"dev\": \"run_pipelines deploy --set deployments.dev.helm.values.containers[0].image=nginx --set-string deployments.dev.helm.values.containers[0].name=mynginx\"\n },\n \"version\": \"v2beta1\"\n}\n" |
| 66 | + rawConfig := map[string]interface{}{} |
| 67 | + err := json.Unmarshal([]byte(jsonStr), &rawConfig) |
| 68 | + if err != nil { |
| 69 | + fmt.Println(err) |
| 70 | + } |
| 71 | + return rawConfig |
| 72 | +} |
0 commit comments