github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/ci/parse_script_test.go (about) 1 package ci 2 3 import ( 4 "path/filepath" 5 "sort" 6 "strings" 7 "testing" 8 9 "github.com/google/go-cmp/cmp" 10 "github.com/quickfeed/quickfeed/internal/env" 11 "github.com/quickfeed/quickfeed/internal/rand" 12 "github.com/quickfeed/quickfeed/qf" 13 ) 14 15 func testRunData(qfTestOrg string) *RunData { 16 repo := qf.RepoURL{ProviderURL: "github.com", Organization: qfTestOrg} 17 runData := &RunData{ 18 Course: &qf.Course{ 19 ID: 1, 20 Code: "DAT320", 21 ScmOrganizationName: qfTestOrg, 22 }, 23 Assignment: &qf.Assignment{ 24 Name: "lab3", 25 }, 26 Repo: &qf.Repository{ 27 HTMLURL: repo.StudentRepoURL("user"), 28 RepoType: qf.Repository_USER, 29 }, 30 JobOwner: "muggles", 31 CommitID: "deadbeef", 32 } 33 return runData 34 } 35 36 func TestLoadRunScript(t *testing.T) { 37 t.Setenv("QUICKFEED_REPOSITORY_PATH", env.TestdataPath()) 38 runData := &RunData{ 39 Course: &qf.Course{ 40 ID: 1, 41 Code: "qf104", 42 ScmOrganizationName: "qf104-2022", 43 }, 44 Assignment: &qf.Assignment{ 45 Name: "lab1", 46 }, 47 } 48 runSh, err := runData.loadRunScript() 49 if err != nil { 50 t.Error(err) 51 } 52 if len(runSh) == 0 { 53 t.Error("run script is empty") 54 } 55 runData.Assignment = &qf.Assignment{Name: "lab2"} 56 runSh, err = runData.loadRunScript() 57 if err != nil { 58 t.Error(err) 59 } 60 if len(runSh) == 0 { 61 t.Error("run script is empty") 62 } 63 } 64 65 func TestParseTestRunnerScript(t *testing.T) { 66 t.Setenv("QUICKFEED_REPOSITORY_PATH", env.TestdataPath()) 67 68 const ( 69 qfTestOrg = "qf104-2022" 70 image = "quickfeed:go" 71 runScriptContent = `#image/quickfeed:go 72 echo "$TESTS" 73 echo "$ASSIGNMENTS" 74 echo "$SUBMITTED" 75 echo "$CURRENT" 76 echo "$QUICKFEED_SESSION_SECRET" 77 ` 78 ) 79 randomSecret := rand.String() 80 81 runData := testRunData(qfTestOrg) 82 job, err := runData.parseTestRunnerScript(randomSecret, "") 83 if err != nil { 84 t.Fatal(err) 85 } 86 if job.Image != image { 87 t.Errorf("job.Image = %s, want %s", job.Image, image) 88 } 89 gotVars := job.Env 90 wantVars := []string{ 91 "HOME=" + QuickFeedPath, 92 "TESTS=" + filepath.Join(QuickFeedPath, qf.TestsRepo), 93 "ASSIGNMENTS=" + filepath.Join(QuickFeedPath, qf.AssignmentsRepo), 94 "SUBMITTED=" + filepath.Join(QuickFeedPath, qf.StudentRepoName("user")), 95 "CURRENT=" + runData.Assignment.GetName(), 96 "QUICKFEED_SESSION_SECRET=" + randomSecret, 97 } 98 trans := cmp.Transformer("Sort", func(in []string) []string { 99 out := append([]string(nil), in...) 100 sort.Strings(out) 101 return out 102 }) 103 if diff := cmp.Diff(wantVars, gotVars, trans); diff != "" { 104 t.Errorf("parseTestRunnerScript() mismatch (-want +got):\n%s", diff) 105 } 106 _, after, found := strings.Cut(runScriptContent, image+"\n") 107 if !found { 108 t.Errorf("No script content found for image: %s", image) 109 } 110 for i, line := range strings.Split(after, "\n") { 111 if line != job.Commands[i] { 112 t.Errorf("job.Commands[%d] = %s, want %s", i, job.Commands[i], line) 113 } 114 } 115 if job.Name != "dat320-lab3-muggles-"+runData.CommitID[:6] { 116 t.Errorf("job.Name = %s, want %s", job.Name, "dat320-lab3-muggles-"+runData.CommitID[:6]) 117 } 118 } 119 120 func TestParseBadTestRunnerScript(t *testing.T) { 121 t.Setenv("QUICKFEED_REPOSITORY_PATH", env.TestdataPath()) 122 123 const qfTestOrg = "qf104-2022" 124 randomSecret := rand.String() 125 126 runData := testRunData(qfTestOrg) 127 runData.Assignment = &qf.Assignment{Name: "lab4-bad-run-script"} 128 job, err := runData.parseTestRunnerScript(randomSecret, "") 129 if err == nil { 130 t.Fatalf("expected error, got nil: %+v", job) 131 } 132 const wantMsg = "failed to parse run script for assignment lab4-bad-run-script in https://github.com/qf104-2022/tests: empty run script" 133 if err.Error() != wantMsg { 134 t.Errorf("err = '%s', want '%s'", err, wantMsg) 135 } 136 137 runData.Assignment = &qf.Assignment{Name: "lab5-bad-run-script"} 138 job, err = runData.parseTestRunnerScript(randomSecret, "") 139 if err == nil { 140 t.Fatalf("expected error, got nil: %+v", job) 141 } 142 const wantMsg2 = "failed to parse run script for assignment lab5-bad-run-script in https://github.com/qf104-2022/tests: no docker image specified in run script" 143 if err.Error() != wantMsg2 { 144 t.Errorf("err = '%s', want '%s'", err, wantMsg2) 145 } 146 }