Skip to content

Commit bd617c2

Browse files
committed
tests: add behave tests for top --layer
Signed-off-by: Danish Prakash <[email protected]>
1 parent 9b8a4d9 commit bd617c2

File tree

7 files changed

+220
-28
lines changed

7 files changed

+220
-28
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
/skiff
2+
bin/
3+
vendor/

cmd/skiff/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import (
1313
)
1414

1515
func main() {
16+
// Customize the help flag to only use --help, not -h
17+
// -h is used for --human-readable
18+
cli.HelpFlag = &cli.BoolFlag{
19+
Name: "help",
20+
Usage: "show help",
21+
}
1622

1723
cmd := &cli.Command{
1824
Name: "skiff",

cmd/skiff/top.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import (
2222
)
2323

2424
var topCommand = cli.Command{
25-
Name: "top",
26-
Usage: "Analyze a container image and list files by size",
25+
Name: "top",
26+
Usage: "Analyze a container image and list files by size",
27+
ArgsUsage: "[image]",
2728
Flags: []cli.Flag{
2829
&cli.BoolFlag{Name: "include-pseudo", Usage: "Include pseudo-filesystems (/dev, /proc, /sys)"},
2930
&cli.BoolFlag{Name: "follow-symlinks", Usage: "Follow symbolic links"},

features/layers.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Feature: `skiff layers` command
1818
And stdout contains
1919
"""
2020
OPTIONS:
21-
--help, -h\s+show help
21+
--help show help
2222
"""
2323

2424
Scenario: Analyze an actual image

features/steps/common.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,20 @@ def step_impl(context):
105105
raise AssertionError(
106106
f"Stdout doesn't equal:\n{expected}\n\nActual stdout:\n{found}"
107107
)
108+
109+
110+
@behave.step('stdout contains "{text}"')
111+
def step_impl(context, text):
112+
found = context.cmd_stdout.rstrip()
113+
if text not in found:
114+
raise AssertionError(
115+
f'Stdout does not contain expected text:\n{text}\n\nActual stdout:\n{found}'
116+
)
117+
118+
@behave.step('stdout does not contain "{text}"')
119+
def step_impl(context, text):
120+
found = context.cmd_stdout.rstrip()
121+
if text in found:
122+
raise AssertionError(
123+
f'Stdout contains unexpected text:\n{text}\n\nActual stdout:\n{found}'
124+
)

features/top.feature

Lines changed: 190 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ Feature: `skiff top` command
1414
Then the exit code is 0
1515
And stdout is
1616
"""
17-
File Path Size Layer ID
18-
-------------------------------------------------------------------
19-
/usr/bin/container-suseconnect 9245304 abb83fe2605d
20-
/usr/lib64/libzypp.so.1735.1.1 8767504 abb83fe2605d
21-
/usr/lib/sysimage/rpm/Packages.db 7837536 dbdff6b3e297
22-
/usr/lib64/libpython3.11.so.1.0 5876440 dbdff6b3e297
23-
/usr/lib64/libcrypto.so.3.1.4 5715672 abb83fe2605d
24-
/usr/lib/sysimage/rpm/Packages.db 5190128 abb83fe2605d
25-
/usr/share/misc/magic.mgc 4983184 abb83fe2605d
26-
/usr/lib/git/git 3726520 dbdff6b3e297
27-
/usr/lib/locale/locale-archive 3058640 abb83fe2605d
28-
/usr/bin/zypper 2915456 abb83fe2605d
17+
FILE PATH SIZE LAYER ID
18+
/usr/bin/container-suseconnect 9245304 abb83fe2605d
19+
/usr/lib64/libzypp.so.1735.1.1 8767504 abb83fe2605d
20+
/usr/lib/sysimage/rpm/Packages.db 7837536 dbdff6b3e297
21+
/usr/lib64/libpython3.11.so.1.0 5876440 dbdff6b3e297
22+
/usr/lib64/libcrypto.so.3.1.4 5715672 abb83fe2605d
23+
/usr/lib/sysimage/rpm/Packages.db 5190128 abb83fe2605d
24+
/usr/share/misc/magic.mgc 4983184 abb83fe2605d
25+
/usr/lib/git/git 3726520 dbdff6b3e297
26+
/usr/lib/locale/locale-archive 3058640 abb83fe2605d
27+
/usr/bin/zypper 2915456 abb83fe2605d
2928
"""
3029

3130
Scenario: Analyze an image from containers-storage with top command
@@ -35,16 +34,183 @@ Feature: `skiff top` command
3534
Then the exit code is 0
3635
And stdout is
3736
"""
38-
File Path Size Layer ID
39-
-------------------------------------------------------------------
40-
/usr/bin/container-suseconnect 9245304 4672d0cba723
41-
/usr/lib64/libzypp.so.1735.1.1 8767504 4672d0cba723
42-
/usr/lib/sysimage/rpm/Packages.db 7837536 88304527ded0
43-
/usr/lib64/libpython3.11.so.1.0 5876440 88304527ded0
44-
/usr/lib64/libcrypto.so.3.1.4 5715672 4672d0cba723
45-
/usr/lib/sysimage/rpm/Packages.db 5190128 4672d0cba723
46-
/usr/share/misc/magic.mgc 4983184 4672d0cba723
47-
/usr/lib/git/git 3726520 88304527ded0
48-
/usr/lib/locale/locale-archive 3058640 4672d0cba723
49-
/usr/bin/zypper 2915456 4672d0cba723
37+
FILE PATH SIZE LAYER ID
38+
/usr/bin/container-suseconnect 9245304 4672d0cba723
39+
/usr/lib64/libzypp.so.1735.1.1 8767504 4672d0cba723
40+
/usr/lib/sysimage/rpm/Packages.db 7837536 88304527ded0
41+
/usr/lib64/libpython3.11.so.1.0 5876440 88304527ded0
42+
/usr/lib64/libcrypto.so.3.1.4 5715672 4672d0cba723
43+
/usr/lib/sysimage/rpm/Packages.db 5190128 4672d0cba723
44+
/usr/share/misc/magic.mgc 4983184 4672d0cba723
45+
/usr/lib/git/git 3726520 88304527ded0
46+
/usr/lib/locale/locale-archive 3058640 4672d0cba723
47+
/usr/bin/zypper 2915456 4672d0cba723
5048
"""
49+
50+
Scenario: Use --layer flag without specifying layer digest
51+
Given I run skiff with the subcommand "top --layer registry.suse.com/bci/python@sha256:677b52cc1d587ff72430f1b607343a3d1f88b15a9bbd999601554ff303d6774f"
52+
Then the exit code is 1
53+
And stderr contains
54+
"""
55+
image URL is required
56+
"""
57+
58+
Scenario: Filter by single layer using full digest
59+
Given I run skiff with the subcommand "top --layer abb83fe2605d registry.suse.com/bci/python@sha256:677b52cc1d587ff72430f1b607343a3d1f88b15a9bbd999601554ff303d6774f"
60+
Then the exit code is 0
61+
And stdout is
62+
"""
63+
FILE PATH SIZE LAYER ID
64+
/usr/bin/container-suseconnect 9245304 abb83fe2605d
65+
/usr/lib64/libzypp.so.1735.1.1 8767504 abb83fe2605d
66+
/usr/lib64/libcrypto.so.3.1.4 5715672 abb83fe2605d
67+
/usr/lib/sysimage/rpm/Packages.db 5190128 abb83fe2605d
68+
/usr/share/misc/magic.mgc 4983184 abb83fe2605d
69+
/usr/lib/locale/locale-archive 3058640 abb83fe2605d
70+
/usr/bin/zypper 2915456 abb83fe2605d
71+
/lib64/libc.so.6 2449832 abb83fe2605d
72+
/usr/lib64/libstdc++.so.6.0.33 2424040 abb83fe2605d
73+
/usr/lib64/ossl-modules/fips.so 2285504 abb83fe2605d
74+
"""
75+
76+
Scenario: Filter by single layer using partial digest
77+
Given I run skiff with the subcommand "top --layer abb83 registry.suse.com/bci/python@sha256:677b52cc1d587ff72430f1b607343a3d1f88b15a9bbd999601554ff303d6774f"
78+
Then the exit code is 0
79+
And stdout is
80+
"""
81+
FILE PATH SIZE LAYER ID
82+
/usr/bin/container-suseconnect 9245304 abb83fe2605d
83+
/usr/lib64/libzypp.so.1735.1.1 8767504 abb83fe2605d
84+
/usr/lib64/libcrypto.so.3.1.4 5715672 abb83fe2605d
85+
/usr/lib/sysimage/rpm/Packages.db 5190128 abb83fe2605d
86+
/usr/share/misc/magic.mgc 4983184 abb83fe2605d
87+
/usr/lib/locale/locale-archive 3058640 abb83fe2605d
88+
/usr/bin/zypper 2915456 abb83fe2605d
89+
/lib64/libc.so.6 2449832 abb83fe2605d
90+
/usr/lib64/libstdc++.so.6.0.33 2424040 abb83fe2605d
91+
/usr/lib64/ossl-modules/fips.so 2285504 abb83fe2605d
92+
"""
93+
94+
Scenario: Filter by single layer using full digest (dbdff6b3e297)
95+
Given I run skiff with the subcommand "top --layer dbdff6b3e297 registry.suse.com/bci/python@sha256:677b52cc1d587ff72430f1b607343a3d1f88b15a9bbd999601554ff303d6774f"
96+
Then the exit code is 0
97+
And stdout is
98+
"""
99+
FILE PATH SIZE LAYER ID
100+
/usr/lib/sysimage/rpm/Packages.db 7837536 dbdff6b3e297
101+
/usr/lib64/libpython3.11.so.1.0 5876440 dbdff6b3e297
102+
/usr/lib/git/git 3726520 dbdff6b3e297
103+
/usr/lib/sysimage/rpm/Index.db 2756608 dbdff6b3e297
104+
/usr/lib/git/git-remote-http 2191584 dbdff6b3e297
105+
/usr/lib/git/git-http-push 2187352 dbdff6b3e297
106+
/usr/lib/git/git-imap-send 2183704 dbdff6b3e297
107+
/usr/lib/git/scalar 2183136 dbdff6b3e297
108+
/usr/lib/git/git-http-fetch 2175096 dbdff6b3e297
109+
/usr/lib/git/git-http-backend 2142032 dbdff6b3e297
110+
"""
111+
112+
Scenario: Filter by multiple layers
113+
Given I run skiff with the subcommand "top --layer abb83fe2605d --layer dbdff6b3e297 registry.suse.com/bci/python@sha256:677b52cc1d587ff72430f1b607343a3d1f88b15a9bbd999601554ff303d6774f"
114+
Then the exit code is 0
115+
And stdout is
116+
"""
117+
FILE PATH SIZE LAYER ID
118+
/usr/bin/container-suseconnect 9245304 abb83fe2605d
119+
/usr/lib64/libzypp.so.1735.1.1 8767504 abb83fe2605d
120+
/usr/lib/sysimage/rpm/Packages.db 7837536 dbdff6b3e297
121+
/usr/lib64/libpython3.11.so.1.0 5876440 dbdff6b3e297
122+
/usr/lib64/libcrypto.so.3.1.4 5715672 abb83fe2605d
123+
/usr/lib/sysimage/rpm/Packages.db 5190128 abb83fe2605d
124+
/usr/share/misc/magic.mgc 4983184 abb83fe2605d
125+
/usr/lib/git/git 3726520 dbdff6b3e297
126+
/usr/lib/locale/locale-archive 3058640 abb83fe2605d
127+
/usr/bin/zypper 2915456 abb83fe2605d
128+
"""
129+
130+
Scenario: Filter by non-existent layer
131+
Given I run skiff with the subcommand "top --layer nonexistentlayer registry.suse.com/bci/python@sha256:677b52cc1d587ff72430f1b607343a3d1f88b15a9bbd999601554ff303d6774f"
132+
Then the exit code is 1
133+
And stderr contains
134+
"""
135+
layer nonexistentlayer not found in image
136+
"""
137+
138+
Scenario: Use --layer with short flag alias
139+
Given I run skiff with the subcommand "top -l abb83fe2605d registry.suse.com/bci/python@sha256:677b52cc1d587ff72430f1b607343a3d1f88b15a9bbd999601554ff303d6774f"
140+
Then the exit code is 0
141+
And stdout contains "abb83fe2605d"
142+
And stdout does not contain "dbdff6b3e297"
143+
144+
Scenario: Use --layer with multiple short flag aliases
145+
Given I run skiff with the subcommand "top -l abb83fe2605d -l dbdff6b3e297 registry.suse.com/bci/python@sha256:677b52cc1d587ff72430f1b607343a3d1f88b15a9bbd999601554ff303d6774f"
146+
Then the exit code is 0
147+
And stdout contains "abb83fe2605d"
148+
And stdout contains "dbdff6b3e297"
149+
150+
Scenario: Use --layer with empty string
151+
Given I run skiff with the subcommand "top --layer '' registry.suse.com/bci/python@sha256:677b52cc1d587ff72430f1b607343a3d1f88b15a9bbd999601554ff303d6774f"
152+
Then the exit code is 1
153+
And stderr contains
154+
"""
155+
multiple layers match shortened digest
156+
"""
157+
158+
Scenario: Use --human-readable flag for human-readable file sizes
159+
Given I run skiff with the subcommand "top --human-readable registry.suse.com/bci/python@sha256:677b52cc1d587ff72430f1b607343a3d1f88b15a9bbd999601554ff303d6774f"
160+
Then the exit code is 0
161+
And stdout is
162+
"""
163+
FILE PATH SIZE LAYER ID
164+
/usr/bin/container-suseconnect 9.2 MB abb83fe2605d
165+
/usr/lib64/libzypp.so.1735.1.1 8.8 MB abb83fe2605d
166+
/usr/lib/sysimage/rpm/Packages.db 7.8 MB dbdff6b3e297
167+
/usr/lib64/libpython3.11.so.1.0 5.9 MB dbdff6b3e297
168+
/usr/lib64/libcrypto.so.3.1.4 5.7 MB abb83fe2605d
169+
/usr/lib/sysimage/rpm/Packages.db 5.2 MB abb83fe2605d
170+
/usr/share/misc/magic.mgc 5.0 MB abb83fe2605d
171+
/usr/lib/git/git 3.7 MB dbdff6b3e297
172+
/usr/lib/locale/locale-archive 3.1 MB abb83fe2605d
173+
/usr/bin/zypper 2.9 MB abb83fe2605d
174+
"""
175+
176+
Scenario: Use --human-readable with layer filtering
177+
Given I run skiff with the subcommand "top --human-readable --layer abb83fe2605d registry.suse.com/bci/python@sha256:677b52cc1d587ff72430f1b607343a3d1f88b15a9bbd999601554ff303d6774f"
178+
Then the exit code is 0
179+
And stdout is
180+
"""
181+
FILE PATH SIZE LAYER ID
182+
/usr/bin/container-suseconnect 9.2 MB abb83fe2605d
183+
/usr/lib64/libzypp.so.1735.1.1 8.8 MB abb83fe2605d
184+
/usr/lib64/libcrypto.so.3.1.4 5.7 MB abb83fe2605d
185+
/usr/lib/sysimage/rpm/Packages.db 5.2 MB abb83fe2605d
186+
/usr/share/misc/magic.mgc 5.0 MB abb83fe2605d
187+
/usr/lib/locale/locale-archive 3.1 MB abb83fe2605d
188+
/usr/bin/zypper 2.9 MB abb83fe2605d
189+
/lib64/libc.so.6 2.4 MB abb83fe2605d
190+
/usr/lib64/libstdc++.so.6.0.33 2.4 MB abb83fe2605d
191+
/usr/lib64/ossl-modules/fips.so 2.3 MB abb83fe2605d
192+
"""
193+
194+
Scenario: Use --human-readable with multiple layer filtering
195+
Given I run skiff with the subcommand "top --human-readable --layer abb83fe2605d --layer dbdff6b3e297 registry.suse.com/bci/python@sha256:677b52cc1d587ff72430f1b607343a3d1f88b15a9bbd999601554ff303d6774f"
196+
Then the exit code is 0
197+
And stdout is
198+
"""
199+
FILE PATH SIZE LAYER ID
200+
/usr/bin/container-suseconnect 9.2 MB abb83fe2605d
201+
/usr/lib64/libzypp.so.1735.1.1 8.8 MB abb83fe2605d
202+
/usr/lib/sysimage/rpm/Packages.db 7.8 MB dbdff6b3e297
203+
/usr/lib64/libpython3.11.so.1.0 5.9 MB dbdff6b3e297
204+
/usr/lib64/libcrypto.so.3.1.4 5.7 MB abb83fe2605d
205+
/usr/lib/sysimage/rpm/Packages.db 5.2 MB abb83fe2605d
206+
/usr/share/misc/magic.mgc 5.0 MB abb83fe2605d
207+
/usr/lib/git/git 3.7 MB dbdff6b3e297
208+
/usr/lib/locale/locale-archive 3.1 MB abb83fe2605d
209+
/usr/bin/zypper 2.9 MB abb83fe2605d
210+
"""
211+
212+
Scenario: Use --human-readable with short flag alias
213+
Given I run skiff with the subcommand "top -h registry.suse.com/bci/python@sha256:677b52cc1d587ff72430f1b607343a3d1f88b15a9bbd999601554ff303d6774f"
214+
Then the exit code is 0
215+
And stdout contains "MB"
216+
And stdout does not contain "9245304"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/containers/image/v5 v5.36.0
1010
github.com/containers/storage v1.59.0
1111
github.com/opencontainers/go-digest v1.0.0
12+
github.com/opencontainers/image-spec v1.1.1
1213
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635
1314
github.com/urfave/cli/v3 v3.3.8
1415
)
@@ -68,7 +69,6 @@ require (
6869
github.com/moby/sys/user v0.4.0 // indirect
6970
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
7071
github.com/modern-go/reflect2 v1.0.2 // indirect
71-
github.com/opencontainers/image-spec v1.1.1 // indirect
7272
github.com/opencontainers/runc v1.3.0 // indirect
7373
github.com/opencontainers/runtime-spec v1.2.1 // indirect
7474
github.com/opencontainers/selinux v1.12.0 // indirect

0 commit comments

Comments
 (0)