Skip to content

Commit

Permalink
incorporated comments No. 4
Browse files Browse the repository at this point in the history
  • Loading branch information
steiler committed Jul 20, 2021
1 parent c426be3 commit b4e25d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
22 changes: 13 additions & 9 deletions clab/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package clab
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"strings"

"github.com/cloudflare/cfssl/log"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
)

const (
Expand All @@ -19,9 +18,16 @@ const (
)

func AppendHostsFileEntries(containers []types.GenericContainer, labname string) error {
filename := "/etc/hosts"
if labname == "" {
return fmt.Errorf("missing lab name")
}
if !utils.FileExists(filename) {
err := utils.CreateFile(filename, "127.0.0.1\tlocalhost")
if err != nil {
return err
}
}
// lets make sure we do not have remaining of a non destroyed run in the hosts file
err := DeleteEntriesFromHostsFile(labname)
if err != nil {
Expand All @@ -31,10 +37,13 @@ func AppendHostsFileEntries(containers []types.GenericContainer, labname string)
if len(data) == 0 {
return nil
}
f, err := os.OpenFile("/etc/hosts", os.O_APPEND|os.O_WRONLY, os.ModeAppend)
var f *os.File

f, err = os.OpenFile("/etc/hosts", os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
return err
}

defer f.Close()
_, err = f.Write(data)
if err != nil {
Expand Down Expand Up @@ -78,12 +87,7 @@ func DeleteEntriesFromHostsFile(labname string) error {
}
f, err := os.OpenFile("/etc/hosts", os.O_RDWR, 0644) // skipcq: GSC-G302
if err != nil {
if errors.Is(err, os.ErrNotExist) {
log.Info("/etc/hosts does not exist")
return nil
} else {
return err
}
return err
}
defer f.Close()
reader := bufio.NewReader(f)
Expand Down
16 changes: 14 additions & 2 deletions tests/01-smoke/01-basic-flow.robot
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ ${n2-ipv4} 172.20.20.100/24
${n2-ipv6} 2001:172:20:20::100/64

*** Test Cases ***
Verify number of Hosts entries before deploy
${rc} ${output} = Run And Return Rc And Output
... cat /etc/hosts | wc -l
Log ${output}
Set Suite Variable ${HostsFileLines} ${output}

Deploy ${lab-name} lab
Log ${CURDIR}
${rc} ${output} = Run And Return Rc And Output
Expand Down Expand Up @@ -110,7 +116,7 @@ Verify l1 environment has MYVAR variable set
Verify Hosts entries exist
[Documentation] Verification that the expected /etc/hosts entries are created. We are also checking for the HEADER and FOOTER values here, which also contain the lab name.
${rc} ${output} = Run And Return Rc And Output
... cat /etc/hosts | grep "2-linux-nodes" | wc -l
... cat /etc/hosts | grep "${lab-name}" | wc -l
Log ${output}
Should Be Equal As Integers ${rc} 0
Should Contain ${output} 6
Expand All @@ -124,10 +130,16 @@ Destroy ${lab-name} lab
Verify Hosts entries are gone
[Documentation] Verification that the previously created /etc/hosts entries are properly removed. (Again including HEADER and FOOTER).
${rc} ${output} = Run And Return Rc And Output
... cat /etc/hosts | grep "2-linux-nodes" | wc -l
... cat /etc/hosts | grep "${lab-name}" | wc -l
Log ${output}
Should Be Equal As Integers ${rc} 0
Should Contain ${output} 0

Verify Hosts file has same number of lines
${rc} ${output} = Run And Return Rc And Output
... cat /etc/hosts | wc -l
Log ${output}
Should Be Equal As Integers ${HostsFileLines} ${output}

*** Keywords ***
Setup
Expand Down

0 comments on commit b4e25d6

Please sign in to comment.