BlueGit

timfa bluegit

Files

bluegit / internal / ci / spec_test.go

spec_test.go

feat: GitLab-achtige blue-ci-cd.yml
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed 17caa14a History
spec_test.go 5.9 KiB Download Raw
1package ci
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestGitLabDocument(t *testing.T) {
9 raw := []byte(`
10stages: [test, deploy]
11default:
12 image: alpine:3.20
13 before_script: ["echo prep"]
14variables:
15 MODE: {value: "ci"}
16.php:
17 image: php:8.3
18 stage: test
19php_tests:
20 extends: .php
21 script: ["phpunit"]
22 rules:
23 - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
24 changes: ["**/*.php"]
25 - when: never
26frontend_checks:
27 stage: test
28 script: ["node check"]
29 rules:
30 - if: $CI_COMMIT_TAG == null
31 exists: ["package.json"]
32 allow_failure: true
33 cache:
34 key: modules
35 paths: ["node_modules"]
36deploy:
37 stage: deploy
38 when: manual
39 resource_group: production
40 timeout: 30m
41 tags: [deploy]
42 only: ["master", "tags"]
43 script: ["echo deploy"]
44 needs:
45 - job: php_tests
46 optional: true
47`)
48 p, err := Parse(raw)
49 if err != nil {
50 t.Fatal(err)
51 }
52 if strings.Join(p.Stages, ",") != "test,deploy" {
53 t.Fatalf("stages %v", p.Stages)
54 }
55 if p.Variables["MODE"] != "ci" {
56 t.Fatal(p.Variables)
57 }
58 by := map[string]Job{}
59 for _, j := range p.Jobs {
60 by[j.Name] = j
61 }
62 if _, ok := by[".php"]; ok {
63 t.Fatal("verborgen job hoort niet te draaien")
64 }
65 php := by["php_tests"]
66 if php.Image != "php:8.3" || php.Stage != "test" || php.Before[0] != "echo prep" {
67 t.Fatalf("extends %+v", php)
68 }
69 front := by["frontend_checks"]
70 if !front.AllowFailure || front.Cache.Key != "modules" || front.Cache.Paths[0] != "node_modules" {
71 t.Fatalf("frontend %+v", front)
72 }
73 dep := by["deploy"]
74 if dep.When != "manual" || dep.ResourceGroup != "production" || dep.Timeout.Minutes() != 30 {
75 t.Fatalf("deploy %+v", dep)
76 }
77 if len(dep.Needs) != 1 || dep.Optional[0] != "php_tests" {
78 t.Fatalf("needs %+v opt %+v", dep.Needs, dep.Optional)
79 }
80
81 env := map[string]string{"CI_COMMIT_BRANCH": "master", "CI_DEFAULT_BRANCH": "master", "CI_COMMIT_REF_NAME": "master"}
82 active, err := p.Select(Eval{Env: env, Changed: []string{"src/App.php"}, Files: []string{"package.json"}})
83 if err != nil {
84 t.Fatal(err)
85 }
86 names := map[string]bool{}
87 for _, j := range active {
88 names[j.Name] = true
89 }
90 if !names["php_tests"] || !names["frontend_checks"] || !names["deploy"] {
91 t.Fatalf("actief %v", names)
92 }
93 active, err = p.Select(Eval{Env: map[string]string{"CI_COMMIT_BRANCH": "feature", "CI_DEFAULT_BRANCH": "master", "CI_COMMIT_REF_NAME": "feature"}, Changed: []string{"README"}, Files: []string{}})
94 if err != nil {
95 t.Fatal(err)
96 }
97 names = map[string]bool{}
98 for _, j := range active {
99 names[j.Name] = true
100 if j.Name == "deploy" && j.When != "manual" {
101 t.Fatal(j.When)
102 }
103 }
104 if names["php_tests"] || names["frontend_checks"] {
105 t.Fatalf("feature hoort die jobs over te slaan: %v", names)
106 }
107 if names["deploy"] {
108 t.Fatal("only master/tags")
109 }
110}
111
112func TestIncludeLocalAndWarnings(t *testing.T) {
113 files := map[string]string{
114 "ci/base.yml": "jobs:\n unit:\n image: alpine\n script: ['echo unit']\n",
115 }
116 raw := []byte("include:\n - local: ci/base.yml\nservices: [db]\njobs:\n unit:\n script: ['echo replaced']\n")
117 p, err := ParseIn(raw, func(path string) ([]byte, error) { return []byte(files[path]), nil })
118 if err != nil {
119 t.Fatal(err)
120 }
121 if len(p.Jobs) != 1 || p.Jobs[0].Script[0] != "echo replaced" || p.Jobs[0].Image != "alpine" {
122 t.Fatalf("%+v", p.Jobs)
123 }
124 if len(p.Warnings) != 1 || p.Warnings[0].Key != "ci.warn.ignored" {
125 t.Fatalf("warnings %+v", p.Warnings)
126 }
127 _, issues := Lint([]byte("include:\n - project: other\njobs:\n a:\n image: alpine\n script: ['x']\n"), nil)
128 found := false
129 for _, is := range issues {
130 if is.Key == "ci.err.includeRemote" {
131 found = true
132 }
133 }
134 if !found {
135 t.Fatalf("issues %+v", issues)
136 }
137}
138
139func TestWhenAndAllowFailure(t *testing.T) {
140 p, err := Parse([]byte(`
141stages: [een, twee]
142jobs:
143 a:
144 stage: een
145 image: alpine
146 script: ['echo a']
147 allow_failure: true
148 b:
149 stage: twee
150 image: alpine
151 script: ['echo b']
152 c:
153 stage: twee
154 image: alpine
155 when: always
156 script: ['echo c']
157 d:
158 stage: twee
159 image: alpine
160 when: on_failure
161 script: ['echo d']
162`))
163 if err != nil {
164 t.Fatal(err)
165 }
166 st := map[string]string{"a": "failed", "b": "queued", "c": "queued", "d": "queued"}
167 if Gate(p.Jobs, st, p.Jobs[1]) != "ready" {
168 t.Fatal("allow_failure houdt de volgende stage niet tegen")
169 }
170 if Gate(p.Jobs, st, p.Jobs[2]) != "ready" || Gate(p.Jobs, st, p.Jobs[3]) != "ready" {
171 t.Fatal("always en on_failure na een mislukking")
172 }
173 ok := map[string]string{"a": "success", "b": "queued", "c": "queued", "d": "queued"}
174 if Gate(p.Jobs, ok, p.Jobs[3]) != "skip" {
175 t.Fatal("on_failure slaat over na succes")
176 }
177 hard, err := Parse([]byte(`
178stages: [een, twee]
179jobs:
180 a:
181 stage: een
182 image: alpine
183 script: ['echo a']
184 b:
185 stage: twee
186 image: alpine
187 script: ['echo b']
188`))
189 if err != nil {
190 t.Fatal(err)
191 }
192 if Gate(hard.Jobs, map[string]string{"a": "failed", "b": "queued"}, hard.Jobs[1]) != "skip" {
193 t.Fatal("harde fout slaat de rest over")
194 }
195}
196
197func TestMaskAndVariables(t *testing.T) {
198 if got := Mask("token=supersecret value", []string{"supersecret", "ab"}); got != "token=[MASKED] value" {
199 t.Fatal(got)
200 }
201 env := Variables(Ref{SHA: "abcdef1234567890", FullRef: "refs/tags/v1.2.0", Title: "Uitgave", DefaultBranch: "master", Source: "push", ProjectPath: "tim/app", ProjectURL: "https://git.example/tim/app", APIURL: "https://git.example/api/v1", PipelineID: 9})
202 if env["CI_COMMIT_TAG"] != "v1.2.0" || env["CI_COMMIT_BRANCH"] != "" || env["CI_COMMIT_SHORT_SHA"] != "abcdef12" {
203 t.Fatalf("%v", env)
204 }
205 if env["CI_DEFAULT_BRANCH"] != "master" || env["CI_PROJECT_NAME"] != "app" || env["CI_PIPELINE_SOURCE"] != "push" {
206 t.Fatalf("%v", env)
207 }
208 ok, err := evalRule(`$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH`, map[string]string{"CI_COMMIT_BRANCH": "master", "CI_DEFAULT_BRANCH": "master"})
209 if err != nil || !ok {
210 t.Fatal(err, ok)
211 }
212 ok, err = evalRule(`$CI_COMMIT_TAG == null`, map[string]string{"CI_COMMIT_TAG": ""})
213 if err != nil || !ok {
214 t.Fatal("null", err, ok)
215 }
216}