|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package pipeline |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/gin-gonic/gin" |
| 13 | + "github.com/sirupsen/logrus" |
| 14 | + "github.com/urfave/cli/v3" |
| 15 | + "go.yaml.in/yaml/v3" |
| 16 | + |
| 17 | + api "github.com/go-vela/server/api/types" |
| 18 | + "github.com/go-vela/server/compiler" |
| 19 | + "github.com/go-vela/server/compiler/native" |
| 20 | + "github.com/go-vela/server/constants" |
| 21 | + "github.com/go-vela/server/internal" |
| 22 | + pipelineMiddleware "github.com/go-vela/server/router/middleware/pipeline" |
| 23 | + repoMiddleware "github.com/go-vela/server/router/middleware/repo" |
| 24 | + userMiddleware "github.com/go-vela/server/router/middleware/user" |
| 25 | +) |
| 26 | + |
| 27 | +func TestExpandPipelineStagesReturnsCleanYAML(t *testing.T) { |
| 28 | + gin.SetMode(gin.TestMode) |
| 29 | + |
| 30 | + assertCleanStages := func(t *testing.T, w *httptest.ResponseRecorder) { |
| 31 | + t.Helper() |
| 32 | + |
| 33 | + if w.Code != http.StatusOK { |
| 34 | + t.Fatalf("ExpandPipeline returned status %d, want %d", w.Code, http.StatusOK) |
| 35 | + } |
| 36 | + |
| 37 | + var got struct { |
| 38 | + Stages map[string]any `yaml:"stages"` |
| 39 | + } |
| 40 | + |
| 41 | + if err := yaml.Unmarshal(w.Body.Bytes(), &got); err != nil { |
| 42 | + t.Fatalf("unable to parse yaml response: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + if len(got.Stages) == 0 { |
| 46 | + t.Fatalf("expected stages in response, got none") |
| 47 | + } |
| 48 | + |
| 49 | + if _, ok := got.Stages["build"]; !ok { |
| 50 | + t.Fatalf("expected build stage in response, got keys %#v", got.Stages) |
| 51 | + } |
| 52 | + |
| 53 | + if _, hasKind := got.Stages["kind"]; hasKind { |
| 54 | + t.Fatalf("unexpected raw yaml node output: %#v", got.Stages) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + setup := func(t *testing.T, requestPath string) (*httptest.ResponseRecorder, *gin.Context) { |
| 59 | + t.Helper() |
| 60 | + |
| 61 | + w := httptest.NewRecorder() |
| 62 | + c, _ := gin.CreateTestContext(w) |
| 63 | + |
| 64 | + req := httptest.NewRequest(http.MethodPost, requestPath, nil) |
| 65 | + c.Request = req |
| 66 | + |
| 67 | + logger := logrus.New() |
| 68 | + logger.SetOutput(io.Discard) |
| 69 | + c.Set("logger", logrus.NewEntry(logger)) |
| 70 | + |
| 71 | + c.Set("metadata", &internal.Metadata{}) |
| 72 | + |
| 73 | + repo := new(api.Repo) |
| 74 | + repo.SetOrg("foo") |
| 75 | + repo.SetName("bar") |
| 76 | + repo.SetFullName("foo/bar") |
| 77 | + repo.SetPipelineType(constants.PipelineTypeYAML) |
| 78 | + repoMiddleware.ToContext(c, repo) |
| 79 | + |
| 80 | + pipeline := new(api.Pipeline) |
| 81 | + pipeline.SetCommit("123") |
| 82 | + pipeline.SetType(constants.PipelineTypeYAML) |
| 83 | + pipeline.SetData([]byte(`version: "1" |
| 84 | +stages: |
| 85 | + build: |
| 86 | + steps: |
| 87 | + - name: test |
| 88 | + image: alpine |
| 89 | + commands: |
| 90 | + - echo hello |
| 91 | +`)) |
| 92 | + pipelineMiddleware.ToContext(c, pipeline) |
| 93 | + |
| 94 | + user := new(api.User) |
| 95 | + userMiddleware.ToContext(c, user) |
| 96 | + |
| 97 | + engine := newTestCompiler(t) |
| 98 | + compiler.WithGinContext(c, engine) |
| 99 | + |
| 100 | + return w, c |
| 101 | + } |
| 102 | + |
| 103 | + t.Run("explicit yaml", func(t *testing.T) { |
| 104 | + w, c := setup(t, "/api/v1/pipelines/foo/bar/123/expand?output=yaml") |
| 105 | + |
| 106 | + ExpandPipeline(c) |
| 107 | + |
| 108 | + assertCleanStages(t, w) |
| 109 | + }) |
| 110 | + |
| 111 | + t.Run("default yaml", func(t *testing.T) { |
| 112 | + w, c := setup(t, "/api/v1/pipelines/foo/bar/123/expand") |
| 113 | + |
| 114 | + ExpandPipeline(c) |
| 115 | + |
| 116 | + assertCleanStages(t, w) |
| 117 | + }) |
| 118 | +} |
| 119 | + |
| 120 | +func newTestCompiler(t *testing.T) compiler.Engine { |
| 121 | + t.Helper() |
| 122 | + |
| 123 | + cmd := &cli.Command{ |
| 124 | + Flags: []cli.Flag{ |
| 125 | + &cli.StringFlag{ |
| 126 | + Name: "clone-image", |
| 127 | + Value: "target/vela-git:latest", |
| 128 | + }, |
| 129 | + &cli.IntFlag{ |
| 130 | + Name: "max-template-depth", |
| 131 | + Value: 5, |
| 132 | + }, |
| 133 | + &cli.BoolFlag{ |
| 134 | + Name: "github-driver", |
| 135 | + Value: false, |
| 136 | + }, |
| 137 | + &cli.StringFlag{ |
| 138 | + Name: "github-url", |
| 139 | + Value: "", |
| 140 | + }, |
| 141 | + &cli.StringFlag{ |
| 142 | + Name: "github-token", |
| 143 | + Value: "", |
| 144 | + }, |
| 145 | + &cli.Int64Flag{ |
| 146 | + Name: "compiler-starlark-exec-limit", |
| 147 | + Value: 0, |
| 148 | + }, |
| 149 | + }, |
| 150 | + } |
| 151 | + |
| 152 | + engine, err := native.FromCLICommand(context.Background(), cmd) |
| 153 | + if err != nil { |
| 154 | + t.Fatalf("unable to create compiler: %v", err) |
| 155 | + } |
| 156 | + |
| 157 | + return engine |
| 158 | +} |
0 commit comments