Skip to content

Commit

Permalink
keys for test report step
Browse files Browse the repository at this point in the history
  • Loading branch information
TimHuynh committed Jan 17, 2025
1 parent 41df509 commit 86d822d
Show file tree
Hide file tree
Showing 16 changed files with 263 additions and 11 deletions.
6 changes: 3 additions & 3 deletions api/admin/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func UploadObject(c *gin.Context) {

return
}
if input.BucketName == "" || input.ObjectName == "" {
if input.Bucket.BucketName == "" || input.ObjectName == "" {
retErr := fmt.Errorf("bucketName and objectName are required")
util.HandleError(c, http.StatusBadRequest, retErr)
return
Expand Down Expand Up @@ -382,7 +382,7 @@ func DownloadObject(c *gin.Context) {

return
}
if input.BucketName == "" || input.ObjectName == "" {
if input.Bucket.BucketName == "" || input.ObjectName == "" {
retErr := fmt.Errorf("bucketName and objectName are required")
util.HandleError(c, http.StatusBadRequest, retErr)
return
Expand Down Expand Up @@ -453,7 +453,7 @@ func GetPresignedURL(c *gin.Context) {
}

input := &types.Object{
BucketName: bucketName,
Bucket: types.Bucket{BucketName: bucketName},
ObjectName: objectName,
}

Expand Down
62 changes: 62 additions & 0 deletions api/types/test_report.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package types

import "fmt"

// TestReport is the API representation of a test report for a pipeline.
//
// swagger:model TestReport
type TestReport struct {
Results *string `json:"results,omitempty"`
Attachments *string `json:"attachments,omitempty"`
}

// GetResults returns the Results field.
//
// When the provided TestReport type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (t *TestReport) GetResults() string {
// return zero value if TestReport type or Results field is nil
if t == nil || t.Results == nil {
return ""
}

return *t.Results
}

// GetAttachments returns the Attachments field.
//
// When the provided TestReport type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (t *TestReport) GetAttachments() string {
// return zero value if TestReport type or Attachments field is nil
if t == nil || t.Attachments == nil {
return ""
}

return *t.Attachments
}

// SetResults sets the Results field.
func (t *TestReport) SetResults(v string) {
// return if TestReport type is nil
if t == nil {
return
}
// set the Results field
t.Results = &v
}

// SetAttachments sets the Attachments field.
func (t *TestReport) SetAttachments(v string) {
// return if TestReport type is nil
if t == nil {
return
}
// set the Attachments field
t.Attachments = &v
}

// String implements the Stringer interface for the TestReport type.
func (t *TestReport) String() string {
return fmt.Sprintf("Results: %s, Attachments: %s", t.GetResults(), t.GetAttachments())
}
35 changes: 35 additions & 0 deletions compiler/native/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,38 @@ func TestNative_Validate_MultiReportAs(t *testing.T) {
t.Errorf("Validate should have returned err")
}
}

func TestNative_Validate_TestReport(t *testing.T) {
// setup types
set := flag.NewFlagSet("test", 0)
set.String("clone-image", defaultCloneImage, "doc")
c := cli.NewContext(nil, set, nil)

str := "foo"
p := &yaml.Build{
Version: "v1",
Steps: yaml.StepSlice{
&yaml.Step{
Commands: raw.StringSlice{"echo hello"},
Image: "alpine",
Name: str,
Pull: "always",
TestReport: yaml.TestReport{
Results: []string{"results.xml"},
Attachments: []string{"attachments"},
},
},
},
}

// run test
compiler, err := FromCLIContext(c)
if err != nil {
t.Errorf("Unable to create new compiler: %v", err)
}

err = compiler.Validate(p)
if err != nil {
t.Errorf("Validate returned err: %v", err)
}
}
4 changes: 3 additions & 1 deletion compiler/types/pipeline/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type (
Pull string `json:"pull,omitempty" yaml:"pull,omitempty"`
Ruleset Ruleset `json:"ruleset,omitempty" yaml:"ruleset,omitempty"`
Secrets StepSecretSlice `json:"secrets,omitempty" yaml:"secrets,omitempty"`
TestReport TestReport `json:"test_report,omitempty" yaml:"test_report,omitempty"`
Ulimits UlimitSlice `json:"ulimits,omitempty" yaml:"ulimits,omitempty"`
Volumes VolumeSlice `json:"volumes,omitempty" yaml:"volumes,omitempty"`
User string `json:"user,omitempty" yaml:"user,omitempty"`
Expand Down Expand Up @@ -137,7 +138,8 @@ func (c *Container) Empty() bool {
len(c.Volumes) == 0 &&
len(c.User) == 0 &&
len(c.ReportAs) == 0 &&
len(c.IDRequest) == 0 {
len(c.IDRequest) == 0 &&
reflect.DeepEqual(c.TestReport, TestReport{}) {
return true
}

Expand Down
54 changes: 54 additions & 0 deletions compiler/types/pipeline/test_report.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package pipeline

// TestReport represents the structure for test report configuration.
type (
// TestReportSlice is the pipleine representation
//of a slice of TestReport.
//
// swagger:model PipelineTestReportSlice
TestReportSlice []*TestReport

// TestReport is the pipeline representation
// of a test report for a pipeline.
//
// swagger:model PipelineTestReport
TestReport struct {
Results []string `yaml:"results,omitempty" json:"results,omitempty"`
Attachments []string `yaml:"attachments,omitempty" json:"attachments,omitempty"`
}
)

// Purge removes the test report configuration from the pipeline
// if it does not match the provided ruledata. If both results
// and attachments are provided, then an empty test report is returned.
//func (t *TestReport) Purge(r *RuleData) (*TestReport, error) {
// // return an empty test report if both results and attachments are provided
// if len(t.Results) > 0 && len(t.Attachments) > 0 {
// return nil, fmt.Errorf("cannot have both results and attachments in the test report")
// }
//
// // purge results if provided
// if len(t.Results) > 0 {
// t.Results = ""
// }
//
// // purge attachments if provided
// if len(t.Attachments) > 0 {
// t.Attachments = ""
// }
//
// // return the purged test report
// return t, nil
//}

// Empty returns true if the provided test report is empty.
func (t *TestReport) Empty() bool {
// return true if every test report field is empty
if len(t.Results) == 0 &&
len(t.Attachments) == 0 {
return true
}

// return false if any of the test report fields are provided
return false
}
29 changes: 29 additions & 0 deletions compiler/types/pipeline/test_report_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package pipeline

import "testing"

func TestPipeline_TestReport_Empty(t *testing.T) {
// setup tests
tests := []struct {
report *TestReport
want bool
}{
{
report: &TestReport{Results: []string{"foo"}},
want: false,
},
{
report: new(TestReport),
want: true,
},
}

// run tests
for _, test := range tests {
got := test.report.Empty()

if got != test.want {
t.Errorf("Empty is %v, want %t", got, test.want)
}
}
}
2 changes: 1 addition & 1 deletion compiler/types/yaml/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (o *Origin) Empty() bool {

// MergeEnv takes a list of environment variables and attempts
// to set them in the secret environment. If the environment
// variable already exists in the secret, than this will
// variable already exists in the secret, then this will
// overwrite the existing environment variable.
func (o *Origin) MergeEnv(environment map[string]string) error {
// check if the secret container is empty
Expand Down
2 changes: 2 additions & 0 deletions compiler/types/yaml/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type (
Entrypoint raw.StringSlice `yaml:"entrypoint,omitempty" json:"entrypoint,omitempty" jsonschema:"description=Command to execute inside the container.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-entrypoint-key"`
Secrets StepSecretSlice `yaml:"secrets,omitempty" json:"secrets,omitempty" jsonschema:"description=Sensitive variables injected into the container environment.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-secrets-key"`
Template StepTemplate `yaml:"template,omitempty" json:"template,omitempty" jsonschema:"oneof_required=template,description=Name of template to expand in the pipeline.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-template-key"`
TestReport TestReport `yaml:"test_report,omitempty" json:"test_report,omitempty" jsonschema:"description=Test report configuration for the step.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-test_report-key"`
Ulimits UlimitSlice `yaml:"ulimits,omitempty" json:"ulimits,omitempty" jsonschema:"description=Set the user limits for the container.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-ulimits-key"`
Volumes VolumeSlice `yaml:"volumes,omitempty" json:"volumes,omitempty" jsonschema:"description=Mount volumes for the container.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-volume-key"`
Image string `yaml:"image,omitempty" json:"image,omitempty" jsonschema:"oneof_required=image,minLength=1,description=Docker image to use to create the ephemeral container.\nReference: https://go-vela.github.io/docs/reference/yaml/steps/#the-image-key"`
Expand Down Expand Up @@ -64,6 +65,7 @@ func (s *StepSlice) ToPipeline() *pipeline.ContainerSlice {
User: step.User,
ReportAs: step.ReportAs,
IDRequest: step.IDRequest,
TestReport: *step.TestReport.ToPipeline(),
})
}

Expand Down
17 changes: 17 additions & 0 deletions compiler/types/yaml/step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func TestYaml_StepSlice_ToPipeline(t *testing.T) {
AccessMode: "ro",
},
},
TestReport: TestReport{
Results: []string{"test.txt"},
Attachments: []string{"test.log"},
},
},
},
want: &pipeline.ContainerSlice{
Expand Down Expand Up @@ -134,6 +138,10 @@ func TestYaml_StepSlice_ToPipeline(t *testing.T) {
AccessMode: "ro",
},
},
TestReport: pipeline.TestReport{
Results: []string{"test.txt"},
Attachments: []string{"test.log"},
},
},
},
},
Expand Down Expand Up @@ -213,6 +221,15 @@ func TestYaml_StepSlice_UnmarshalYAML(t *testing.T) {
},
},
},
{
Name: "test-reports",
Pull: "always",
Image: "golang:1.20",
TestReport: TestReport{
Results: []string{"test-results/*.xml"},
Attachments: []string{"screenshots/**/*.png", " video/*.mp4"},
},
},
},
},
{
Expand Down
40 changes: 40 additions & 0 deletions compiler/types/yaml/test_report.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package yaml

import "github.com/go-vela/server/compiler/types/pipeline"

// TestReport represents the structure for test report configuration.
type TestReport struct {
Results []string `yaml:"results,omitempty" json:"results,omitempty"`
Attachments []string `yaml:"attachments,omitempty" json:"attachments,omitempty"`
}

// ToPipeline converts the TestReport type
// to a pipeline TestReport type.
func (t *TestReport) ToPipeline() *pipeline.TestReport {
return &pipeline.TestReport{
Results: t.Results,
Attachments: t.Attachments,
}
}

// UnmarshalYAML implements the Unmarshaler interface for the TestReport type.
func (t *TestReport) UnmarshalYAML(unmarshal func(interface{}) error) error {
// test report we try unmarshalling to
testReport := new(struct {
Results []string `yaml:"results,omitempty" json:"results,omitempty"`
Attachments []string `yaml:"attachments,omitempty" json:"attachments,omitempty"`
})

// attempt to unmarshal test report type
err := unmarshal(testReport)
if err != nil {
return err
}

// set the results field
t.Results = testReport.Results
// set the attachments field
t.Attachments = testReport.Attachments

return nil
}
7 changes: 7 additions & 0 deletions compiler/types/yaml/testdata/step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@
registry: index.docker.io
repo: github/octocat
tags: [ latest, dev ]

- name: test-reports
image: golang:1.20
pull: true
test_report:
results: ["test-results/*.xml"]
attachments: ["screenshots/**/*.png", " video/*.mp4"]
6 changes: 5 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ services:
#
# https://go-vela.github.io/docs/administration/worker/
worker:
# container_name: worker
# image: target/vela-worker:latest
build:
context: ../.
dockerfile: Dockerfile
container_name: worker
image: target/vela-worker:latest
networks:
- vela
environment:
Expand Down
2 changes: 1 addition & 1 deletion storage/minio/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func New(endpoint string, opts ...ClientOpt) (*MinioClient, error) {
c := new(MinioClient)

// default to secure connection
urlEndpoint := "s3.amazonaws.com"
var urlEndpoint string
useSSL := true

// create new fields
Expand Down
2 changes: 1 addition & 1 deletion storage/minio/minio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

var (
endpoint = "localhost:9000"
endpoint = "http://localhost:9000"
_accessKey = "minio_access_user"
_secretKey = "minio_secret_key"
_useSSL = false
Expand Down
2 changes: 1 addition & 1 deletion storage/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestSetup_Minio(t *testing.T) {
setup := &Setup{
Enable: true,
Driver: "minio",
Endpoint: "minio.example.com",
Endpoint: "http://minio.example.com",
AccessKey: "access-key",
SecretKey: "secret-key",
Bucket: "bucket-name",
Expand Down
Loading

0 comments on commit 86d822d

Please sign in to comment.