|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/tar" |
| 5 | + "bytes" |
| 6 | + "encoding/json" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestRemapEnvSlice(t *testing.T) { |
| 11 | + env := []any{"A=1", "PORT=5000", 123} |
| 12 | + |
| 13 | + got := remapEnvSlice(env, "5000", "9999") |
| 14 | + expected := []any{"A=1", "PORT=9999", 123} |
| 15 | + |
| 16 | + if len(got) != len(expected) { |
| 17 | + t.Errorf("Expected env length %d, got %d", len(expected), len(got)) |
| 18 | + } |
| 19 | + for i := range expected { |
| 20 | + if got[i] != expected[i] { |
| 21 | + t.Errorf("Expected env[%d]=%v, got %v", i, expected[i], got[i]) |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestRemapEnvRecursive(t *testing.T) { |
| 27 | + obj := map[string]any{ |
| 28 | + "env": []any{"PORT=5000", "FOO=bar"}, |
| 29 | + "process": map[string]any{ |
| 30 | + "env": []any{"A=1", "PORT=5000"}, |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + remapEnvRecursive(obj, "5000", "9999") |
| 35 | + |
| 36 | + rootEnv := obj["env"].([]any) |
| 37 | + if rootEnv[0] != "PORT=9999" { |
| 38 | + t.Errorf("Expected root env PORT to be remapped, got %v", rootEnv[0]) |
| 39 | + } |
| 40 | + |
| 41 | + nestedEnv := obj["process"].(map[string]any)["env"].([]any) |
| 42 | + if nestedEnv[1] != "PORT=9999" { |
| 43 | + t.Errorf("Expected nested env PORT to be remapped, got %v", nestedEnv[1]) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestRemapPortMappings(t *testing.T) { |
| 48 | + obj := map[string]any{ |
| 49 | + "newPortMappings": []any{ |
| 50 | + map[string]any{"container_port": float64(5000)}, |
| 51 | + map[string]any{"container_port": float64(6000)}, |
| 52 | + }, |
| 53 | + "nested": map[string]any{ |
| 54 | + "portMappings": []any{ |
| 55 | + map[string]any{"container_port": float64(5000)}, |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + remapPortMappings(obj, "5000", "9999") |
| 61 | + |
| 62 | + top := obj["newPortMappings"].([]any) |
| 63 | + if top[0].(map[string]any)["container_port"].(float64) != 9999 { |
| 64 | + t.Errorf("Expected top-level container_port to be 9999") |
| 65 | + } |
| 66 | + if top[1].(map[string]any)["container_port"].(float64) != 6000 { |
| 67 | + t.Errorf("Expected non-target top-level container_port to stay 6000") |
| 68 | + } |
| 69 | + |
| 70 | + nested := obj["nested"].(map[string]any)["portMappings"].([]any) |
| 71 | + if nested[0].(map[string]any)["container_port"].(float64) != 9999 { |
| 72 | + t.Errorf("Expected nested container_port to be 9999") |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestRemapConfigDump(t *testing.T) { |
| 77 | + input := []byte(`{ |
| 78 | + "newPortMappings":[{"container_port":5000},{"container_port":7000}], |
| 79 | + "env":["FOO=bar","PORT=5000"], |
| 80 | + "nested":{"env":["PORT=5000","X=1"]} |
| 81 | + }`) |
| 82 | + hdr := &tar.Header{Name: "config.dump", Size: int64(len(input))} |
| 83 | + |
| 84 | + newHdr, out, err := remapConfigDump(hdr, bytes.NewReader(input), "5000", "9999") |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("Unexpected error: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + if newHdr.Size != int64(len(out)) { |
| 90 | + t.Errorf("Expected header size %d, got %d", len(out), newHdr.Size) |
| 91 | + } |
| 92 | + |
| 93 | + var got map[string]any |
| 94 | + if err := json.Unmarshal(out, &got); err != nil { |
| 95 | + t.Fatalf("Failed to unmarshal output JSON: %v", err) |
| 96 | + } |
| 97 | + |
| 98 | + ports := got["newPortMappings"].([]any) |
| 99 | + if ports[0].(map[string]any)["container_port"].(float64) != 9999 { |
| 100 | + t.Errorf("Expected first mapped port to be 9999") |
| 101 | + } |
| 102 | + if ports[1].(map[string]any)["container_port"].(float64) != 7000 { |
| 103 | + t.Errorf("Expected non-target mapped port to remain 7000") |
| 104 | + } |
| 105 | + |
| 106 | + env := got["env"].([]any) |
| 107 | + if env[1] != "PORT=9999" { |
| 108 | + t.Errorf("Expected root PORT env to be remapped, got %v", env[1]) |
| 109 | + } |
| 110 | + |
| 111 | + nestedEnv := got["nested"].(map[string]any)["env"].([]any) |
| 112 | + if nestedEnv[0] != "PORT=9999" { |
| 113 | + t.Errorf("Expected nested PORT env to be remapped, got %v", nestedEnv[0]) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestRemapSpecDump(t *testing.T) { |
| 118 | + input := []byte(`{"process":{"env":["PORT=5000","FOO=bar"]}}`) |
| 119 | + hdr := &tar.Header{Name: "spec.dump", Size: int64(len(input))} |
| 120 | + |
| 121 | + newHdr, out, err := remapSpecDump(hdr, bytes.NewReader(input), "5000", "9999") |
| 122 | + if err != nil { |
| 123 | + t.Fatalf("Unexpected error: %v", err) |
| 124 | + } |
| 125 | + |
| 126 | + if newHdr.Size != int64(len(out)) { |
| 127 | + t.Errorf("Expected header size %d, got %d", len(out), newHdr.Size) |
| 128 | + } |
| 129 | + |
| 130 | + var got map[string]any |
| 131 | + if err := json.Unmarshal(out, &got); err != nil { |
| 132 | + t.Fatalf("Failed to unmarshal output JSON: %v", err) |
| 133 | + } |
| 134 | + |
| 135 | + env := got["process"].(map[string]any)["env"].([]any) |
| 136 | + if env[0] != "PORT=9999" { |
| 137 | + t.Errorf("Expected PORT env to be remapped, got %v", env[0]) |
| 138 | + } |
| 139 | +} |
0 commit comments