TensorFlow Serving C++ API Documentation
server_core_test_util.h
1 /* Copyright 2016 Google Inc. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_SERVING_MODEL_SERVERS_TEST_UTIL_SERVER_CORE_TEST_UTIL_H_
17 #define TENSORFLOW_SERVING_MODEL_SERVERS_TEST_UTIL_SERVER_CORE_TEST_UTIL_H_
18 
19 #include <array>
20 #include <utility>
21 
22 #include <gtest/gtest.h>
23 #include "tensorflow_serving/core/servable_id.h"
24 #include "tensorflow_serving/model_servers/server_core.h"
25 
26 namespace tensorflow {
27 namespace serving {
28 namespace test_util {
29 
30 // A test utility that provides access to private ServerCore members.
32  public:
33  explicit ServerCoreTestAccess(ServerCore* core) : core_(core) {}
34 
35  ServerCore::Options* mutable_options() { return &core_->options_; }
36 
37  private:
38  ServerCore* const core_;
39 };
40 
41 constexpr char kTestModelName[] = "test_model";
42 constexpr int kTestModelVersion = 123;
43 constexpr int kTestModelLargerVersion = 124;
44 constexpr int kTestModelBogusVersion = 777;
45 constexpr std::array<int64_t, 2> kAspiredVersions = {kTestModelVersion,
46  kTestModelLargerVersion};
47 // The name of the platform associated with FakeLoaderSourceAdapter.
48 constexpr char kFakePlatform[] = "fake_servable";
49 
50 // ServerCoreTest is parameterized based on the TestType enum defined below.
51 // TODO(b/32248363): remove the parameter and TestType after we switch Model
52 // Server to Saved Model.
53 class ServerCoreTest : public ::testing::TestWithParam<std::tuple<int, bool>> {
54  public:
55  // The parameter of this test.
56  enum TestType {
57  // SavedModelBundle is used on export.
58  SAVED_MODEL_BACKWARD_COMPATIBILITY,
59  // SavedModelBundle is used on native Saved Model.
60  SAVED_MODEL,
61  // This should always be the last value.
62  NUM_TEST_TYPES,
63  };
64 
65  static string GetNameOfTestType(int test_type) {
66  switch (static_cast<TestType>(test_type)) {
67  case SAVED_MODEL_BACKWARD_COMPATIBILITY:
68  return "SAVED_MODEL_BACKWARD_COMPATIBILITY";
69  case SAVED_MODEL:
70  return "SAVED_MODEL";
71  default:
72  return "unknown";
73  }
74  }
75 
76  // Creates some reasonable default ServerCore options for tests.
77  static ServerCore::Options GetDefaultOptions();
78 
79  protected:
80  // Returns ModelServerConfig that contains test model for the fake platform.
81  ModelServerConfig GetTestModelServerConfigForFakePlatform();
82 
83  // Returns ModelServerConfig that contains test model for the tensorflow
84  // platform.
85  ModelServerConfig GetTestModelServerConfigForTensorflowPlatform();
86 
87  // Mutates 'config' by changing the model's base path to point to a variant
88  // of half-plus-two that has two versions instead of one.
89  void SwitchToHalfPlusTwoWith2Versions(ModelServerConfig* config);
90 
91  // Creates a ServerCore object configured with both a fake platform and the
92  // tensorflow platform, using the supplied options.
93  Status CreateServerCore(const ModelServerConfig& config,
94  ServerCore::Options options,
95  std::unique_ptr<ServerCore>* server_core);
96 
97  // Creates a ServerCore object configured with both a fake platform and the
98  // tensorflow platform, using GetDefaultOptions().
99  Status CreateServerCore(const ModelServerConfig& config,
100  std::unique_ptr<ServerCore>* server_core) {
101  return CreateServerCore(config, GetDefaultOptions(), server_core);
102  }
103 
104  // Returns test type.
105  // This is the first parameter of this test.
106  TestType GetTestType() {
107  return static_cast<TestType>(std::get<0>(GetParam()));
108  }
109 
110  // Returns whether to assume paths are URIs.
111  // This is the second parameter of this test.
112  bool PrefixPathsWithURIScheme() { return std::get<1>(GetParam()); }
113 
114  // Returns a string corresponding to the parameterized test-case.
115  string GetNameForTestCase() {
116  return GetNameOfTestType(GetTestType()) + "_" +
117  (PrefixPathsWithURIScheme() ? "URI" : "Path");
118  }
119 };
120 
121 // Creates a ServerCore object with the supplied options.
122 Status CreateServerCore(const ModelServerConfig& config,
123  ServerCore::Options options,
124  std::unique_ptr<ServerCore>* server_core);
125 
126 // Creates a ServerCore object with sane defaults.
127 Status CreateServerCore(const ModelServerConfig& config,
128  std::unique_ptr<ServerCore>* server_core);
129 
130 // A helper class to avoid long lines accessing mutable configuration.
132  public:
133  explicit ModelConfigMutator(ModelConfig* const config) : config_(config) {}
134 
135  // Sets or updates label and its version.
136  ModelConfigMutator SetLabelVersion(const string& label, const int& version) {
137  (*config_->mutable_version_labels())[label] = version;
138  return *this;
139  }
140 
141  private:
142  ModelConfig* config_;
143 };
144 
145 inline ModelConfigMutator MutateModelConfig(ModelServerConfig* const config) {
146  return ModelConfigMutator(
147  config->mutable_model_config_list()->mutable_config(0));
148 }
149 
150 } // namespace test_util
151 } // namespace serving
152 } // namespace tensorflow
153 
154 #endif // TENSORFLOW_SERVING_MODEL_SERVERS_TEST_UTIL_SERVER_CORE_TEST_UTIL_H_
Options for configuring a ServerCore object.
Definition: server_core.h:96