khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 16 | package etcd |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 17 | |
| 18 | import ( |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 19 | "context" |
Divya Desai | 660dbba | 2019-10-16 07:06:49 +0000 | [diff] [blame] | 20 | "fmt" |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 21 | "go.etcd.io/etcd/embed" |
Divya Desai | 660dbba | 2019-10-16 07:06:49 +0000 | [diff] [blame] | 22 | "net/url" |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 23 | "os" |
| 24 | "time" |
| 25 | ) |
| 26 | |
| 27 | const ( |
Divya Desai | 660dbba | 2019-10-16 07:06:49 +0000 | [diff] [blame] | 28 | serverStartUpTimeout = 10 * time.Second // Maximum time allowed to wait for the Etcd server to be ready |
| 29 | defaultLocalPersistentStorage = "voltha.test.embed.etcd" |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | //EtcdServer represents an embedded Etcd server. It is used for testing only. |
| 33 | type EtcdServer struct { |
| 34 | server *embed.Etcd |
| 35 | } |
| 36 | |
Divya Desai | 660dbba | 2019-10-16 07:06:49 +0000 | [diff] [blame] | 37 | func islogLevelValid(logLevel string) bool { |
| 38 | valid := []string{"debug", "info", "warn", "error", "panic", "fatal"} |
| 39 | for _, l := range valid { |
| 40 | if l == logLevel { |
| 41 | return true |
| 42 | } |
| 43 | } |
| 44 | return false |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * MKConfig creates an embedded Etcd config |
| 49 | * :param configName: A name for this config |
| 50 | * :param clientPort: The port the etcd client will connect to (do not use 2379 for unit test) |
| 51 | * :param peerPort: The port the etcd server will listen for its peers (do not use 2380 for unit test) |
| 52 | * :param localPersistentStorageDir: The name of a local directory which will hold the Etcd server data |
| 53 | * :param logLevel: One of debug, info, warn, error, panic, or fatal. Default 'info'. |
| 54 | */ |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 55 | func MKConfig(ctx context.Context, configName string, clientPort, peerPort int, localPersistentStorageDir string, logLevel string) *embed.Config { |
Divya Desai | 660dbba | 2019-10-16 07:06:49 +0000 | [diff] [blame] | 56 | cfg := embed.NewConfig() |
| 57 | cfg.Name = configName |
| 58 | cfg.Dir = localPersistentStorageDir |
| 59 | cfg.Logger = "zap" |
| 60 | if !islogLevelValid(logLevel) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 61 | logger.Fatalf(ctx, "Invalid log level -%s", logLevel) |
Divya Desai | 660dbba | 2019-10-16 07:06:49 +0000 | [diff] [blame] | 62 | } |
| 63 | cfg.LogLevel = logLevel |
| 64 | acurl, err := url.Parse(fmt.Sprintf("http://localhost:%d", clientPort)) |
| 65 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 66 | logger.Fatalf(ctx, "Invalid client port -%d", clientPort) |
Divya Desai | 660dbba | 2019-10-16 07:06:49 +0000 | [diff] [blame] | 67 | } |
| 68 | cfg.ACUrls = []url.URL{*acurl} |
| 69 | cfg.LCUrls = []url.URL{*acurl} |
| 70 | |
| 71 | apurl, err := url.Parse(fmt.Sprintf("http://localhost:%d", peerPort)) |
| 72 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 73 | logger.Fatalf(ctx, "Invalid peer port -%d", peerPort) |
Divya Desai | 660dbba | 2019-10-16 07:06:49 +0000 | [diff] [blame] | 74 | } |
| 75 | cfg.LPUrls = []url.URL{*apurl} |
| 76 | cfg.APUrls = []url.URL{*apurl} |
| 77 | |
| 78 | cfg.ClusterState = embed.ClusterStateFlagNew |
| 79 | cfg.InitialCluster = cfg.Name + "=" + apurl.String() |
| 80 | |
| 81 | return cfg |
| 82 | } |
| 83 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 84 | //getDefaultCfg specifies the default config |
| 85 | func getDefaultCfg() *embed.Config { |
| 86 | cfg := embed.NewConfig() |
Divya Desai | 660dbba | 2019-10-16 07:06:49 +0000 | [diff] [blame] | 87 | cfg.Dir = defaultLocalPersistentStorage |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 88 | cfg.Logger = "zap" |
| 89 | cfg.LogLevel = "error" |
| 90 | return cfg |
| 91 | } |
| 92 | |
| 93 | //StartEtcdServer creates and starts an embedded Etcd server. A local directory to store data is created for the |
| 94 | //embedded server lifetime (for the duration of a unit test. The server runs at localhost:2379. |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 95 | func StartEtcdServer(ctx context.Context, cfg *embed.Config) *EtcdServer { |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 96 | // If the server is already running, just return |
| 97 | if cfg == nil { |
| 98 | cfg = getDefaultCfg() |
| 99 | } |
| 100 | // Remove the local directory as |
| 101 | // a safeguard for the case where a prior test failed |
Divya Desai | 660dbba | 2019-10-16 07:06:49 +0000 | [diff] [blame] | 102 | if err := os.RemoveAll(cfg.Dir); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 103 | logger.Fatalf(ctx, "Failure removing local directory %s", cfg.Dir) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 104 | } |
| 105 | e, err := embed.StartEtcd(cfg) |
| 106 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 107 | logger.Fatal(ctx, err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 108 | } |
| 109 | select { |
| 110 | case <-e.Server.ReadyNotify(): |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 111 | logger.Debug(ctx, "Embedded Etcd server is ready!") |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 112 | case <-time.After(serverStartUpTimeout): |
| 113 | e.Server.HardStop() // trigger a shutdown |
| 114 | e.Close() |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 115 | logger.Fatal(ctx, "Embedded Etcd server took too long to start!") |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 116 | case err := <-e.Err(): |
| 117 | e.Server.HardStop() // trigger a shutdown |
| 118 | e.Close() |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 119 | logger.Fatalf(ctx, "Embedded Etcd server errored out - %s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 120 | } |
| 121 | return &EtcdServer{server: e} |
| 122 | } |
| 123 | |
| 124 | //Stop closes the embedded Etcd server and removes the local data directory as well |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 125 | func (es *EtcdServer) Stop(ctx context.Context) { |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 126 | if es != nil { |
Divya Desai | 660dbba | 2019-10-16 07:06:49 +0000 | [diff] [blame] | 127 | storage := es.server.Config().Dir |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 128 | es.server.Server.HardStop() |
| 129 | es.server.Close() |
Divya Desai | 660dbba | 2019-10-16 07:06:49 +0000 | [diff] [blame] | 130 | if err := os.RemoveAll(storage); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 131 | logger.Fatalf(ctx, "Failure removing local directory %s", es.server.Config().Dir) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |