Skip to content

Commit

Permalink
destroy: fix folder as topology ref
Browse files Browse the repository at this point in the history
  • Loading branch information
steiler committed Nov 27, 2023
1 parent ab128e4 commit 468efb7
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 55 deletions.
71 changes: 37 additions & 34 deletions clab/clab.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,32 +128,10 @@ func WithKeepMgmtNet() ClabOption {

func WithTopoPath(path, varsFile string) ClabOption {
return func(c *CLab) error {
var file string
var err error

switch {
case path == "-" || path == "stdin":
file, err = c.readFromStdin()
if err != nil {
return err
}
// if the path is not a local file and a URL, download the file and store it in the tmp dir
case !utils.FileOrDirExists(path) && utils.IsHttpURL(path, true):
file, err = c.downloadTopoFile(path)
if err != nil {
return err
}

case path == "":
return fmt.Errorf("provide a path to the clab topology file")

default:
file, err = findTopoFileByPath(path)
if err != nil {
return err
}
file, err := ProcessTopoPath(path, c.TopoPaths.ClabTmpDir())
if err != nil {
return err
}

if err := c.GetTopology(file, varsFile); err != nil {
return fmt.Errorf("failed to read topology file: %v", err)
}
Expand All @@ -162,9 +140,38 @@ func WithTopoPath(path, varsFile string) ClabOption {
}
}

func ProcessTopoPath(path string, tmpDir string) (string, error) {
var file string
var err error

switch {
case path == "-" || path == "stdin":
file, err = readFromStdin(tmpDir)
if err != nil {
return "", err
}
// if the path is not a local file and a URL, download the file and store it in the tmp dir
case !utils.FileOrDirExists(path) && utils.IsHttpURL(path, true):
file, err = downloadTopoFile(path, tmpDir)
if err != nil {
return "", err
}

case path == "":
return "", fmt.Errorf("provide a path to the clab topology file")

default:
file, err = FindTopoFileByPath(path)
if err != nil {
return "", err
}
}
return file, nil
}

// findTopoFileByPath takes a topology path, which might be the path to a directory
// and returns the topology file name if found.
func findTopoFileByPath(path string) (string, error) {
func FindTopoFileByPath(path string) (string, error) {
finfo, err := os.Stat(path)
if err != nil {
return "", err
Expand Down Expand Up @@ -206,10 +213,8 @@ func findTopoFileByPath(path string) (string, error) {
// readFromStdin reads the topology file from stdin
// creates a temp file with topology contents
// and returns a path to the temp file.
func (c *CLab) readFromStdin() (string, error) {
c.TopoPaths.CreateTmpDir()

tmpFile, err := os.CreateTemp(c.TopoPaths.ClabTmpDir(), "topo-*.clab.yml")
func readFromStdin(tempDir string) (string, error) {
tmpFile, err := os.CreateTemp(tempDir, "topo-*.clab.yml")
if err != nil {
return "", err
}
Expand All @@ -222,10 +227,8 @@ func (c *CLab) readFromStdin() (string, error) {
return tmpFile.Name(), nil
}

func (c *CLab) downloadTopoFile(url string) (string, error) {
c.TopoPaths.CreateTmpDir()

tmpFile, err := os.CreateTemp(c.TopoPaths.ClabTmpDir(), "topo-*.clab.yml")
func downloadTopoFile(url, tempDir string) (string, error) {
tmpFile, err := os.CreateTemp(tempDir, "topo-*.clab.yml")
if err != nil {
return "", err
}
Expand Down
3 changes: 0 additions & 3 deletions clab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,6 @@ func (c *CLab) processStartupConfig(nodeCfg *types.NodeConfig) error {
isDownloadableConfig := utils.IsHttpURL(p, false)

if isEmbeddedConfig || isDownloadableConfig {
// both embedded and downloadable configs are require clab tmp dir to be created
c.TopoPaths.CreateTmpDir()

switch {
case isEmbeddedConfig:
log.Debugf("Node %q startup-config is an embedded config: %q", nodeCfg.ShortName, p)
Expand Down
5 changes: 5 additions & 0 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ func listContainers(ctx context.Context, topo string) ([]runtime.GenericContaine

// when topo file is provided, filter containers by lab name
if topo != "" {
topo, err = clab.ProcessTopoPath(topo, c.TopoPaths.ClabTmpDir())
if err != nil {
return nil, err
}

// read topo yaml file to get the lab name
topo, err := os.ReadFile(topo)
if err != nil {
Expand Down
44 changes: 32 additions & 12 deletions tests/01-smoke/12-cloned-lab.robot
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
Library Process
Resource ../common.robot

Suite Setup Cleanup
Suite Teardown Cleanup
Test Teardown Cleanup


*** Variables ***
Expand All @@ -16,6 +15,7 @@ ${lab1-gitlab-url} https://github.com/hellt/clab-test-repo
${lab1-gitlab-url2} https://github.com/hellt/clab-test-repo/blob/main/lab1.clab.yml
${lab2-gitlab-url} https://github.com/hellt/clab-test-repo/tree/branch1
${http-lab-url} https://gist.githubusercontent.com/hellt/66a5d8fca7bf526b46adae9008a5e04b/raw/034a542c3fbb17333afd20e6e7d21869fee6aeb5/linux.clab.yml
${single-topo-folder} tests/01-smoke/single-topo-folder
${runtime} docker


Expand All @@ -33,7 +33,6 @@ Test lab1 with Github
# check that node3 was filtered and not present in the lab output
Should Contain ${output.stdout} clab-lab1-node1

Cleanup

Test lab1 with Gitlab
${output} = Process.Run Process
Expand All @@ -48,7 +47,6 @@ Test lab1 with Gitlab
# check that node3 was filtered and not present in the lab output
Should Contain ${output.stdout} clab-lab1-node1

Cleanup

Test lab2 with Github
${output} = Process.Run Process
Expand All @@ -63,7 +61,6 @@ Test lab2 with Github
# check that node3 was filtered and not present in the lab output
Should Contain ${output.stdout} clab-lab1-node1

Cleanup

Test lab2 with Gitlab
${output} = Process.Run Process
Expand All @@ -78,7 +75,6 @@ Test lab2 with Gitlab
# check that node3 was filtered and not present in the lab output
Should Contain ${output.stdout} clab-lab1-node1

Cleanup

Test lab3 with Github
${output} = Process.Run Process
Expand All @@ -93,7 +89,6 @@ Test lab3 with Github
# check that node3 was filtered and not present in the lab output
Should Contain ${output.stdout} clab-lab2-node1

Cleanup

Test lab3 with Gitlab
${output} = Process.Run Process
Expand All @@ -108,7 +103,6 @@ Test lab3 with Gitlab
# check that node3 was filtered and not present in the lab output
Should Contain ${output.stdout} clab-lab2-node1

Cleanup

Test lab1 with short github url
${output} = Process.Run Process
Expand All @@ -120,10 +114,8 @@ Test lab1 with short github url

Should Be Equal As Integers ${output.rc} 0

# check that node3 was filtered and not present in the lab output
Should Contain ${output.stdout} clab-lab1-node1

Cleanup

Test lab1 downloaded from https url
${output} = Process.Run Process
Expand All @@ -135,10 +127,38 @@ Test lab1 downloaded from https url

Should Be Equal As Integers ${output.rc} 0

# check that node3 was filtered and not present in the lab output
Should Contain ${output.stdout} clab-alpine-l1

Cleanup

Test deploy referencing folder as topo
${output_pre} = Process.Run Process
... sudo -E ${CLAB_BIN} --runtime ${runtime} deploy -t ${single-topo-folder}
... shell=True

Log ${output_pre.stdout}
Log ${output_pre.stderr}

Should Be Equal As Integers ${output_pre.rc} 0

## double check deletion via runtime ps
${output_post2} = Process.Run Process
... ${runtime} ps
... shell=True

Should Contain ${output_post2.stdout} clab-lab1-node1


## destroy with just a reference to a folder
${output_post1} = Process.Run Process
... sudo -E ${CLAB_BIN} --runtime ${runtime} destroy -t ${single-topo-folder}
... shell=True

## double check deletion via runtime ps
${output_post2} = Process.Run Process
... ${runtime} ps
... shell=True

Should Not Contain ${output_post2.stdout} clab-lab1-node1


*** Keywords ***
Expand Down
7 changes: 7 additions & 0 deletions tests/01-smoke/single-topo-folder/lab1.clab.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: lab1

topology:
nodes:
node1:
kind: linux
image: alpine:3
10 changes: 4 additions & 6 deletions types/topo_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,13 @@ func (t *TopoPaths) TopologyFilenameAbsPath() string {
}

// ClabTmpDir returns the path to the temporary directory where clab stores temporary and/or downloaded files.
func (*TopoPaths) ClabTmpDir() string {
func (t *TopoPaths) ClabTmpDir() string {
if !utils.DirExists(clabTmpDir) {
utils.CreateDirectory(t.ClabTmpDir(), 0755)
}
return clabTmpDir
}

// CreateTmpDir creates a clab temp directory.
func (t *TopoPaths) CreateTmpDir() {
utils.CreateDirectory(t.ClabTmpDir(), 0755)
}

// StartupConfigDownloadFileAbsPath returns the absolute path to the startup-config file
// when it is downloaded from a remote location to the clab temp directory.
func (t *TopoPaths) StartupConfigDownloadFileAbsPath(node, postfix string) string {
Expand Down

0 comments on commit 468efb7

Please sign in to comment.