-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathinstaller_test.go
1401 lines (1309 loc) · 37.2 KB
/
installer_test.go
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 Google LLC
//
// 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 installer
import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/user"
"path/filepath"
"runtime"
"testing"
"github.com/google/fresnel/cli/config"
"github.com/google/fresnel/models"
"github.com/google/go-cmp/cmp"
"github.com/google/winops/storage"
)
// fakeConfig inherits all members of config.Configuration through embedding.
// Unimplemented members send a clear signal during tests because they will
// panic if called, allowing us to implement only the minimum set of members
// required for testing.
type fakeConfig struct {
// config.Configuration is embedded, fakeConfig inherits all its members.
config.Configuration
dismount bool
eject bool
elevated bool
update bool
err error // the error returned when isElevated is called.
distroLabel string
image string
imageFile string
seedDest string
seedFile string
seedServer string
track string
ffuPath string
ffuManifest string
}
func (f *fakeConfig) Dismount() bool {
return f.dismount
}
func (f *fakeConfig) DistroLabel() string {
return f.distroLabel
}
func (f *fakeConfig) Image() string {
return f.image
}
func (f *fakeConfig) ImageFile() string {
return f.imageFile
}
func (f *fakeConfig) Elevated() bool {
return f.elevated
}
func (f *fakeConfig) PowerOff() bool {
return f.eject
}
func (f *fakeConfig) SeedDest() string {
return f.seedDest
}
func (f *fakeConfig) SeedFile() string {
return f.seedFile
}
func (f *fakeConfig) SeedServer() string {
return f.seedServer
}
func (f *fakeConfig) UpdateOnly() bool {
return f.update
}
func (f *fakeConfig) FFUManifest() string {
return f.ffuManifest
}
func (f *fakeConfig) FFUPath() string {
return f.ffuPath
}
func TestNew(t *testing.T) {
// Generate a fake config to use with New.
c := &fakeConfig{
image: `https://foo.bar.com/test_installer.img`,
seedServer: `https://bar.baz.com/endpoint`,
}
tests := []struct {
desc string
config Configuration
fakeConnect func(string, string) (httpDoer, error)
wantInstaller bool
err error
}{
{
desc: "nil config",
config: nil,
err: errConfig,
},
{
desc: "connect error",
config: c,
fakeConnect: func(string, string) (httpDoer, error) { return nil, errors.New("error") },
err: errConnect,
},
{
desc: "success",
config: c,
fakeConnect: func(string, string) (httpDoer, error) { return nil, nil },
wantInstaller: true,
err: nil,
},
}
for _, tt := range tests {
connect = tt.fakeConnect
got, err := New(tt.config)
if !errors.Is(err, tt.err) {
t.Errorf("%s: New() err: %v, want err: %v", tt.desc, err, tt.err)
}
if (got == nil) == tt.wantInstaller {
t.Errorf("%s: New() got: %t, want: %t", tt.desc, (got != nil), tt.wantInstaller)
}
}
}
func TestUserName(t *testing.T) {
// stdUser represents the user actually running the binary.
stdUser := "stdUser"
tests := []struct {
desc string
fakeCurrentUser func() (*user.User, error)
want string
err error
}{
{
desc: "user.Current error",
fakeCurrentUser: func() (*user.User, error) { return nil, errUser },
want: "",
err: errUser,
},
{
desc: "as root",
fakeCurrentUser: func() (*user.User, error) { return &user.User{Username: "root"}, nil },
want: stdUser,
err: nil,
},
{
desc: "as user",
fakeCurrentUser: func() (*user.User, error) { return &user.User{Username: stdUser}, nil },
want: stdUser,
err: nil,
},
}
if err := os.Setenv("SUDO_USER", stdUser); err != nil {
t.Fatalf(`os.SetEnv("SUDO_USER", %q) returned %v`, stdUser, err)
}
for _, tt := range tests {
currentUser = tt.fakeCurrentUser
got, err := username()
if !errors.Is(err, tt.err) {
t.Errorf("%s: isRoot() err: %v, want err: %v", tt.desc, err, tt.err)
}
if got != tt.want {
t.Errorf("%s: isRoot() got: %q, want: %q", tt.desc, got, tt.want)
}
}
// Cleanup
if err := os.Unsetenv("SUDO_USER"); err != nil {
t.Errorf(`os.UnsetEnv("SUDO_USER") returned %v`, err)
}
}
func TestRetrieve(t *testing.T) {
// Generate a fake config to use with Installer.
c := &fakeConfig{
image: `https://foo.bar.com/test_installer.img`,
imageFile: `test_installer.img`,
}
// Setup a temp folder.
cache, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatalf(`ioutil.TempDir("", "test") returned %v`, err)
}
tests := []struct {
desc string
installer *Installer
download func(client httpDoer, path string, w io.Writer) error
want error
}{
{
desc: "missing image path",
installer: &Installer{config: &fakeConfig{}},
want: errConfig,
},
{
desc: "missing cache",
installer: &Installer{config: c},
want: errCache,
},
{
desc: "image file failure",
installer: &Installer{cache: `\0`, config: c},
want: errFile,
},
{
desc: "download failure",
installer: &Installer{cache: cache, config: c},
download: func(client httpDoer, path string, w io.Writer) error { return errInput },
want: errInput,
},
{
desc: "download success",
installer: &Installer{cache: cache, config: c},
download: func(client httpDoer, path string, w io.Writer) error { return nil },
want: nil,
},
}
for _, tt := range tests {
downloadFile = tt.download
got := tt.installer.Retrieve()
if !errors.Is(got, tt.want) {
t.Errorf("%s: Retrieve() got: %v, want: %v", tt.desc, got, tt.want)
}
}
// Cleanup
if err := os.RemoveAll(cache); err != nil {
t.Errorf(`cleanup of %q returned %v`, cache, err)
}
}
// fakeHTTPDoer serves as a replacemenet for an http client for testing.
// The contents of body are returned when the Do is called. This method
// is used instead of httptest as a workaround for b/122585482.
type fakeHTTPDoer struct {
statusCode int
body []byte
err error
}
// Do provides the contents of fakeHTTPDoer.body as an http.Response by
// wrapping it with an io.ReadCloser.
func (c *fakeHTTPDoer) Do(req *http.Request) (*http.Response, error) {
reader := bytes.NewReader(c.body)
readCloser := ioutil.NopCloser(reader)
return &http.Response{StatusCode: c.statusCode, Body: readCloser}, c.err
}
// fakeWriter serves as a replacement for an io.Writer for testing.
type fakeWriter struct {
err error
}
func (f *fakeWriter) Write(p []byte) (n int, err error) {
return 0, f.err
}
func TestDownload(t *testing.T) {
path := "http://foo.bar.com/source/image.img"
tests := []struct {
desc string
doer httpDoer
path string
writer io.Writer
want error
}{
{
desc: "missing client",
want: errConnect,
},
{
desc: "missing path",
doer: &fakeHTTPDoer{},
want: errInput,
},
{
desc: "missing writer",
doer: &fakeHTTPDoer{},
path: path,
want: errFile,
},
{
desc: "doer failure",
doer: &fakeHTTPDoer{err: errDownload},
path: path,
writer: &fakeWriter{},
want: errDownload,
},
{
desc: "failed response code",
doer: &fakeHTTPDoer{statusCode: http.StatusForbidden},
path: path,
writer: &fakeWriter{},
want: errStatus,
},
}
for _, tt := range tests {
got := download(tt.doer, tt.path, tt.writer)
if !errors.Is(got, tt.want) {
t.Errorf("%s: download() got: %v, want: %v", tt.desc, got, tt.want)
}
}
}
// fakeDevice inherits all members of storage.Device through embedding.
// Unimplemented members send a clear signal during tests because they will
// panic if called, allowing us to implement only the minimum set of members
// required.
type fakeDevice struct {
// storage.Device is embedded, fakeDevice inherits all its members.
storage.Device
part partition
dmErr error
ejectErr error
detectErr error
partErr error
selErr error
wipeErr error
writeErr error
}
func (f *fakeDevice) Dismount() error {
return f.dmErr
}
func (f *fakeDevice) Eject() error {
return f.ejectErr
}
func (f *fakeDevice) Partition(label string) error {
return f.partErr
}
func (f *fakeDevice) DetectPartitions(bool) error {
return f.detectErr
}
func (f *fakeDevice) SelectPartition(uint64, storage.FileSystem) (*storage.Partition, error) {
return nil, f.partErr
}
func (f *fakeDevice) Wipe() error {
return f.wipeErr
}
// fakePartition represents storage.Partition.
type fakePartition struct {
contents []string
id string
label string
mount string
mountErr error
err error
}
func (f *fakePartition) Contents() ([]string, error) {
return f.contents, f.err
}
func (f *fakePartition) Identifier() string {
return f.id
}
func (f *fakePartition) Erase() error {
return f.err
}
func (f *fakePartition) Mount(string) error {
return f.mountErr
}
func (f *fakePartition) Format(label string) error {
return f.err
}
func (f *fakePartition) Label() string {
return f.label
}
func (f *fakePartition) MountPoint() string {
return f.mount
}
func TestPrepare(t *testing.T) {
// Prepare stand-ins for an image file.
badImage, err := ioutil.TempDir("", "bad.txt")
if err != nil {
t.Fatalf("ioutil.TempDir('', 'bad.txt') returned %v", err)
}
goodISO, err := ioutil.TempDir("", "good.iso")
if err != nil {
t.Fatalf("ioutil.TempDir('', 'good.iso') returned %v", err)
}
goodRaw, err := ioutil.TempDir("", "good.img")
if err != nil {
t.Fatalf("ioutil.TempDir('', 'good.img') returned %v", err)
}
tests := []struct {
desc string
installer *Installer
config Configuration
device Device
selPart func(Device, uint64, storage.FileSystem) (partition, error)
want error
}{
{
desc: "missing config",
installer: &Installer{},
device: &fakeDevice{},
want: errConfig,
},
{
desc: "missing image file",
installer: &Installer{config: &fakeConfig{}},
device: &fakeDevice{},
want: errInput,
},
{
desc: "no image file extension",
installer: &Installer{config: &fakeConfig{imageFile: "bad"}},
device: &fakeDevice{},
want: errFile,
},
{
desc: "missing download",
installer: &Installer{config: &fakeConfig{imageFile: "nonexistent.img"}},
device: &fakeDevice{},
want: errPath,
},
{
desc: "not a supported image",
installer: &Installer{config: &fakeConfig{imageFile: badImage}},
device: &fakeDevice{},
want: errProvision,
},
{
desc: "prepare for raw failure",
installer: &Installer{config: &fakeConfig{imageFile: goodRaw, elevated: true}},
device: &fakeDevice{dmErr: errProvision},
want: errProvision,
},
{
desc: "prepare for raw success",
installer: &Installer{config: &fakeConfig{imageFile: goodRaw, elevated: true}},
device: &fakeDevice{},
want: nil,
},
{
desc: "prepare for iso with elevation failure",
installer: &Installer{config: &fakeConfig{imageFile: goodISO, elevated: true}},
device: &fakeDevice{wipeErr: errors.New("error")},
want: errWipe,
},
{
desc: "prepare for iso with elevation success",
installer: &Installer{config: &fakeConfig{distroLabel: "test", imageFile: goodISO, elevated: true}},
device: &fakeDevice{},
selPart: func(Device, uint64, storage.FileSystem) (partition, error) { return &fakePartition{}, nil },
want: nil,
},
{
desc: "prepare for iso without elevation failure",
installer: &Installer{config: &fakeConfig{imageFile: goodISO, elevated: false, update: true}},
device: &fakeDevice{},
selPart: func(Device, uint64, storage.FileSystem) (partition, error) { return nil, errors.New("error") },
want: errPartition,
},
{
desc: "prepare for iso without elevation success",
installer: &Installer{config: &fakeConfig{distroLabel: "test", imageFile: goodISO, elevated: false, update: true}},
device: &fakeDevice{},
selPart: func(Device, uint64, storage.FileSystem) (partition, error) { return &fakePartition{}, nil },
want: nil,
},
}
for _, tt := range tests {
selectPart = tt.selPart
got := tt.installer.Prepare(tt.device)
if !errors.Is(got, tt.want) {
t.Errorf("%s: Prepare() got: %v, want: %v", tt.desc, got, tt.want)
}
}
}
func TestPrepareForISOWithElevation(t *testing.T) {
tests := []struct {
desc string
installer *Installer
device *fakeDevice
selPart func(Device, uint64, storage.FileSystem) (partition, error)
want error
}{
{
desc: "elevation missing",
installer: &Installer{config: &fakeConfig{}},
device: &fakeDevice{wipeErr: errors.New("error")},
want: errElevation,
},
{
desc: "wipe error",
installer: &Installer{config: &fakeConfig{elevated: true}},
device: &fakeDevice{wipeErr: errors.New("error")},
want: errWipe,
},
{
desc: "partition error",
installer: &Installer{config: &fakeConfig{elevated: true}},
device: &fakeDevice{partErr: errors.New("error")},
want: errPartition,
},
{
desc: "SelectPartition error",
installer: &Installer{config: &fakeConfig{elevated: true}},
device: &fakeDevice{},
selPart: func(Device, uint64, storage.FileSystem) (partition, error) { return nil, errors.New("error") },
want: func() error {
if runtime.GOOS != "darwin" {
return errPrepare
}
return nil
}(),
},
{
desc: "format error",
installer: &Installer{config: &fakeConfig{elevated: true}},
device: &fakeDevice{},
selPart: func(Device, uint64, storage.FileSystem) (partition, error) {
return &fakePartition{err: errors.New("error")}, nil
},
want: func() error {
if runtime.GOOS != "darwin" {
return errFormat
}
return nil
}(),
},
{
desc: "success",
installer: &Installer{config: &fakeConfig{elevated: true}},
device: &fakeDevice{},
selPart: func(Device, uint64, storage.FileSystem) (partition, error) { return &fakePartition{}, nil },
want: nil,
},
}
for _, tt := range tests {
selectPart = tt.selPart
got := tt.installer.prepareForISOWithElevation(tt.device, uint64(1024))
if !errors.Is(got, tt.want) {
t.Errorf("%s: prepareForISOWithElevation() got: %v, want: %v", tt.desc, got, tt.want)
}
}
}
func TestPrepareForISOWithoutElevation(t *testing.T) {
tests := []struct {
desc string
installer *Installer
selPart func(Device, uint64, storage.FileSystem) (partition, error)
want error
}{
{
desc: "SelectPartition error",
installer: &Installer{config: &fakeConfig{}},
selPart: func(Device, uint64, storage.FileSystem) (partition, error) { return nil, errors.New("error") },
want: errPartition,
},
{
desc: "mount error",
installer: &Installer{config: &fakeConfig{}},
selPart: func(Device, uint64, storage.FileSystem) (partition, error) {
return &fakePartition{mountErr: errors.New("error")}, nil
},
want: errMount,
},
{
desc: "erase error",
installer: &Installer{config: &fakeConfig{}},
selPart: func(Device, uint64, storage.FileSystem) (partition, error) {
return &fakePartition{err: errors.New("error")}, nil
},
want: errWipe,
},
{
desc: "success",
installer: &Installer{config: &fakeConfig{}},
selPart: func(Device, uint64, storage.FileSystem) (partition, error) { return &fakePartition{}, nil },
want: nil,
},
}
for _, tt := range tests {
selectPart = tt.selPart
got := tt.installer.prepareForISOWithoutElevation(&fakeDevice{}, uint64(1024))
if !errors.Is(got, tt.want) {
t.Errorf("%s: prepareForISOWithoutElevation() got: %v, want: %v", tt.desc, got, tt.want)
}
}
}
func TestPrepareForRaw(t *testing.T) {
tests := []struct {
desc string
device *fakeDevice
want error
}{
{
desc: "dismount error",
device: &fakeDevice{dmErr: ErrLabel},
want: ErrLabel,
},
{
desc: "success",
device: &fakeDevice{},
want: nil,
},
}
for _, tt := range tests {
installer := &Installer{}
got := installer.prepareForRaw(tt.device)
if !errors.Is(got, tt.want) {
t.Errorf("%s: prepareForRaw() got: %v, want: %v", tt.desc, got, tt.want)
}
}
}
// fakeHandler reprsents iso.Handler for testing. We do not use embedding here
// because we must abstract a concrete return from Mount using an interface.
type fakeHandler struct {
contents []string
mount string
path string
err error
}
func (f *fakeHandler) Contents() []string {
return f.contents
}
func (f *fakeHandler) Copy(string) error {
return f.err
}
func (f *fakeHandler) Dismount() error {
return f.err
}
func (f *fakeHandler) ImagePath() string {
return f.path
}
func (f *fakeHandler) MountPath() string {
return f.mount
}
func (f *fakeHandler) Size() uint64 {
return 1
}
func TestProvision(t *testing.T) {
// A fake cache for testing.
fakeCache, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("ioutil.TempDir('', '') returned %v", err)
}
// A fake image for testing.
fakeImagePath := filepath.Join(fakeCache, "fake.iso")
if _, err := os.Create(fakeImagePath); err != nil {
t.Fatalf("os.Create(%q) returned %v", fakeImagePath, err)
}
tests := []struct {
desc string
installer *Installer
mount func(string) (isoHandler, error)
writeISO func(isoHandler, partition) error
want error
}{
{
desc: "missing config",
installer: &Installer{},
want: errConfig,
},
{
desc: "missing cache",
installer: &Installer{config: &fakeConfig{}},
want: errCache,
},
{
desc: "missing image file",
installer: &Installer{cache: "/fake/path", config: &fakeConfig{}},
want: errInput,
},
{
desc: "no image file extension",
installer: &Installer{cache: "/fake/path", config: &fakeConfig{imageFile: "bad"}},
want: errFile,
},
{
desc: "image file missing from cache",
installer: &Installer{cache: "/fake/path", config: &fakeConfig{imageFile: "missing.iso"}},
want: errPath,
},
{
desc: "provision ISO error",
installer: &Installer{cache: "/fake/path", config: &fakeConfig{imageFile: "fake.iso"}},
want: errPath,
},
{
desc: "success",
installer: &Installer{cache: fakeCache, config: &fakeConfig{imageFile: "fake.iso"}},
mount: func(string) (isoHandler, error) { return &fakeHandler{}, nil },
writeISO: func(isoHandler, partition) error { return nil },
want: nil,
},
}
for _, tt := range tests {
mount = tt.mount
writeISOFunc = tt.writeISO
got := tt.installer.Provision(&fakeDevice{})
if !errors.Is(got, tt.want) {
t.Errorf("%s: Provision() got: %v, want: %v", tt.desc, got, tt.want)
}
}
}
func TestProvisionISO(t *testing.T) {
// A fake cache for testing.
fakeCache, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("ioutil.TempDir('', '') returned %v", err)
}
// A fake image for testing.
fakeImagePath := filepath.Join(fakeCache, "fake.iso")
if _, err := os.Create(fakeImagePath); err != nil {
t.Fatalf("os.Create(%q) returned %v", fakeImagePath, err)
}
tests := []struct {
desc string
installer *Installer
device *fakeDevice
mount func(string) (isoHandler, error)
selPart func(Device, uint64, storage.FileSystem) (partition, error)
writeISO func(isoHandler, partition) error
want error
}{
{
desc: "mount error",
installer: &Installer{cache: fakeCache, config: &fakeConfig{imageFile: "fake.iso"}},
mount: func(string) (isoHandler, error) { return &fakeHandler{}, errors.New("error") },
want: errMount,
},
{
desc: "select partition error",
installer: &Installer{cache: fakeCache, config: &fakeConfig{imageFile: "fake.iso"}},
mount: func(string) (isoHandler, error) { return &fakeHandler{}, nil },
device: &fakeDevice{},
selPart: func(Device, uint64, storage.FileSystem) (partition, error) {
return &fakePartition{label: "test"}, errors.New("error")
},
want: errPartition,
},
{
desc: "writeISO error",
installer: &Installer{cache: fakeCache, config: &fakeConfig{imageFile: "fake.iso"}},
mount: func(string) (isoHandler, error) { return &fakeHandler{}, nil },
device: &fakeDevice{},
selPart: func(Device, uint64, storage.FileSystem) (partition, error) { return &fakePartition{label: "test"}, nil },
writeISO: func(isoHandler, partition) error { return errPath },
want: errProvision,
},
{
desc: "dismount deferred error",
installer: &Installer{cache: fakeCache, config: &fakeConfig{imageFile: "fake.iso"}},
mount: func(string) (isoHandler, error) { return &fakeHandler{err: errIO}, nil },
device: &fakeDevice{},
selPart: func(Device, uint64, storage.FileSystem) (partition, error) { return &fakePartition{label: "test"}, nil },
writeISO: func(isoHandler, partition) error { return nil },
want: errIO,
},
{
desc: "success",
installer: &Installer{cache: fakeCache, config: &fakeConfig{imageFile: "fake.iso"}},
mount: func(string) (isoHandler, error) { return &fakeHandler{}, nil },
device: &fakeDevice{},
selPart: func(Device, uint64, storage.FileSystem) (partition, error) { return &fakePartition{label: "test"}, nil },
writeISO: func(isoHandler, partition) error { return nil },
want: nil,
},
}
for _, tt := range tests {
mount = tt.mount
writeISOFunc = tt.writeISO
selectPart = tt.selPart
got := tt.installer.provisionISO(tt.device)
if !errors.Is(got, tt.want) {
t.Errorf("%s: provisionISO() got: %v, want: %v", tt.desc, got, tt.want)
}
}
}
// fakeISO represents iso.Handler. It inherits all members of iso.Handler
// through embedding. Unimplemented members send a clear signal during tests
// because they will panic if called, allowing us to implement only the minimum
// set of members required for testing.
type fakeISO struct {
contents []string
copyErr error
imagePath string
mount string
size uint64
}
func (f *fakeISO) Size() uint64 {
return f.size
}
func (f *fakeISO) MountPath() string {
return f.mount
}
func (f *fakeISO) Contents() []string {
return f.contents
}
func (f *fakeISO) Copy(dest string) error {
return f.copyErr
}
func (f *fakeISO) Dismount() error {
return nil
}
func (f *fakeISO) ImagePath() string {
return f.imagePath
}
// fakeFileSystems returns a fake mount point and contents for testing
// purposes to simulate mounted filesystems. The caller is responsible
// for cleaning up the folders after their tests are complete.
func fakeFileSystems() (string, []string, error) {
m, err := ioutil.TempDir("", "")
if err != nil {
return "", []string{}, fmt.Errorf("ioutil.TempDir() returned %v", err)
}
c := []string{"one", "two", "three"}
return m, c, nil
}
func TestWriteISO(t *testing.T) {
// Temp folders representing file system contents.
mount, contents, err := fakeFileSystems()
if err != nil {
t.Fatalf("fakeFileSystems() returned %v", err)
}
defer os.RemoveAll(mount)
tests := []struct {
desc string
part partition
iso isoHandler
want error
}{
{
desc: "empty partition",
part: nil,
want: errPartition,
},
{
desc: "partition not mounted",
part: &fakePartition{},
iso: &fakeISO{},
want: errMount,
},
{
desc: "partition not empty",
part: &fakePartition{mount: mount, contents: contents},
iso: &fakeISO{},
want: errNotEmpty,
},
{
desc: "iso not mounted",
part: &fakePartition{mount: mount},
iso: &fakeISO{},
want: errInput,
},
{
desc: "empty iso",
part: &fakePartition{mount: mount},
iso: &fakeISO{mount: `/fake/path`},
want: errEmpty,
},
}
for _, tt := range tests {
got := writeISO(tt.iso, tt.part)
if !errors.Is(got, tt.want) {
t.Errorf("%s: WriteISO got = %q, want = %q", tt.desc, got, tt.want)
}
}
}
func TestWriteSeed(t *testing.T) {
// Create a temporary file and folder for the test.
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf(`ioutil.TempDir("","") returned %v`, err)
}
filePath := filepath.Join(tempDir, "fake.wim")
f, err := os.Create(filePath)
if err != nil {
t.Fatalf("os.Create(%q) returned %v", filePath, err)
}
defer f.Close()
if _, err := f.Write([]byte("test content")); err != nil {
t.Fatalf("failed to write to %q with %v", f.Name(), err)
}
// A fake seed response.
good, err := json.Marshal(&models.SeedResponse{ErrorCode: models.StatusSuccess})
if err != nil {
t.Fatalf("json.Marshal of good request returned %v", err)
}
tests := []struct {
desc string
installer *Installer
fakeConnect func(string, string) (httpDoer, error)
handler *fakeHandler
part *fakePartition
want error
}{
{
desc: "not mounted",
part: &fakePartition{label: "Test"},
want: errInput,
},
{
desc: "file hash error",
installer: &Installer{config: &fakeConfig{}},
handler: &fakeHandler{},
part: &fakePartition{label: "Test", mount: tempDir},
want: errFile,
},
{
desc: "connect error",
installer: &Installer{config: &fakeConfig{}},
fakeConnect: func(string, string) (httpDoer, error) { return nil, errors.New("error") },
handler: &fakeHandler{},
part: &fakePartition{label: "Test", mount: tempDir},
want: errFile,
},
{
desc: "seed request error",
installer: &Installer{config: &fakeConfig{seedServer: `:`, seedFile: f.Name()}},
fakeConnect: func(string, string) (httpDoer, error) { return nil, nil },
handler: &fakeHandler{},
part: &fakePartition{label: "Test", mount: tempDir},
want: errDownload,
},
{
desc: "success",
installer: &Installer{
config: &fakeConfig{
seedDest: "test",
seedFile: "fake.wim",
seedServer: `https://foo.bar.com/seed`,
},