Skip to content
This repository was archived by the owner on Dec 3, 2024. It is now read-only.

Commit 4a82bb9

Browse files
committed
feat: add version checker test
1 parent 84c0b11 commit 4a82bb9

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

tuf/version.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ func (e *InvalidVersionError) Error() string {
3333
}
3434

3535
func NewDefaultVersionChecker() *DefaultVersionChecker {
36-
return &DefaultVersionChecker{}
36+
return &DefaultVersionChecker{
37+
VersionFetcher: version.NewGoVersionFetcher(),
38+
}
3739
}
3840

39-
type DefaultVersionChecker struct{}
41+
type DefaultVersionChecker struct {
42+
VersionFetcher version.Fetcher
43+
}
4044

4145
func (vc *DefaultVersionChecker) CheckVersion(client Downloader) error {
42-
fetcher := version.NewGoVersionFetcher()
43-
attestVersion, err := fetcher.Get()
46+
attestVersion, err := vc.VersionFetcher.Get()
4447
if err != nil {
4548
return fmt.Errorf("failed to get version: %w", err)
4649
}

tuf/version_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package tuf
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/Masterminds/semver/v3"
10+
"github.com/docker/attest/internal/test"
11+
"github.com/docker/attest/version"
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
const (
16+
invalidVersion = "0.0.1"
17+
validVersion = "v1.0.0-0"
18+
versionConstraint = ">=v1.0.0-0"
19+
)
20+
21+
func TestDefaultVersionChecker(t *testing.T) {
22+
testDir := test.CreateTempDir(t, "", "tuf_temp")
23+
versionConstraintsPath := filepath.Join(testDir, "version-constraints")
24+
err := os.WriteFile(versionConstraintsPath, []byte(versionConstraint), 0o600)
25+
assert.NoError(t, err)
26+
tufClient := NewMockTufClient(testDir)
27+
28+
expectedError := fmt.Sprintf("%s version %s does not satisfy constraints %s: %s is less than %s", version.ThisModulePath, invalidVersion, versionConstraint, invalidVersion, validVersion)
29+
30+
testCases := []struct {
31+
name string
32+
expectedError string
33+
version string
34+
}{
35+
{name: "version is less than the minimum", expectedError: expectedError, version: "0.0.1"},
36+
{name: "version is equal to the minimum", version: "1.0.0"},
37+
{name: "version is greater than the minimum", version: "1.0.1"},
38+
}
39+
40+
for _, tc := range testCases {
41+
t.Run(tc.name, func(t *testing.T) {
42+
checker := NewDefaultVersionChecker()
43+
checker.VersionFetcher = &MockVersionFetcher{version: tc.version}
44+
err := checker.CheckVersion(tufClient)
45+
if tc.expectedError != "" {
46+
assert.Error(t, err)
47+
assert.Equal(t, tc.expectedError, err.Error())
48+
return
49+
}
50+
assert.NoError(t, err)
51+
})
52+
}
53+
}
54+
55+
type MockVersionFetcher struct {
56+
version string
57+
}
58+
59+
func (m *MockVersionFetcher) Get() (*semver.Version, error) {
60+
return semver.NewVersion(m.version)
61+
}

0 commit comments

Comments
 (0)