Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tests/plugins/meshroom/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"key": "CONFIG_PATH",
"type": "path",
"value": "sharedTemplate.mg"
},
{
"key": "ERRONEOUS_CONFIG_PATH",
"type": "path",
"value": "erroneous_path"
},
{
"key": "CONFIG_STRING",
"type": "string",
"value": "configFile"
}
]
120 changes: 120 additions & 0 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from meshroom.core import pluginManager, loadClassesNodes
from meshroom.core.plugins import NodePluginStatus, Plugin
from .utils import overrideOsEnvironmentVariables, registeredPlugins

from pathlib import Path
import os
import time

Expand Down Expand Up @@ -214,3 +216,121 @@ def test_reloadNodePlugin(self):
pluginManager.unregisterNode(node)
assert node.status == NodePluginStatus.DESC_ERROR # Not NOT_LOADED
assert not pluginManager.isRegistered(nodeName)


class TestPluginsConfiguration:
CONFIG_PATH = ("CONFIG_PATH", "sharedTemplate.mg", "config.json")
ERRONEOUS_CONFIG_PATH = ("ERRONEOUS_CONFIG_PATH", "erroneous_path", "not_erroneous_path")
CONFIG_STRING = ("CONFIG_STRING", "configFile", "notConfigFile")

CONFIG_KEYS = [CONFIG_PATH[0], ERRONEOUS_CONFIG_PATH[0], CONFIG_STRING[0]]

def test_loadedConfig(self):
# Check that the config.json file for the plugins in the "plugins" directory is
# correctly loaded
folder = os.path.join(os.path.dirname(__file__), "plugins")
with registeredPlugins(folder):
plugin = pluginManager.getPlugin("pluginA")
assert plugin

# Check that the config file has been properly loaded
config = plugin.configEnv
configFullEnv = plugin.configFullEnv
assert len(config) == 3, "The configuration file contains exactly 3 keys."
assert len(configFullEnv) >= len(os.environ) and \
len(configFullEnv) == len(os.environ) + len(config), \
"The configuration environment should have the same number of keys as " \
"os.environ and the configuration file"

# Check that all the keys have been properly read
assert list(config.keys()) == self.CONFIG_KEYS

# Check that the valid path has been correctly read, resolved and set
assert configFullEnv[self.CONFIG_PATH[0]] == config[self.CONFIG_PATH[0]]
assert configFullEnv[self.CONFIG_PATH[0]] == Path(
os.path.join(plugin.path, self.CONFIG_PATH[1])).resolve().as_posix()

# Check that the invalid path has been read, unresolved, and set
assert configFullEnv[self.ERRONEOUS_CONFIG_PATH[0]] == self.ERRONEOUS_CONFIG_PATH[1]
assert config[self.ERRONEOUS_CONFIG_PATH[0]] == self.ERRONEOUS_CONFIG_PATH[1]

# Check that the string has been correctly read and set
assert configFullEnv[self.CONFIG_STRING[0]] == self.CONFIG_STRING[1]
assert config[self.CONFIG_STRING[0]] == self.CONFIG_STRING[1]

def test_loadedConfigWithOnlyExistingKeys(self):
# Set the keys from the config file in the current environment
environment = {
self.CONFIG_PATH[0]: self.CONFIG_PATH[2],
self.ERRONEOUS_CONFIG_PATH[0]: self.ERRONEOUS_CONFIG_PATH[2],
self.CONFIG_STRING[0]: self.CONFIG_STRING[2]
}

folder = os.path.join(os.path.dirname(__file__), "plugins")
with (overrideOsEnvironmentVariables(environment), registeredPlugins(folder)):
plugin = pluginManager.getPlugin("pluginA")
assert plugin

# Check that the config file has been properly loaded and read
# Environment variables that are already set should not have any effect on that
# reading of values
config = plugin.configEnv
assert len(config) == 3
assert list(config.keys()) == self.CONFIG_KEYS
assert config[self.CONFIG_PATH[0]] == Path(
os.path.join(plugin.path, self.CONFIG_PATH[1])).resolve().as_posix()
assert config[self.ERRONEOUS_CONFIG_PATH[0]] == self.ERRONEOUS_CONFIG_PATH[1]
assert config[self.CONFIG_STRING[0]] == self.CONFIG_STRING[1]

