Skip to content

Commit

Permalink
add test for import config
Browse files Browse the repository at this point in the history
  • Loading branch information
haitham911 committed Feb 9, 2025
1 parent 8516156 commit da24379
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 11 deletions.
28 changes: 22 additions & 6 deletions pkg/config/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ import (
"github.com/spf13/viper"
)

// import Resolved Paths
type ResolvedPaths struct {
filePath string // path to the resolved config file
importPaths string // import path from atmos config
}

func (cl *ConfigLoader) downloadRemoteConfig(url string, tempDir string) (string, error) {
// uniq name for the temp file
fileName := fmt.Sprintf("atmos-%d.yaml", time.Now().UnixNano())
fileName := fmt.Sprintf("atmos-import-%d.yaml", time.Now().UnixNano())
tempFile := filepath.Join(tempDir, fileName)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
Expand All @@ -35,7 +41,7 @@ func (cl *ConfigLoader) downloadRemoteConfig(url string, tempDir string) (string
return tempFile, nil
}

func (cl *ConfigLoader) processImports(importPaths []string, tempDir string, currentDepth, maxDepth int) (resolvedPaths []string, err error) {
func (cl *ConfigLoader) processImports(importPaths []string, tempDir string, currentDepth, maxDepth int) (resolvedPaths []ResolvedPaths, err error) {
if tempDir == "" {
return nil, fmt.Errorf("tempDir required to process imports")
}
Expand Down Expand Up @@ -76,7 +82,7 @@ func isRemoteImport(importPath string) bool {
}

// Process remote imports
func (cl *ConfigLoader) processRemoteImport(importPath, tempDir string, currentDepth, maxDepth int) ([]string, error) {
func (cl *ConfigLoader) processRemoteImport(importPath, tempDir string, currentDepth, maxDepth int) ([]ResolvedPaths, error) {
parsedURL, err := url.Parse(importPath)
if err != nil || (parsedURL.Scheme != "http" && parsedURL.Scheme != "https") {
return nil, fmt.Errorf("unsupported URL '%s': %v", importPath, err)
Expand All @@ -99,7 +105,11 @@ func (cl *ConfigLoader) processRemoteImport(importPath, tempDir string, currentD
return nil, fmt.Errorf("failed to unmarshal remote config '%s': %v", importPath, err)
}

resolvedPaths := []string{tempFile}
resolvedPaths := make([]ResolvedPaths, 0)
resolvedPaths = append(resolvedPaths, ResolvedPaths{
filePath: tempFile,
importPaths: importPath,
})

// Recursively process imports from the remote file
if importedConfig.Import != nil {
Expand All @@ -114,7 +124,7 @@ func (cl *ConfigLoader) processRemoteImport(importPath, tempDir string, currentD
}

// Process local imports
func (cl *ConfigLoader) processLocalImport(importPath, tempDir string, currentDepth, maxDepth int) ([]string, error) {
func (cl *ConfigLoader) processLocalImport(importPath, tempDir string, currentDepth, maxDepth int) ([]ResolvedPaths, error) {
basePath, err := filepath.Abs(cl.atmosConfig.BasePath)
if err != nil {
return nil, fmt.Errorf("failed to resolve base path: %v", err)
Expand All @@ -126,7 +136,13 @@ func (cl *ConfigLoader) processLocalImport(importPath, tempDir string, currentDe
return nil, fmt.Errorf("failed to resolve local import path '%s': %v", importPath, err)
}

resolvedPaths := paths
resolvedPaths := make([]ResolvedPaths, 0)
for _, path := range paths {
resolvedPaths = append(resolvedPaths, ResolvedPaths{
filePath: path,
importPaths: importPath,
})
}

// Load the local configuration file to check for further imports
for _, path := range paths {
Expand Down
10 changes: 5 additions & 5 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,18 +799,18 @@ func (cl *ConfigLoader) processConfigImports() error {
return err
}

for _, configPath := range resolvedPaths {
err := cl.loadConfigFileViber(cl.atmosConfig, configPath, cl.viper)
for _, resolvedPath := range resolvedPaths {
err := cl.loadConfigFileViber(cl.atmosConfig, resolvedPath.filePath, cl.viper)
if err != nil {
log.Debug("error load config file", "path", configPath, "error", err)
log.Debug("error load config file", "import", resolvedPath.importPaths, "file_path", resolvedPath.filePath, "error", err)
continue
}
log.Debug("atmos merged config from import path", "path", configPath)
err = cl.deepMergeConfig()
if err != nil {
log.Debug("error merge config after imports", "path", configPath, "error", err)
log.Debug("error merge config after imports", "import", resolvedPath.importPaths, "file_path", resolvedPath.filePath, "error", err)
continue
}
log.Debug("atmos merged config from import", "import", resolvedPath.importPaths, "file_path", resolvedPath.filePath)
}
cl.atmosConfig.Import = importPaths
}
Expand Down
3 changes: 3 additions & 0 deletions tests/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ func sanitizeOutput(output string) (string, error) {
fixedRemainder := collapseExtraSlashes(groups[2])
return groups[1] + fixedRemainder
})
// 6. Replace lines atmos-import log
filePathRegex := regexp.MustCompile(`file_path=[^ ]+/atmos-import-\d+/atmos-import-\d+\.yaml`)
result = filePathRegex.ReplaceAllString(result, "file_path=/atmos-import/atmos-import.yaml")

return result, nil
}
Expand Down
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions tests/test-cases/atmos-cli-imports.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# yaml-language-server: $schema=schema.json
tests:
- name: atmos_describe_config_imports
snapshot: true
enabled: true
description: "Ensure atmos execute import configuration"
workdir: "fixtures/scenarios/atmos-cli-imports"
command: "atmos"
args:
- "describe"
- "config"
expect:
exit_code: 0

0 comments on commit da24379

Please sign in to comment.