|
| 1 | +package state |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/spf13/afero" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/replicatedhq/ship/pkg/constants" |
| 12 | +) |
| 13 | + |
| 14 | +func Test_readUpstreamFiles(t *testing.T) { |
| 15 | + type fileStruct struct { |
| 16 | + name string |
| 17 | + data string |
| 18 | + } |
| 19 | + |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + wantErr bool |
| 23 | + inputFiles []fileStruct |
| 24 | + wantUpstreamContents UpstreamContents |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "basic", |
| 28 | + inputFiles: []fileStruct{ |
| 29 | + { |
| 30 | + name: "abc.test", |
| 31 | + data: "abc.test", |
| 32 | + }, |
| 33 | + }, |
| 34 | + wantUpstreamContents: UpstreamContents{ |
| 35 | + UpstreamFiles: []UpstreamFile{ |
| 36 | + { |
| 37 | + FilePath: "abc.test", |
| 38 | + FileContents: "YWJjLnRlc3Q=", |
| 39 | + }, |
| 40 | + }, |
| 41 | + AppRelease: nil, |
| 42 | + }, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "app release", |
| 46 | + inputFiles: []fileStruct{ |
| 47 | + { |
| 48 | + name: "abc.test", |
| 49 | + data: "abc.test", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "appRelease.json", |
| 53 | + data: `{"id":"appRelease.json"}`, |
| 54 | + }, |
| 55 | + }, |
| 56 | + wantUpstreamContents: UpstreamContents{ |
| 57 | + UpstreamFiles: nil, |
| 58 | + AppRelease: &ShipRelease{ID: "appRelease.json"}, |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + for _, tt := range tests { |
| 64 | + t.Run(tt.name, func(t *testing.T) { |
| 65 | + req := require.New(t) |
| 66 | + // setup input FS |
| 67 | + mockFs := afero.Afero{Fs: afero.NewMemMapFs()} |
| 68 | + req.NoError(mockFs.MkdirAll(constants.UpstreamContentsPath, os.FileMode(0644))) |
| 69 | + for _, inFile := range tt.inputFiles { |
| 70 | + req.NoError(mockFs.WriteFile(filepath.Join(constants.UpstreamContentsPath, inFile.name), []byte(inFile.data), os.FileMode(0644))) |
| 71 | + } |
| 72 | + |
| 73 | + got, err := readUpstreamFiles(mockFs, State{}) |
| 74 | + if (err != nil) != tt.wantErr { |
| 75 | + t.Errorf("readUpstreamFiles() error = %v, wantErr %v", err, tt.wantErr) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + wantState := State{V1: &V1{UpstreamContents: &tt.wantUpstreamContents}} |
| 80 | + req.Equal(wantState, got) |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func Test_UpstreamFilesCycle(t *testing.T) { |
| 86 | + |
| 87 | + tests := []struct { |
| 88 | + name string |
| 89 | + contents UpstreamContents |
| 90 | + }{ |
| 91 | + { |
| 92 | + name: "basic", |
| 93 | + contents: UpstreamContents{ |
| 94 | + UpstreamFiles: []UpstreamFile{ |
| 95 | + { |
| 96 | + FilePath: "abc.test", |
| 97 | + FileContents: "YWJjLnRlc3Q=", |
| 98 | + }, |
| 99 | + }, |
| 100 | + AppRelease: nil, |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "app release", |
| 105 | + contents: UpstreamContents{ |
| 106 | + UpstreamFiles: nil, |
| 107 | + AppRelease: &ShipRelease{ID: "appRelease.json"}, |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "multiple files", |
| 112 | + contents: UpstreamContents{ |
| 113 | + UpstreamFiles: []UpstreamFile{ |
| 114 | + { |
| 115 | + FilePath: "abc.test", |
| 116 | + FileContents: "YWJjLnRlc3Q=", |
| 117 | + }, |
| 118 | + { |
| 119 | + FilePath: "subdir/abc.test", |
| 120 | + FileContents: "YWJjLnRlc3Q=", |
| 121 | + }, |
| 122 | + }, |
| 123 | + AppRelease: nil, |
| 124 | + }, |
| 125 | + }, |
| 126 | + } |
| 127 | + |
| 128 | + for _, tt := range tests { |
| 129 | + t.Run(tt.name, func(t *testing.T) { |
| 130 | + req := require.New(t) |
| 131 | + // setup input FS |
| 132 | + mockFs := afero.Afero{Fs: afero.NewMemMapFs()} |
| 133 | + req.NoError(mockFs.MkdirAll(constants.UpstreamContentsPath, os.FileMode(0644))) |
| 134 | + |
| 135 | + wantState := State{V1: &V1{UpstreamContents: &tt.contents}} |
| 136 | + |
| 137 | + err := writeUpstreamFiles(mockFs, wantState) |
| 138 | + req.NoError(err) |
| 139 | + |
| 140 | + got, err := readUpstreamFiles(mockFs, State{}) |
| 141 | + req.NoError(err) |
| 142 | + |
| 143 | + req.Equal(wantState, got) |
| 144 | + }) |
| 145 | + } |
| 146 | +} |
0 commit comments