khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | /* |
Joey Armstrong | 9cdee9f | 2024-01-03 04:56:14 -0500 | [diff] [blame] | 2 | * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 3 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 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 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 7 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 9 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 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. |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 15 | */ |
khenaidoo | b6238b3 | 2020-04-07 12:07:36 -0400 | [diff] [blame] | 16 | package etcd |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 17 | |
| 18 | import ( |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 19 | "context" |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 20 | "fmt" |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 21 | "net/url" |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 22 | "os" |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 23 | "strings" |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 24 | "time" |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 25 | |
| 26 | "go.etcd.io/etcd/embed" |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | const ( |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 30 | serverStartUpTimeout = 10 * time.Second // Maximum time allowed to wait for the Etcd server to be ready |
| 31 | defaultLocalPersistentStorage = "voltha.test.embed.etcd" |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 32 | ) |
| 33 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 34 | // EtcdServer represents an embedded Etcd server. It is used for testing only. |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 35 | type EtcdServer struct { |
| 36 | server *embed.Etcd |
| 37 | } |
| 38 | |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 39 | func islogLevelValid(logLevel string) bool { |
| 40 | valid := []string{"debug", "info", "warn", "error", "panic", "fatal"} |
| 41 | for _, l := range valid { |
| 42 | if l == logLevel { |
| 43 | return true |
| 44 | } |
| 45 | } |
| 46 | return false |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * MKConfig creates an embedded Etcd config |
| 51 | * :param configName: A name for this config |
| 52 | * :param clientPort: The port the etcd client will connect to (do not use 2379 for unit test) |
| 53 | * :param peerPort: The port the etcd server will listen for its peers (do not use 2380 for unit test) |
| 54 | * :param localPersistentStorageDir: The name of a local directory which will hold the Etcd server data |
| 55 | * :param logLevel: One of debug, info, warn, error, panic, or fatal. Default 'info'. |
| 56 | */ |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 57 | func MKConfig(ctx context.Context, configName string, clientPort, peerPort int, localPersistentStorageDir string, logLevel string) *embed.Config { |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 58 | cfg := embed.NewConfig() |
| 59 | cfg.Name = configName |
| 60 | cfg.Dir = localPersistentStorageDir |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 61 | // cfg.Logger = "zap" |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 62 | if !islogLevelValid(logLevel) { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 63 | logger.Fatalf(ctx, "Invalid log level -%s", logLevel) |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 64 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 65 | // cfg.LogLevel = logLevel |
| 66 | cfg.Debug = strings.EqualFold(logLevel, "debug") |
| 67 | cfg.LogPkgLevels = "*=C" |
| 68 | cfg.SetupLogging() |
| 69 | |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 70 | acurl, err := url.Parse(fmt.Sprintf("http://localhost:%d", clientPort)) |
| 71 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 72 | logger.Fatalf(ctx, "Invalid client port -%d", clientPort) |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 73 | } |
| 74 | cfg.ACUrls = []url.URL{*acurl} |
| 75 | cfg.LCUrls = []url.URL{*acurl} |
| 76 | |
| 77 | apurl, err := url.Parse(fmt.Sprintf("http://localhost:%d", peerPort)) |
| 78 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 79 | logger.Fatalf(ctx, "Invalid peer port -%d", peerPort) |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 80 | } |
| 81 | cfg.LPUrls = []url.URL{*apurl} |
| 82 | cfg.APUrls = []url.URL{*apurl} |
| 83 | |
| 84 | cfg.ClusterState = embed.ClusterStateFlagNew |
| 85 | cfg.InitialCluster = cfg.Name + "=" + apurl.String() |
| 86 | |
| 87 | return cfg |
| 88 | } |
| 89 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 90 | // getDefaultCfg specifies the default config |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 91 | func getDefaultCfg() *embed.Config { |
| 92 | cfg := embed.NewConfig() |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 93 | cfg.Debug = false |
| 94 | cfg.LogPkgLevels = "*=C" |
| 95 | cfg.SetupLogging() |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 96 | cfg.Dir = defaultLocalPersistentStorage |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 97 | return cfg |
| 98 | } |
| 99 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 100 | // StartEtcdServer creates and starts an embedded Etcd server. A local directory to store data is created for the |
| 101 | // embedded server lifetime (for the duration of a unit test. The server runs at localhost:2379. |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 102 | func StartEtcdServer(ctx context.Context, cfg *embed.Config) *EtcdServer { |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 103 | // If the server is already running, just return |
| 104 | if cfg == nil { |
| 105 | cfg = getDefaultCfg() |
| 106 | } |
| 107 | // Remove the local directory as |
| 108 | // a safeguard for the case where a prior test failed |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 109 | if err := os.RemoveAll(cfg.Dir); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 110 | logger.Fatalf(ctx, "Failure removing local directory %s", cfg.Dir) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 111 | } |
| 112 | e, err := embed.StartEtcd(cfg) |
| 113 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 114 | logger.Fatal(ctx, err) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 115 | } |
| 116 | select { |
| 117 | case <-e.Server.ReadyNotify(): |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 118 | logger.Debug(ctx, "Embedded Etcd server is ready!") |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 119 | case <-time.After(serverStartUpTimeout): |
| 120 | e.Server.HardStop() // trigger a shutdown |
| 121 | e.Close() |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 122 | logger.Fatal(ctx, "Embedded Etcd server took too long to start!") |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 123 | case err := <-e.Err(): |
| 124 | e.Server.HardStop() // trigger a shutdown |
| 125 | e.Close() |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 126 | logger.Fatalf(ctx, "Embedded Etcd server errored out - %s", err) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 127 | } |
| 128 | return &EtcdServer{server: e} |
| 129 | } |
| 130 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 131 | // Stop closes the embedded Etcd server and removes the local data directory as well |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 132 | func (es *EtcdServer) Stop(ctx context.Context) { |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 133 | if es != nil { |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 134 | storage := es.server.Config().Dir |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 135 | es.server.Server.HardStop() |
| 136 | es.server.Close() |
khenaidoo | c7005fc | 2019-11-18 19:23:57 -0500 | [diff] [blame] | 137 | if err := os.RemoveAll(storage); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 138 | logger.Fatalf(ctx, "Failure removing local directory %s", es.server.Config().Dir) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |