Refactor package types to use dedicated schema types per package type (NPM, PyPI, NuGet, OCI, MCPB) (#634)

This commit is contained in:
Radoslav Dimitrov 2025-10-11 10:53:45 +03:00 • committed by GitHub
commit 632cbce815
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -138,11 +138,10 @@ func TestValidateOCI_RealPackages(t *testing.T) {
func TestValidateOCI_UnsupportedRegistry(t *testing.T) {
ctx := context.Background()
// Test with unsupported registry in canonical reference format
pkg := model.Package{
RegistryType: model.RegistryTypeOCI,
RegistryBaseURL: "https://unsupported-registry.com",
Identifier: "test/image",
Version: "latest",
RegistryType: model.RegistryTypeOCI,
Identifier: "unsupported-registry.com/test/image:latest",
}
err := registries.ValidateOCI(ctx, pkg, "com.example/test")
@ -155,34 +154,32 @@ func TestValidateOCI_SupportedRegistries(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
registryURL string
expected bool
name string
identifier string
expected bool
}{
{
name: "Docker Hub should be supported",
registryURL: model.RegistryURLDocker,
expected: true,
name: "Docker Hub should be supported",
identifier: "docker.io/test/image:latest",
expected: true,
},
{
name: "GHCR should be supported",
registryURL: model.RegistryURLGHCR,
expected: true,
name: "GHCR should be supported",
identifier: "ghcr.io/test/image:latest",
expected: true,
},
{
name: "Unsupported registry should fail",
registryURL: "https://quay.io",
expected: false,
name: "Unsupported registry should fail",
identifier: "quay.io/test/image:latest",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pkg := model.Package{
RegistryType: model.RegistryTypeOCI,
RegistryBaseURL: tt.registryURL,
Identifier: "test/image",
Version: "latest",
RegistryType: model.RegistryTypeOCI,
Identifier: tt.identifier,
}
err := registries.ValidateOCI(ctx, pkg, "com.example/test")
@ -199,3 +196,65 @@ func TestValidateOCI_SupportedRegistries(t *testing.T) {
})
}
}
func TestValidateOCI_RejectsOldFormat(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
pkg model.Package
errorMessage string
}{
{
name: "OCI package with registryBaseUrl should be rejected",
pkg: model.Package{
RegistryType: model.RegistryTypeOCI,
RegistryBaseURL: "https://docker.io",
Identifier: "docker.io/test/image:latest",
},
errorMessage: "OCI packages must not have 'registryBaseUrl' field",
},
{
name: "OCI package with version field should be rejected",
pkg: model.Package{
RegistryType: model.RegistryTypeOCI,
Identifier: "docker.io/test/image:latest",
Version: "1.0.0",
},
errorMessage: "OCI packages must not have 'version' field",
},
{
name: "OCI package with both old format fields should fail on registryBaseUrl first",
pkg: model.Package{
RegistryType: model.RegistryTypeOCI,
RegistryBaseURL: "https://docker.io",
Identifier: "test/image",
Version: "1.0.0",
},
errorMessage: "OCI packages must not have 'registryBaseUrl' field",
},
{
name: "OCI package with canonical format should pass old format validation",
pkg: model.Package{
RegistryType: model.RegistryTypeOCI,
Identifier: "docker.io/test/image:latest",
},
errorMessage: "", // Should pass old format check (will fail later due to image not existing)
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := registries.ValidateOCI(ctx, tt.pkg, "com.example/test")
if tt.errorMessage != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.errorMessage)
} else if err != nil {
// Should not fail with old format error (may fail with other errors like image not found)
assert.NotContains(t, err.Error(), "must not have 'registryBaseUrl'")
assert.NotContains(t, err.Error(), "must not have 'version'")
}
})
}
}