-
Notifications
You must be signed in to change notification settings - Fork 67
Network: support down NIC #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 19 commits
bacff83
d836369
b94544b
6a7e0a7
c591c0b
d0336c5
e965e69
f74fd55
be47675
2aef494
aab691f
04f85b0
ad42bf6
e19ff4a
b910e22
757cca1
6d9be7b
f569070
899ed84
a250152
9d0c306
54e8072
2af752c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,24 @@ func (networkAttack) Attack(options core.AttackConfig, env Environment) (err err | |
| return errors.WithStack(err) | ||
| } | ||
| } | ||
|
|
||
| case core.NetworkNICDownAction: | ||
| if err := env.Chaos.getNICIP(attack); err != nil { | ||
| return errors.WithStack(err) | ||
| } | ||
|
|
||
| NICDownCommand := fmt.Sprintf("ifconfig %s down", attack.Device) | ||
|
|
||
| cmd := exec.Command("bash", "-c", NICDownCommand) | ||
| _, err := cmd.CombinedOutput() | ||
| if err != nil { | ||
| return errors.WithStack(err) | ||
| } | ||
|
|
||
| if attack.Duration != "-1" { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this should be
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not check this field before executing this attack?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This field has been checked for necessity before executing,
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remember you add a note that the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, this |
||
| err := env.Chaos.recoverNICDownScheduled(attack) | ||
| return errors.WithStack(err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
|
|
@@ -348,6 +366,8 @@ func (networkAttack) Recover(exp core.Experiment, env Environment) error { | |
| return errors.WithStack(err) | ||
| } | ||
| } | ||
| case core.NetworkNICDownAction: | ||
| return env.Chaos.recoverNICDown(attack) | ||
| } | ||
| return nil | ||
| } | ||
|
|
@@ -511,3 +531,49 @@ func (s *Server) recoverEtcHosts(attack *core.NetworkCommand, uid string) error | |
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (s *Server) recoverNICDown(attack *core.NetworkCommand) error { | ||
| NICUpCommand := fmt.Sprintf("ifconfig %s %s up", attack.Device, attack.IPAddress) | ||
|
|
||
| recoverCmd := exec.Command("bash", "-c", NICUpCommand) | ||
| _, err := recoverCmd.CombinedOutput() | ||
| if err != nil { | ||
| return errors.WithStack(err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (s *Server) recoverNICDownScheduled(attack *core.NetworkCommand) error { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this way, we need to set the experiment's status to I think it's better to refine the
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can refine the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this solution is temporary, real |
||
| NICUpCommand := fmt.Sprintf("sleep %s && ifconfig %s %s up", attack.Duration, attack.Device, attack.IPAddress) | ||
|
|
||
| recoverCmd := exec.Command("bash", "-c", NICUpCommand) | ||
| _, err := recoverCmd.CombinedOutput() | ||
| if err != nil { | ||
| return errors.WithStack(err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (s *Server) getNICIP(attack *core.NetworkCommand) error { | ||
| getIPCommand := fmt.Sprintf("ifconfig %s | awk '/inet\\>/ {print $2}'", attack.Device) | ||
|
|
||
| cmd := exec.Command("bash", "-c", getIPCommand) | ||
| stdout, err := cmd.StdoutPipe() | ||
| if err != nil { | ||
| return errors.WithStack(err) | ||
| } | ||
|
|
||
| if err = cmd.Start(); err != nil { | ||
| return errors.WithStack(err) | ||
| } | ||
|
|
||
| stdoutBytes := make([]byte, 1024) | ||
| _, err = stdout.Read(stdoutBytes) | ||
| if err != nil { | ||
| return errors.WithStack(err) | ||
| } | ||
| attack.IPAddress = strings.TrimRight(string(stdoutBytes), "\n\x00") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add some comments to explain why need to trim
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DONE IT! |
||
|
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Copyright 2022 Chaos Mesh Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| set -u | ||
|
|
||
| cur=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) | ||
| cd $cur | ||
|
|
||
| bin_path=../../../bin | ||
|
|
||
| # test nic down | ||
| nic=$(cat /proc/net/dev | awk '{i++; if(i>2){print $1}}' | sed 's/^[\t]*//g' | sed 's/[:]*$//g' | head -n 1) | ||
|
|
||
| ifconfig ${nic}:0 192.168.10.28 up | ||
|
|
||
| test_nic=$(ifconfig | grep ${nic}:0 | sed 's/:0:.*/:0/g') | ||
| if [[ test_nic == "" ]]; then | ||
| echo "create test nic failed" | ||
| exit 1 | ||
| fi | ||
|
|
||
| ${bin_path}/chaosd attack network down -d ${test_nic} --duration 1s | ||
| stat=$(ifconfig | grep ${test_nic} | sed 's/:0:.*/:0/g') | ||
|
|
||
| if [[ -n ${stat} ]]; then | ||
| echo "fail to disable the test nic" | ||
| ifconfig ${test_nic} down | ||
| exit 1 | ||
| fi | ||
|
|
||
| sleep 1s | ||
|
|
||
| stat=$(ifconfig | grep ${test_nic} | sed 's/:0:.*/:0/g') | ||
| if [[ ${stat} != ${test_nic} ]]; then | ||
| echo "fail to enable the test nic" | ||
| exit 1 | ||
| fi | ||
|
|
||
| ifconfig ${test_nic} down | ||
|
|
||
| exit 0 |
Uh oh!
There was an error while loading. Please reload this page.