Skip to content

Commit e1ae5e0

Browse files
steilerhellt
andauthored
destroy: fix folder as topology ref (#1747)
* destroy: fix folder as topology ref * update * execute runtime ps via sudo * remove param * added comment --------- Co-authored-by: Roman Dodin <dodin.roman@gmail.com>
1 parent ab128e4 commit e1ae5e0

File tree

6 files changed

+87
-54
lines changed

6 files changed

+87
-54
lines changed

clab/clab.go

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -128,32 +128,10 @@ func WithKeepMgmtNet() ClabOption {
128128

129129
func WithTopoPath(path, varsFile string) ClabOption {
130130
return func(c *CLab) error {
131-
var file string
132-
var err error
133-
134-
switch {
135-
case path == "-" || path == "stdin":
136-
file, err = c.readFromStdin()
137-
if err != nil {
138-
return err
139-
}
140-
// if the path is not a local file and a URL, download the file and store it in the tmp dir
141-
case !utils.FileOrDirExists(path) && utils.IsHttpURL(path, true):
142-
file, err = c.downloadTopoFile(path)
143-
if err != nil {
144-
return err
145-
}
146-
147-
case path == "":
148-
return fmt.Errorf("provide a path to the clab topology file")
149-
150-
default:
151-
file, err = findTopoFileByPath(path)
152-
if err != nil {
153-
return err
154-
}
131+
file, err := c.ProcessTopoPath(path)
132+
if err != nil {
133+
return err
155134
}
156-
157135
if err := c.GetTopology(file, varsFile); err != nil {
158136
return fmt.Errorf("failed to read topology file: %v", err)
159137
}
@@ -162,9 +140,40 @@ func WithTopoPath(path, varsFile string) ClabOption {
162140
}
163141
}
164142

143+
// ProcessTopoPath takes a topology path, which might be the path to a directory or a file
144+
// or stdin or a URL and returns the topology file name if found.
145+
func (c *CLab) ProcessTopoPath(path string) (string, error) {
146+
var file string
147+
var err error
148+
149+
switch {
150+
case path == "-" || path == "stdin":
151+
file, err = readFromStdin(c.TopoPaths.ClabTmpDir())
152+
if err != nil {
153+
return "", err
154+
}
155+
// if the path is not a local file and a URL, download the file and store it in the tmp dir
156+
case !utils.FileOrDirExists(path) && utils.IsHttpURL(path, true):
157+
file, err = downloadTopoFile(path, c.TopoPaths.ClabTmpDir())
158+
if err != nil {
159+
return "", err
160+
}
161+
162+
case path == "":
163+
return "", fmt.Errorf("provide a path to the clab topology file")
164+
165+
default:
166+
file, err = FindTopoFileByPath(path)
167+
if err != nil {
168+
return "", err
169+
}
170+
}
171+
return file, nil
172+
}
173+
165174
// findTopoFileByPath takes a topology path, which might be the path to a directory
166175
// and returns the topology file name if found.
167-
func findTopoFileByPath(path string) (string, error) {
176+
func FindTopoFileByPath(path string) (string, error) {
168177
finfo, err := os.Stat(path)
169178
if err != nil {
170179
return "", err
@@ -206,10 +215,8 @@ func findTopoFileByPath(path string) (string, error) {
206215
// readFromStdin reads the topology file from stdin
207216
// creates a temp file with topology contents
208217
// and returns a path to the temp file.
209-
func (c *CLab) readFromStdin() (string, error) {
210-
c.TopoPaths.CreateTmpDir()
211-
212-
tmpFile, err := os.CreateTemp(c.TopoPaths.ClabTmpDir(), "topo-*.clab.yml")
218+
func readFromStdin(tempDir string) (string, error) {
219+
tmpFile, err := os.CreateTemp(tempDir, "topo-*.clab.yml")
213220
if err != nil {
214221
return "", err
215222
}
@@ -222,10 +229,8 @@ func (c *CLab) readFromStdin() (string, error) {
222229
return tmpFile.Name(), nil
223230
}
224231

225-
func (c *CLab) downloadTopoFile(url string) (string, error) {
226-
c.TopoPaths.CreateTmpDir()
227-
228-
tmpFile, err := os.CreateTemp(c.TopoPaths.ClabTmpDir(), "topo-*.clab.yml")
232+
func downloadTopoFile(url, tempDir string) (string, error) {
233+
tmpFile, err := os.CreateTemp(tempDir, "topo-*.clab.yml")
229234
if err != nil {
230235
return "", err
231236
}

clab/config.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,6 @@ func (c *CLab) processStartupConfig(nodeCfg *types.NodeConfig) error {
267267
isDownloadableConfig := utils.IsHttpURL(p, false)
268268

269269
if isEmbeddedConfig || isDownloadableConfig {
270-
// both embedded and downloadable configs are require clab tmp dir to be created
271-
c.TopoPaths.CreateTmpDir()
272-
273270
switch {
274271
case isEmbeddedConfig:
275272
log.Debugf("Node %q startup-config is an embedded config: %q", nodeCfg.ShortName, p)

cmd/destroy.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ func listContainers(ctx context.Context, topo string) ([]runtime.GenericContaine
271271

272272
// when topo file is provided, filter containers by lab name
273273
if topo != "" {
274+
topo, err = c.ProcessTopoPath(topo)
275+
if err != nil {
276+
return nil, err
277+
}
278+
274279
// read topo yaml file to get the lab name
275280
topo, err := os.ReadFile(topo)
276281
if err != nil {

tests/01-smoke/12-cloned-lab.robot

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
Library Process
33
Resource ../common.robot
44

5-
Suite Setup Cleanup
6-
Suite Teardown Cleanup
5+
Test Teardown Cleanup
76

87

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

2121

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

36-
Cleanup
3736

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

51-
Cleanup
5250

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

66-
Cleanup
6764

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

81-
Cleanup
8278

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

96-
Cleanup
9792

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

111-
Cleanup
112106

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

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

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

126-
Cleanup
127119

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

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

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

141-
Cleanup
132+
133+
Test deploy referencing folder as topo
134+
${output_pre} = Process.Run Process
135+
... sudo -E ${CLAB_BIN} --runtime ${runtime} deploy -t ${single-topo-folder}
136+
... shell=True
137+
138+
Log ${output_pre.stdout}
139+
Log ${output_pre.stderr}
140+
141+
Should Be Equal As Integers ${output_pre.rc} 0
142+
143+
## double check deletion via runtime ps
144+
${output_post2} = Process.Run Process
145+
... sudo -E ${runtime} ps
146+
... shell=True
147+
148+
Should Contain ${output_post2.stdout} clab-lab1-node1
149+
150+
151+
## destroy with just a reference to a folder
152+
${output_post1} = Process.Run Process
153+
... sudo -E ${CLAB_BIN} --runtime ${runtime} destroy -t ${single-topo-folder}
154+
... shell=True
155+
156+
## double check deletion via runtime ps
157+
${output_post2} = Process.Run Process
158+
... sudo -E ${runtime} ps
159+
... shell=True
160+
161+
Should Not Contain ${output_post2.stdout} clab-lab1-node1
142162

143163

144164
*** Keywords ***
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: lab1
2+
3+
topology:
4+
nodes:
5+
node1:
6+
kind: linux
7+
image: alpine:3

types/topo_paths.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,14 @@ func (t *TopoPaths) TopologyFilenameAbsPath() string {
175175
}
176176

177177
// ClabTmpDir returns the path to the temporary directory where clab stores temporary and/or downloaded files.
178+
// Creates the directory if it does not exist.
178179
func (*TopoPaths) ClabTmpDir() string {
180+
if !utils.DirExists(clabTmpDir) {
181+
utils.CreateDirectory(clabTmpDir, 0755)
182+
}
179183
return clabTmpDir
180184
}
181185

182-
// CreateTmpDir creates a clab temp directory.
183-
func (t *TopoPaths) CreateTmpDir() {
184-
utils.CreateDirectory(t.ClabTmpDir(), 0755)
185-
}
186-
187186
// StartupConfigDownloadFileAbsPath returns the absolute path to the startup-config file
188187
// when it is downloaded from a remote location to the clab temp directory.
189188
func (t *TopoPaths) StartupConfigDownloadFileAbsPath(node, postfix string) string {

0 commit comments

Comments
 (0)