Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | */ |
| 16 | |
| 17 | package core |
| 18 | |
| 19 | import ( |
Matteo Scandolo | f7b260d | 2019-07-31 13:43:02 -0700 | [diff] [blame] | 20 | "bytes" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 21 | "context" |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 22 | "fmt" |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 23 | "os/exec" |
| 24 | "strconv" |
| 25 | "time" |
| 26 | |
Zack Williams | 2abf393 | 2019-08-05 14:07:05 -0700 | [diff] [blame] | 27 | "github.com/opencord/voltha-bbsim/common/logger" |
| 28 | "github.com/opencord/voltha-bbsim/device" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 29 | ) |
| 30 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 31 | // TestManager is the structure for test manager |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 32 | type TestManager struct { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 33 | DhcpServerIP string |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 34 | Pid []int |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 35 | testers map[string]map[device.Devkey]*Tester |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 36 | ctx context.Context |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 37 | cancel context.CancelFunc |
| 38 | } |
| 39 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 40 | // Tester is the structure for Test |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 41 | type Tester struct { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 42 | Type string |
| 43 | Key device.Devkey |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 44 | Testfunc func(device.Devkey) error |
| 45 | Waitsec int |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 46 | ctx context.Context |
| 47 | cancel context.CancelFunc |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 48 | } |
| 49 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 50 | // NewTestManager returns new TestManager |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 51 | func NewTestManager(opt *option) *TestManager { |
| 52 | t := new(TestManager) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 53 | t.DhcpServerIP = opt.dhcpservip |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 54 | return t |
| 55 | } |
| 56 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 57 | // CreateTester creates instance of Tester |
| 58 | func (*TestManager) CreateTester(testtype string, opt *option, key device.Devkey, fn func(device.Devkey) error, waitsec int) *Tester { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 59 | logger.Debug("CreateTester() called") |
| 60 | t := new(Tester) |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 61 | t.Type = testtype |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 62 | t.Key = key |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 63 | t.Testfunc = fn |
| 64 | t.Waitsec = waitsec |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 65 | return t |
| 66 | } |
| 67 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 68 | // Start does starting action - Blocking Call |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 69 | func (tm *TestManager) Start() error { |
| 70 | ctx, cancel := context.WithCancel(context.Background()) |
| 71 | tm.ctx = ctx |
| 72 | tm.cancel = cancel |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 73 | tm.testers = make(map[string]map[device.Devkey]*Tester) |
| 74 | tm.testers["AAA"] = map[device.Devkey]*Tester{} |
| 75 | tm.testers["DHCP"] = map[device.Devkey]*Tester{} |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 76 | logger.Info("TestManager start") |
| 77 | return nil |
| 78 | } |
| 79 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 80 | // Stop does stopping action |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 81 | func (tm *TestManager) Stop() error { |
| 82 | if tm.cancel != nil { |
| 83 | tm.cancel() |
| 84 | } |
| 85 | tm.Initialize() |
| 86 | logger.Debug("TestManager Done") |
| 87 | return nil |
| 88 | } |
| 89 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 90 | func (tm *TestManager) StartTester(t *Tester) error { |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 91 | testtype := t.Type |
| 92 | key := t.Key |
| 93 | waitsec := t.Waitsec |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 94 | |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 95 | // TODO use timers instead of this crazy loop: https://gobyexample.com/timers |
| 96 | |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 97 | logger.Debug("StartTester type:%s called with key:%v", testtype, key) |
| 98 | child, cancel := context.WithCancel(tm.ctx) |
| 99 | t.ctx = child |
| 100 | t.cancel = cancel |
| 101 | go func() error { |
| 102 | tick := time.NewTicker(time.Second) |
| 103 | counter := 0 |
| 104 | defer func() { |
| 105 | tick.Stop() |
| 106 | logger.Debug("Tester type:%s with key %v Done", testtype, key) |
| 107 | }() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 108 | |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 109 | L: |
| 110 | for counter < waitsec { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 111 | select { |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 112 | case <-tick.C: |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 113 | counter++ |
| 114 | if counter == waitsec { // TODO: This should be fixed |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 115 | break L |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 116 | } |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 117 | case <-child.Done(): |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 118 | return nil |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 119 | } |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 120 | } |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 121 | err := t.Testfunc(key) |
| 122 | if err != nil { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 123 | return err |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 124 | } |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 125 | return nil |
| 126 | }() |
| 127 | |
| 128 | tm.testers[testtype][key] = t |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 129 | return nil |
| 130 | } |
| 131 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 132 | // StopTester stops the test |
| 133 | func (tm *TestManager) StopTester(testtype string, key device.Devkey) error { |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 134 | ts := tm.testers[testtype][key] |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 135 | ts.cancel() |
Keita NISHIMOTO | 2807c54 | 2019-06-04 22:58:32 +0900 | [diff] [blame] | 136 | delete(tm.testers[testtype], key) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 137 | return nil |
| 138 | } |
| 139 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 140 | // Initialize test manager |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 141 | func (tm *TestManager) Initialize() { |
| 142 | logger.Info("TestManager Initialize () called") |
| 143 | pids := tm.Pid |
| 144 | logger.Debug("Runnig Process: %v", pids) |
| 145 | KillProcesses(pids) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 146 | exec.Command("rm", "/var/run/dhcpd.pid").Run() // This is for DHCP server activation |
| 147 | exec.Command("touch", "/var/run/dhcpd.pid").Run() // This is for DHCP server activation |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 148 | } |
| 149 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 150 | // KillProcesses kill process by specified pid |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 151 | func KillProcesses(pids []int) error { |
| 152 | for _, pname := range pids { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 153 | killProcess(pname) |
| 154 | } |
| 155 | return nil |
| 156 | } |
| 157 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 158 | func killProcess(pid int) error { |
| 159 | err := exec.Command("kill", strconv.Itoa(pid)).Run() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 160 | if err != nil { |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 161 | logger.Error("Fail to kill %d: %v", pid, err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 162 | return err |
| 163 | } |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 164 | logger.Info("Successfully killed %d", pid) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 165 | return nil |
| 166 | } |
| 167 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 168 | func activateWPASupplicant(key device.Devkey) (err error) { |
| 169 | if err = startEAPClient(key.Intfid, key.ID); err != nil { |
| 170 | errmsg := fmt.Sprintf("Failed to activate WPA Supplicant intfid: %d onuid: %d", key.Intfid, key.ID) |
| 171 | logger.Error(errmsg) |
Keita NISHIMOTO | 621a43d | 2019-01-30 20:53:03 +0900 | [diff] [blame] | 172 | } |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 173 | logger.Info("Successfuly activateWPASupplicant() for intfid:%d onuid:%d", key.Intfid, key.ID) |
| 174 | return nil |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 175 | } |
| 176 | |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 177 | func activateDHCPClient(key device.Devkey) (err error) { |
| 178 | if err = startDHCPClient(key.Intfid, key.ID); err != nil { |
| 179 | errmsg := fmt.Sprintf("Failed to activate DHCP client intfid: %d onuid: %d", key.Intfid, key.ID) |
| 180 | logger.Error(errmsg) |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 181 | } |
Keita NISHIMOTO | dd9f673 | 2019-02-09 09:41:31 +0900 | [diff] [blame] | 182 | return nil |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 183 | } |
| 184 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 185 | func activateDHCPServer(veth string, serverip string) error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 186 | err := exec.Command("ip", "addr", "add", serverip, "dev", veth).Run() |
| 187 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 188 | logger.Error("Fail to add ip to %s address: %s", veth, err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 189 | return err |
| 190 | } |
| 191 | err = exec.Command("ip", "link", "set", veth, "up").Run() |
| 192 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 193 | logger.Error("Fail to set %s up: %s", veth, err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 194 | return err |
| 195 | } |
Matteo Scandolo | f7b260d | 2019-07-31 13:43:02 -0700 | [diff] [blame] | 196 | dhcp := "/usr/local/bin/dhcpd" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 197 | conf := "/etc/dhcp/dhcpd.conf" |
Keita NISHIMOTO | 2f8a6a4 | 2019-02-08 09:47:07 +0900 | [diff] [blame] | 198 | logfile := "/tmp/dhcplog" |
Matteo Scandolo | f7b260d | 2019-07-31 13:43:02 -0700 | [diff] [blame] | 199 | var stderr bytes.Buffer |
| 200 | cmd := exec.Command(dhcp, "-cf", conf, veth, "-tf", logfile) |
| 201 | cmd.Stderr = &stderr |
| 202 | err = cmd.Run() |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 203 | if err != nil { |
Matteo Scandolo | f7b260d | 2019-07-31 13:43:02 -0700 | [diff] [blame] | 204 | logger.Error("Fail to activateDHCP Server (): %s, %s", err, stderr.String()) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 205 | return err |
| 206 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 207 | logger.Info("DHCP Server is successfully activated !") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 208 | return err |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 209 | } |