-
Notifications
You must be signed in to change notification settings - Fork 38
feat: Add support for JUnit XML material type (#120) #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
435ef46
feat: Add support fot JUnit XML material type (#120)
danlishka 4e7bd90
feat: Add support fot JUnit XML material type (#120) - added tests
danlishka 4b865fb
feat: Add support fot JUnit XML material type (#120) - go mod tidy
danlishka 07c26e9
feat: Add support fot JUnit XML material type (#120) - refactoring
danlishka c5a9b43
feat: Add support fot JUnit XML material type (#120) - revert the com…
danlishka d555e0d
feat: Add support fot JUnit XML material type (#120) - apply feedback…
danlishka 09e4779
feat: Add support fot JUnit XML material type (#120) - apply feedback…
danlishka dc3800a
feat: Add support fot JUnit XML material type (#120) - apply feedback…
danlishka a77e7cf
feat: Add support fot JUnit XML material type (#120) - apply feedback…
danlishka 101da0a
feat: Add support fot JUnit XML material type (#120) - apply feedback…
danlishka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 7 additions & 1 deletion
8
app/controlplane/api/gen/frontend/workflowcontract/v1/crafting_schema.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
24 changes: 14 additions & 10 deletions
24
app/controlplane/api/workflowcontract/v1/crafting_schema.pb.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// | ||
// Copyright 2023 The Chainloop Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package materials | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"encoding/xml" | ||
|
||
api "github.com/chainloop-dev/chainloop/app/cli/api/attestation/v1" | ||
schemaapi "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1" | ||
"github.com/chainloop-dev/chainloop/internal/casclient" | ||
junit "github.com/joshdk/go-junit" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
type JUnitXMLCrafter struct { | ||
*crafterCommon | ||
uploader casclient.Uploader | ||
} | ||
|
||
func NewJUnitXMLCrafter(schema *schemaapi.CraftingSchema_Material, uploader casclient.Uploader, l *zerolog.Logger) (*JUnitXMLCrafter, error) { | ||
if schema.Type != schemaapi.CraftingSchema_Material_JUNIT_XML { | ||
return nil, fmt.Errorf("material type is not JUnit XML") | ||
} | ||
craftCommon := &crafterCommon{logger: l, input: schema} | ||
return &JUnitXMLCrafter{uploader: uploader, crafterCommon: craftCommon}, nil | ||
} | ||
|
||
func (i *JUnitXMLCrafter) Craft(ctx context.Context, filePath string) (*api.Attestation_Material, error) { | ||
if err := i.validate(filePath); err != nil { | ||
return nil, err | ||
} | ||
|
||
return uploadAndCraft(ctx, i.input, i.uploader, filePath) | ||
} | ||
|
||
func (i *JUnitXMLCrafter) validate(filePath string) error { | ||
f, err := os.Open(filePath) | ||
if err != nil { | ||
return fmt.Errorf("can't open the file: %w", err) | ||
} | ||
defer f.Close() | ||
|
||
bytes, err := io.ReadAll(f) | ||
if err != nil { | ||
return fmt.Errorf("can't read the file: %w", err) | ||
} | ||
danlishka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if err := xml.Unmarshal(bytes, &junit.Suite{}); err != nil { | ||
return fmt.Errorf("invalid JUnit XML file: %w", ErrInvalidMaterialType) | ||
} | ||
|
||
_, err = junit.IngestReader(f) | ||
if err != nil { | ||
i.logger.Debug().Err(err).Msgf("error decoding file: %s", filePath) | ||
return fmt.Errorf("invalid JUnit XML file: %w", ErrInvalidMaterialType) | ||
} | ||
|
||
return nil | ||
} |
130 changes: 130 additions & 0 deletions
130
internal/attestation/crafter/materials/junit_xml_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// | ||
// Copyright 2023 The Chainloop Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package materials_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
attestationApi "github.com/chainloop-dev/chainloop/app/cli/api/attestation/v1" | ||
contractAPI "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1" | ||
"github.com/chainloop-dev/chainloop/internal/attestation/crafter/materials" | ||
"github.com/chainloop-dev/chainloop/internal/casclient" | ||
mUploader "github.com/chainloop-dev/chainloop/internal/casclient/mocks" | ||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewJUnitXMLCrafter(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input *contractAPI.CraftingSchema_Material | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "happy path", | ||
input: &contractAPI.CraftingSchema_Material{ | ||
Type: contractAPI.CraftingSchema_Material_JUNIT_XML, | ||
}, | ||
}, | ||
{ | ||
name: "wrong type", | ||
input: &contractAPI.CraftingSchema_Material{ | ||
Type: contractAPI.CraftingSchema_Material_CONTAINER_IMAGE, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
_, err := materials.NewJUnitXMLCrafter(tc.input, nil, nil) | ||
if tc.wantErr { | ||
assert.Error(t, err) | ||
return | ||
} | ||
|
||
assert.NoError(t, err) | ||
}) | ||
} | ||
} | ||
func TestJUnitXMLCraft(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
filePath string | ||
wantErr string | ||
}{ | ||
{ | ||
name: "invalid path", | ||
filePath: "./testdata/non-existing.json", | ||
wantErr: "no such file or directory", | ||
}, | ||
{ | ||
name: "invalid artifact type", | ||
filePath: "./testdata/simple.txt", | ||
wantErr: "unexpected material type", | ||
}, | ||
{ | ||
name: "invalid artifact type", | ||
filePath: "./testdata/junit-invalid.xml", | ||
wantErr: "unexpected material type", | ||
}, | ||
{ | ||
name: "valid artifact type", | ||
filePath: "./testdata/junit.xml", | ||
}, | ||
} | ||
|
||
assert := assert.New(t) | ||
schema := &contractAPI.CraftingSchema_Material{ | ||
Name: "test", | ||
Type: contractAPI.CraftingSchema_Material_JUNIT_XML, | ||
} | ||
l := zerolog.Nop() | ||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
// Mock uploader | ||
uploader := mUploader.NewUploader(t) | ||
if tc.wantErr == "" { | ||
uploader.On("UploadFile", context.TODO(), tc.filePath). | ||
Return(&casclient.UpDownStatus{ | ||
Digest: "deadbeef", | ||
Filename: "test.xml", | ||
}, nil) | ||
} | ||
|
||
crafter, err := materials.NewJUnitXMLCrafter(schema, uploader, &l) | ||
require.NoError(t, err) | ||
|
||
got, err := crafter.Craft(context.TODO(), tc.filePath) | ||
if tc.wantErr != "" { | ||
assert.ErrorContains(err, tc.wantErr) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
assert.Equal(contractAPI.CraftingSchema_Material_JUNIT_XML.String(), got.MaterialType.String()) | ||
assert.WithinDuration(time.Now(), got.AddedAt.AsTime(), 5*time.Second) | ||
|
||
// The result includes the digest reference | ||
assert.Equal(got.GetArtifact(), &attestationApi.Attestation_Material_Artifact{ | ||
Id: "test", Digest: "deadbeef", Name: "test.xml", | ||
}) | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.