Skip to content

Commit faae574

Browse files
committed
increase unit test coverage
1 parent c10ef12 commit faae574

5 files changed

Lines changed: 180 additions & 0 deletions

File tree

image-templates/ubuntu24-x86_64-edge-raw.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ packageRepositories:
4040
pkey: "<PUBLIC_KEY_URL>" # Uncomment and replace in real config
4141
component: "restricted"
4242

43+
# - codename: "localdeb"
44+
# path: "/data/os-image-composer/localdeb" # Uncomment and replace with local path
45+
# pkey: "[trusted=yes]" # Make sure to put trusted
46+
# component: "main" # Make sure to put main or leave empty
47+
4348
systemConfig:
4449
name: edge
4550
description: edge ubuntu image with immutable rootfs

internal/config/apt_sources_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,34 @@ func TestGenerateAptSourcesFromRepositories_NonDEBSystem(t *testing.T) {
387387
}
388388
}
389389

390+
func TestGenerateAptSourcesFromRepositories_PathOnlyRepo(t *testing.T) {
391+
template := &ImageTemplate{
392+
Target: TargetInfo{
393+
OS: "ubuntu",
394+
},
395+
PackageRepositories: []PackageRepository{
396+
{
397+
Codename: "localdeb",
398+
Path: "/data/os-image-composer/localdeb",
399+
PKey: "[trusted=yes]",
400+
Component: "main",
401+
},
402+
},
403+
SystemConfig: SystemConfig{
404+
AdditionalFiles: []AdditionalFileInfo{},
405+
},
406+
}
407+
408+
err := template.GenerateAptSourcesFromRepositories()
409+
if err != nil {
410+
t.Fatalf("GenerateAptSourcesFromRepositories() failed for path-only repo: %v", err)
411+
}
412+
413+
if len(template.SystemConfig.AdditionalFiles) != 0 {
414+
t.Errorf("expected no apt additional files for path-only repo, got %d", len(template.SystemConfig.AdditionalFiles))
415+
}
416+
}
417+
390418
func TestAddUniqueAdditionalFile(t *testing.T) {
391419
template := &ImageTemplate{
392420
SystemConfig: SystemConfig{
@@ -618,6 +646,13 @@ func TestNormalizeRepositoryPriorities(t *testing.T) {
618646
}
619647
}
620648

649+
func TestNormalizeRepositoryPriorities_NilInput(t *testing.T) {
650+
result := normalizeRepositoryPriorities(nil)
651+
if len(result) != 0 {
652+
t.Errorf("expected empty result for nil input, got len=%d", len(result))
653+
}
654+
}
655+
621656
func TestGetRepositoryName(t *testing.T) {
622657
tests := []struct {
623658
name string

internal/config/config_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3206,6 +3206,77 @@ systemConfig:
32063206
}
32073207
}
32083208

3209+
func TestPackageRepositoryYAMLParsingLocalPath(t *testing.T) {
3210+
yamlContent := `image:
3211+
name: test-local-repo-parsing
3212+
version: "1.0.0"
3213+
3214+
target:
3215+
os: ubuntu
3216+
dist: ubuntu24
3217+
arch: x86_64
3218+
imageType: raw
3219+
3220+
packageRepositories:
3221+
- codename: "localdeb"
3222+
path: "/data/os-image-composer/localdeb"
3223+
pkey: "[trusted=yes]"
3224+
component: "main"
3225+
3226+
systemConfig:
3227+
name: test
3228+
packages:
3229+
- test-package
3230+
kernel:
3231+
version: "6.12"
3232+
cmdline: "quiet"
3233+
`
3234+
3235+
tmpFile, err := os.CreateTemp("", "test-local-repo-*.yml")
3236+
if err != nil {
3237+
t.Fatalf("failed to create temp file: %v", err)
3238+
}
3239+
if err := tmpFile.Chmod(0600); err != nil {
3240+
tmpFile.Close()
3241+
os.Remove(tmpFile.Name())
3242+
return
3243+
}
3244+
defer os.Remove(tmpFile.Name())
3245+
3246+
if _, err := tmpFile.WriteString(yamlContent); err != nil {
3247+
t.Fatalf("failed to write temp file: %v", err)
3248+
}
3249+
tmpFile.Close()
3250+
3251+
template, err := LoadTemplate(tmpFile.Name(), false)
3252+
if err != nil {
3253+
t.Fatalf("failed to load YAML template with local package repository: %v", err)
3254+
}
3255+
3256+
repos := template.GetPackageRepositories()
3257+
if len(repos) != 1 {
3258+
t.Fatalf("expected 1 parsed repository, got %d", len(repos))
3259+
}
3260+
3261+
repo := template.GetRepositoryByCodename("localdeb")
3262+
if repo == nil {
3263+
t.Fatalf("expected to find localdeb repository")
3264+
}
3265+
3266+
if repo.Path != "/data/os-image-composer/localdeb" {
3267+
t.Errorf("expected repo path '/data/os-image-composer/localdeb', got '%s'", repo.Path)
3268+
}
3269+
if repo.PKey != "[trusted=yes]" {
3270+
t.Errorf("expected repo pkey '[trusted=yes]', got '%s'", repo.PKey)
3271+
}
3272+
if repo.Component != "main" {
3273+
t.Errorf("expected repo component 'main', got '%s'", repo.Component)
3274+
}
3275+
if repo.URL != "" {
3276+
t.Errorf("expected repo URL to be empty for local path repository, got '%s'", repo.URL)
3277+
}
3278+
}
3279+
32093280
func TestPackageRepositoriesWithDuplicateCodenames(t *testing.T) {
32103281
repos := []PackageRepository{
32113282
{Codename: "duplicate", URL: "https://first.com", PKey: "https://first.com/key.pub"},

internal/config/validate/validate_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,3 +686,45 @@ systemConfig:
686686
t.Errorf("expected template with URL pkey to pass validation, but got: %v", err)
687687
}
688688
}
689+
690+
// TestPackageRepositoryWithLocalPath validates that a local path repository
691+
// with [trusted=yes] is accepted by schema validation.
692+
func TestPackageRepositoryWithLocalPath(t *testing.T) {
693+
templateYAML := `image:
694+
name: test-local-path-repo
695+
version: "1.0.0"
696+
697+
target:
698+
os: ubuntu
699+
dist: ubuntu24
700+
arch: x86_64
701+
imageType: raw
702+
703+
packageRepositories:
704+
- codename: "localdeb"
705+
path: "/data/os-image-composer/localdeb"
706+
pkey: "[trusted=yes]"
707+
component: "main"
708+
709+
systemConfig:
710+
name: test
711+
packages:
712+
- test-package
713+
kernel:
714+
version: "6.14"
715+
`
716+
717+
var raw interface{}
718+
if err := yaml.Unmarshal([]byte(templateYAML), &raw); err != nil {
719+
t.Fatalf("YAML parsing error: %v", err)
720+
}
721+
722+
dataJSON, err := json.Marshal(raw)
723+
if err != nil {
724+
t.Fatalf("JSON marshaling error: %v", err)
725+
}
726+
727+
if err := ValidateImageTemplateJSON(dataJSON); err != nil {
728+
t.Errorf("expected template with local path repo to pass validation, but got: %v", err)
729+
}
730+
}

internal/provider/ubuntu/ubuntu_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,3 +2194,30 @@ func TestBuildUserRepoListFieldMapping(t *testing.T) {
21942194
t.Errorf("ID: expected %q, got %q", expectedID, r.ID)
21952195
}
21962196
}
2197+
2198+
func TestBuildUserRepoListSkipsPathOnlyRepos(t *testing.T) {
2199+
input := []config.PackageRepository{
2200+
{
2201+
Codename: "localdeb",
2202+
Path: "/data/os-image-composer/localdeb",
2203+
PKey: "[trusted=yes]",
2204+
},
2205+
{
2206+
URL: "https://example.com/repo",
2207+
Codename: "noble",
2208+
PKey: "https://example.com/key.gpg",
2209+
},
2210+
}
2211+
2212+
result := buildUserRepoList(input)
2213+
if len(result) != 1 {
2214+
t.Fatalf("expected 1 URL-based repo, got %d", len(result))
2215+
}
2216+
2217+
if result[0].URL != "https://example.com/repo" {
2218+
t.Errorf("expected URL repo to be retained, got %q", result[0].URL)
2219+
}
2220+
if result[0].Codename != "noble" {
2221+
t.Errorf("expected codename %q, got %q", "noble", result[0].Codename)
2222+
}
2223+
}

0 commit comments

Comments
 (0)