blob: 59d52159f69c786b13d33762df4cfbb921873b38 [file] [log] [blame]
khenaidoo59ce9dd2019-11-11 13:05:32 -05001/*
Joey Armstrong9cdee9f2024-01-03 04:56:14 -05002* Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors
khenaidoo59ce9dd2019-11-11 13:05:32 -05003
Joey Armstrong7f8436c2023-07-09 20:23:27 -04004* 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
khenaidoo59ce9dd2019-11-11 13:05:32 -05007
Joey Armstrong7f8436c2023-07-09 20:23:27 -04008* http://www.apache.org/licenses/LICENSE-2.0
khenaidoo59ce9dd2019-11-11 13:05:32 -05009
Joey Armstrong7f8436c2023-07-09 20:23:27 -040010* 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.
khenaidoo59ce9dd2019-11-11 13:05:32 -050015 */
khenaidoob6238b32020-04-07 12:07:36 -040016package etcd
khenaidoo59ce9dd2019-11-11 13:05:32 -050017
18import (
Neha Sharma94f16a92020-06-26 04:17:55 +000019 "context"
khenaidooc7005fc2019-11-18 19:23:57 -050020 "fmt"
khenaidooc7005fc2019-11-18 19:23:57 -050021 "net/url"
khenaidoo59ce9dd2019-11-11 13:05:32 -050022 "os"
khenaidoo26721882021-08-11 17:42:52 -040023 "strings"
khenaidoo59ce9dd2019-11-11 13:05:32 -050024 "time"
khenaidoo26721882021-08-11 17:42:52 -040025
26 "go.etcd.io/etcd/embed"
khenaidoo59ce9dd2019-11-11 13:05:32 -050027)
28
29const (
khenaidooc7005fc2019-11-18 19:23:57 -050030 serverStartUpTimeout = 10 * time.Second // Maximum time allowed to wait for the Etcd server to be ready
31 defaultLocalPersistentStorage = "voltha.test.embed.etcd"
khenaidoo59ce9dd2019-11-11 13:05:32 -050032)
33
Joey Armstrong7f8436c2023-07-09 20:23:27 -040034// EtcdServer represents an embedded Etcd server. It is used for testing only.
khenaidoo59ce9dd2019-11-11 13:05:32 -050035type EtcdServer struct {
36 server *embed.Etcd
37}
38
khenaidooc7005fc2019-11-18 19:23:57 -050039func 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 Sharma94f16a92020-06-26 04:17:55 +000057func MKConfig(ctx context.Context, configName string, clientPort, peerPort int, localPersistentStorageDir string, logLevel string) *embed.Config {
khenaidooc7005fc2019-11-18 19:23:57 -050058 cfg := embed.NewConfig()
59 cfg.Name = configName
60 cfg.Dir = localPersistentStorageDir
khenaidoo26721882021-08-11 17:42:52 -040061 // cfg.Logger = "zap"
khenaidooc7005fc2019-11-18 19:23:57 -050062 if !islogLevelValid(logLevel) {
Neha Sharma94f16a92020-06-26 04:17:55 +000063 logger.Fatalf(ctx, "Invalid log level -%s", logLevel)
khenaidooc7005fc2019-11-18 19:23:57 -050064 }
khenaidoo26721882021-08-11 17:42:52 -040065 // cfg.LogLevel = logLevel
66 cfg.Debug = strings.EqualFold(logLevel, "debug")
67 cfg.LogPkgLevels = "*=C"
68 cfg.SetupLogging()
69
khenaidooc7005fc2019-11-18 19:23:57 -050070 acurl, err := url.Parse(fmt.Sprintf("http://localhost:%d", clientPort))
71 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000072 logger.Fatalf(ctx, "Invalid client port -%d", clientPort)
khenaidooc7005fc2019-11-18 19:23:57 -050073 }
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 Sharma94f16a92020-06-26 04:17:55 +000079 logger.Fatalf(ctx, "Invalid peer port -%d", peerPort)
khenaidooc7005fc2019-11-18 19:23:57 -050080 }
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 Armstrong7f8436c2023-07-09 20:23:27 -040090// getDefaultCfg specifies the default config
khenaidoo59ce9dd2019-11-11 13:05:32 -050091func getDefaultCfg() *embed.Config {
92 cfg := embed.NewConfig()
khenaidoo26721882021-08-11 17:42:52 -040093 cfg.Debug = false
94 cfg.LogPkgLevels = "*=C"
95 cfg.SetupLogging()
khenaidooc7005fc2019-11-18 19:23:57 -050096 cfg.Dir = defaultLocalPersistentStorage
khenaidoo59ce9dd2019-11-11 13:05:32 -050097 return cfg
98}
99
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400100// 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 Sharma94f16a92020-06-26 04:17:55 +0000102func StartEtcdServer(ctx context.Context, cfg *embed.Config) *EtcdServer {
khenaidoo59ce9dd2019-11-11 13:05:32 -0500103 // 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
khenaidooc7005fc2019-11-18 19:23:57 -0500109 if err := os.RemoveAll(cfg.Dir); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000110 logger.Fatalf(ctx, "Failure removing local directory %s", cfg.Dir)
khenaidoo59ce9dd2019-11-11 13:05:32 -0500111 }
112 e, err := embed.StartEtcd(cfg)
113 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000114 logger.Fatal(ctx, err)
khenaidoo59ce9dd2019-11-11 13:05:32 -0500115 }
116 select {
117 case <-e.Server.ReadyNotify():
Neha Sharma94f16a92020-06-26 04:17:55 +0000118 logger.Debug(ctx, "Embedded Etcd server is ready!")
khenaidoo59ce9dd2019-11-11 13:05:32 -0500119 case <-time.After(serverStartUpTimeout):
120 e.Server.HardStop() // trigger a shutdown
121 e.Close()
Neha Sharma94f16a92020-06-26 04:17:55 +0000122 logger.Fatal(ctx, "Embedded Etcd server took too long to start!")
khenaidoo59ce9dd2019-11-11 13:05:32 -0500123 case err := <-e.Err():
124 e.Server.HardStop() // trigger a shutdown
125 e.Close()
Neha Sharma94f16a92020-06-26 04:17:55 +0000126 logger.Fatalf(ctx, "Embedded Etcd server errored out - %s", err)
khenaidoo59ce9dd2019-11-11 13:05:32 -0500127 }
128 return &EtcdServer{server: e}
129}
130
Joey Armstrong7f8436c2023-07-09 20:23:27 -0400131// Stop closes the embedded Etcd server and removes the local data directory as well
Neha Sharma94f16a92020-06-26 04:17:55 +0000132func (es *EtcdServer) Stop(ctx context.Context) {
khenaidoo59ce9dd2019-11-11 13:05:32 -0500133 if es != nil {
khenaidooc7005fc2019-11-18 19:23:57 -0500134 storage := es.server.Config().Dir
khenaidoo59ce9dd2019-11-11 13:05:32 -0500135 es.server.Server.HardStop()
136 es.server.Close()
khenaidooc7005fc2019-11-18 19:23:57 -0500137 if err := os.RemoveAll(storage); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000138 logger.Fatalf(ctx, "Failure removing local directory %s", es.server.Config().Dir)
khenaidoo59ce9dd2019-11-11 13:05:32 -0500139 }
140 }
141}