| 1 |
// Copyright 2024 The Archivista Contributors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package artifactstore |
| 16 |
|
| 17 |
import ( |
| 18 |
"os" |
| 19 |
"path/filepath" |
| 20 |
"testing" |
| 21 |
|
| 22 |
"github.com/stretchr/testify/assert" |
| 23 |
"github.com/stretchr/testify/require" |
| 24 |
) |
| 25 |
|
| 26 |
func createTestConfigFile(t *testing.T, workingDir, version, distroFilePath, distroDigest string) string { |
| 27 |
testConfig := `artifacts: |
| 28 |
witness: |
| 29 |
versions: |
| 30 |
` + version + `: |
| 31 |
description: test |
| 32 |
distributions: |
| 33 |
linux: |
| 34 |
filelocation: ` + distroFilePath + ` |
| 35 |
sha256digest: ` + distroDigest |
| 36 |
testConfigFilePath := filepath.Join(workingDir, "config.yaml") |
| 37 |
testConfigFile, err := os.Create(testConfigFilePath) |
| 38 |
require.NoError(t, err) |
| 39 |
_, err = testConfigFile.WriteString(testConfig) |
| 40 |
require.NoError(t, err) |
| 41 |
require.NoError(t, testConfigFile.Close()) |
| 42 |
return testConfigFilePath |
| 43 |
} |
| 44 |
|
| 45 |
func TestStore(t *testing.T) { |
| 46 |
workingDir := t.TempDir() |
| 47 |
testDistroFilePath := filepath.Join(workingDir, "test") |
| 48 |
testDistroDigest := "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" |
| 49 |
testVersion := "v0.1.0" |
| 50 |
testDistroFile, err := os.Create(testDistroFilePath) |
| 51 |
require.NoError(t, err) |
| 52 |
_, err = testDistroFile.Write([]byte("test")) |
| 53 |
require.NoError(t, err) |
| 54 |
require.NoError(t, testDistroFile.Close()) |
| 55 |
|
| 56 |
t.Run("all good", func(t *testing.T) { |
| 57 |
testArtifactName := "witness" |
| 58 |
testDistroName := "linux" |
| 59 |
testConfigFilePath := createTestConfigFile(t, workingDir, testVersion, testDistroFilePath, testDistroDigest) |
| 60 |
as, err := New(WithConfigFile(testConfigFilePath)) |
| 61 |
require.NoError(t, err) |
| 62 |
artifacts := as.Artifacts() |
| 63 |
assert.Len(t, artifacts, 1) |
| 64 |
versions, ok := as.Versions(testArtifactName) |
| 65 |
assert.True(t, ok) |
| 66 |
assert.Len(t, versions, 1) |
| 67 |
version, ok := as.Version(testArtifactName, testVersion) |
| 68 |
assert.True(t, ok) |
| 69 |
assert.Len(t, version.Distributions, 1) |
| 70 |
testDistro, ok := as.Distribution(testArtifactName, testVersion, testDistroName) |
| 71 |
assert.True(t, ok) |
| 72 |
assert.Equal(t, testDistro.FileLocation, testDistroFilePath) |
| 73 |
assert.Equal(t, testDistro.SHA256Digest, testDistroDigest) |
| 74 |
}) |
| 75 |
|
| 76 |
t.Run("wrong file path", func(t *testing.T) { |
| 77 |
testConfigFilePath := createTestConfigFile(t, workingDir, testVersion, "garbage", testDistroDigest) |
| 78 |
_, err := New(WithConfigFile(testConfigFilePath)) |
| 79 |
assert.Error(t, err) |
| 80 |
|
| 81 |
}) |
| 82 |
|
| 83 |
t.Run("wrong file digest", func(t *testing.T) { |
| 84 |
testConfigFilePath := createTestConfigFile(t, workingDir, testVersion, testDistroFilePath, "garbage") |
| 85 |
_, err := New(WithConfigFile(testConfigFilePath)) |
| 86 |
assert.Error(t, err) |
| 87 |
}) |
| 88 |
} |