Skip to content

Commit d7dfac4

Browse files
committed
Add tests for core template ubuntu-jammy and invalid image. Better errs.
1 parent f8989ad commit d7dfac4

File tree

3 files changed

+91
-18
lines changed

3 files changed

+91
-18
lines changed

pkg/core/image/coretmpl_test.go

+59-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,70 @@
11
package image
22

33
import (
4-
"fmt"
54
"testing"
65
)
76

8-
func BasicTestCoreTemplateOSValidImage(t *testing.T) {
7+
func TestCoreTemplateUbuntuJammy(t *testing.T) {
98
validCoreTemplateOS, err := newCoreTemplateOS("ubuntu-jammy")
109
if err != nil {
1110
t.Errorf("Error creating core template OS: %s", err)
1211
}
13-
fmt.Println(validCoreTemplateOS)
12+
if validCoreTemplateOS.name != "ubuntu-jammy" {
13+
t.Errorf("Expected core template OS name to be ubuntu-jammy, got %s", validCoreTemplateOS.name)
14+
}
15+
if len(validCoreTemplateOS.rootScripts) == 0 {
16+
t.Errorf("Expected core template OS rootScripts to be non-empty, got empty")
17+
}
18+
// Check that all predefined root scripts exist in the FS
19+
for _, fileDir := range validCoreTemplateOS.rootScripts {
20+
_, err := coreTemplatesFS.ReadFile(fileDir)
21+
if err != nil {
22+
t.Errorf("Error reading root script %s: %s", fileDir, err)
23+
}
24+
}
25+
26+
if len(validCoreTemplateOS.patches) == 0 {
27+
t.Errorf("Expected core template OS patches to be non-empty, got empty")
28+
}
29+
30+
if validCoreTemplateOS.dockerfile == "" {
31+
t.Errorf("Expected core template OS dockerfile to be non-empty, got empty")
32+
}
33+
_, err = coreTemplatesFS.ReadFile(validCoreTemplateOS.dockerfile)
34+
if err != nil {
35+
t.Errorf("Error reading Dockerfile %s: %s", validCoreTemplateOS.dockerfile, err)
36+
}
37+
38+
if len(validCoreTemplateOS.commonModules) == 0 {
39+
t.Errorf("Expected core template OS commonModules to be non-empty, got empty")
40+
}
41+
42+
for module, fileDir := range validCoreTemplateOS.commonModules {
43+
_, err := coreTemplatesFS.ReadFile(fileDir)
44+
if err != nil {
45+
t.Errorf("Error reading common module %s: %s", module, err)
46+
}
47+
}
48+
49+
if len(validCoreTemplateOS.modules) == 0 {
50+
t.Errorf("Expected core template OS modules to be non-empty, got empty")
51+
}
52+
53+
for module, fileDir := range validCoreTemplateOS.modules {
54+
_, err := coreTemplatesFS.ReadFile(fileDir)
55+
if err != nil {
56+
t.Errorf("Error reading module %s: %s", module, err)
57+
}
58+
}
59+
60+
}
61+
62+
func TestCoreTemplateInvalidImage(t *testing.T) {
63+
_, err := newCoreTemplateOS("nonexistent-os")
64+
if err == nil {
65+
t.Errorf("Expected error creating core template OS, got nil")
66+
}
67+
if _, ok := err.(*ErrCoreTemplateOSNotFound); !ok {
68+
t.Errorf("Expected error type ErrCoreTemplateOSNotFound, got %T", err)
69+
}
1470
}

pkg/core/image/errors.go

+22-13
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,46 @@ package image
22

33
import "desktopus/pkg/core/utils"
44

5-
// Specific error types
5+
// Specific error types embedding DesktopusError
66
type ErrCoreTemplateReadingIndexMeta struct {
7-
utils.DesktopusError
7+
*utils.DesktopusError
8+
}
9+
10+
func (e *ErrCoreTemplateReadingIndexMeta) Error() string {
11+
return e.DesktopusError.Error()
812
}
913

1014
type ErrCoreTemplateReadingCoreMeta struct {
11-
utils.DesktopusError
15+
*utils.DesktopusError
16+
}
17+
18+
func (e *ErrCoreTemplateReadingCoreMeta) Error() string {
19+
return e.DesktopusError.Error()
1220
}
1321

1422
type ErrCoreTemplateOSNotFound struct {
15-
utils.DesktopusError
23+
*utils.DesktopusError
24+
}
25+
26+
func (e *ErrCoreTemplateOSNotFound) Error() string {
27+
return e.DesktopusError.Error()
1628
}
1729

1830
// Functions to create specific errors with parameters
1931
func newErrCoreTemplateReadingIndexMeta(errMessage string) error {
20-
return &utils.DesktopusError{
21-
Message: "error reading core template index metadata: %s",
22-
Params: []interface{}{errMessage},
32+
return &ErrCoreTemplateReadingIndexMeta{
33+
DesktopusError: utils.NewDesktopusError("error reading core templates index metadata: %s", errMessage),
2334
}
2435
}
2536

2637
func newErrCoreTemplateReadingCoreMeta(os string, errMessage string) error {
27-
return &utils.DesktopusError{
28-
Message: "error reading OS %s metadata: %s",
29-
Params: []interface{}{os, errMessage},
38+
return &ErrCoreTemplateReadingCoreMeta{
39+
DesktopusError: utils.NewDesktopusError("error reading OS %s metadata: %s", os, errMessage),
3040
}
3141
}
3242

3343
func newErrCoreTemplateOSNotFound(name string) error {
34-
return &utils.DesktopusError{
35-
Message: "error: OS %s not found in core templates",
36-
Params: []interface{}{name},
44+
return &ErrCoreTemplateOSNotFound{
45+
DesktopusError: utils.NewDesktopusError("error: OS %s not found in core templates", name),
3746
}
3847
}

pkg/core/utils/error.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@ import (
44
"fmt"
55
)
66

7-
// BaseError is the base struct for all custom errors
7+
// DesktopusError is the base struct for all custom errors
88
type DesktopusError struct {
99
Message string
1010
Params []interface{}
1111
}
1212

13-
// Error implements the error interface for BaseError
13+
// Error implements the error interface for DesktopusError
1414
func (e *DesktopusError) Error() string {
1515
return fmt.Sprintf(e.Message, e.Params...)
1616
}
17+
18+
// NewDesktopusError creates a new DesktopusError with the given message and parameters
19+
func NewDesktopusError(message string, params ...interface{}) *DesktopusError {
20+
return &DesktopusError{
21+
Message: message,
22+
Params: params,
23+
}
24+
}

0 commit comments

Comments
 (0)