Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wildum committed Sep 23, 2024
1 parent ec9b23f commit 767df0b
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 18 deletions.
37 changes: 36 additions & 1 deletion internal/runtime/alloy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestController_LoadSource_Evaluation(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, f)

err = ctrl.LoadSource(f, nil)
err = ctrl.LoadSource(f, nil, "")
require.NoError(t, err)
require.Len(t, ctrl.loader.Components(), 4)

Expand All @@ -54,6 +54,41 @@ func TestController_LoadSource_Evaluation(t *testing.T) {
require.Equal(t, "hello, world!", out.(testcomponents.PassthroughExports).Output)
}

var modulePathTestFile = `
testcomponents.tick "ticker" {
frequency = "1s"
}
testcomponents.passthrough "static" {
input = module_path
}
testcomponents.passthrough "ticker" {
input = testcomponents.tick.ticker.tick_time
}
testcomponents.passthrough "forwarded" {
input = testcomponents.passthrough.ticker.output
}
`

func TestController_LoadSource_WithModulePath_Evaluation(t *testing.T) {
defer verifyNoGoroutineLeaks(t)
ctrl := New(testOptions(t))
defer cleanUpController(ctrl)

f, err := ParseSource(t.Name(), []byte(modulePathTestFile))
require.NoError(t, err)
require.NotNil(t, f)

err = ctrl.LoadSource(f, nil, "path/to/config/main.alloy")
require.NoError(t, err)
require.Len(t, ctrl.loader.Components(), 4)

// Check the inputs and outputs of things that should be immediately resolved
// without having to run the components.
in, out := getFields(t, ctrl.loader.Graph(), "testcomponents.passthrough.static")
require.Equal(t, "path/to/config", in.(testcomponents.PassthroughConfig).Input)
require.Equal(t, "path/to/config", out.(testcomponents.PassthroughExports).Output)
}

