|
| 1 | +package agentapi_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + "go.uber.org/mock/gomock" |
| 10 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 11 | + |
| 12 | + agentproto "github.com/coder/coder/v2/agent/proto" |
| 13 | + "github.com/coder/coder/v2/coderd/agentapi" |
| 14 | + "github.com/coder/coder/v2/coderd/database" |
| 15 | + "github.com/coder/coder/v2/coderd/database/dbmock" |
| 16 | + "github.com/coder/coder/v2/coderd/database/dbtime" |
| 17 | +) |
| 18 | + |
| 19 | +func TestScriptCompleted(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + tests := []struct { |
| 23 | + scriptID uuid.UUID |
| 24 | + timing *agentproto.Timing |
| 25 | + }{ |
| 26 | + { |
| 27 | + scriptID: uuid.New(), |
| 28 | + timing: &agentproto.Timing{ |
| 29 | + Stage: agentproto.Timing_START, |
| 30 | + DisplayName: "Start Script", |
| 31 | + Start: timestamppb.New(dbtime.Now()), |
| 32 | + End: timestamppb.New(dbtime.Now().Add(time.Second)), |
| 33 | + TimedOut: false, |
| 34 | + ExitCode: 0, |
| 35 | + }, |
| 36 | + }, |
| 37 | + { |
| 38 | + scriptID: uuid.New(), |
| 39 | + timing: &agentproto.Timing{ |
| 40 | + Stage: agentproto.Timing_STOP, |
| 41 | + DisplayName: "Stop Script", |
| 42 | + Start: timestamppb.New(dbtime.Now()), |
| 43 | + End: timestamppb.New(dbtime.Now().Add(time.Second)), |
| 44 | + TimedOut: false, |
| 45 | + ExitCode: 0, |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + scriptID: uuid.New(), |
| 50 | + timing: &agentproto.Timing{ |
| 51 | + Stage: agentproto.Timing_CRON, |
| 52 | + DisplayName: "Cron Script", |
| 53 | + Start: timestamppb.New(dbtime.Now()), |
| 54 | + End: timestamppb.New(dbtime.Now().Add(time.Second)), |
| 55 | + TimedOut: false, |
| 56 | + ExitCode: 0, |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + scriptID: uuid.New(), |
| 61 | + timing: &agentproto.Timing{ |
| 62 | + Stage: agentproto.Timing_START, |
| 63 | + DisplayName: "Timed Out Script", |
| 64 | + Start: timestamppb.New(dbtime.Now()), |
| 65 | + End: timestamppb.New(dbtime.Now().Add(time.Second)), |
| 66 | + TimedOut: true, |
| 67 | + ExitCode: 255, |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + scriptID: uuid.New(), |
| 72 | + timing: &agentproto.Timing{ |
| 73 | + Stage: agentproto.Timing_START, |
| 74 | + DisplayName: "Failed Script", |
| 75 | + Start: timestamppb.New(dbtime.Now()), |
| 76 | + End: timestamppb.New(dbtime.Now().Add(time.Second)), |
| 77 | + TimedOut: true, |
| 78 | + ExitCode: 1, |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tt := range tests { |
| 84 | + // Setup the script ID |
| 85 | + tt.timing.ScriptId = tt.scriptID[:] |
| 86 | + |
| 87 | + mDB := dbmock.NewMockStore(gomock.NewController(t)) |
| 88 | + mDB.EXPECT().InsertWorkspaceAgentScriptTimings(gomock.Any(), database.InsertWorkspaceAgentScriptTimingsParams{ |
| 89 | + ScriptID: tt.scriptID, |
| 90 | + Stage: protoScriptTimingStageToDatabase(tt.timing.Stage), |
| 91 | + DisplayName: tt.timing.DisplayName, |
| 92 | + StartedAt: tt.timing.Start.AsTime(), |
| 93 | + EndedAt: tt.timing.End.AsTime(), |
| 94 | + TimedOut: tt.timing.TimedOut, |
| 95 | + ExitCode: tt.timing.ExitCode, |
| 96 | + }) |
| 97 | + |
| 98 | + api := &agentapi.ScriptsAPI{Database: mDB} |
| 99 | + api.ScriptCompleted(context.Background(), &agentproto.WorkspaceAgentScriptCompletedRequest{ |
| 100 | + Timing: tt.timing, |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func protoScriptTimingStageToDatabase(stage agentproto.Timing_Stage) database.WorkspaceAgentScriptTimingStage { |
| 106 | + var dbStage database.WorkspaceAgentScriptTimingStage |
| 107 | + switch stage { |
| 108 | + case agentproto.Timing_START: |
| 109 | + dbStage = database.WorkspaceAgentScriptTimingStageStart |
| 110 | + case agentproto.Timing_STOP: |
| 111 | + dbStage = database.WorkspaceAgentScriptTimingStageStop |
| 112 | + case agentproto.Timing_CRON: |
| 113 | + dbStage = database.WorkspaceAgentScriptTimingStageCron |
| 114 | + } |
| 115 | + return dbStage |
| 116 | +} |
0 commit comments