Skip to content

Commit

Permalink
test: include tests (e2e) for policy
Browse files Browse the repository at this point in the history
Signed-off-by: Kairo Araujo <[email protected]>
  • Loading branch information
kairoaraujo committed May 2, 2024
1 parent 3178ffb commit 0ab4698
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
79 changes: 43 additions & 36 deletions cmd/archivistactl/cmd/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 The Archivista Contributors
// Copyright 2023-2024 The Archivista Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -76,7 +76,7 @@ func (e2e *E2EStoreSuite) Test_E2E() {
// define test cases struct
type testCases struct {
name string
attestation string // files are stored in test/
envelope string // files are stored in test/
sha256 string
expectedStore string
gitoidStore string // this value is added during `activistactl store command`
Expand All @@ -89,7 +89,7 @@ func (e2e *E2EStoreSuite) Test_E2E() {
testTable := []testCases{
{
name: "valid build attestation",
attestation: "../../../test/build.attestation.json",
envelope: "../../../test/build.attestation.json",
sha256: "423da4cff198bbffbe3220ed9510d32ba96698e4b1f654552521d1f541abb6dc",
expectedStore: "stored with gitoid",
expectedSearch: "Collection name: build",
Expand All @@ -98,48 +98,53 @@ func (e2e *E2EStoreSuite) Test_E2E() {
{
name: "valid package attestation",
sha256: "10cbf0f3d870934921276f669ab707983113f929784d877f1192f43c581f2070",
attestation: "../../../test/package.attestation.json",
envelope: "../../../test/package.attestation.json",
expectedStore: "stored with gitoid",
expectedSearch: "Collection name: package",
expectedRetrieveSub: "Name: https://witness.dev/attestations/git/v0.1/commithash:be20100af602c780deeef50c54f5338662ce917c",
},
{
name: "duplicated package attestation",
sha256: "10cbf0f3d870934921276f669ab707983113f929784d877f1192f43c581f2070",
attestation: "../../../test/package.attestation.json",
envelope: "../../../test/package.attestation.json",
expectedStore: "",
expectedSearch: "Collection name: package",
expectedError: "uplicate",
},
{
name: "fail attestation",
attestation: "../../../test/fail.attestation.json",
envelope: "../../../test/fail.attestation.json",
sha256: "5e8c57df8ae58fe9a29b29f9993e2fc3b25bd75eb2754f353880bad4b9ebfdb3",
expectedStore: "stored with gitoid",
expectedSearch: "",
expectedRetrieveSub: "Name: https://witness.dev/attestations/git/v0.1/parenthash:aa35c1f4b1d41c87e139c2d333f09117fd0daf4f",
},
{
name: "invalid payload attestation",
attestation: "../../../test/invalid_payload.attestation.json",
envelope: "../../../test/invalid_payload.attestation.json",
sha256: "5e8c57df8ae58fe9a29b29f9993e2fc3b25bd75eb2754f353880bad4b9ebfdb3",
expectedStore: "stored with gitoid",
expectedSearch: "",
expectedError: "value is less than the required length",
},
{
name: "nonexistent payload file",
attestation: "../../../test/missing.attestation.json",
envelope: "../../../test/missing.attestation.json",
expectedError: "no such file or directory",
},
{
name: "valid signed policy",
envelope: "../../../test/policy-signed.json",
expectedStore: "stored with gitoid",
},
}
for _, test := range testTable {
// test `archivistactl store`
e2e.T().Log("Test `archivistactl store` " + test.name)
storeOutput := bytes.NewBufferString("")
rootCmd.SetOut(storeOutput)
rootCmd.SetErr(storeOutput)
rootCmd.SetArgs([]string{"store", test.attestation})
rootCmd.SetArgs([]string{"store", test.envelope})
err := rootCmd.Execute()
if err != nil {
// if return error assert if is expected error from test case
Expand All @@ -152,35 +157,37 @@ func (e2e *E2EStoreSuite) Test_E2E() {
}

// test `archivistactl search`
e2e.T().Log("Test `archivistactl search`" + test.name)
searchOutput := bytes.NewBufferString("")
rootCmd.SetOut(searchOutput)
rootCmd.SetErr(searchOutput)
rootCmd.SetArgs([]string{"search", "sha256:" + test.sha256})
err = rootCmd.Execute()
if err != nil {
e2e.FailNow(err.Error())
}
searchActual := searchOutput.String()
e2e.Contains(searchActual, test.expectedSearch)

if test.expectedRetrieveSub != "" {
// test `archivistactl retrieve subjects`
e2e.T().Log("Test `archivistactl retrieve subjects` " + test.name)
subjectsOutput := bytes.NewBufferString("")
rootCmd.SetOut(subjectsOutput)
rootCmd.SetErr(subjectsOutput)
rootCmd.SetArgs([]string{"retrieve", "subjects", test.gitoidStore})
if test.sha256 == "" {
e2e.T().Log("Test `archivistactl search`" + test.name)
searchOutput := bytes.NewBufferString("")
rootCmd.SetOut(searchOutput)
rootCmd.SetErr(searchOutput)
rootCmd.SetArgs([]string{"search", "sha256:" + test.sha256})
err = rootCmd.Execute()
if err != nil {
e2e.FailNow(err.Error())
}
subjectsActual := subjectsOutput.String()
e2e.Contains(subjectsActual, test.expectedRetrieveSub)
if test.name == "fail attestation" {
e2e.NotContains(subjectsActual, "sha256:"+test.sha256)
} else {
e2e.Contains(subjectsActual, "sha256:"+test.sha256)
searchActual := searchOutput.String()
e2e.Contains(searchActual, test.expectedSearch)

if test.expectedRetrieveSub != "" {
// test `archivistactl retrieve subjects`
e2e.T().Log("Test `archivistactl retrieve subjects` " + test.name)
subjectsOutput := bytes.NewBufferString("")
rootCmd.SetOut(subjectsOutput)
rootCmd.SetErr(subjectsOutput)
rootCmd.SetArgs([]string{"retrieve", "subjects", test.gitoidStore})
err = rootCmd.Execute()
if err != nil {
e2e.FailNow(err.Error())
}
subjectsActual := subjectsOutput.String()
e2e.Contains(subjectsActual, test.expectedRetrieveSub)
if test.name == "fail attestation" {
e2e.NotContains(subjectsActual, "sha256:"+test.sha256)
} else {
e2e.Contains(subjectsActual, "sha256:"+test.sha256)
}
}
}
if test.expectedError == "" {
Expand All @@ -195,8 +202,8 @@ func (e2e *E2EStoreSuite) Test_E2E() {
if err != nil {
e2e.FailNow(err.Error())
}
// compares file attestation with the retrieved attestation
fileAtt, err := os.ReadFile(test.attestation)
// compares file envelope with the retrieved envelope
fileAtt, err := os.ReadFile(test.envelope)
if err != nil {
e2e.FailNow(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion internal/metadatastorage/sqlstore/store.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 The Archivista Contributors
// Copyright 2022-2024 The Archivista Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions test/policy-signed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"payload":"ewogICJleHBpcmVzIjogIjIwMjUtMTItMTdUMjM6NTc6NDAtMDU6MDAiLAogICJyZXBvc2l0b3JpZXMiOiB7CiAgICAiZ2l0IjogWwogICAgICAiZ2l0aHViLmNvbS9rYWlyb2FyYXVqby93aXRuZXNzLmdpdCIsCiAgICAgICJnaXRodWIuY29tL2luLXRvdG8vd2l0bmVzcy5naXQiCiAgICBdCiAgfSwKICAic3RlcHMiOiB7CiAgICAiYnVpbGQiOiB7CiAgICAgICJuYW1lIjogImJ1aWxkIiwKICAgICAgImF0dGVzdGF0aW9ucyI6IFsKICAgICAgICB7CiAgICAgICAgICAidHlwZSI6ICJodHRwczovL3dpdG5lc3MuZGV2L2F0dGVzdGF0aW9ucy9tYXRlcmlhbC92MC4xIiwKICAgICAgICAgICJyZWdvcG9saWNpZXMiOiBbXQogICAgICAgIH0sCiAgICAgICAgewogICAgICAgICAgInR5cGUiOiAiaHR0cHM6Ly93aXRuZXNzLmRldi9hdHRlc3RhdGlvbnMvY29tbWFuZC1ydW4vdjAuMSIsCiAgICAgICAgICAicmVnb3BvbGljaWVzIjogWwogICAgICAgICAgICB7CiAgICAgICAgICAgICAgIm5hbWUiOiAiZXhwZWN0ZWQgY29tbWFuZCIsCiAgICAgICAgICAgICAgIm1vZHVsZSI6ICJjR0ZqYTJGblpTQmpiMjF0WVc1a2NuVnVMbU50WkFvS1pHVnVlVnR0YzJkZElIc0tDV2x1Y0hWMExtTnRaQ0FoUFNCYkltZHZJaXdnSW1KMWFXeGtJaXdnSWkxdlBYUmxjM1JoY0hBaUxDQWlMaUpkQ2dsdGMyY2dPajBnSW5WdVpYaHdaV04wWldRZ1kyMWtJZ3A5Q2c9PSIKICAgICAgICAgICAgfQogICAgICAgICAgXQogICAgICAgIH0sCiAgICAgICAgewogICAgICAgICAgInR5cGUiOiAiaHR0cHM6Ly93aXRuZXNzLmRldi9hdHRlc3RhdGlvbnMvcHJvZHVjdC92MC4xIiwKICAgICAgICAgICJyZWdvcG9saWNpZXMiOiBbXQogICAgICAgIH0KICAgICAgXSwKICAgICAgImZ1bmN0aW9uYXJpZXMiOiBbCiAgICAgICAgewogICAgICAgICAgInR5cGUiOiAicHVibGlja2V5IiwKICAgICAgICAgICJwdWJsaWNrZXlpZCI6ICJhZTJkY2M5ODllYTljMTA5YTM2ZThlYmE1YzRiYzE2ZDhmYWZjZmU4ZTFhNjE0MTY0NjcwZDUwYWVkYWNkNjQ3IgogICAgICAgIH0KICAgICAgXQogICAgfSwKICAgICJwYWNrYWdlIjogewogICAgICAibmFtZSI6ICJwYWNrYWdlIiwKICAgICAgImF0dGVzdGF0aW9ucyI6IFsKICAgICAgICB7CiAgICAgICAgICAidHlwZSI6ICJodHRwczovL3dpdG5lc3MuZGV2L2F0dGVzdGF0aW9ucy9tYXRlcmlhbC92MC4xIiwKICAgICAgICAgICJyZWdvcG9saWNpZXMiOiBbXQogICAgICAgIH0sCiAgICAgICAgewogICAgICAgICAgInR5cGUiOiAiaHR0cHM6Ly93aXRuZXNzLmRldi9hdHRlc3RhdGlvbnMvY29tbWFuZC1ydW4vdjAuMSIsCiAgICAgICAgICAicmVnb3BvbGljaWVzIjogWwogICAgICAgICAgICB7CiAgICAgICAgICAgICAgIm5hbWUiOiAiZXhwZWN0ZWQgY29tbWFuZCIsCiAgICAgICAgICAgICAgIm1vZHVsZSI6ICJjR0ZqYTJGblpTQmpiMjF0WVc1a2NuVnVMbU50WkFvS1pHVnVlVnR0YzJkZElIc0tDV2x1Y0hWMExtTnRaQ0FoUFNCYkluUmhjaUlzSUNKamVtWWlMQ0FpTGk5MFpYTjBZWEJ3TG5SaGNpNTBaM29pTENBaUxpOTBaWE4wWVhCd0lsMEtDVzF6WnlBNlBTQWlkVzVsZUhCbFkzUmxaQ0JqYldRaUNuMEsiCiAgICAgICAgICAgIH0KICAgICAgICAgIF0KICAgICAgICB9LAogICA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plVkZtVFdncmVraExlRE5aVjNWblpqTnZiakF5Y0hRS1NHVjZUbFpqUVRZclZrUTJXbkpwTkdWcFpFa3JhbkJqYW1kT2VrMTZiblJ6U1dRNFJGcFVkV1ZQUlhWSGRVWkZZMU1yVGxGbk5uaHRSelF6VkhSdE5ncHdWbXd6YWtGaWRFVkJiRXd6ZVVwUGFURjRiMU0xWm0xMmJGTlBUbFZFYm1oWWNreExOa3BYVG5oNllVMDRSbEJGVm5kemJYUkxjWGRvYVM4NFRrNHJDbXBDVFhwcVJFdGFRbWRxT1hGdWVrSlhTSGRPTld4alEwRjNSVUZCVVQwOUNpMHRMUzB0UlU1RUlGQlZRa3hKUXlCTFJWa3RMUzB0TFFvPSIKICAgIH0KICB9Cn0=","payloadType":"https://witness.testifysec.com/policy/v0.1","signatures":[{"keyid":"ae2dcc989ea9c109a36e8eba5c4bc16d8fafcfe8e1a614164670d50aedacd647","sig":"azCV12o2L3AABWc/kZhF6dAb5kRYct3S0rJcOzU8OVvPaIqRhjmPeafS2Z0h6J0Lyh9WjAAlxvuy1qBFllA1CA=="}]}

0 comments on commit 0ab4698

Please sign in to comment.