# Check that the values of the configuration file are not taking precedence over
# those in the environment
configFullEnv = plugin.configFullEnv
assert all(key in configFullEnv for key in config.keys())

assert config[self.CONFIG_PATH[0]] != self.CONFIG_PATH[2]
assert configFullEnv[self.CONFIG_PATH[0]] == self.CONFIG_PATH[2]

assert config[self.ERRONEOUS_CONFIG_PATH[0]] != self.ERRONEOUS_CONFIG_PATH[2]
assert configFullEnv[self.ERRONEOUS_CONFIG_PATH[0]] == self.ERRONEOUS_CONFIG_PATH[2]

assert config[self.CONFIG_STRING[0]] != self.CONFIG_STRING[2]
assert configFullEnv[self.CONFIG_STRING[0]] == self.CONFIG_STRING[2]

def test_loadedConfigWithSomeExistingKeys(self):
# Set some keys from the config file in the current environment
environment = {
self.ERRONEOUS_CONFIG_PATH[0]: self.ERRONEOUS_CONFIG_PATH[2],
self.CONFIG_STRING[0]: self.CONFIG_STRING[2]
}

folder = os.path.join(os.path.dirname(__file__), "plugins")
with (overrideOsEnvironmentVariables(environment), registeredPlugins(folder)):
plugin = pluginManager.getPlugin("pluginA")
assert plugin

# Check that the config file has been properly loaded and read
# Environment variables that are already set should not have any effect on that
# reading of values
config = plugin.configEnv
assert len(config) == 3
assert list(config.keys()) == self.CONFIG_KEYS
assert config[self.CONFIG_PATH[0]] == Path(
os.path.join(plugin.path, self.CONFIG_PATH[1])).resolve().as_posix()
assert config[self.ERRONEOUS_CONFIG_PATH[0]] == self.ERRONEOUS_CONFIG_PATH[1]
assert config[self.CONFIG_STRING[0]] == self.CONFIG_STRING[1]

# Check that the values of the configuration file are not taking precedence over
# those in the environment
configFullEnv = plugin.configFullEnv
assert all(key in configFullEnv for key in config.keys())

assert config[self.CONFIG_PATH[0]] == Path(os.path.join(
plugin.path, self.CONFIG_PATH[1])).resolve().as_posix()
assert configFullEnv[self.CONFIG_PATH[0]] == Path(os.path.join(
plugin.path, self.CONFIG_PATH[1])).resolve().as_posix()

assert config[self.ERRONEOUS_CONFIG_PATH[0]] != self.ERRONEOUS_CONFIG_PATH[2]
assert configFullEnv[self.ERRONEOUS_CONFIG_PATH[0]] == self.ERRONEOUS_CONFIG_PATH[2]

assert config[self.CONFIG_STRING[0]] != self.CONFIG_STRING[2]
assert configFullEnv[self.CONFIG_STRING[0]] == self.CONFIG_STRING[2]
17 changes: 16 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
from unittest.mock import patch

import meshroom
from meshroom.core import desc, pluginManager
from meshroom.core import desc, pluginManager, loadPluginFolder
from meshroom.core.plugins import NodePlugin, NodePluginStatus

import os

@contextmanager
def registeredNodeTypes(nodeTypes: list[desc.Node]):
Expand Down Expand Up @@ -45,3 +46,17 @@ def unregisterNodeDesc(nodeDesc: desc.Node):
plugin = pluginManager.getRegisteredNodePlugin(name)
plugin.status = NodePluginStatus.NOT_LOADED
del pluginManager._nodePlugins[name]

@contextmanager
def registeredPlugins(folder: str):
plugins = loadPluginFolder(folder)

yield

for plugin in plugins:
pluginManager.removePlugin(plugin)

@contextmanager
def overrideOsEnvironmentVariables(envVariables: dict):
with patch.dict(os.environ, envVariables, clear=False):
yield
Loading