|
| 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 | +funcTestExtractAndInitializeDevcontainerScripts(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 | +typeargsstruct { |
| 24 | +expandPathfunc(string) (string,error) |
| 25 | +devcontainers []codersdk.WorkspaceAgentDevcontainer |
| 26 | +scripts []codersdk.WorkspaceAgentScript |
| 27 | +} |
| 28 | +tests:= []struct { |
| 29 | +namestring |
| 30 | +argsargs |
| 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(sstring) (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:=rangetests { |
| 186 | +t.Run(tt.name,func(t*testing.T) { |
| 187 | +t.Parallel() |
| 188 | +logger:=slogtest.Make(t,nil) |
| 189 | +iftt.args.expandPath==nil { |
| 190 | +tt.args.expandPath=func(sstring) (string,error) { |
| 191 | +returns,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 | +ifdiff:=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 | +fori:=rangegotDevcontainerScripts { |
| 207 | +gotDevcontainerScripts[i].Script=textGrep("devcontainer up",gotDevcontainerScripts[i].Script) |
| 208 | +require.NotEmpty(t,gotDevcontainerScripts[i].Script,"devcontainer up script not found") |
| 209 | +} |
| 210 | +ifdiff:=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 | +functextGrep(want,gotstring) (filteredstring) { |
| 219 | +varlines []string |
| 220 | +for_,line:=rangestrings.Split(got,"\n") { |
| 221 | +ifstrings.Contains(line,want) { |
| 222 | +lines=append(lines,line) |
| 223 | +} |
| 224 | +} |
| 225 | +returnstrings.Join(lines,"\n") |
| 226 | +} |