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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
// 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 handlers
import (
"archive/tar"
"bytes"
"fmt"
"io"
"testing"
"github.com/mendersoftware/mender-artifact/artifact"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewBootstrapArtifact(t *testing.T) {
expected := &BootstrapArtifact{version: 3}
result := NewBootstrapArtifact()
assert.Equal(t, expected, result)
}
func TestGetVersion(t *testing.T) {
expectedVersion := 3
bootstrapArtifact := NewBootstrapArtifact()
version := bootstrapArtifact.GetVersion()
assert.Equal(t, expectedVersion, version)
}
func TestGetUpdateType(t *testing.T) {
var expected *string
bootstrapArtifact := NewBootstrapArtifact()
result := bootstrapArtifact.GetUpdateType()
assert.Equal(t, expected, result)
}
func TestGetUpdateOriginalType(t *testing.T) {
var expected *string
bootstrapArtifact := NewBootstrapArtifact()
result := bootstrapArtifact.GetUpdateOriginalType()
assert.Equal(t, expected, result)
}
func TestGetUpdateDepends(t *testing.T) {
type testCase struct {
typeInfo *artifact.TypeInfoV3
expected artifact.TypeInfoDepends
expectedError error
}
testCases := []testCase{
{
typeInfo: &artifact.TypeInfoV3{
ArtifactDepends: artifact.TypeInfoDepends{
"depends": "value",
},
},
expected: artifact.TypeInfoDepends{
"depends": "value",
},
expectedError: nil,
},
}
for n, tc := range testCases {
t.Run(fmt.Sprintf("test case %d", n), func(t *testing.T) {
bootstrapArtifact := NewBootstrapArtifact()
bootstrapArtifact.typeInfoV3 = tc.typeInfo
result, err := bootstrapArtifact.GetUpdateDepends()
assert.Equal(t, tc.expected, result)
assert.Equal(t, tc.expectedError, err)
})
}
}
func TestGetUpdateProvides(t *testing.T) {
type testCase struct {
typeInfo *artifact.TypeInfoV3
expected artifact.TypeInfoProvides
expectedError error
}
testCases := []testCase{
{
typeInfo: &artifact.TypeInfoV3{
ArtifactProvides: artifact.TypeInfoProvides{
"provides": "value",
},
},
expected: artifact.TypeInfoProvides{
"provides": "value",
},
expectedError: nil,
},
}
for n, tc := range testCases {
t.Run(fmt.Sprintf("test case %d", n), func(t *testing.T) {
bootstrapArtifact := NewBootstrapArtifact()
bootstrapArtifact.typeInfoV3 = tc.typeInfo
result, err := bootstrapArtifact.GetUpdateProvides()
assert.Equal(t, tc.expected, result)
assert.Equal(t, tc.expectedError, err)
})
}
}
func TestGetUpdateMetaData(t *testing.T) {
var expected map[string]interface{}
bootstrapArtifact := NewBootstrapArtifact()
result, err := bootstrapArtifact.GetUpdateMetaData()
assert.Equal(t, expected, result)
assert.Nil(t, err)
}
func TestGetUpdateClearsProvides(t *testing.T) {
type testCase struct {
typeInfo *artifact.TypeInfoV3
expected []string
}
testCases := []testCase{
{
typeInfo: &artifact.TypeInfoV3{
ClearsArtifactProvides: []string{
"clears", "value",
},
},
expected: []string{
"clears", "value",
},
},
}
for n, tc := range testCases {
t.Run(fmt.Sprintf("test case %d", n), func(t *testing.T) {
bootstrapArtifact := NewBootstrapArtifact()
bootstrapArtifact.typeInfoV3 = tc.typeInfo
result := bootstrapArtifact.GetUpdateClearsProvides()
assert.Equal(t, tc.expected, result)
})
}
}
func TestGetUpdateOriginalDepends(t *testing.T) {
var expected artifact.TypeInfoDepends
bootstrapArtifact := NewBootstrapArtifact()
result := bootstrapArtifact.GetUpdateOriginalDepends()
assert.Equal(t, expected, result)
}
func TestGetUpdateOriginalProvides(t *testing.T) {
var expected artifact.TypeInfoProvides
bootstrapArtifact := NewBootstrapArtifact()
result := bootstrapArtifact.GetUpdateOriginalProvides()
assert.Equal(t, expected, result)
}
func TestGetUpdateOriginalMetaData(t *testing.T) {
var expected map[string]interface{}
bootstrapArtifact := NewBootstrapArtifact()
result := bootstrapArtifact.GetUpdateOriginalMetaData()
assert.Equal(t, expected, result)
}
func TestGetUpdateOriginalClearsProvides(t *testing.T) {
var expected []string
bootstrapArtifact := NewBootstrapArtifact()
result := bootstrapArtifact.GetUpdateOriginalClearsProvides()
assert.Equal(t, expected, result)
}
func TestGetUpdateAugmentDepends(t *testing.T) {
var expected artifact.TypeInfoDepends
bootstrapArtifact := NewBootstrapArtifact()
result := bootstrapArtifact.GetUpdateAugmentDepends()
assert.Equal(t, expected, result)
}
func TestGetUpdateAugmentProvides(t *testing.T) {
var expected artifact.TypeInfoProvides
bootstrapArtifact := NewBootstrapArtifact()
result := bootstrapArtifact.GetUpdateAugmentProvides()
assert.Equal(t, expected, result)
}
func TestGetUpdateAugmentMetaData(t *testing.T) {
var expected map[string]interface{}
bootstrapArtifact := NewBootstrapArtifact()
result := bootstrapArtifact.GetUpdateAugmentMetaData()
assert.Equal(t, expected, result)
}
func TestGetUpdateAugmentClearsProvides(t *testing.T) {
var expected []string
bootstrapArtifact := NewBootstrapArtifact()
result := bootstrapArtifact.GetUpdateAugmentClearsProvides()
assert.Equal(t, expected, result)
}
func TestGetUpdateOriginalTypeInfoWriter(t *testing.T) {
var expected io.Writer
bootstrapArtifact := NewBootstrapArtifact()
result := bootstrapArtifact.GetUpdateOriginalTypeInfoWriter()
assert.Equal(t, expected, result)
}
func TestGetUpdateAugmentTypeInfoWriter(t *testing.T) {
var expected io.Writer
bootstrapArtifact := NewBootstrapArtifact()
result := bootstrapArtifact.GetUpdateAugmentTypeInfoWriter()
assert.Equal(t, expected, result)
}
func TestSetUpdateFiles(t *testing.T) {
bootstrapArtifact := NewBootstrapArtifact()
expected := [](*DataFile){
&DataFile{Name: "test_name", Size: 1},
}
files := [](*DataFile){
&DataFile{Name: "test_name", Size: 1},
}
err := bootstrapArtifact.SetUpdateFiles(files)
assert.Equal(t, expected, bootstrapArtifact.files)
assert.Nil(t, err)
}
func TestComposeHeaderBootstrap(t *testing.T) {
tests := map[string]struct {
rfs *BootstrapArtifact
args ComposeHeaderArgs
err error
verifyFunc func(args ComposeHeaderArgs)
}{
"version 3 - no tar writer": {
rfs: NewBootstrapArtifact(),
args: ComposeHeaderArgs{},
err: errors.New("ComposeHeader: Payload: can not tar type-info: arch: Can not write to empty tar-writer"),
},
"version 3 - success": {
rfs: NewBootstrapArtifact(),
args: ComposeHeaderArgs{
TarWriter: tar.NewWriter(bytes.NewBuffer(nil)),
},
err: errors.New("ComposeHeader: Payload: can not tar type-info: arch: Can not write to empty tar-writer"),
},
"error: metadata not empty": {
rfs: NewBootstrapArtifact(),
args: ComposeHeaderArgs{
TarWriter: tar.NewWriter(bytes.NewBuffer(nil)),
MetaData: map[string]interface{}{"foo": "bar"},
},
err: errors.New("MetaData not empty in Rootfs.ComposeHeader. This is a bug in the application."),
},
"error: metadata not marshallable": {
rfs: NewBootstrapArtifact(),
args: ComposeHeaderArgs{
TarWriter: tar.NewWriter(bytes.NewBuffer(nil)),
MetaData: map[string]interface{}{"asd": make(chan int)},
},
err: errors.New("MetaData field unmarshalable. This is a bug in the application: json: unsupported type: chan int"),
},
}
for name, test := range tests {
err := test.rfs.ComposeHeader(&test.args)
if err != nil {
if test.err == nil {
t.Errorf("Test %s failed with an unexpected error: %v", name, err)
continue
}
assert.EqualError(t, err, test.err.Error())
if test.verifyFunc != nil {
test.verifyFunc(test.args)
}
}
}
}
func TestBootstrapReadHeader(t *testing.T) {
r := NewBootstrapArtifact()
tc := []struct {
version int
rootfs Installer
data string
name string
shouldErr bool
errMsg string
}{
{rootfs: r, data: "", name: "headers/0000/non-existing", shouldErr: true,
errMsg: "unsupported file", version: 3},
{rootfs: r, data: "data", name: "headers/0000/type-info", shouldErr: true, version: 3,
errMsg: "invalid character 'd' looking for beginning of value"},
{rootfs: r, data: "data", name: "headers/0000/type-info", shouldErr: true, version: 2,
errMsg: "version 2 not supported"},
{rootfs: r, data: "{\"type\":null}", name: "headers/0000/type-info", shouldErr: false, version: 3},
}
for _, test := range tc {
t.Run("", func(t *testing.T) {
buf := bytes.NewBuffer(nil)
tw := tar.NewWriter(buf)
err := tw.WriteHeader(&tar.Header{
Name: "not-needed",
Size: int64(len(test.data)),
})
require.NoError(t, err)
_, err = tw.Write([]byte(test.data))
require.NoError(t, err)
err = tw.Close()
require.NoError(t, err)
tr := tar.NewReader(buf)
_, err = tr.Next()
require.NoError(t, err)
err = r.ReadHeader(tr, test.name, test.version, false)
if test.shouldErr {
require.Error(t, err)
if test.errMsg != "" {
require.Contains(t, errors.Cause(err).Error(), test.errMsg)
}
} else {
require.NoError(t, err)
}
})
}
}
func TestNewInstance(t *testing.T) {
bootstrapArtifact := NewBootstrapArtifact()
expected := &BootstrapArtifact{
version: 3,
}
result := bootstrapArtifact.NewInstance()
assert.Equal(t, expected, result)
}
func TestNewAugmentedInstanceBootstrap(t *testing.T) {
bootstrapArtifact := NewBootstrapArtifact()
input := NewModuleImage("foo-module")
result, err := bootstrapArtifact.NewAugmentedInstance(input)
assert.Equal(t, nil, result)
assert.Nil(t, err)
}
func TestNewUpdateStorer(t *testing.T) {
bootstrapArtifact := NewBootstrapArtifact()
updateType := "type"
payloadNum := 0
expected := devNullUpdateStorer{}
result, err := bootstrapArtifact.NewUpdateStorer(&updateType, payloadNum)
assert.Equal(t, &expected, result)
assert.Nil(t, err)
}
func TestGetUpdateAllFiles(t *testing.T) {
bootstrapArtifact := NewBootstrapArtifact()
var expected [](*DataFile)
result := bootstrapArtifact.GetUpdateAllFiles()
assert.Equal(t, expected, result)
}
func TestGetUpdateAugmentFiles(t *testing.T) {
bootstrapArtifact := NewBootstrapArtifact()
var expected [](*DataFile)
result := bootstrapArtifact.GetUpdateAugmentFiles()
assert.Equal(t, expected, result)
}
func TestGetUpdateFiles(t *testing.T) {
bootstrapArtifact := NewBootstrapArtifact()
var expected [](*DataFile)
result := bootstrapArtifact.GetUpdateFiles()
assert.Equal(t, expected, result)
}
func TestSetUpdateAugmentFiles(t *testing.T) {
bootstrapArtifact := NewBootstrapArtifact()
files := [](*DataFile){&DataFile{Name: "name"}}
err := bootstrapArtifact.SetUpdateAugmentFiles(files)
assert.Nil(t, err)
}
|