func getFields(t *testing.T, g *dag.Graph, nodeID string) (component.Arguments, component.Exports) {
t.Helper()

Expand Down
67 changes: 50 additions & 17 deletions internal/runtime/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ const mainFile = "main.alloy"

// The tests are using the .txtar files stored in the testdata folder.
type testImportFile struct {
description string // description at the top of the txtar file
main string // root config that the controller should load
module string // module imported by the root config
nestedModule string // nested module that can be imported by the module
reloadConfig string // root config that the controller should apply on reload
otherNestedModule string // another nested module
update *updateFile // update can be used to update the content of a file at runtime
description string // description at the top of the txtar file
main string // root config that the controller should load
module string // module imported by the root config
nestedModule string // nested module that can be imported by the module
reloadConfig string // root config that the controller should apply on reload
otherNestedModule string // another nested module
nestedPathModule string // a module in a subdirectory
deeplyNestedPathModule string // a module in a sub-subdirectory
update *updateFile // update can be used to update the content of a file at runtime
}

type updateFile struct {
Expand Down Expand Up @@ -70,6 +72,10 @@ func buildTestImportFile(t *testing.T, filename string) testImportFile {
tc.reloadConfig = string(alloyConfig.Data)
case "other_nested_module.alloy":
tc.otherNestedModule = string(alloyConfig.Data)
case "nested_test/module.alloy":
tc.nestedPathModule = string(alloyConfig.Data)
case "nested_test/utils/module.alloy":
tc.deeplyNestedPathModule = string(alloyConfig.Data)
}
}
return tc
Expand All @@ -91,6 +97,18 @@ func TestImportFile(t *testing.T) {
require.NoError(t, os.WriteFile("other_nested_module.alloy", []byte(tc.otherNestedModule), 0664))
}

if tc.nestedPathModule != "" || tc.deeplyNestedPathModule != "" {
require.NoError(t, os.Mkdir("nested_test", 0700))
defer os.RemoveAll("nested_test")
if tc.nestedPathModule != "" {
require.NoError(t, os.WriteFile("nested_test/module.alloy", []byte(tc.nestedPathModule), 0664))
}
if tc.deeplyNestedPathModule != "" {
require.NoError(t, os.Mkdir("nested_test/utils", 0700))
require.NoError(t, os.WriteFile("nested_test/utils/module.alloy", []byte(tc.deeplyNestedPathModule), 0664))
}
}

if tc.update != nil {
testConfig(t, tc.main, tc.reloadConfig, func() {
require.NoError(t, os.WriteFile(tc.update.name, []byte(tc.update.updateConfig), 0664))
Expand Down Expand Up @@ -146,13 +164,14 @@ func TestImportHTTP(t *testing.T) {
}

type testImportFileFolder struct {
description string // description at the top of the txtar file
main string // root config that the controller should load
module1 string // module imported by the root config
module2 string // another module imported by the root config
removed string // module will be removed in the dir on update
added string // module which will be added in the dir on update
update *updateFile // update can be used to update the content of a file at runtime
description string // description at the top of the txtar file
main string // root config that the controller should load
module1 string // module imported by the root config
module2 string // another module imported by the root config
utilsModule2 string // another module in a nested subdirectory
removed string // module will be removed in the dir on update
added string // module which will be added in the dir on update
update *updateFile // update can be used to update the content of a file at runtime
}

func buildTestImportFileFolder(t *testing.T, filename string) testImportFileFolder {
Expand All @@ -168,6 +187,8 @@ func buildTestImportFileFolder(t *testing.T, filename string) testImportFileFold
tc.module1 = string(alloyConfig.Data)
case "module2.alloy":
tc.module2 = string(alloyConfig.Data)
case "utils/module2.alloy":
tc.utilsModule2 = string(alloyConfig.Data)
case "added.alloy":
tc.added = string(alloyConfig.Data)
case "removed.alloy":
Expand All @@ -184,6 +205,12 @@ func buildTestImportFileFolder(t *testing.T, filename string) testImportFileFold
name: "module2.alloy",
updateConfig: string(alloyConfig.Data),
}
case "utils/update_module2.alloy":
require.Nil(t, tc.update)
tc.update = &updateFile{
name: "utils/module2.alloy",
updateConfig: string(alloyConfig.Data),
}
}
}
return tc
Expand All @@ -210,6 +237,12 @@ func TestImportFileFolder(t *testing.T) {
require.NoError(t, os.WriteFile(filepath.Join(dir, "removed.alloy"), []byte(tc.removed), 0700))
}

if tc.utilsModule2 != "" {
nestedDir := filepath.Join(dir, "utils")
require.NoError(t, os.Mkdir(nestedDir, 0700))
require.NoError(t, os.WriteFile(filepath.Join(nestedDir, "module2.alloy"), []byte(tc.utilsModule2), 0700))
}

// TODO: ideally we would like to check the health of the node but that's not yet possible for import nodes.
// We should expect that adding or removing files in the dir is gracefully handled and the node should be
// healthy once it polls the content of the dir again.
Expand Down Expand Up @@ -265,7 +298,7 @@ func testConfig(t *testing.T, config string, reloadConfig string, update func())
defer verifyNoGoroutineLeaks(t)
ctrl, f := setup(t, config)

err := ctrl.LoadSource(f, nil)
err := ctrl.LoadSource(f, nil, "")
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -303,7 +336,7 @@ func testConfig(t *testing.T, config string, reloadConfig string, update func())
require.NotNil(t, f)

// Reload the controller with the new config.
err = ctrl.LoadSource(f, nil)
err = ctrl.LoadSource(f, nil, "")
require.NoError(t, err)

// Export should be -10 after update
Expand All @@ -317,7 +350,7 @@ func testConfig(t *testing.T, config string, reloadConfig string, update func())
func testConfigError(t *testing.T, config string, expectedError string) {
defer verifyNoGoroutineLeaks(t)
ctrl, f := setup(t, config)
err := ctrl.LoadSource(f, nil)
err := ctrl.LoadSource(f, nil, "")
require.ErrorContains(t, err, expectedError)
ctx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
Expand Down
50 changes: 50 additions & 0 deletions internal/runtime/testdata/import_file/import_file_18.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Import nested passthrough module with relative import path.

-- main.alloy --
testcomponents.count "inc" {
frequency = "10ms"
max = 10
}

import.file "testImport" {
filename = "nested_test/module.alloy"
}

testImport.a "cc" {
input = testcomponents.count.inc.count
}

testcomponents.summation "sum" {
input = testImport.a.cc.output
}

-- nested_test/module.alloy --
import.file "testImport" {
filename = file.path_join(module_path, "utils/module.alloy")
}

declare "a" {
argument "input" {}

testImport.a "cc" {
input = argument.input.value
}

export "output" {
value = testImport.a.cc.output
}
}

-- nested_test/utils/module.alloy --
declare "a" {
argument "input" {}

testcomponents.passthrough "pt" {
input = argument.input.value
lag = "1ms"
}

export "output" {
value = testcomponents.passthrough.pt.output
}
}
49 changes: 49 additions & 0 deletions internal/runtime/testdata/import_file/import_file_19.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Import string with import file with relative import path.

-- main.alloy --
testcomponents.count "inc" {
frequency = "10ms"
max = 10
}

import.string "testImport" {
content = `
import.file "testImport" {
filename = file.path_join(module_path, "nested_test/module.alloy")
}

declare "a" {
argument "input" {}

testImport.a "cc" {
input = argument.input.value
}

export "output" {
value = testImport.a.cc.output
}
}
`
}

testImport.a "cc" {
input = testcomponents.count.inc.count
}

testcomponents.summation "sum" {
input = testImport.a.cc.output
}

-- nested_test/module.alloy --
declare "a" {
argument "input" {}

testcomponents.passthrough "pt" {
input = argument.input.value
lag = "1ms"
}

export "output" {
value = testcomponents.passthrough.pt.output
}
}
50 changes: 50 additions & 0 deletions internal/runtime/testdata/import_file/import_file_20.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Import nested passthrough module with relative import path in a declare.

-- main.alloy --
testcomponents.count "inc" {
frequency = "10ms"
max = 10
}

import.file "testImport" {
filename = "nested_test/module.alloy"
}

testImport.a "cc" {
input = testcomponents.count.inc.count
}

testcomponents.summation "sum" {
input = testImport.a.cc.output
}

-- nested_test/module.alloy --
declare "a" {
argument "input" {}

import.file "testImport" {
filename = file.path_join(module_path, "utils/module.alloy")
}

testImport.a "cc" {
input = argument.input.value
}

export "output" {
value = testImport.a.cc.output
}
}

-- nested_test/utils/module.alloy --
declare "a" {
argument "input" {}

testcomponents.passthrough "pt" {
input = argument.input.value
lag = "1ms"
}

export "output" {
value = testcomponents.passthrough.pt.output
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Import nested folder with relative path.

-- main.alloy --
testcomponents.count "inc" {
frequency = "10ms"
max = 10
}

import.file "testImport" {
filename = "tmpTest"
}

testImport.a "cc" {
input = testcomponents.count.inc.count
}

testcomponents.summation "sum" {
input = testImport.a.cc.output
}

-- module1.alloy --
import.file "testImport" {
filename = file.path_join(module_path, "utils")
}
declare "a" {
argument "input" {}

testImport.b "cc" {
input = argument.input.value
}

export "output" {
value = testImport.b.cc.output
}
}

-- utils/module2.alloy --
declare "b" {
argument "input" {}

testcomponents.passthrough "pt" {
input = argument.input.value
lag = "1ms"
}

export "output" {
value = testcomponents.passthrough.pt.output
}
}

-- utils/update_module2.alloy --
declare "b" {
argument "input" {}

export "output" {
value = -argument.input.value
}
}
Loading

0 comments on commit 767df0b

Please sign in to comment.