Repository: envoyproxy/envoy
File: test/extensions/dynamic_modules/dynamic_modules_test.cc (C++)

Code before:
  EXPECT_THAT(result.status().message(),
              testing::HasSubstr("ABI version mismatch: got invalid-version-hash, but expected"));
}

TEST(CreateDynamicModulesByName, ENVOY_DYNAMIC_MODULES_SEARCH_PATH_Set) {
  TestEnvironment::setEnvVar(
      "ENVOY_DYNAMIC_MODULES_SEARCH_PATH",
      TestEnvironment::substitute(
          "{{ test_rundir }}/test/extensions/dynamic_modules/test_data/rust"),
      1);

  absl::StatusOr<DynamicModulePtr> module = newDynamicModuleByName("no_op", false);
  EXPECT_TRUE(module.ok()) << "Failed to load module: " << module.status().message();
  TestEnvironment::unsetEnvVar("ENVOY_DYNAMIC_MODULES_SEARCH_PATH");
}

TEST(CreateDynamicModulesByName, ENVOY_DYNAMIC_MODULES_SEARCH_PATH_notSetFallbackToCwd) {
  std::filesystem::path test_lib = testSharedObjectPath("no_op", "c");
  std::filesystem::path staged_lib = TestEnvironment::substitute("{{ test_rundir }}/libfoo.so");
  std::filesystem::copy(test_lib, staged_lib);
  absl::StatusOr<DynamicModulePtr> module = newDynamicModuleByName("foo", false);
  EXPECT_TRUE(module.ok()) << "Failed to load module: " << module.status().message();
  std::filesystem::remove(staged_lib);
}

TEST(CreateDynamicModulesByName, dlopenDefaultSearchPath) {
  TestEnvironment::setEnvVar("ENVOY_DYNAMIC_MODULES_SEARCH_PATH", "/should/not/find/this/path", 1);

  std::filesystem::path test_lib = testSharedObjectPath("no_op", "c");
  std::filesystem::path staged_lib =
      TestEnvironment::substitute("{{ test_rundir }}/libwhatever.so");
  std::filesystem::copy(test_lib, staged_lib);
  absl::StatusOr<DynamicModulePtr> module = newDynamicModuleByName("whatever", false);
  EXPECT_TRUE(module.ok()) << "Failed to load module: " << module.status().message();

  TestEnvironment::unsetEnvVar("ENVOY_DYNAMIC_MODULES_SEARCH_PATH");
  std::filesystem::remove(staged_lib);
}

TEST(CreateDynamicModulesByName, NotFound) {
  // Without setting the search path, this should fail.
  absl::StatusOr<DynamicModulePtr> module = newDynamicModuleByName("no_op", false);
  EXPECT_FALSE(module.ok());
  EXPECT_EQ(module.status().code(), absl::StatusCode::kInvalidArgument);
  EXPECT_THAT(module.status().message(),
              testing::HasSubstr(
                  "Failed to load dynamic module: libno_op.so not found in any search path"));
}

} // namespace DynamicModules
} // namespace Extensions

Review comment (suggestion):
```suggestion
TEST(CreateDynamicModulesByName, DlopenDefaultSearchPath) {
```

Code after:
  EXPECT_THAT(result.status().message(),
              testing::HasSubstr("ABI version mismatch: got invalid-version-hash, but expected"));
}

TEST(CreateDynamicModulesByName, EnvoyDynamicModulesSearchPathSet) {
  TestEnvironment::setEnvVar(
      "ENVOY_DYNAMIC_MODULES_SEARCH_PATH",
      TestEnvironment::substitute(
          "{{ test_rundir }}/test/extensions/dynamic_modules/test_data/rust"),
      1);

  absl::StatusOr<DynamicModulePtr> module = newDynamicModuleByName("no_op", false);
  EXPECT_TRUE(module.ok()) << "Failed to load module: " << module.status().message();
  TestEnvironment::unsetEnvVar("ENVOY_DYNAMIC_MODULES_SEARCH_PATH");
}

TEST(CreateDynamicModulesByName, EnvoyDynamicModulesSearchPathNotSetFallbackToCwd) {
  std::filesystem::path test_lib = testSharedObjectPath("no_op", "c");
  std::filesystem::path staged_lib = TestEnvironment::substitute("{{ test_rundir }}/libfoo.so");
  std::filesystem::copy(test_lib, staged_lib);
  absl::StatusOr<DynamicModulePtr> module = newDynamicModuleByName("foo", false);
  EXPECT_TRUE(module.ok()) << "Failed to load module: " << module.status().message();
  std::filesystem::remove(staged_lib);
}

TEST(CreateDynamicModulesByName, DlopenDefaultSearchPath) {
  TestEnvironment::setEnvVar("ENVOY_DYNAMIC_MODULES_SEARCH_PATH", "/should/not/find/this/path", 1);

  std::filesystem::path test_lib = testSharedObjectPath("no_op", "c");
  std::filesystem::path staged_lib =
      TestEnvironment::substitute("{{ test_rundir }}/libwhatever.so");
  std::filesystem::copy(test_lib, staged_lib);
  absl::StatusOr<DynamicModulePtr> module = newDynamicModuleByName("whatever", false);
  EXPECT_TRUE(module.ok()) << "Failed to load module: " << module.status().message();

  TestEnvironment::unsetEnvVar("ENVOY_DYNAMIC_MODULES_SEARCH_PATH");
  std::filesystem::remove(staged_lib);
}

TEST(CreateDynamicModulesByName, ModuleNotFound) {
  absl::StatusOr<DynamicModulePtr> module = newDynamicModuleByName("no_op", false);
  EXPECT_FALSE(module.ok());
  EXPECT_EQ(module.status().code(), absl::StatusCode::kInvalidArgument);
  EXPECT_THAT(module.status().message(),
              testing::HasSubstr(
                  "Failed to load dynamic module: libno_op.so not found in any search path"));
}

} // namespace DynamicModules
} // namespace Extensions
} // namespace Envoy