Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/test/test_common/test_runtime.h
Line
Count
Source
1
#pragma once
2
3
// A simple test utility to easily allow for runtime feature overloads in unit tests.
4
//
5
// As long as this class is in scope one can do runtime feature overrides:
6
//
7
//  TestScopedRuntime scoped_runtime;
8
//  scoped_runtime.mergeValues(
9
//      {{"envoy.reloadable_features.test_feature_true", "false"}});
10
11
#pragma once
12
13
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
14
15
#include "source/common/runtime/runtime_impl.h"
16
#include "source/common/stats/isolated_store_impl.h"
17
18
#include "test/mocks/common.h"
19
#include "test/mocks/event/mocks.h"
20
#include "test/mocks/init/mocks.h"
21
#include "test/mocks/local_info/mocks.h"
22
#include "test/mocks/protobuf/mocks.h"
23
#include "test/mocks/runtime/mocks.h"
24
#include "test/mocks/thread_local/mocks.h"
25
26
#include "gmock/gmock.h"
27
28
namespace Envoy {
29
30
class TestScopedRuntime {
31
public:
32
57.0k
  TestScopedRuntime() : api_(Api::createApiForTest()) {
33
57.0k
    envoy::config::bootstrap::v3::LayeredRuntime config;
34
    // The existence of an admin layer is required for mergeValues() to work.
35
57.0k
    config.add_layers()->mutable_admin_layer();
36
37
57.0k
    Runtime::LoaderPtr runtime_ptr = std::make_unique<Runtime::LoaderImpl>(
38
57.0k
        dispatcher_, tls_, config, local_info_, store_, generator_, validation_visitor_, *api_);
39
    // This will ignore values set in test, but just use flag defaults!
40
57.0k
    runtime_ = std::move(runtime_ptr);
41
57.0k
  }
42
43
48.6k
  Runtime::Loader& loader() { return *runtime_; }
44
45
46.5k
  void mergeValues(const absl::node_hash_map<std::string, std::string>& values) {
46
46.5k
    loader().mergeValues(values);
47
46.5k
  }
48
49
protected:
50
  absl::FlagSaver saver_;
51
  Event::MockDispatcher dispatcher_;
52
  testing::NiceMock<ThreadLocal::MockInstance> tls_;
53
  Stats::TestUtil::TestStore store_;
54
  Random::MockRandomGenerator generator_;
55
  Api::ApiPtr api_;
56
  testing::NiceMock<LocalInfo::MockLocalInfo> local_info_;
57
  testing::NiceMock<ProtobufMessage::MockValidationVisitor> validation_visitor_;
58
  std::unique_ptr<Runtime::Loader> runtime_;
59
};
60
61
class TestDeprecatedV2Api : public TestScopedRuntime {
62
public:
63
2.10k
  TestDeprecatedV2Api() { allowDeprecatedV2(); }
64
2.10k
  void allowDeprecatedV2() {
65
2.10k
    loader().mergeValues({
66
2.10k
        {"envoy.test_only.broken_in_production.enable_deprecated_v2_api", "true"},
67
2.10k
        {"envoy.features.enable_all_deprecated_features", "true"},
68
2.10k
    });
69
2.10k
  }
70
};
71
72
} // namespace Envoy