|
8 | 8 | "github.com/stretchr/testify/assert" |
9 | 9 | "github.com/stretchr/testify/require" |
10 | 10 |
|
| 11 | + "github.com/osbuild/image-builder/pkg/distro" |
11 | 12 | "github.com/osbuild/image-builder/pkg/rpmmd" |
| 13 | + testrepos "github.com/osbuild/image-builder/test/data/repositories" |
12 | 14 |
|
13 | 15 | main "github.com/osbuild/image-builder/cmd/image-builder" |
14 | 16 | ) |
@@ -46,6 +48,110 @@ func TestNewPkgSearchFormatter(t *testing.T) { |
46 | 48 | } |
47 | 49 | } |
48 | 50 |
|
| 51 | +var lastCapturedCacheDir string |
| 52 | + |
| 53 | +func fakePkgSearcher(_ distro.Distro, _, cacheDir string, _ []rpmmd.RepoConfig, _ []string) (rpmmd.PackageList, error) { |
| 54 | + lastCapturedCacheDir = cacheDir |
| 55 | + return rpmmd.PackageList{ |
| 56 | + {Name: "bash", Version: "5.1.8", Release: "2.el9", Arch: "x86_64"}, |
| 57 | + {Name: "zsh", Version: "5.8", Release: "7.el9", Arch: "x86_64"}, |
| 58 | + }, nil |
| 59 | +} |
| 60 | + |
| 61 | +func TestCmdPkgSearch(t *testing.T) { |
| 62 | + restorePkgSearcher := main.MockPkgSearcher(fakePkgSearcher) |
| 63 | + defer restorePkgSearcher() |
| 64 | + |
| 65 | + restoreRepoRegistry := main.MockNewRepoRegistry(testrepos.New) |
| 66 | + defer restoreRepoRegistry() |
| 67 | + |
| 68 | + for _, tc := range []struct { |
| 69 | + name string |
| 70 | + args []string |
| 71 | + expectedPkgs int |
| 72 | + expectedErr string |
| 73 | + expectedCacheDir string |
| 74 | + }{ |
| 75 | + { |
| 76 | + name: "with type", |
| 77 | + args: []string{"pkgsearch", "bash", "--distro=centos-9", "--arch=x86_64", "--type=qcow2"}, |
| 78 | + expectedPkgs: 2, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "without type", |
| 82 | + args: []string{"pkgsearch", "bash", "--distro=centos-9", "--arch=x86_64"}, |
| 83 | + expectedPkgs: 2, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "with json format", |
| 87 | + args: []string{"pkgsearch", "bash", "--distro=centos-9", "--arch=x86_64", "--format=json"}, |
| 88 | + expectedPkgs: 2, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "multiple packages", |
| 92 | + args: []string{"pkgsearch", "bash", "zsh", "--distro=centos-9", "--arch=x86_64"}, |
| 93 | + expectedPkgs: 2, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "unsupported format", |
| 97 | + args: []string{"pkgsearch", "bash", "--distro=centos-9", "--arch=x86_64", "--format=text"}, |
| 98 | + expectedErr: "unsupported", |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "invalid distro", |
| 102 | + args: []string{"pkgsearch", "bash", "--distro=not-a-distro", "--arch=x86_64"}, |
| 103 | + expectedErr: "not-a-distro", |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "invalid type", |
| 107 | + args: []string{"pkgsearch", "bash", "--distro=centos-9", "--arch=x86_64", "--type=not-a-type"}, |
| 108 | + expectedErr: "not-a-type", |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "zero args (no packages)", |
| 112 | + args: []string{"pkgsearch", "--distro=centos-9", "--arch=x86_64"}, |
| 113 | + expectedPkgs: 2, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "with rpmmd-cache", |
| 117 | + args: []string{"pkgsearch", "bash", "--distro=centos-9", "--arch=x86_64", "--rpmmd-cache=/tmp/test-cache"}, |
| 118 | + expectedPkgs: 2, |
| 119 | + expectedCacheDir: "/tmp/test-cache", |
| 120 | + }, |
| 121 | + } { |
| 122 | + t.Run(tc.name, func(t *testing.T) { |
| 123 | + restore := main.MockOsArgs(tc.args) |
| 124 | + defer restore() |
| 125 | + |
| 126 | + var fakeStdout bytes.Buffer |
| 127 | + restore = main.MockOsStdout(&fakeStdout) |
| 128 | + defer restore() |
| 129 | + |
| 130 | + err := main.Run() |
| 131 | + if tc.expectedErr != "" { |
| 132 | + require.Error(t, err) |
| 133 | + assert.ErrorContains(t, err, tc.expectedErr) |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + require.NoError(t, err) |
| 138 | + |
| 139 | + var result struct { |
| 140 | + Packages []struct { |
| 141 | + Name string `json:"name"` |
| 142 | + } `json:"packages"` |
| 143 | + } |
| 144 | + err = json.Unmarshal(fakeStdout.Bytes(), &result) |
| 145 | + require.NoError(t, err) |
| 146 | + assert.Len(t, result.Packages, tc.expectedPkgs) |
| 147 | + |
| 148 | + if tc.expectedCacheDir != "" { |
| 149 | + assert.Equal(t, tc.expectedCacheDir, lastCapturedCacheDir) |
| 150 | + } |
| 151 | + }) |
| 152 | + } |
| 153 | +} |
| 154 | + |
49 | 155 | func TestPkgSearchFormatterJSONOutput(t *testing.T) { |
50 | 156 | for _, tc := range []struct { |
51 | 157 | name string |
|
0 commit comments