|
| 1 | +package agentcontainers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 9 | + "github.com/google/uuid" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "cdr.dev/slog/sloggers/slogtest" |
| 13 | + "github.com/coder/coder/v2/agent/agentcontainers" |
| 14 | + "github.com/coder/coder/v2/codersdk" |
| 15 | +) |
| 16 | + |
| 17 | +func TestExtractAndInitializeDevcontainerScripts(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + scriptIDs := []uuid.UUID{uuid.New(), uuid.New()} |
| 21 | + devcontainerIDs := []uuid.UUID{uuid.New(), uuid.New()} |
| 22 | + |
| 23 | + type args struct { |
| 24 | + expandPath func(string) (string, error) |
| 25 | + devcontainers []codersdk.WorkspaceAgentDevcontainer |
| 26 | + scripts []codersdk.WorkspaceAgentScript |
| 27 | + } |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + args args |
| 31 | + wantFilteredScripts []codersdk.WorkspaceAgentScript |
| 32 | + wantDevcontainerScripts []codersdk.WorkspaceAgentScript |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "no scripts", |
| 36 | + args: args{ |
| 37 | + expandPath: nil, |
| 38 | + devcontainers: nil, |
| 39 | + scripts: nil, |
| 40 | + }, |
| 41 | + wantFilteredScripts: nil, |
| 42 | + wantDevcontainerScripts: nil, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "no devcontainers", |
| 46 | + args: args{ |
| 47 | + expandPath: nil, |
| 48 | + devcontainers: nil, |
| 49 | + scripts: []codersdk.WorkspaceAgentScript{ |
| 50 | + {ID: scriptIDs[0]}, |
| 51 | + {ID: scriptIDs[1]}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + wantFilteredScripts: []codersdk.WorkspaceAgentScript{ |
| 55 | + {ID: scriptIDs[0]}, |
| 56 | + {ID: scriptIDs[1]}, |
| 57 | + }, |
| 58 | + wantDevcontainerScripts: nil, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "no scripts match devcontainers", |
| 62 | + args: args{ |
| 63 | + expandPath: nil, |
| 64 | + devcontainers: []codersdk.WorkspaceAgentDevcontainer{ |
| 65 | + {ID: devcontainerIDs[0]}, |
| 66 | + {ID: devcontainerIDs[1]}, |
| 67 | + }, |
| 68 | + scripts: []codersdk.WorkspaceAgentScript{ |
| 69 | + {ID: scriptIDs[0]}, |
| 70 | + {ID: scriptIDs[1]}, |
| 71 | + }, |
| 72 | + }, |
| 73 | + wantFilteredScripts: []codersdk.WorkspaceAgentScript{ |
| 74 | + {ID: scriptIDs[0]}, |
| 75 | + {ID: scriptIDs[1]}, |
| 76 | + }, |
| 77 | + wantDevcontainerScripts: nil, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "scripts match devcontainers and sets RunOnStart=false", |
| 81 | + args: args{ |
| 82 | + expandPath: nil, |
| 83 | + devcontainers: []codersdk.WorkspaceAgentDevcontainer{ |
| 84 | + {ID: devcontainerIDs[0], WorkspaceFolder: "workspace1"}, |
| 85 | + {ID: devcontainerIDs[1], WorkspaceFolder: "workspace2"}, |
| 86 | + }, |
| 87 | + scripts: []codersdk.WorkspaceAgentScript{ |
| 88 | + {ID: scriptIDs[0], RunOnStart: true}, |
| 89 | + {ID: scriptIDs[1], RunOnStart: true}, |
| 90 | + {ID: devcontainerIDs[0], RunOnStart: true}, |
| 91 | + {ID: devcontainerIDs[1], RunOnStart: true}, |
| 92 | + }, |
| 93 | + }, |
| 94 | + wantFilteredScripts: []codersdk.WorkspaceAgentScript{ |
| 95 | + {ID: scriptIDs[0], RunOnStart: true}, |
| 96 | + {ID: scriptIDs[1], RunOnStart: true}, |
| 97 | + }, |
| 98 | + wantDevcontainerScripts: []codersdk.WorkspaceAgentScript{ |
| 99 | + { |
| 100 | + ID: devcontainerIDs[0], |
| 101 | + Script: "devcontainer up --workspace-folder \"workspace1\"", |
| 102 | + RunOnStart: false, |
| 103 | + }, |
| 104 | + { |
| 105 | + ID: devcontainerIDs[1], |
| 106 | + Script: "devcontainer up --workspace-folder \"workspace2\"", |
| 107 | + RunOnStart: false, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "scripts match devcontainers with config path", |
| 113 | + args: args{ |
| 114 | + expandPath: nil, |
| 115 | + devcontainers: []codersdk.WorkspaceAgentDevcontainer{ |
| 116 | + { |
| 117 | + ID: devcontainerIDs[0], |
| 118 | + WorkspaceFolder: "workspace1", |
| 119 | + ConfigPath: "config1", |
| 120 | + }, |
| 121 | + { |
| 122 | + ID: devcontainerIDs[1], |
| 123 | + WorkspaceFolder: "workspace2", |
| 124 | + ConfigPath: "config2", |
| 125 | + }, |
| 126 | + }, |
| 127 | + scripts: []codersdk.WorkspaceAgentScript{ |
| 128 | + {ID: devcontainerIDs[0]}, |
| 129 | + {ID: devcontainerIDs[1]}, |
| 130 | + }, |
| 131 | + }, |
| 132 | + wantFilteredScripts: []codersdk.WorkspaceAgentScript{}, |
| 133 | + wantDevcontainerScripts: []codersdk.WorkspaceAgentScript{ |
| 134 | + { |
| 135 | + ID: devcontainerIDs[0], |
| 136 | + Script: "devcontainer up --workspace-folder \"workspace1\" --config \"config1\"", |
| 137 | + RunOnStart: false, |
| 138 | + }, |
| 139 | + { |
| 140 | + ID: devcontainerIDs[1], |
| 141 | + Script: "devcontainer up --workspace-folder \"workspace2\" --config \"config2\"", |
| 142 | + RunOnStart: false, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "scripts match devcontainers with expand path", |
| 148 | + args: args{ |
| 149 | + expandPath: func(s string) (string, error) { |
| 150 | + return "expanded/" + s, nil |
| 151 | + }, |
| 152 | + devcontainers: []codersdk.WorkspaceAgentDevcontainer{ |
| 153 | + { |
| 154 | + ID: devcontainerIDs[0], |
| 155 | + WorkspaceFolder: "workspace1", |
| 156 | + ConfigPath: "config1", |
| 157 | + }, |
| 158 | + { |
| 159 | + ID: devcontainerIDs[1], |
| 160 | + WorkspaceFolder: "workspace2", |
| 161 | + ConfigPath: "config2", |
| 162 | + }, |
| 163 | + }, |
| 164 | + scripts: []codersdk.WorkspaceAgentScript{ |
| 165 | + {ID: devcontainerIDs[0], RunOnStart: true}, |
| 166 | + {ID: devcontainerIDs[1], RunOnStart: true}, |
| 167 | + }, |
| 168 | + }, |
| 169 | + wantFilteredScripts: []codersdk.WorkspaceAgentScript{}, |
| 170 | + wantDevcontainerScripts: []codersdk.WorkspaceAgentScript{ |
| 171 | + { |
| 172 | + ID: devcontainerIDs[0], |
| 173 | + Script: "devcontainer up --workspace-folder \"expanded/workspace1\" --config \"expanded/config1\"", |
| 174 | + RunOnStart: false, |
| 175 | + }, |
| 176 | + { |
| 177 | + ID: devcontainerIDs[1], |
| 178 | + Script: "devcontainer up --workspace-folder \"expanded/workspace2\" --config \"expanded/config2\"", |
| 179 | + RunOnStart: false, |
| 180 | + }, |
| 181 | + }, |
| 182 | + }, |
| 183 | + } |
| 184 | + // nolint:foo |
| 185 | + for _, tt := range tests { |
| 186 | + t.Run(tt.name, func(t *testing.T) { |
| 187 | + t.Parallel() |
| 188 | + logger := slogtest.Make(t, nil) |
| 189 | + if tt.args.expandPath == nil { |
| 190 | + tt.args.expandPath = func(s string) (string, error) { |
| 191 | + return s, nil |
| 192 | + } |
| 193 | + } |
| 194 | + gotFilteredScripts, gotDevcontainerScripts := agentcontainers.ExtractAndInitializeDevcontainerScripts( |
| 195 | + logger, |
| 196 | + tt.args.expandPath, |
| 197 | + tt.args.devcontainers, |
| 198 | + tt.args.scripts, |
| 199 | + ) |
| 200 | + |
| 201 | + if diff := cmp.Diff(tt.wantFilteredScripts, gotFilteredScripts, cmpopts.EquateEmpty()); diff != "" { |
| 202 | + t.Errorf("ExtractAndInitializeDevcontainerScripts() gotFilteredScripts mismatch (-want +got):\n%s", diff) |
| 203 | + } |
| 204 | + |
| 205 | + // Preprocess the devcontainer scripts to remove scripting part. |
| 206 | + for i := range gotDevcontainerScripts { |
| 207 | + gotDevcontainerScripts[i].Script = textGrep("devcontainer up", gotDevcontainerScripts[i].Script) |
| 208 | + require.NotEmpty(t, gotDevcontainerScripts[i].Script, "devcontainer up script not found") |
| 209 | + } |
| 210 | + if diff := cmp.Diff(tt.wantDevcontainerScripts, gotDevcontainerScripts); diff != "" { |
| 211 | + t.Errorf("ExtractAndInitializeDevcontainerScripts() gotDevcontainerScripts mismatch (-want +got):\n%s", diff) |
| 212 | + } |
| 213 | + }) |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +// textGrep returns matching lines from multiline string. |
| 218 | +func textGrep(want, got string) (filtered string) { |
| 219 | + var lines []string |
| 220 | + for _, line := range strings.Split(got, "\n") { |
| 221 | + if strings.Contains(line, want) { |
| 222 | + lines = append(lines, line) |
| 223 | + } |
| 224 | + } |
| 225 | + return strings.Join(lines, "\n") |
| 226 | +} |
0 commit comments