Skip to content

Commit

Permalink
comments applied
Browse files Browse the repository at this point in the history
  • Loading branch information
michalpristas committed Sep 19, 2023
1 parent a75f74a commit 230126e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,14 @@ func (v *Verifier) verifyAsc(fullPath string, skipDefaultPgp bool, pgpSources ..
continue
}
raw, err := download.PgpBytesFromSource(check, v.client)
if errors.Is(err, download.ErrRemotePGPDownloadFailed) {
v.log.Warnf("Skipped remote PGP located at %q because it's unavailable: %v", strings.TrimPrefix(check, download.PgpSourceURIPrefix), err)
continue
}
if err != nil {
if errors.Is(err, download.ErrRemotePGPDownloadFailed) {
v.log.Warnf("Skipped remote PGP located at %q because it's unavailable: %v", strings.TrimPrefix(check, download.PgpSourceURIPrefix), err)
continue
}
return err
}

if len(raw) == 0 {
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ func prepareFetchVerifyTests(dropPath, targetDir, targetFilePath, hashTargetFile
func TestVerify(t *testing.T) {
log, obs := logger.NewTesting("TestVerify")
targetDir, err := ioutil.TempDir(os.TempDir(), "")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

timeout := 30 * time.Second

Expand All @@ -191,44 +189,37 @@ func TestVerify(t *testing.T) {
},
}

if err := prepareTestCase(beatSpec, version, config); err != nil {
t.Fatal(err)
}
err = prepareTestCase(beatSpec, version, config)
require.NoError(t, err)

testClient := NewDownloader(config)
artifact, err := testClient.Download(context.Background(), beatSpec, version)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

t.Cleanup(func() {
os.Remove(artifact)
os.Remove(artifact + ".sha512")
os.RemoveAll(config.DropPath)
})

_, err = os.Stat(artifact)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

testVerifier, err := NewVerifier(log, config, true, nil)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

err = testVerifier.Verify(beatSpec, version, false)
require.NoError(t, err)

// log message informing remote PGP was skipped
logs := obs.FilterMessageSnippet("Skipped remote PGP located at")
require.Equal(t, 0, logs.Len())

os.Remove(artifact)
os.Remove(artifact + ".sha512")
os.RemoveAll(config.DropPath)
require.Empty(t, logs)
}

func TestVerifyUnreachableUri(t *testing.T) {
log, obs := logger.NewTesting("TestVerifyUnreachableUri")
targetDir, err := ioutil.TempDir(os.TempDir(), "")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

timeout := 30 * time.Second

Expand All @@ -242,25 +233,24 @@ func TestVerifyUnreachableUri(t *testing.T) {
},
}

if err := prepareTestCase(beatSpec, version, config); err != nil {
t.Fatal(err)
}
err = prepareTestCase(beatSpec, version, config)
require.NoError(t, err)

testClient := NewDownloader(config)
artifact, err := testClient.Download(context.Background(), beatSpec, version)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

t.Cleanup(func() {
os.Remove(artifact)
os.Remove(artifact + ".sha512")
os.RemoveAll(config.DropPath)
})

_, err = os.Stat(artifact)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

testVerifier, err := NewVerifier(log, config, true, nil)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

unreachableURI := "https://127.0.0.1:3452/path/does/not/exist"
unreachablePGP := download.PgpSourceURIPrefix + unreachableURI
Expand All @@ -271,10 +261,6 @@ func TestVerifyUnreachableUri(t *testing.T) {
// log message informing remote PGP was skipped
logs := obs.FilterMessageSnippet(fmt.Sprintf("Skipped remote PGP located at %q", unreachableURI))
require.Equal(t, 1, logs.Len())

os.Remove(artifact)
os.Remove(artifact + ".sha512")
os.RemoveAll(config.DropPath)
}

func prepareTestCase(a artifact.Artifact, version string, cfg *artifact.Config) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,14 @@ func (v *Verifier) verifyAsc(a artifact.Artifact, version string, skipDefaultPgp
continue
}
raw, err := download.PgpBytesFromSource(check, v.client)
if errors.Is(err, download.ErrRemotePGPDownloadFailed) {
v.log.Warnf("Skipped remote PGP located at %q because it's unavailable: %v", strings.TrimPrefix(check, download.PgpSourceURIPrefix), err)
continue
}
if err != nil {
if errors.Is(err, download.ErrRemotePGPDownloadFailed) {
v.log.Warnf("Skipped remote PGP located at %q because it's unavailable: %v", strings.TrimPrefix(check, download.PgpSourceURIPrefix), err)
continue
}
return err
}

if len(raw) == 0 {
continue
}
Expand Down

0 comments on commit 230126e

Please sign in to comment.