1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
// Copyright 2022 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cli
import (
"fmt"
"io/ioutil"
"os"
"path"
"sort"
"strings"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func makeFile(t *testing.T, tmpdir, name, content string) {
err := ioutil.WriteFile(path.Join(tmpdir, name), []byte(content), 0644)
require.NoError(t, err)
}
func runAndCollectStdout(args []string) (string, error) {
savedStdout := os.Stdout
pipeR, pipeW, err := os.Pipe()
if err != nil {
return "", err
}
os.Stdout = pipeW
defer func() {
os.Stdout = savedStdout
pipeW.Close()
pipeR.Close()
}()
var goRoutineErr error
go func() {
defer func() {
if r := recover(); r != nil {
goRoutineErr = errors.Errorf("%v", r)
}
os.Stdout.Close()
}()
goRoutineErr = getCliContext().Run(args)
}()
printed, err := ioutil.ReadAll(pipeR)
if err != nil {
return "", err
}
if goRoutineErr != nil {
return "", goRoutineErr
}
// Trim null byte (from --print0-cmdline).
if printed[len(printed)-1] == 0 {
printed = printed[:len(printed)-1]
}
return strings.TrimSpace(string(printed)), nil
}
func TestDumpContent(t *testing.T) {
for _, printCmdline := range []string{"--print-cmdline", "--print0-cmdline"} {
for _, imageType := range []string{"rootfs-image", "my-own-type"} {
t.Run(fmt.Sprintf("%s/%s", imageType, printCmdline), func(t *testing.T) {
testDumpContent(t, imageType, printCmdline)
})
}
}
}
func testDumpContent(t *testing.T, imageType, printCmdline string) {
tmpdir, err := ioutil.TempDir("", "")
require.NoError(t, err)
defer os.RemoveAll(tmpdir)
var sep string
switch printCmdline {
case "--print-cmdline":
sep = " "
case "--print0-cmdline":
sep = "\x00"
default:
t.Fatal("Unknown --print-cmdline mode")
}
makeFile(t, tmpdir, "file", "payload")
makeFile(t, tmpdir, "file2", "payload2")
makeFile(t, tmpdir, "meta-data", "{\"a\":\"b\"}")
makeFile(t, tmpdir, "ArtifactInstall_Enter_45_test", "Bash magic")
makeFile(t, tmpdir, "ArtifactCommit_Leave_55", "More Bash magic")
// --------------------------------------------------------------------
// Single values
// --------------------------------------------------------------------
// Use "module-image" writer here, so that we can insert some extra
// fields that aren't typically in rootfs-images. One of these is
// meta-data, which we don't use at the time of writing this, but which
// may be used later.
err = getCliContext().Run([]string{"mender-artifact", "write", "module-image",
"-o", path.Join(tmpdir, "artifact.mender"),
"-n", "Name",
"-t", "TestDevice",
"-T", imageType,
"-N", "dependsOnArtifact",
"-f", path.Join(tmpdir, "file"),
"-m", path.Join(tmpdir, "meta-data"),
"-s", path.Join(tmpdir, "ArtifactInstall_Enter_45_test"),
"-d", "testDepends:someDep",
"-p", "testProvides:someProv",
"-g", "providesGroup",
"-G", "dependsGroup",
"--no-default-software-version"})
require.NoError(t, err)
printed, err := runAndCollectStdout([]string{"mender-artifact", "dump",
"--scripts", path.Join(tmpdir, "scripts"),
"--meta-data", path.Join(tmpdir, "meta"),
"--files", path.Join(tmpdir, "files"),
printCmdline,
path.Join(tmpdir, "artifact.mender")})
assert.NoError(t, err)
assert.Equal(t, strings.ReplaceAll(fmt.Sprintf(
"write module-image"+
" --artifact-name Name"+
" --provides-group providesGroup"+
" --artifact-name-depends dependsOnArtifact"+
" --device-type TestDevice"+
" --depends-groups dependsGroup"+
" --type %s"+
" --no-default-software-version"+
" --provides testProvides:someProv"+
" --depends testDepends:someDep"+
" --no-default-clears-provides"+
" --script %s/scripts/ArtifactInstall_Enter_45_test"+
" --meta-data %s/meta/0000.meta-data"+
" --file %s/files/file",
imageType, tmpdir, tmpdir, tmpdir),
// Replacing all spaces with sep is not safe in general when
// using --print0-cmdline, but we know there are no
// literal spaces in our test arguments.
" ", sep),
string(printed))
// --------------------------------------------------------------------
// Multiple values
// --------------------------------------------------------------------
if imageType == "rootfs-image" {
// "rootfs-image" doesn't support multiple payload files, so
// skip testing that any further.
return
}
os.RemoveAll(path.Join(tmpdir, "scripts"))
os.RemoveAll(path.Join(tmpdir, "meta"))
os.RemoveAll(path.Join(tmpdir, "files"))
err = getCliContext().Run([]string{"mender-artifact", "write", "module-image",
"-o", path.Join(tmpdir, "artifact.mender"),
"-n", "Name",
"-t", "TestDevice",
"-t", "TestDevice2",
"-T", imageType,
"--clears-provides", imageType + ".*",
"-N", "dependsOnArtifact",
"-N", "dependsOnArtifact2",
"-f", path.Join(tmpdir, "file"),
"-f", path.Join(tmpdir, "file2"),
"-m", path.Join(tmpdir, "meta-data"),
"-s", path.Join(tmpdir, "ArtifactInstall_Enter_45_test"),
"-s", path.Join(tmpdir, "ArtifactCommit_Leave_55"),
"-d", "testDepends:someDep",
"-p", "testProvides:someProv",
"-d", "testDepends2:someDep2",
"-p", "testProvides2:someProv2",
"-g", "providesGroup",
"-G", "dependsGroup",
"-G", "dependsGroup2"})
require.NoError(t, err)
printed, err = runAndCollectStdout([]string{"mender-artifact", "dump",
"--scripts", path.Join(tmpdir, "scripts"),
"--meta-data", path.Join(tmpdir, "meta"),
"--files", path.Join(tmpdir, "files"),
printCmdline,
path.Join(tmpdir, "artifact.mender")})
assert.NoError(t, err)
printedStr := string(printed)
// The provides, depends and scripts are stored in maps, where the order
// is unpredictable, so split on the start of the flag, sort, and
// compare that.
expected := strings.Split(strings.ReplaceAll(
"write module-image"+
" --artifact-name Name"+
" --provides-group providesGroup"+
" --artifact-name-depends dependsOnArtifact"+
" --artifact-name-depends dependsOnArtifact2"+
" --device-type TestDevice"+
" --device-type TestDevice2"+
" --depends-groups dependsGroup"+
" --depends-groups dependsGroup2"+
fmt.Sprintf(" --type %s", imageType)+
" --no-default-software-version"+
" --no-default-clears-provides"+
" --provides testProvides:someProv"+
" --provides testProvides2:someProv2"+
fmt.Sprintf(" --provides rootfs-image.%s.version:Name", imageType)+
" --depends testDepends:someDep"+
" --depends testDepends2:someDep2"+
fmt.Sprintf(" --script %s/scripts/ArtifactInstall_Enter_45_test", tmpdir)+
fmt.Sprintf(" --script %s/scripts/ArtifactCommit_Leave_55", tmpdir)+
fmt.Sprintf(" --clears-provides %s.*", imageType)+
fmt.Sprintf(" --clears-provides rootfs-image.%s.*", imageType)+
fmt.Sprintf(" --meta-data %s/meta/0000.meta-data", tmpdir)+
fmt.Sprintf(" --file %s/files/file", tmpdir)+
fmt.Sprintf(" --file %s/files/file2", tmpdir),
// Replacing all spaces with sep is not safe in general when
// using --print0-cmdline, but we know there are no
// literal spaces in our test arguments.
" ", sep),
// Split separator.
fmt.Sprintf("%s--", sep))
actual := strings.Split(printedStr, fmt.Sprintf("%s--", sep))
sort.Strings(expected[1:])
sort.Strings(actual[1:])
assert.Equal(t, expected, actual)
// --------------------------------------------------------------------
// Flags
// --------------------------------------------------------------------
// Check that all flags which are documented on the command line are taken into
// account in the "dump" command. *DO NOT* add flags to this list without making
// sure that either:
//
// 1. It is tested somewhere in this file, by using the flag, dumping it, and
// checking that it is recreated correctly.
//
// -or-
//
// 2. It does not need to be tested (no effect on dumping or tested elsewhere).
flagChecker := newFlagChecker("write")
flagChecker.addFlags([]string{
"artifact-name",
"artifact-name-depends",
"clears-provides",
"compression", // Not tested in "dump".
"depends",
"depends-groups",
"device-type",
"file",
"gcp-kms-key", // Not tested in "dump".
"key", // Not tested in "dump".
"legacy-rootfs-image-checksum", // Not relevant for "dump", which uses "module-image".
"meta-data",
"no-checksum-provide", // Not relevant for "dump", which uses "module-image".
"no-default-clears-provides",
"no-default-software-version",
"output-path", // Not relevant for "dump".
"provides",
"provides-group",
"script",
"software-filesystem", // These three indirectly handled by --provides.
"software-name", // <
"software-version", // <
"ssh-args", // Not relevant for "dump".
"type",
"version", // Could be supported, but in practice we only support >= v3.
"no-progress",
})
flagChecker.checkAllFlagsTested(t)
}
|